This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/lib/mbdiesel.c

374 lines
9.8 KiB
C
Raw Normal View History

2002-03-29 22:58:53 +00:00
/*****************************************************************************
*
* $Id$
* Purpose ...............: MBSE BBS functions for TURBODIESEL
*
*****************************************************************************
2007-02-13 14:28:43 +00:00
* Copyright (C) 1997-2007
2002-03-29 22:58:53 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2002-03-29 22:58:53 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 14:24:03 +00:00
#include "mbselib.h"
2002-03-29 22:58:53 +00:00
#include "diesel.h"
2004-02-21 14:24:03 +00:00
static int firstrandom = TRUE;
2002-03-29 22:58:53 +00:00
2002-03-31 13:09:23 +00:00
void MacroVars( const char *codes, const char *fmt, ...)
{
char *tmp1, *tmp2, *vs, vc;
va_list ap;
int j, dieselrc, vd;
double vf;
tmp1 = calloc(MAXSTR, sizeof(char));
tmp2 = calloc(MAXSTR, sizeof(char));
va_start(ap,fmt);
for (j = 0; (codes[j] != '\0') && (fmt[j] != '\0') ; j++ ){
tmp1[0] = '\0';
switch (fmt[j]) {
case 's': /* string */
2002-03-31 13:09:23 +00:00
vs = va_arg(ap, char *);
2007-02-13 14:28:43 +00:00
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,\"%s\")",codes[j], clencode(vs));
2002-03-31 13:09:23 +00:00
break;
case 'd': /* int */
2002-03-31 13:09:23 +00:00
vd = va_arg(ap, int);
2005-08-28 09:42:08 +00:00
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,%d)",codes[j],vd);
2002-03-31 13:09:23 +00:00
break;
case 'c': /* char */
2002-05-02 17:19:49 +00:00
vc = va_arg(ap, int);
2005-08-28 09:42:08 +00:00
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,%c)",codes[j],vc);
2002-03-31 13:09:23 +00:00
break;
2007-02-13 14:28:43 +00:00
case 'f': /* float */
2002-03-31 13:09:23 +00:00
vf = va_arg(ap, double);
2005-08-28 09:42:08 +00:00
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,%f)",codes[j],vf);
2002-03-31 13:09:23 +00:00
break;
}
dieselrc = diesel(tmp1,tmp2);
2007-02-13 14:28:43 +00:00
if (dieselrc) {
Syslog('!', "MacroVars error %d argument %d, macro %c type %c", dieselrc, j, codes[j], fmt[j]);
Syslogp('!', printable(tmp1, 0));
}
}
va_end(ap);
free(tmp1);
free(tmp2);
2002-03-31 13:09:23 +00:00
}
void MacroClear(void)
{
int dieselrc;
char tmp1[] = "@(CLEAR)", *tmp2;
tmp2 = calloc(10,sizeof(char));
dieselrc = diesel(tmp1, tmp2);
if (dieselrc)
Syslog('!', "MacroClear error %d", dieselrc);
free(tmp2);
}
char *ParseMacro( const char *line, int *dieselrc)
{
2002-04-10 20:29:57 +00:00
static char res[MAXSTR];
2007-02-13 14:28:43 +00:00
const char *i;
char *tmp1, *tmp2, *tmp3;
2002-03-31 13:09:23 +00:00
int j, l;
char code;
res[0]='\0';
*dieselrc=0;
if ( *line == '#' )
return res;
2002-04-10 20:29:57 +00:00
tmp1 = calloc(MAXSTR, sizeof(char));
tmp2 = calloc(MAXSTR, sizeof(char));
tmp3 = calloc(MAXSTR, sizeof(char));
2002-03-31 13:09:23 +00:00
tmp1[0]='\0';
2007-02-13 14:28:43 +00:00
for (i = line; i[0] != '\0'; i++) {
2002-03-31 13:09:23 +00:00
if ( (i[0] == '@') && isalpha(i[1]) ){
l=2;
i++;
if (i[0] != '@') {
if ((code = i[0]) != '\0' )
i++;
while (( i[0] == '_') || ( i[0] == '>') || ( i[0] == '<') ){
l++;
i++;
}
i--;
2005-08-28 13:34:43 +00:00
snprintf(tmp2, MAXSTR, "@(GETVAR,%c)",code);
2002-03-31 13:09:23 +00:00
if (!diesel(tmp2,tmp3)==0){
2005-08-28 13:34:43 +00:00
snprintf(tmp3, MAXSTR, "%c%c",'@',code);
2002-03-31 13:09:23 +00:00
}
if (l>2){
if ( *i != '>')
l=-l;
2005-08-28 13:34:43 +00:00
snprintf(&tmp1[strlen(tmp1)], MAXSTR, "%*.*s", l, l, tmp3);
2002-03-31 13:09:23 +00:00
}else{
2005-08-28 13:34:43 +00:00
snprintf(&tmp1[strlen(tmp1)], MAXSTR, "%s", tmp3);
2002-03-31 13:09:23 +00:00
}
}else{
tmp1[(j=strlen(tmp1))]='@';
tmp1[j+1]='\0';
}
}else{
tmp1[(j=strlen(tmp1))]=i[0];
tmp1[j+1]='\0';
}
}
i = tmp1;
2005-08-28 13:34:43 +00:00
snprintf(tmp2, MAXSTR, "%s", tmp1);
2002-03-31 13:09:23 +00:00
if ((tmp1[0]=='@') && (tmp1[1]=='{')){
i++;
i++;
for (j=2; ((tmp1[j]!='}') && (tmp1[j]!='\0'));j++){
i++;
}
if ( (tmp1[j]=='}') ){
i++;
res[0]='\0';
if (j>2)
2005-08-28 13:34:43 +00:00
snprintf(res, MAXSTR, "%.*s",j-2, &tmp1[2]);
2002-03-31 13:09:23 +00:00
if ((diesel(res,tmp3)!=0) || (atoi(tmp3)==0))
2005-08-28 13:34:43 +00:00
snprintf(tmp2, MAXSTR, "@!%s",i);
2002-03-31 13:09:23 +00:00
else
2005-08-28 13:34:43 +00:00
snprintf(tmp2, MAXSTR, "%s",i);
2002-03-31 13:09:23 +00:00
}
}
*dieselrc=diesel(tmp2, res);
free(tmp1);
free(tmp2);
free(tmp3);
while (isspace(res[strlen(res) - 1])) {
res[strlen(res) - 1] = EOS;
}
if ((res[0] == '@') && (res[1] =='!' ))
res[0]='\0';
2007-02-13 14:28:43 +00:00
cldecode(res);
2002-03-31 13:09:23 +00:00
return res;
}
2002-03-29 22:58:53 +00:00
/*
* Add random fortune cookie to the macrovars
*/
2002-05-11 15:30:34 +00:00
void Cookie(int);
void Cookie(int HtmlMode)
{
FILE *olf;
2002-05-11 15:30:34 +00:00
char *fname, outbuf[256];
2005-10-11 20:49:41 +00:00
int recno, records;
MacroVars("F", "s", "");
fname = calloc(PATH_MAX, sizeof(char));
2005-08-28 09:42:08 +00:00
snprintf(fname, PATH_MAX -1, "%s/etc/oneline.data", getenv("MBSE_ROOT"));
if ((olf = fopen(fname, "r")) == NULL) {
WriteError("Can't open %s", fname);
free(fname);
return;
}
fread(&olhdr, sizeof(olhdr), 1, olf);
fseek(olf, 0, SEEK_END);
records = (ftell(olf) - olhdr.hdrsize) / olhdr.recsize;
if (firstrandom) {
srand(getpid());
firstrandom = FALSE;
}
recno = (1+(int) (1.0 * records * rand() / (RAND_MAX + 1.0))) - 1;
if (fseek(olf, olhdr.hdrsize + (recno * olhdr.recsize), SEEK_SET) == 0) {
if (fread(&ol, olhdr.recsize, 1, olf) == 1) {
2002-05-11 15:30:34 +00:00
if (HtmlMode) {
2005-08-28 09:42:08 +00:00
html_massage(ol.Oneline, outbuf, 255);
2002-05-11 15:30:34 +00:00
MacroVars("F", "s", outbuf);
} else {
MacroVars("F", "s", ol.Oneline);
}
} else {
WriteError("Can't read record %d from %s", recno, fname);
}
} else {
WriteError("Can't seek record %d in %s", recno, fname);
}
fclose(olf);
free(fname);
return;
}
2002-04-10 20:29:57 +00:00
/*
* Translate ISO 8859-1 characters to named character entities
*/
2005-08-28 09:42:08 +00:00
void html_massage(char *inbuf, char *outbuf, size_t size)
2002-04-10 20:29:57 +00:00
{
char *inptr = inbuf;
char *outptr = outbuf;
memset(outbuf, 0, sizeof(outbuf));
while (*inptr) {
switch ((unsigned char)*inptr) {
2005-08-28 09:42:08 +00:00
case '"': snprintf(outptr, size, "&quot;"); break;
case '&': snprintf(outptr, size, "&amp;"); break;
case '<': snprintf(outptr, size, "&lt;"); break;
case '>': snprintf(outptr, size, "&gt;"); break;
2002-04-10 20:29:57 +00:00
default: *outptr++ = *inptr; *outptr = '\0'; break;
}
while (*outptr)
outptr++;
inptr++;
}
*outptr = '\0';
}
FILE *OpenMacro(const char *filename, int Language, int htmlmode)
2002-03-29 22:58:53 +00:00
{
FILE *pLang, *fi = NULL;
2002-04-10 20:29:57 +00:00
char *temp, *aka, linebuf[1024], outbuf[1024];
2002-03-29 22:58:53 +00:00
temp = calloc(PATH_MAX, sizeof(char));
2002-04-10 20:29:57 +00:00
aka = calloc(81, sizeof(char));
2002-03-29 22:58:53 +00:00
temp[0] = '\0';
if (Language != '\0') {
/*
* Maybe a valid language character, try to load the language
*/
2005-08-28 09:42:08 +00:00
snprintf(temp, PATH_MAX -1, "%s/etc/language.data", getenv("MBSE_ROOT"));
2002-03-29 22:58:53 +00:00
if ((pLang = fopen(temp, "rb")) == NULL) {
WriteError("mbdiesel: Can't open language file: %s", temp);
} else {
fread(&langhdr, sizeof(langhdr), 1, pLang);
while (fread(&lang, langhdr.recsize, 1, pLang) == 1) {
if ((lang.LangKey[0] == Language) && (lang.Available)) {
snprintf(temp, PATH_MAX -1, "%s/share/int/macro/%s/%s", getenv("MBSE_ROOT"), lang.lc, filename);
2002-03-29 22:58:53 +00:00
break;
}
}
fclose(pLang);
}
}
/*
* Try to open the selected language
*/
if (temp[0] != '\0')
fi = fopen(temp, "r");
/*
* If no selected language is loaded, try default language
*/
if (fi == NULL) {
2002-05-18 13:49:55 +00:00
Syslog('-', "Macro file \"%s\" for language %c not found, trying default", filename, Language);
snprintf(temp, PATH_MAX -1, "%s/share/int/macro/%s/%s", getenv("MBSE_ROOT"), CFG.deflang, filename);
2002-03-29 22:58:53 +00:00
fi = fopen(temp,"r");
}
if (fi == NULL)
WriteError("OpenMacro(%s, %c): not found", filename, Language);
2002-03-31 15:04:34 +00:00
else {
2007-02-25 20:28:00 +00:00
/*
* Check macro file for update correct charset.
*/
while (fgets(linebuf, sizeof(linebuf) -1, fi)) {
if (strcasestr(linebuf, (char *)"text/html")) {
if (! strcasestr(linebuf, (char *)"UTF-8")) {
WriteError("Macro file %s doesn't define 'Content-Type' content='text/html; charset=UTF-8'", temp);
}
}
}
rewind(fi);
2005-08-28 09:42:08 +00:00
snprintf(temp, PATH_MAX -1, "%s-%s", OsName(), OsCPU());
2002-04-10 20:29:57 +00:00
if (CFG.aka[0].point)
2005-08-28 09:42:08 +00:00
snprintf(aka, 80, "%d:%d/%d.%d@%s", CFG.aka[0].zone, CFG.aka[0].net, CFG.aka[0].node, CFG.aka[0].point, CFG.aka[0].domain);
2002-04-10 20:29:57 +00:00
else
2005-08-28 09:42:08 +00:00
snprintf(aka, 80, "%d:%d/%d@%s", CFG.aka[0].zone, CFG.aka[0].net, CFG.aka[0].node, CFG.aka[0].domain);
2002-04-10 20:29:57 +00:00
if (htmlmode) {
2002-05-18 13:49:55 +00:00
MacroVars("O", "s", temp);
2005-08-28 13:34:43 +00:00
snprintf(linebuf, 1024, "%s", CFG.sysop);
html_massage(linebuf, outbuf, 1024);
2002-05-18 13:49:55 +00:00
MacroVars("U", "s", outbuf);
2005-08-28 13:34:43 +00:00
snprintf(linebuf, 1024, "%s", CFG.location);
html_massage(linebuf, outbuf, 1024);
2002-04-10 20:29:57 +00:00
MacroVars("L", "s", outbuf);
2005-08-28 13:34:43 +00:00
snprintf(linebuf, 1024, "%s", CFG.bbs_name);
html_massage(linebuf, outbuf, 1024);
2002-04-10 20:29:57 +00:00
MacroVars("N", "s", outbuf);
2005-08-28 13:34:43 +00:00
snprintf(linebuf, 1024, "%s", CFG.sysop_name);
html_massage(linebuf, outbuf, 1024);
2002-04-10 20:29:57 +00:00
MacroVars("S", "s", outbuf);
2005-08-28 13:34:43 +00:00
snprintf(linebuf, 1024, "%s", CFG.comment);
html_massage(linebuf, outbuf, 1024);
2002-04-10 20:29:57 +00:00
MacroVars("T", "s", outbuf);
} else {
2002-05-18 13:49:55 +00:00
MacroVars("L", "s", CFG.location);
MacroVars("N", "s", CFG.bbs_name);
MacroVars("O", "s", temp);
MacroVars("S", "s", CFG.sysop_name);
MacroVars("T", "s", CFG.comment);
MacroVars("U", "s", CFG.sysop);
2002-04-10 20:29:57 +00:00
}
2002-05-18 13:49:55 +00:00
MacroVars("H", "s", CFG.www_url);
MacroVars("M", "s", CFG.sysdomain);
MacroVars("V", "s", VERSION);
MacroVars("Y", "s", aka);
MacroVars("Z", "d", 0);
2002-05-11 15:30:34 +00:00
Cookie(htmlmode);
2002-03-31 15:04:34 +00:00
}
2002-03-29 22:58:53 +00:00
2002-04-10 20:29:57 +00:00
free(aka);
2002-03-29 22:58:53 +00:00
free(temp);
return fi;
}