home *** CD-ROM | disk | FTP | other *** search
- /* XBBS Home Message System GateKeeper */
- /* Copyright (c) 1989 by M. Kimes */
- /* This could probably use some kind of local dupe checking mechanism... */
- /* Doesn't SHARE yet */
-
- #include "msgg.h"
-
- struct _config conf;
- struct _user user; /* Dodging some kinda bug */
- struct _user useit;
- word userno=0;
-
- char reader[48]="XHMS";
- struct ffblk f;
- char nodenumber=1;
- word othernumber;
-
- void pascal getline (char *);
- char * pascal say_prompt(word);
- char * pascal addtolog (char *);
- void pascal readconfig(void);
- void killall(void);
- ulong merge_area(void);
- char * stripcr(char *);
- char * fidodate(void);
- word find__msgarea(char *,char *);
-
- main (int argc, char *argv[]) {
-
- struct fdate {
- bit day: 5;
- bit month: 4;
- bit year: 7;
- };
- union df {
- struct fdate fd;
- int x;
- } fdf;
- struct date dos_date;
- FILE *fp;
- char s[256];
- char *pp;
- word zone;
- word net;
- word node;
- ulong tempposts;
- char indir[133]="MPORT"; /* Would be MPORT0 for node 0, MPORT2 for node 2, etc. */
-
- if(argc>1) nodenumber=(char)atoi(argv[1]);
- if(nodenumber!=1) {
- sprintf(indir,"MPORT%01hu",nodenumber);
- }
-
- printf("\n\n\n\x1b[A\x1b[0;2;37;7mXGATEKPR copyright (c) 1989/90 by M. Kimes running...\x1b[0;2;37m\n");
- printf(" (Pass node number if not 1)\n");
-
- readconfig(); /* Get config info */
- if (!conf.lastusernum) { /* Check to see valid user was last */
- addtolog("XGATEKPR: Last user number was 0");
- printf("\nLast user number is 0, aborting...\n");
- exit(253);
- }
- sprintf(s,"%sonline.xbs",conf.homepath);
- if((fp=fopen(s,"rb"))) {
- fread(&useit,sizeof(struct _user),1,fp);
- fread(&userno,sizeof(userno),1,fp);
- fclose(fp);
- if (userno!=conf.lastusernum) {
-
- char temp[30];
- char logem=60;
- char keyin=0;
-
- printf("\nMismatch between user number from ONLINE.XBS and CONFIG.BBS!\x7\n");
- printf("\n[ESC] to process as %s, [Enter] to abort: ",conf.lastcaller);
- while (--logem) {
- printf("%s%s%02u",BACKSPACE,BACKSPACE,logem);
- if (kbhit()) keyin=(char)getch();
- if (keyin==13) {
- printf("\nAborting...\n");
- exit(0);
- }
- if (keyin==27) {
- printf("\nProcessing anyway...\n");
- break;
- }
- if (!(logem % 5)) printf("\x7");
- sleep(1);
- }
- if (keyin!=27) {
- printf("\nTime out: aborting...\n");
- addtolog("XGATEKPR: Mismatch in last user # ONLINE.XBS <-> CONFIG.BBS");
- exit(240);
- }
- }
- }
- if(findfirst("users.bbs",&f,0)) { /* Get user info */
- addtolog("XGATEKPR: Can't find userfile");
- printf("\nYou don't have a userfile.\n");
- exit(253);
- }
- if (!(fp=fopen("users.bbs","rb"))) {
- addtolog("XGATEKPR: Can't open userfile");
- printf("\nError opening userfile.\n");
- exit(253);
- }
- fseek(fp,((long)sizeof(struct _user)*(long)(conf.lastusernum-1L)),SEEK_SET);
- fread(&useit,sizeof(struct _user),1,fp);
- fclose(fp);
- tempposts=useit.posts;
-
- printf("\nUser#%u: %s (%s)\n",conf.lastusernum,useit.name,useit.handle);
-
- sprintf(s,"%s\\*.RPK",indir);
- if (findfirst(s,&f,0)) { /* Check for packet */
- addtolog("XGATEKPR: No mail to process");
- printf("\nNo mail to process in directory %s.\n",indir);
- exit(254);
- }
- if (chdir (indir)) { /* Switch to response directory */
- addtolog("XGATEKPR: Directory switching error");
- printf("\nCouldn't switch to response directory\n");
- printf("\nAborting...\n");
- exit(252);
- }
- sprintf(s,"%04x%04x.RPK",conf.net,conf.node); /* Check filename */
- strupr(s);
- strupr(f.ff_name);
- if (strcmp(s,f.ff_name)) {
- printf("\nMail packet %s is not for this BBS!\n",f.ff_name);
- remove(f.ff_name);
- chdir("..");
- sprintf(s,"XGATEKPR: Mail packet %s not for us",f.ff_name);
- addtolog(s);
- exit(247);
- }
-
- printf("\nDisassembling reply packet...\n");
- sprintf(s,say_prompt(357),f.ff_name);
- system (s); /* Unarchive packet */
- remove(f.ff_name);
- if (findfirst("rareas.xbs",&f,0)) f.ff_ftime=1;
- if (f.ff_ftime!=0) {
- printf("\nFile has been tampered with!\n");
- printf("\nClearing out response directory...\n");
- killall();
- chdir("..");
- addtolog("XGATEKPR: Tampered files wiped");
- exit(248);
- }
- getdate(&dos_date);
- fdf.x=f.ff_fdate;
- if (((fdf.fd.year+1980)<(dos_date.da_year-1)) || fdf.fd.month>dos_date.da_mon || fdf.fd.day>dos_date.da_day) {
- printf("\nQuestionable packet date: %02u/%02u/%02u -- %02d/%02hu/%02hu\n",(word)((fdf.fd.year+80)%100),(word)fdf.fd.month,(word)fdf.fd.day,dos_date.da_year,dos_date.da_mon,dos_date.da_day);
- sprintf(s,"XGATEKPR: Questionable pkt date: %02u/%02u/%04u",(word)fdf.fd.month,(word)fdf.fd.day,(word)(fdf.fd.year+1980));
- addtolog(s);
- }
- if (!(fp=fopen("rareas.xbs","r"))) {
- addtolog("XGATEKPR: No RAREAS.XBS in packet");
- printf("\nCan't open RAREAS.XBS control file\n");
- printf("\nClearing out response directory...\n");
- killall();
- chdir("..");
- exit(251);
- }
- fgets(s,256,fp); /* Check first line for valid address */
- stripcr(s);
- zone=(word)atol(strtok(s,":"));
- net=(word)atol(strtok(0,"/"));
- node=(word)atol(strtok(0,"."));
- pp=strtok(0,"-");
- if (pp) strncpy(reader,pp,48);
- reader[47]=0;
- printf("\nPacket is for %u:%u/%u\n",zone,net,node);
- if (zone!=conf.zone || net!=conf.net || node!=conf.node) {
- printf("\nReply packet is not for this BBS!\n");
- printf("\nClearing out response directory...\n");
- killall();
- chdir("..");
- sprintf(s,"XGATEKPR: Mail packet for %u:%u/%u",zone,net,node);
- addtolog(s);
- exit(250);
- }
-
- /* Ok, we've got an apparently valid RAREAS.XBS control file.
- Now we can process the msg areas listed in it, merging them
- into our own message base */
-
- printf("Processing reply packet from %s...\n",reader);
- while (!feof(fp)) { /* Get area from uploaded list */
- if (!fgets(s,255,fp)) break;
- if (*s=='\n' || *s==';') continue;
- stripcr(s);
- strncpy(conf.mboard.name,strtok(s,","),48);
- conf.mboard.name[47]=0;
- conf.mboard.attr=(word)atoi(strtok(0," ,"));
- conf.mboard.max=(word)atoi(strtok(0," ,"));
- conf.mboard.number=(word)atoi(strtok(0," ,"));
- if(conf.mboard.attr & READONLY || conf.mboard.attr & NET || conf.mboard.attr & ALTERNATE) {
- printf("\nMsg in unsupported area type\n");
- continue;
- }
- if (conf.mboard.number) { /* Check for moved area against BBS list
- and use our info, anyway */
-
- word tempnumber;
- char tmp[133];
-
- sprintf(tmp,"%sMSGAREAS.XBS",conf.messpath);
- tempnumber=find__msgarea(tmp,conf.mboard.name);
- if (!tempnumber) { /* Couldn't find area at all in BBS list */
- printf("\nUnmatched or unsupported area #%u: `%s' untossed\n",conf.mboard.number,conf.mboard.name);
- sprintf(s,"XGATEKPR: Unmatched or unsupported area #%u: `%s'",conf.mboard.number,conf.mboard.name);
- addtolog(s);
- continue;
- }
- if (tempnumber!=conf.mboard.number) { /* Correct moved area # */
- printf("\nArea #%u (%s) changed to #%u per MSGAREAS.XBS\n",conf.mboard.number,conf.mboard.name,tempnumber);
- sprintf(s,"XGATEKPR: Area #%u (%s) -> #%u",conf.mboard.number,conf.mboard.name,tempnumber);
- addtolog(s);
- }
- othernumber=conf.mboard.number;
- conf.mboard.number=tempnumber;
- }
- useit.posts+=merge_area();
- }
- fclose(fp);
-
- /* We've finished processing the mail, clean up */
-
- printf("\nMail processing complete...imported %lu msgs.\n",useit.posts-tempposts);
- killall();
- chdir("..");
-
- /* Update number of user posts if we imported anything */
-
- if (useit.posts!=tempposts) {
- if (!(fp=fopen("users.bbs","rb"))) {
- printf("\nError opening userfile.\n");
- exit(250);
- }
- fseek(fp,((long)sizeof(struct _user)*((long)conf.lastusernum-1L)),SEEK_SET);
- fwrite(&useit,sizeof(struct _user),1,fp);
- fclose(fp);
- }
- exit(0); /* We are outta here */
- }
-
-
-
- char * rstrip (char *a)
-
- {
- while ((strlen(a)) && (a[strlen(a)-1]==' ')) a[strlen(a)-1]=0;
- return a;
- }
-
-
-
- char * lstrip (char *a)
-
- {
- while ((strlen(a)) && (*a==' ')) memmove (a,(a+1),strlen(a));
- return (a);
- }
-
-
-
- char *stripcr (char *a)
-
- {
-
- while (a[strlen(a)-1]=='\n' || a[strlen(a)-1]=='\r') a[strlen(a)-1]=0;
- return a;
-
- }
-
-
-
- void killall (void) {
-
- if (findfirst("*.*",&f,0)) return;
- remove(f.ff_name);
- while (!findnext(&f)) remove (f.ff_name);
- }
-
-
- void pascal readconfig (void) {
-
- FILE *fp;
- char s[15];
-
- if (nodenumber!=1) sprintf(s,"config%01hu.bbs",nodenumber);
- else strcpy(s,"config.bbs");
-
- if(!(fp=fopen(s,"rb"))) {
- Fatal:
- printf("\nFatal Error\n");
- perror("\nCONFIG ERROR");
- chdir("..");
- exit(254);
- }
- if (fread(&conf,sizeof(conf),1,fp)!=1) goto Fatal;
- fclose(fp);
-
- }
-
-
- ulong merge_area (void) {
-
- char filename[132];
- char textname[132];
- char arcfile[20];
- char arctext[20];
- char message[81];
- char origin[67]="";
- struct ffblk origsize;
- struct ffblk replysize;
- FILE *origfile;
- FILE *origtext;
- FILE *replyfile;
- FILE *replytext;
- FILE *pp;
- char *p;
- word lenmess;
- word exported=0;
- char once=0;
- struct _xmsg msg;
- char *hold;
- char *usethis;
- word messno=1;
- word nomess;
- char lastreply[145]="";
- char assocfile[145]="";
- static long counter;
- char idstr[12];
-
- sprintf(filename,"%sXDATA.%03x",conf.messpath,conf.mboard.number);
- sprintf(textname,"%sXTEXT.%03x",conf.messpath,conf.mboard.number);
- sprintf(arcfile,"XXDATA.%03x",othernumber);
- sprintf(arctext,"XXTEXT.%03x",othernumber);
-
- if (findfirst(arctext,&f,0)) {
- printf("\nArea #%u (%s) listed but not present\n",othernumber,conf.mboard.name);
- BadArea:
- remove(arcfile);
- remove(arctext);
- return 0;
- }
- if (findfirst(arcfile,&replysize,0)) {
- printf("\nArea #%u (%s) listed but not present\n",othernumber,conf.mboard.name);
- goto BadArea;
- }
- if (replysize.ff_fsize==0) {
- printf("\nArea #%u (%s) is empty.\n",othernumber,conf.mboard.name);
- goto BadArea;
- }
- if (findfirst(filename,&origsize,0)) origsize.ff_fsize=0;
- nomess=(word)(replysize.ff_fsize/(long)sizeof(struct _xmsg));
-
- if (!(replyfile=fopen(arcfile,"rb"))) {
- printf("\nUnable to open reply datafile\n");
- return 0;
- }
- if (!(replytext=fopen(arctext,"rb"))) {
- printf("\nUnable to open reply textfile\n");
- fclose(replyfile);
- return 0;
- }
- if (!(origfile=fopen(filename,"a+b"))) {
- printf("\nUnable to open BBS datafile\n");
- fclose(replyfile);
- fclose(replytext);
- return 0;
- }
- if (!(origtext=fopen(textname,"a+b"))) {
- printf("\nUnable to open BBS textfile\n");
- return 0;
- }
- fseek(origtext,0L,SEEK_END);
- fseek(origfile,0L,SEEK_END);
- fseek(replyfile,0L,SEEK_SET);
- fseek(replytext,0L,SEEK_SET);
-
- printf("\nWorking on #%u...",conf.mboard.number);
-
- while (messno<(nomess+1)) {
-
- if ((fseek(replyfile,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET)) || (fread(&msg,sizeof(struct _xmsg),1,replyfile)!=1)) {
- if (ferror(replyfile)) perror ("\nSEEK ERROR");
- fclose(replyfile);
- goto EndIt;
- }
-
- #ifdef DEBUG
-
- printf("\nMSG.LENGTH: %u bytes\n",msg.length);
-
- #endif
-
- once=0;
- TryThatAgain:
- if (fseek(replytext,msg.start,SEEK_SET)) {
- if (ferror(replytext)) {
- if (!once) {
- once++;
- goto TryThatAgain;
- }
- else perror ("\nSEEK ERROR");
- goto EndIt;
- }
- }
- hold=(char *)malloc(msg.length+1);
- if (hold==NULL) {
- printf("\nOut of memory\n");
- chdir("..");
- exit(249);
- }
- fread(hold,msg.length,1,replytext);
- hold[msg.length-1]=0;
-
- #ifdef DEBUG
-
- printf("\nLENGTH before strip: %u bytes\n",strlen(hold));
-
- #endif
- /* Strip out nasty stuff */
-
- *lastreply=0;
- *assocfile=0;
- if(p=strstr(hold,"\01REPLY: ")) {
- if(p==hold) {
- p++;
- usethis=p;
- if(p=strchr(usethis,'\r')) {
- p++;
- usethis=p;
- }
- }
- else usethis=hold;
- }
- else usethis=hold;
- if(p=strstr(usethis,"\01ASSOC: ")) {
- if(p==usethis) {
- p++;
- usethis=p;
- }
- }
-
- while (p=strstr(usethis,"\r\n")) memmove(&p[1],&p[2],strlen(&p[2])+1);
- while (p=strchr(usethis,'\n')) *p='\r';
- while (p=strstr(usethis," \x8d")) memmove(&p[1],&p[2],strlen(&p[2])+1);
- while (p=strchr(usethis,'\x8d')) *p=' ';
- while (p=strstr(usethis,"\r--- ")) memmove(&p[2],&p[3],strlen(&p[3])+1);
- while (p=strstr(usethis,"\r * Origin: ")) p[2]='@';
- while (p=strchr(usethis,'\x1')) *p='@';
- while(hold[strlen(hold)-1]=='\r' && hold[strlen(hold)-2]=='\r') hold[strlen(hold)-1]=0;
-
- #ifdef DEBUG
-
- printf("\nLENGTH after strip: %u bytes\n",strlen(hold));
-
- #endif
-
-
- msg.start=ftell(origtext);
- msg.attr = msg.attr | MSGLOCAL;
- msg.m_attr = msg.m_attr & (~MSGTAGGED);
- msg.subj[63]=0;
- msg.d_point=0;
- msg.o_point=0;
- msg.times=0;
- lenmess=0;
-
- /* Build 'unique id string' for MSGID using time & counter
- You would have to import more than 15 msgs/sec to get a
- dupe MSGID, in which case you could shift two bytes of
- the counter into the id string (%256, idstr[10]=0, &idstr[2]) */
-
- sprintf(idstr,"%08lX%01hX",time(NULL),(char)(counter%16L));
- idstr[9]=0;
- counter++;
-
- if(nodenumber==1) {
- if((conf.mboard.attr & ALTECHO) || (conf.mboard.attr & ALTERNATE)) lenmess+=fprintf(origtext,"\01MSGID: %u:%u/%u.0@%s %-8.8s\r", conf.alt_zone,conf.alt_net,conf.alt_node,conf.alt_domain,&idstr[1]);
- else lenmess+=fprintf(origtext,"\01MSGID: %u:%u/%u.0@%s %-8.8s\r", conf.zone,conf.net,conf.node,conf.domain,&idstr[1]);
- }
- else {
-
- /* Note: Non-standard but very smart */
-
- if((conf.mboard.attr & ALTECHO) || (conf.mboard.attr & ALTERNATE)) lenmess+=fprintf(origtext,"\01MSGID: %u:%u/%u.0.n%01hu@%s %-8.8s\r", conf.alt_zone,conf.alt_net,conf.alt_node,nodenumber,conf.alt_domain,&idstr[1]);
- else lenmess+=fprintf(origtext,"\01MSGID: %u:%u/%u.0.n%01hu@%s %-8.8s\r", conf.zone,conf.net,conf.node,nodenumber,conf.domain,&idstr[1]);
- }
-
- if(*lastreply) {
- lenmess+=fprintf(origtext,"%s",lastreply);
- *lastreply=0;
- }
- if(*assocfile) {
- lenmess+=fprintf(origtext,"%s",assocfile);
- *assocfile=0;
- }
-
- if (conf.mboard.attr & REAL) strcpy(msg.from,useit.name);
- else strcpy(msg.from,useit.handle);
-
- fwrite(hold,strlen(hold),1,origtext);
- lenmess+=strlen(hold);
- if ((conf.mboard.attr & ALTECHO) || (conf.mboard.attr & ECHO)) {
- if (!*origin) {
-
- char originpath[133];
-
- sprintf(originpath,"%sORIGINS.BBS",conf.messpath);
- if(!(pp=fopen(originpath,"r"))) {
- strcpy(origin,conf.origin);
- goto SkipOrigins;
- }
- rewind(pp);
- while (!(feof(pp)) && ((word)atol(message)!=conf.mboard.number)) {
- fgets(message,80,pp);
- message[66]=0;
- stripcr(message);
- }
- fclose(pp);
- if ((word)atol(message)!=conf.mboard.number) {
- strcpy(origin,conf.origin);
- }
- else {
- strtok(message,"; ");
- strcpy(origin,strtok(0,"\n"));
- }
- }
- SkipOrigins:
- if(conf.mboard.attr & ALTECHO) lenmess+=(fprintf(origtext,"\r\n--- XBBS/%s\r\n * Origin: %s (%u:%u/%u)\r\n",reader,origin,conf.alt_zone,conf.alt_net,conf.alt_node));
- lenmess+=(fprintf(origtext,"\r\n--- XBBS/%s\r\n * Origin: %s (%u:%u/%u)\r\n",reader,origin,conf.zone,conf.net,conf.node));
- }
- else {
- fputc('\r',origtext);
- lenmess++;
- }
-
- GotOrigin:
- fputc('\0',origtext);
- lenmess+=2;
-
- #ifdef DEBUG
-
- printf("\nLENGTH in header: %u bytes\n",lenmess);
-
- #endif
-
- msg.length=lenmess;
- msg.m_attr |= MSGTREATED;
- fwrite(&msg,sizeof(struct _xmsg),1,origfile);
- if (hold) free(hold);
- exported++;
- messno++;
- printf("%-5u\b\b\b\b\b",exported);
- }
- EndIt:
- fclose(replyfile);
- fclose(replytext);
- fclose(origfile);
- fclose(origtext);
- if (exported) printf("Imported %u msgs",exported);
- sprintf(message,"XGATEKPR: Imported %u msgs to area #%u",exported,conf.mboard.number);
- addtolog(message);
- return (ulong)exported;
-
- }
-
-
- char * fidodate (void)
-
- {
-
- char months[12][4]={
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec"
- };
- static char fdate[20];
- struct date dos_date;
- struct time dos_time;
-
- /* 26 Jul 89 06:23:47 */
-
- getdate(&dos_date);
- gettime(&dos_time);
-
- sprintf(fdate,"%02hu %s %02d %02hu:%02hu:%02hu",dos_date.da_day,months[dos_date.da_mon-1],dos_date.da_year%100,dos_time.ti_hour,dos_time.ti_min,dos_time.ti_sec);
- return(fdate);
-
- }
-
-
- char * pascal addtolog (char *text) { /* WRITE LOGFILE ENTRIES */
-
- FILE *pf;
- char p[127];
-
- pf = fopen(conf.logfile,"a");
- if (pf == NULL) return text;
- fseek(pf,0,SEEK_END);
- if (text[0]!='*') {
- strcpy(p,fidodate());
- p[16]=0;
- strcat(p," ");
- strncat(p,text,126-strlen(p));
- p[126]=0;
- }
- else {
- strncpy(p,text,79);
- p[79]=0;
- }
- fputs(p,pf);
- if(nodenumber!=1) fprintf(pf," (Node %01hu)",nodenumber);
- fputs("\n",pf);
- fclose(pf);
- return text;
-
- }
-
-
- word find__msgarea (char *filename,char *areaname) {
-
- FILE *fp;
- char s[256];
- char *p;
- word attr;
-
- fp=fopen(filename,"r");
- if (fp==NULL) {
- printf("\nCan't open %s...continuing...\n",filename);
- return (conf.mboard.number);
- }
- strupr(areaname);
- while (!feof(fp) && !ferror(fp)) {
- if (!fgets(s,256,fp)) break;
- if (*s=='\n' || *s==';') continue;
- stripcr(s);
- strtok(s,",");
- strupr(s);
- if (strcmp(s,areaname)) continue;
- conf.mboard.attr=(word)atoi(strtok(0," ,"));
- conf.mboard.max=(word)atoi(strtok(0," ,"));
- fclose(fp);
- if(conf.mboard.attr & READONLY || conf.mboard.attr & NET || conf.mboard.attr & ALTERNATE) {
- printf("\nUnsupported area type skipped...\n");
- return 0;
- }
- return((word)atol(strtok(0," ,")));
- }
- fclose(fp);
- return (0);
- }
-
-
- char * pascal say_prompt (word x) {
-
- char ss[]="LHARC e %s";
- char s[257];
- long pos;
- FILE *fp;
-
- fp=fopen(searchpath("XBBS.IDX"),"rb");
- if(!fp) return ss;
- if(fseek(fp,(long)(x*(word)sizeof(long)),SEEK_SET)!=0) return ss;
- if(fread(&pos,sizeof(long),1,fp)!=1) return ss;
- fclose(fp);
- fp=fopen(searchpath("XBBS.TXT"),"r");
- if(!fp) return ss;
- if(fseek(fp,pos,SEEK_SET)!=0) {
- fclose(fp);
- return ss;
- }
- if(!fgets(s,256,fp)) {
- fclose(fp);
- return ss;
- }
- fclose(fp);
- if(s[strlen(s)-1]=='\n') s[strlen(s)-1]=0;
- if(s[strlen(s)-1]==';') s[strlen(s)-1]=0;
- if(!*s || !s[1]) return ss;
- return &s[1];
- }