458 lines
18 KiB
C
458 lines
18 KiB
C
/*****************************************************************************
|
|
*
|
|
* $Id$
|
|
* Purpose ...............: MBSE BBS functions for TURBODIESEL
|
|
*
|
|
*****************************************************************************
|
|
* Copyright (C) 1997-2005
|
|
*
|
|
* 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
|
|
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
*****************************************************************************/
|
|
|
|
#include "../config.h"
|
|
#include "mbselib.h"
|
|
#include "diesel.h"
|
|
|
|
|
|
static int firstrandom = TRUE;
|
|
|
|
|
|
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 */
|
|
vs = va_arg(ap, char *);
|
|
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,\"%s\")",codes[j],vs);
|
|
break;
|
|
case 'd': /* int */
|
|
vd = va_arg(ap, int);
|
|
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,%d)",codes[j],vd);
|
|
break;
|
|
case 'c': /* char */
|
|
vc = va_arg(ap, int);
|
|
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,%c)",codes[j],vc);
|
|
break;
|
|
case 'f': /* char */
|
|
vf = va_arg(ap, double);
|
|
snprintf(tmp1, MAXSTR -1, "@(setvar,%c,%f)",codes[j],vf);
|
|
break;
|
|
}
|
|
dieselrc = diesel(tmp1,tmp2);
|
|
if (dieselrc) {
|
|
Syslog('!', "MacroVars error %d argument %d, macro %c type %c", dieselrc, j, codes[j], fmt[j]);
|
|
if (fmt[j] == 's')
|
|
Syslogp('!', printable(va_arg(ap, char *), 0));
|
|
Syslogp('!', printable(tmp1, 0));
|
|
Syslogp('!', printable(tmp2, 0));
|
|
}
|
|
}
|
|
va_end(ap);
|
|
|
|
free(tmp1);
|
|
free(tmp2);
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
{
|
|
static char res[MAXSTR];
|
|
char *tmp1, *tmp2, *tmp3, *i;
|
|
int j, l;
|
|
char code;
|
|
|
|
res[0]='\0';
|
|
*dieselrc=0;
|
|
|
|
if ( *line == '#' )
|
|
return res;
|
|
|
|
tmp1 = calloc(MAXSTR, sizeof(char));
|
|
tmp2 = calloc(MAXSTR, sizeof(char));
|
|
tmp3 = calloc(MAXSTR, sizeof(char));
|
|
|
|
tmp1[0]='\0';
|
|
|
|
for ( i=line ; i[0] != '\0'; i++){
|
|
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--;
|
|
snprintf(tmp2, MAXSTR -1, "@(GETVAR,%c)",code);
|
|
if (!diesel(tmp2,tmp3)==0){
|
|
snprintf(tmp3, MAXSTR -1, "%c%c",'@',code);
|
|
}
|
|
if (l>2){
|
|
if ( *i != '>')
|
|
l=-l;
|
|
snprintf(&tmp1[strlen(tmp1)], MAXSTR -1, "%*.*s", l, l, tmp3);
|
|
}else{
|
|
snprintf(&tmp1[strlen(tmp1)], MAXSTR -1, "%s", tmp3);
|
|
}
|
|
}else{
|
|
tmp1[(j=strlen(tmp1))]='@';
|
|
tmp1[j+1]='\0';
|
|
}
|
|
}else{
|
|
tmp1[(j=strlen(tmp1))]=i[0];
|
|
tmp1[j+1]='\0';
|
|
}
|
|
}
|
|
|
|
i = tmp1;
|
|
snprintf(tmp2, MAXSTR -1, "%s", tmp1);
|
|
|
|
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)
|
|
snprintf(res, MAXSTR -1, "%.*s",j-2, &tmp1[2]);
|
|
if ((diesel(res,tmp3)!=0) || (atoi(tmp3)==0))
|
|
snprintf(tmp2, MAXSTR -1, "@!%s",i);
|
|
else
|
|
snprintf(tmp2, MAXSTR -1, "%s",i);
|
|
}
|
|
}
|
|
*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';
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* Add random fortune cookie to the macrovars
|
|
*/
|
|
void Cookie(int);
|
|
void Cookie(int HtmlMode)
|
|
{
|
|
FILE *olf;
|
|
char *fname, outbuf[256];
|
|
long recno, records;
|
|
|
|
MacroVars("F", "s", "");
|
|
fname = calloc(PATH_MAX, sizeof(char));
|
|
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) {
|
|
if (HtmlMode) {
|
|
html_massage(ol.Oneline, outbuf, 255);
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* Translate ISO 8859-1 characters to named character entities
|
|
*/
|
|
void html_massage(char *inbuf, char *outbuf, size_t size)
|
|
{
|
|
char *inptr = inbuf;
|
|
char *outptr = outbuf;
|
|
|
|
memset(outbuf, 0, sizeof(outbuf));
|
|
|
|
while (*inptr) {
|
|
|
|
switch ((unsigned char)*inptr) {
|
|
|
|
case '"': snprintf(outptr, size, """); break;
|
|
case '&': snprintf(outptr, size, "&"); break;
|
|
case '<': snprintf(outptr, size, "<"); break;
|
|
case '>': snprintf(outptr, size, ">"); break;
|
|
case 160: snprintf(outptr, size, " "); break;
|
|
case 161: snprintf(outptr, size, "¡"); break;
|
|
case 162: snprintf(outptr, size, "¢"); break;
|
|
case 163: snprintf(outptr, size, "£"); break;
|
|
case 164: snprintf(outptr, size, "¤"); break;
|
|
case 165: snprintf(outptr, size, "¥"); break;
|
|
case 166: snprintf(outptr, size, "¦"); break;
|
|
case 167: snprintf(outptr, size, "§"); break;
|
|
case 168: snprintf(outptr, size, "¨"); break;
|
|
case 169: snprintf(outptr, size, "©"); break;
|
|
case 170: snprintf(outptr, size, "ª"); break;
|
|
case 171: snprintf(outptr, size, "«"); break;
|
|
case 172: snprintf(outptr, size, "¬"); break;
|
|
case 173: snprintf(outptr, size, "­"); break;
|
|
case 174: snprintf(outptr, size, "®"); break;
|
|
case 175: snprintf(outptr, size, "¯"); break;
|
|
case 176: snprintf(outptr, size, "°"); break;
|
|
case 177: snprintf(outptr, size, "&plumin;"); break;
|
|
case 178: snprintf(outptr, size, "²"); break;
|
|
case 179: snprintf(outptr, size, "³"); break;
|
|
case 180: snprintf(outptr, size, "´"); break;
|
|
case 181: snprintf(outptr, size, "µ"); break;
|
|
case 182: snprintf(outptr, size, "¶"); break;
|
|
case 183: snprintf(outptr, size, "·"); break;
|
|
case 184: snprintf(outptr, size, "¸"); break;
|
|
case 185: snprintf(outptr, size, "&supl;"); break;
|
|
case 186: snprintf(outptr, size, "º"); break;
|
|
case 187: snprintf(outptr, size, "»"); break;
|
|
case 188: snprintf(outptr, size, "¼"); break;
|
|
case 189: snprintf(outptr, size, "½"); break;
|
|
case 190: snprintf(outptr, size, "¾"); break;
|
|
case 191: snprintf(outptr, size, "¿"); break;
|
|
case 192: snprintf(outptr, size, "À"); break;
|
|
case 193: snprintf(outptr, size, "Á"); break;
|
|
case 194: snprintf(outptr, size, "Â"); break;
|
|
case 195: snprintf(outptr, size, "Ã"); break;
|
|
case 196: snprintf(outptr, size, "Ä"); break;
|
|
case 197: snprintf(outptr, size, "Å"); break;
|
|
case 198: snprintf(outptr, size, "Æ"); break;
|
|
case 199: snprintf(outptr, size, "Ç"); break;
|
|
case 200: snprintf(outptr, size, "È"); break;
|
|
case 201: snprintf(outptr, size, "É"); break;
|
|
case 202: snprintf(outptr, size, "Ê"); break;
|
|
case 203: snprintf(outptr, size, "Ë"); break;
|
|
case 204: snprintf(outptr, size, "Ì"); break;
|
|
case 205: snprintf(outptr, size, "Í"); break;
|
|
case 206: snprintf(outptr, size, "Î"); break;
|
|
case 207: snprintf(outptr, size, "Ï"); break;
|
|
case 208: snprintf(outptr, size, "Ð"); break;
|
|
case 209: snprintf(outptr, size, "Ñ"); break;
|
|
case 210: snprintf(outptr, size, "Ò"); break;
|
|
case 211: snprintf(outptr, size, "Ó"); break;
|
|
case 212: snprintf(outptr, size, "Ô"); break;
|
|
case 213: snprintf(outptr, size, "Õ"); break;
|
|
case 214: snprintf(outptr, size, "Ö"); break;
|
|
case 215: snprintf(outptr, size, "×"); break;
|
|
case 216: snprintf(outptr, size, "Ø"); break;
|
|
case 217: snprintf(outptr, size, "Ù"); break;
|
|
case 218: snprintf(outptr, size, "Ú"); break;
|
|
case 219: snprintf(outptr, size, "Û"); break;
|
|
case 220: snprintf(outptr, size, "Ü"); break;
|
|
case 221: snprintf(outptr, size, "Ý"); break;
|
|
case 222: snprintf(outptr, size, "Þ"); break;
|
|
case 223: snprintf(outptr, size, "ß"); break;
|
|
case 224: snprintf(outptr, size, "à"); break;
|
|
case 225: snprintf(outptr, size, "á"); break;
|
|
case 226: snprintf(outptr, size, "â"); break;
|
|
case 227: snprintf(outptr, size, "ã"); break;
|
|
case 228: snprintf(outptr, size, "ä"); break;
|
|
case 229: snprintf(outptr, size, "å"); break;
|
|
case 230: snprintf(outptr, size, "æ"); break;
|
|
case 231: snprintf(outptr, size, "ç"); break;
|
|
case 232: snprintf(outptr, size, "è"); break;
|
|
case 233: snprintf(outptr, size, "é"); break;
|
|
case 234: snprintf(outptr, size, "ê"); break;
|
|
case 235: snprintf(outptr, size, "ë"); break;
|
|
case 236: snprintf(outptr, size, "ì"); break;
|
|
case 237: snprintf(outptr, size, "í"); break;
|
|
case 238: snprintf(outptr, size, "î"); break;
|
|
case 239: snprintf(outptr, size, "ï"); break;
|
|
case 240: snprintf(outptr, size, "ð"); break;
|
|
case 241: snprintf(outptr, size, "ñ"); break;
|
|
case 242: snprintf(outptr, size, "ò"); break;
|
|
case 243: snprintf(outptr, size, "ó"); break;
|
|
case 244: snprintf(outptr, size, "ô"); break;
|
|
case 245: snprintf(outptr, size, "õ"); break;
|
|
case 246: snprintf(outptr, size, "ö"); break;
|
|
case 247: snprintf(outptr, size, "÷"); break;
|
|
case 248: snprintf(outptr, size, "ø"); break;
|
|
case 249: snprintf(outptr, size, "ù"); break;
|
|
case 250: snprintf(outptr, size, "ú"); break;
|
|
case 251: snprintf(outptr, size, "û"); break;
|
|
case 252: snprintf(outptr, size, "ü"); break;
|
|
case 253: snprintf(outptr, size, "ý"); break;
|
|
case 254: snprintf(outptr, size, "þ"); break;
|
|
case 255: snprintf(outptr, size, "ÿ"); break;
|
|
default: *outptr++ = *inptr; *outptr = '\0'; break;
|
|
}
|
|
while (*outptr)
|
|
outptr++;
|
|
|
|
inptr++;
|
|
}
|
|
*outptr = '\0';
|
|
}
|
|
|
|
|
|
|
|
FILE *OpenMacro(const char *filename, int Language, int htmlmode)
|
|
{
|
|
FILE *pLang, *fi = NULL;
|
|
char *temp, *aka, linebuf[1024], outbuf[1024];
|
|
|
|
temp = calloc(PATH_MAX, sizeof(char));
|
|
aka = calloc(81, sizeof(char));
|
|
temp[0] = '\0';
|
|
|
|
if (Language != '\0') {
|
|
/*
|
|
* Maybe a valid language character, try to load the language
|
|
*/
|
|
snprintf(temp, PATH_MAX -1, "%s/etc/language.data", getenv("MBSE_ROOT"));
|
|
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/%s", lang.MacroPath, filename);
|
|
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) {
|
|
Syslog('-', "Macro file \"%s\" for language %c not found, trying default", filename, Language);
|
|
snprintf(temp, PATH_MAX -1, "%s/%s", CFG.bbs_macros, filename);
|
|
fi = fopen(temp,"r");
|
|
}
|
|
|
|
if (fi == NULL)
|
|
WriteError("OpenMacro(%s, %c): not found", filename, Language);
|
|
else {
|
|
snprintf(temp, PATH_MAX -1, "%s-%s", OsName(), OsCPU());
|
|
if (CFG.aka[0].point)
|
|
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);
|
|
else
|
|
snprintf(aka, 80, "%d:%d/%d@%s", CFG.aka[0].zone, CFG.aka[0].net, CFG.aka[0].node, CFG.aka[0].domain);
|
|
|
|
if (htmlmode) {
|
|
MacroVars("O", "s", temp);
|
|
snprintf(linebuf, 1023, "%s", CFG.sysop);
|
|
html_massage(linebuf, outbuf, 1023);
|
|
MacroVars("U", "s", outbuf);
|
|
snprintf(linebuf, 1023, "%s", CFG.location);
|
|
html_massage(linebuf, outbuf, 1023);
|
|
MacroVars("L", "s", outbuf);
|
|
snprintf(linebuf, 1023, "%s", CFG.bbs_name);
|
|
html_massage(linebuf, outbuf, 1023);
|
|
MacroVars("N", "s", outbuf);
|
|
snprintf(linebuf, 1023, "%s", CFG.sysop_name);
|
|
html_massage(linebuf, outbuf, 1023);
|
|
MacroVars("S", "s", outbuf);
|
|
snprintf(linebuf, 1023, "%s", CFG.comment);
|
|
html_massage(linebuf, outbuf, 1023);
|
|
MacroVars("T", "s", outbuf);
|
|
} else {
|
|
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);
|
|
}
|
|
MacroVars("H", "s", CFG.www_url);
|
|
MacroVars("M", "s", CFG.sysdomain);
|
|
MacroVars("V", "s", VERSION);
|
|
MacroVars("Y", "s", aka);
|
|
MacroVars("Z", "d", 0);
|
|
Cookie(htmlmode);
|
|
}
|
|
|
|
free(aka);
|
|
free(temp);
|
|
return fi;
|
|
}
|
|
|
|
|