From c73cc11d7f97698f3deb4924b3b03abed5f1beea Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 4 Dec 2016 17:51:29 +1000 Subject: [PATCH] Updated the rest of the message generating utils to use new msgid generation --- bbs.h | 1 + utils/mgpost/Makefile | 2 +- utils/mgpost/mgpost.c | 286 ++++++++++++++++++++++++++++++++++------ utils/mgpost/mgpost.ini | 9 ++ utils/rascalbot.sh | 26 ---- www_msgs.c | 7 +- 6 files changed, 260 insertions(+), 71 deletions(-) create mode 100644 utils/mgpost/mgpost.ini delete mode 100755 utils/rascalbot.sh diff --git a/bbs.h b/bbs.h index 68cc21a..c109f19 100644 --- a/bbs.h +++ b/bbs.h @@ -204,6 +204,7 @@ extern char *editor(struct user_record *user, char *quote, char *from, int email extern char *external_editor(struct user_record *user, char *to, char *from, char *quote, char *qfrom, char *subject, int email); extern int msg_is_to(struct user_record *user, char *addressed_to, char *address, int type, int rn, int msgconf); extern int msg_is_from(struct user_record *user, char *addressed_from, char *address, int type, int rn, int msgconf); +extern unsigned long generate_msgid(); extern int door_menu(struct user_record *user); extern void rundoor(struct user_record *user, char *cmd, int stdio); diff --git a/utils/mgpost/Makefile b/utils/mgpost/Makefile index 0d59091..66f976c 100644 --- a/utils/mgpost/Makefile +++ b/utils/mgpost/Makefile @@ -2,7 +2,7 @@ CC=cc CFLAGS=-I/usr/local/include DEPS = mgpost.c JAMLIB = ../../jamlib/jamlib.a -OBJ = mgpost.o +OBJ = mgpost.o ../../inih/ini.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/utils/mgpost/mgpost.c b/utils/mgpost/mgpost.c index a12618a..24fea31 100644 --- a/utils/mgpost/mgpost.c +++ b/utils/mgpost/mgpost.c @@ -3,7 +3,9 @@ #include #include #include +#include #include "../../jamlib/jam.h" +#include "../../inih/ini.h" s_JamBase *open_jam_base(char *path) { int ret; @@ -24,8 +26,194 @@ s_JamBase *open_jam_base(char *path) { return jb; } +struct fido_addr { + unsigned short zone; + unsigned short net; + unsigned short node; + unsigned short point; +}; + +struct fido_addr *parse_fido_addr(const char *str) { + struct fido_addr *ret = (struct fido_addr *)malloc(sizeof(struct fido_addr)); + int c; + int state = 0; + + ret->zone = 0; + ret->net = 0; + ret->node = 0; + ret->point = 0; + + for (c=0;czone = ret->zone * 10 + (str[c] - '0'); + break; + case 1: + ret->net = ret->net * 10 + (str[c] - '0'); + break; + case 2: + ret->node = ret->node * 10 + (str[c] - '0'); + break; + case 3: + ret->point = ret->point * 10 + (str[c] - '0'); + break; + } + } + break; + default: + free(ret); + return NULL; + } + } + return ret; +} + + +struct msg_t { + int echo; + char *bbs_path; + char *filename; + char *jambase; + char *from; + char *subject; + char *origin; + struct fido_addr *localAddress; +}; + +static int handler(void* user, const char* section, const char* name, + const char* value) +{ + struct msg_t *msg = (struct msg_t *)user; + + if (strcasecmp(section, "main") == 0) { + if (strcasecmp(name, "echomail") == 0) { + if (strcasecmp(value, "true") == 0) { + msg->echo = 1; + } else { + msg->echo = 0; + } + } else if (strcasecmp(name, "BBS Path") == 0) { + msg->bbs_path = strdup(value); + } else if (strcasecmp(name, "Message File") == 0) { + msg->filename = strdup(value); + } else if (strcasecmp(name, "JAM Base") == 0) { + msg->jambase = strdup(value); + } else if (strcasecmp(name, "From") == 0) { + msg->from = strdup(value); + } else if (strcasecmp(name, "Subject") == 0) { + msg->subject = strdup(value); + } else if (strcasecmp(name, "Local AKA") == 0) { + msg->localAddress = parse_fido_addr(value); + } else if (strcasecmp(name, "Origin Line") == 0) { + msg->origin = strdup(value); + } + } + return 1; +} + +unsigned long generate_msgid(char *bbs_path) { + time_t theTime; + + char buffer[1024]; + + struct tm timeStruct; + struct tm fileStruct; + unsigned long m; + unsigned long y; + unsigned long ya; + unsigned long j; + unsigned long msgid; + unsigned long c; + unsigned long d; + time_t lastread; + unsigned long lastid; + FILE *fptr; + + theTime = time(NULL); + localtime_r(&theTime, &timeStruct); + + m = timeStruct.tm_mon + 1; + y = timeStruct.tm_year + 1900; + d = timeStruct.tm_mday; + + if (m > 2) { + m = m - 3; + } else { + m = m + 9; + y = y - 1; + } + + c = y / 100; + ya = y - 100 * c; + j = (146097 * c) / 4 + (1461 * ya) / 4 + (153 * m + 2) / 5 + d + 1721119; + + msgid = (j % 0x800) * 0x200000; + + snprintf(buffer, 1024, "%s/msgserial", bbs_path); + + fptr = fopen(buffer, "rw"); + if (fptr) { + flock(fileno(fptr), LOCK_EX); + fread(&lastread, sizeof(time_t), 1, fptr); + fread(&lastid, sizeof(unsigned long), 1, fptr); + localtime_r(&lastread, &fileStruct); + + + if (fileStruct.tm_mon != timeStruct.tm_mon || fileStruct.tm_mday != timeStruct.tm_mday || fileStruct.tm_year != timeStruct.tm_year) { + lastread = time(NULL); + lastid = 1; + } else { + lastid++; + } + rewind(fptr); + fwrite(&lastread, sizeof(time_t), 1, fptr); + fwrite(&lastid, sizeof(unsigned long), 1, fptr); + flock(fileno(fptr), LOCK_UN); + fclose(fptr); + } else { + fptr = fopen(buffer, "w"); + if (fptr) { + lastread = time(NULL); + lastid = 1; + flock(fileno(fptr), LOCK_EX); + fwrite(&lastread, sizeof(time_t), 1, fptr); + fwrite(&lastid, sizeof(unsigned long), 1, fptr); + flock(fileno(fptr), LOCK_UN); + fclose(fptr); + } else { + printf("Unable to open message id log\n"); + return 0; + } + } + + msgid += lastid; + + return msgid; +} + int main(int argc, char **argv) { - char buffer[256]; + char buffer[1024]; char *body; char *subject; char *from; @@ -36,44 +224,33 @@ int main(int argc, char **argv) { int z; int i; + struct msg_t msg; + + if (argc < 2) { + printf("Usage: %s inifile\n", argv[0]); + exit(1); + } + + if (ini_parse(argv[1], handler, &msg) <0) { + fprintf(stderr, "Unable to load configuration ini (%s)!\n", argv[1]); + exit(-1); + } + s_JamBase *jb; s_JamMsgHeader jmh; s_JamSubPacket* jsp; s_JamSubfield jsf; - if (argc < 6) { - printf("Usage:\n"); - printf("%s [l|e] filename jambase from subject laddress\n", argv[0]); - printf(" l = Local Message, e = Echomail Message\n"); - printf(" laddress is your network address, and only required on echomail.\n"); - exit(1); - } - if (tolower(argv[1][0]) != 'l' && tolower(argv[1][0]) != 'e') { - printf("Usage:\n"); - printf("%s [l|e] filename jambase from subject laddress\n", argv[0]); - printf(" l = Local Message, e = Echomail Message\n"); - printf(" laddress is your network address, and only required on echomail.\n"); - exit(1); - } - - if (tolower(argv[1][0]) == 'e' && argc < 7) { - printf("Usage:\n"); - printf("%s [l|e] filename jambase from subject laddress\n", argv[0]); - printf(" l = Local Message, e = Echomail Message\n"); - printf(" laddress is your network address, and only required on echomail.\n"); - exit(1); - } - - fptr = fopen(argv[2], "r"); + fptr = fopen(msg.filename, "r"); if (!fptr) { - printf("Unable to open %s\n", argv[2]); + printf("Unable to open %s\n", msg.filename); exit(1); } body = NULL; totlen = 0; - len = fread(buffer, 1, 256, fptr); + len = fread(buffer, 1, 1024, fptr); while (len > 0) { totlen += len; if (body == NULL) { @@ -83,7 +260,7 @@ int main(int argc, char **argv) { } memcpy(&body[totlen - len], buffer, len); body[totlen] = '\0'; - len = fread(buffer, 1, 256, fptr); + len = fread(buffer, 1, 1024, fptr); } fclose(fptr); @@ -93,10 +270,23 @@ int main(int argc, char **argv) { body[i] = '\r'; } } + + if (msg.localAddress->point == 0) { + snprintf(buffer, 1024, "\r--- mgpost\r * Origin: %s (%d:%d/%d)\r", msg.origin, msg.localAddress->zone, msg.localAddress->net, msg.localAddress->node); + } else { + snprintf(buffer, 1024, "\r--- mgpost\r * Origin: %s (%d:%d/%d.%d)\r", msg.origin, msg.localAddress->zone, msg.localAddress->net, msg.localAddress->node, msg.localAddress->point); + } - jb = open_jam_base(argv[3]); + totlen += strlen(buffer); + + body = (char *)realloc(body, totlen + 1); + + memcpy(&body[totlen - strlen(buffer)], buffer, strlen(buffer)); + body[totlen] = '\0'; + + jb = open_jam_base(msg.jambase); if (!jb) { - printf("Unable to open JAM base %s\n", argv[2]); + printf("Unable to open JAM base %s\n", msg.jambase); exit(1); } thetime = time(NULL); @@ -106,17 +296,17 @@ int main(int argc, char **argv) { jmh.Attribute |= MSG_LOCAL; - if (tolower(argv[1][0]) == 'l') { + if (!msg.echo) { jmh.Attribute |= MSG_TYPELOCAL; - } else if (tolower(argv[1][0]) == 'e') { + } else { jmh.Attribute |= MSG_TYPEECHO; } jsp = JAM_NewSubPacket(); jsf.LoID = JAMSFLD_SENDERNAME; jsf.HiID = 0; - jsf.DatLen = strlen(argv[4]); - jsf.Buffer = (char *)argv[4]; + jsf.DatLen = strlen(msg.from); + jsf.Buffer = (char *)msg.from; JAM_PutSubfield(jsp, &jsf); jsf.LoID = JAMSFLD_RECVRNAME; @@ -127,16 +317,34 @@ int main(int argc, char **argv) { jsf.LoID = JAMSFLD_SUBJECT; jsf.HiID = 0; - jsf.DatLen = strlen(argv[5]); - jsf.Buffer = (char *)argv[5]; + jsf.DatLen = strlen(msg.subject); + jsf.Buffer = (char *)msg.subject; JAM_PutSubfield(jsp, &jsf); - if (tolower(argv[1][0]) == 'e') { + if (msg.echo) { + if (msg.localAddress->point == 0) { + sprintf(buffer, "%d:%d/%d", msg.localAddress->zone, msg.localAddress->net, msg.localAddress->node); + } else { + sprintf(buffer, "%d:%d/%d.%d", msg.localAddress->zone, msg.localAddress->net, msg.localAddress->node, msg.localAddress->point); + } + jsf.LoID = JAMSFLD_OADDRESS; jsf.HiID = 0; - jsf.DatLen = strlen(argv[6]); - jsf.Buffer = (char *)argv[6]; + jsf.DatLen = strlen(buffer); + jsf.Buffer = (char *)buffer; JAM_PutSubfield(jsp, &jsf); + + sprintf(buffer, "%d:%d/%d.%d %08lx", msg.localAddress->zone, + msg.localAddress->net, + msg.localAddress->node, + msg.localAddress->point, + generate_msgid(msg.bbs_path)); + + jsf.LoID = JAMSFLD_MSGID; + jsf.HiID = 0; + jsf.DatLen = strlen(buffer); + jsf.Buffer = (char *)buffer; + JAM_PutSubfield(jsp, &jsf); } while (1) { diff --git a/utils/mgpost/mgpost.ini b/utils/mgpost/mgpost.ini new file mode 100644 index 0000000..25af8a8 --- /dev/null +++ b/utils/mgpost/mgpost.ini @@ -0,0 +1,9 @@ +[main] +Echomail = TRUE +BBS Path = /home/andrew/MagickaBBS +Message File = /home/andrew/MagickaBBS/advert.txt +JAM Base = /home/andrew/MagickaBBS/msgs/fsxnet/fsx_bot +From = SysOp +Subject = Magicka BBS Test +Local AKA = 21:1/125.1 +Origin Line = Cauldron, Magicka Test BBS diff --git a/utils/rascalbot.sh b/utils/rascalbot.sh deleted file mode 100755 index 498000c..0000000 --- a/utils/rascalbot.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash - -# BEGIN CONFIGURATION-------------------------------------------------- -MGPOST=/home/pi/MagickaBBS/utils/mgpost/mgpost -NODE=21:1/140 -AREA=/home/pi/MagickaBBS/msgs/fsxnet/fsx_bot -ORIGINTXT="Underland - telnet://andrew.homeunix.org:2023" -FROM=rascal-bot -SEMAPHORE=/home/pi/MagickaBBS/echomail.out -# END CONFIGURATION---------------------------------------------------- - -SUBJECT=`head -1 $1` -TMP=/tmp/$RANDOM-rascal - -mkdir -p $TMP - -tail -n +2 $1 | cat > $TMP/rascal.txt - -echo "" >> $TMP/rascal.txt -echo "--- rascal-bot (v0.1)" >> $TMP/rascal.txt -echo " * Origin: $ORIGINTXT ($NODE)" >> $TMP/rascal.txt - -$MGPOST e $TMP/rascal.txt "$AREA" "$FROM" "$SUBJECT" $NODE -touch $SEMAPHORE - -rm -rf $TMP diff --git a/www_msgs.c b/www_msgs.c index c737d25..c8932d6 100644 --- a/www_msgs.c +++ b/www_msgs.c @@ -672,7 +672,6 @@ int www_send_msg(struct user_record *user, char *to, char *subj, int conference, int max_len; int len; char buffer[256]; - char timestr[17]; char *body2; char *tagline; struct utsname name; @@ -744,13 +743,11 @@ int www_send_msg(struct user_record *user, char *to, char *subj, int conference, jsf.Buffer = (char *)buffer; JAM_PutSubfield(jsp, &jsf); - snprintf(timestr, 16, "%016lx", time(NULL)); - - sprintf(buffer, "%d:%d/%d.%d %s", conf.mail_conferences[conference]->fidoaddr->zone, + sprintf(buffer, "%d:%d/%d.%d %08lx", conf.mail_conferences[conference]->fidoaddr->zone, conf.mail_conferences[conference]->fidoaddr->net, conf.mail_conferences[conference]->fidoaddr->node, conf.mail_conferences[conference]->fidoaddr->point, - ×tr[strlen(timestr) - 8]); + generate_msgid()); jsf.LoID = JAMSFLD_MSGID; jsf.HiID = 0;