sbbsecho.c 142 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* SBBSECHO.C */

/* Developed 1990-2000 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885	*/

/* Portions written by Allen Christiansen 1994-1996 						*/

/* Synchronet FidoNet EchoMail Scanning/Tossing and NetMail Tossing Utility */

#ifdef _WIN32
	#include <windows.h>
	#include <io.h>			/* filelength */
	#include <direct.h>		/* _mkdir */
#endif

#include <dos.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <fcntl.h>
#include <share.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <sys\stat.h>

#ifdef __WATCOMC__
	#include <mem.h>
#endif

33
34
35
36
#ifndef __FreeBSD__
	#include <malloc.h>
#endif

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "sbbs.h"			/* load_cfg() */
#include "sbbsdefs.h"
#include "smblib.h"
#include "scfglib.h"
#include "post.h"
#include "lzh.h"
#include "sbbsecho.h"

#ifdef __TURBOC__
    unsigned _stklen=20000;
#endif

#ifdef __WATCOMC__
    #define O_DENYNONE SH_DENYNO
#endif

const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
const char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
            ,"Jul","Aug","Sep","Oct","Nov","Dec"};

smb_t *smb,*email;
long misc=(IMPORT_PACKETS|IMPORT_NETMAIL|IMPORT_ECHOMAIL|EXPORT_ECHOMAIL
			|DELETE_NETMAIL|DELETE_PACKETS);
ulong netmail=0;
char tmp[256],pkt_type=0;
int secure,cur_smb=0;
FILE *fidologfile=NULL;
two_two_t two_two;
two_plus_t two_plus;

faddr_t		sys_faddr;
config_t	cfg;
scfg_t		scfg;

rswindell's avatar
rswindell committed
71
72
73
ulong	crc32(char *buf, ulong len);


74
BOOL pause_on_exit=FALSE;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#ifdef __WATCOMC__
/******************************************************************************
 A DOS to Unix function - because WATCOM doesn't have one
******************************************************************************/
time_t dostounix(struct date *d,struct time *t)
{
	struct tm tm;

tm.tm_sec=t->ti_sec;
tm.tm_min=t->ti_min;
tm.tm_hour=t->ti_hour;
tm.tm_mday=d->da_day;
tm.tm_mon=(d->da_mon-1);
tm.tm_year=d->da_year-1900;
tm.tm_isdst=0;

return(mktime(&tm));
}
#endif

#ifndef __NT__
#define delfile(x) remove(x)
#else
int delfile(char *filename)
{
	int i=0;

while(remove(filename) && i++<120)	/* Wait up to 60 seconds to delete file */
	delay(500); 					/* for Win95 bug fix */
return(i);
}
#endif

rswindell's avatar
rswindell committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/******************************************************************************
 Displays characters locally
******************************************************************************/
long lputs(char *str)
{
    char tmp[256];
    int i,j,k;


	j=strlen(str);
	for(i=k=0;i<j;i++)      /* remove CRs */
		if(str[i]==CR && str[i+1]==LF)
			continue;
		else
			tmp[k++]=str[i];
	tmp[k]=0;
	return(fputs(tmp,stdout));
}

/****************************************************************************/
/* Performs printf() through local assembly routines                        */
/* Called from everywhere                                                   */
/****************************************************************************/
int lprintf(char *fmat, ...)
{
	va_list argptr;
	char sbuf[256];
	int chcount;

	va_start(argptr,fmat);
139
	chcount=vsnprintf(sbuf,sizeof(sbuf),fmat,argptr);
140
	sbuf[sizeof(sbuf)-1]=0;
rswindell's avatar
rswindell committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	va_end(argptr);
	lputs(sbuf);
	return(chcount);
}
#if 0

/****************************************************************************/
/* Converts an ASCII Hex string into an ulong                       */
/****************************************************************************/
ulong ahtoul(char *str)
{
	ulong l,val=0;

	while((l=(*str++)|0x20)!=0x20)
		val=(l&0xf)+(l>>6&1)*9+val*16;
	return(val);
}
/****************************************************************************/
/* Truncates white-space chars off end of 'str' and terminates at first tab */
/****************************************************************************/
void truncsp(char *str)
{
	int c;

	c=strlen(str);
	while(c && (uchar)str[c-1]<=SP) c--;
	str[c]=0;
}

/****************************************************************************/
/* Puts a backslash on path strings 										*/
/****************************************************************************/
void backslash(char *str)
{
    int i;

	i=strlen(str);
	if(i && str[i-1]!='\\' && str[i-1]!='/') {
		str[i]=BACKSLASH; 
		str[i+1]=0; 
	}
}

/****************************************************************************/
/* Puts a backslash on path strings if not just a drive letter and colon	*/
/****************************************************************************/
void backslashcolon(char *str)
{
    int i;

	i=strlen(str);
	if(i && str[i-1]!='\\' && str[i-1]!='/' && str[i-1]!=':') {
		str[i]=BACKSLASH; 
		str[i+1]=0; 
	}
}

/****************************************************************************/
/* Updates 16-bit "rcrc" with character 'ch'                                */
/****************************************************************************/
void ucrc16(uchar ch, ushort *rcrc) 
{
	ushort i, cy;
    uchar nch=ch;
 
	for (i=0; i<8; i++) {
		cy=*rcrc & 0x8000;
		*rcrc<<=1;
		if (nch & 0x80) *rcrc |= 1;
		nch<<=1;
		if (cy) *rcrc ^= 0x1021; }
}

/****************************************************************************/
/* Returns CRC-16 of ASCIIZ string (not including terminating NULL)			*/
/****************************************************************************/
ushort DLLCALL crc16(char *str)
{
	int 	i=0;
	ushort	crc=0;

	ucrc16(0,&crc);
	while(str[i])
		ucrc16(str[i++],&crc);
	ucrc16(0,&crc);
	ucrc16(0,&crc);
	return(crc);
}
#endif
230
231
232
233
234
235
236
237
238
239
/**********************/
/* Log print function */
/**********************/
void logprintf(char *str, ...)
{
    va_list argptr;
    char buf[256];
    time_t now;
    struct tm *gm;

240
241
242
243
244
245
246
247
248
249
250
251
252
	if(!(misc&LOGFILE) || fidologfile==NULL)
		return;
	va_start(argptr,str);
	vsnprintf(buf,sizeof(buf),str,argptr);
	buf[sizeof(buf)-1]=0;
	va_end(argptr);
	now=time(NULL);
	gm=localtime(&now);
	fseek(fidologfile,0L,SEEK_END);
	fprintf(fidologfile,"%02u/%02u/%02u %02u:%02u:%02u %s\r\n"
		,gm->tm_mon+1,gm->tm_mday,TM_YEAR(gm->tm_year),gm->tm_hour,gm->tm_min,gm->tm_sec
		,buf);
	fflush(fidologfile);
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
}

/*****************************************************************************/
/* Returns command line generated from instr with %c replacments             */
/*****************************************************************************/
char *cmdstr(scfg_t* cfg, char *instr, char *fpath, char *fspec)
{
    static char cmd[128];
    char str[256],str2[128];
    int i,j,len;

len=strlen(instr);
for(i=j=0;i<len && j<128;i++) {
    if(instr[i]=='%') {
        i++;
        cmd[j]=0;
        switch(toupper(instr[i])) {
            case 'F':   /* File path */
                strcat(cmd,fpath);
                break;
            case 'G':   /* Temp directory */
                if(cfg->temp_dir[0]!='\\' && cfg->temp_dir[1]!=':') {
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->temp_dir);
277
                    if(FULLPATH(str2,str,40))
278
279
280
281
282
283
284
285
286
287
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str);}
                else
                    strcat(cmd,cfg->temp_dir);
                break;
            case 'J':
                if(cfg->data_dir[0]!='\\' && cfg->data_dir[1]!=':') {
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->data_dir);
288
                    if(FULLPATH(str2,str,40))
289
290
291
292
293
294
295
296
297
298
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str); }
                else
                    strcat(cmd,cfg->data_dir);
                break;
            case 'K':
                if(cfg->ctrl_dir[0]!='\\' && cfg->ctrl_dir[1]!=':') {
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->ctrl_dir);
299
                    if(FULLPATH(str2,str,40))
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str); }
                else
                    strcat(cmd,cfg->ctrl_dir);
                break;
            case 'N':   /* Node Directory (same as SBBSNODE environment var) */
                strcat(cmd,cfg->node_dir);
                break;
            case 'O':   /* SysOp */
                strcat(cmd,cfg->sys_op);
                break;
            case 'Q':   /* QWK ID */
                strcat(cmd,cfg->sys_id);
                break;
            case 'S':   /* File Spec */
                strcat(cmd,fspec);
                break;
            case '!':   /* EXEC Directory */
                if(cfg->exec_dir[0]!='\\' && cfg->exec_dir[1]!=':') {
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->exec_dir);
322
                    if(FULLPATH(str2,str,40))
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str); }
                else
                    strcat(cmd,cfg->exec_dir);
                break;
            case '#':   /* Node number (same as SBBSNNUM environment var) */
                sprintf(str,"%d",cfg->node_num);
                strcat(cmd,str);
                break;
            case '*':
                sprintf(str,"%03d",cfg->node_num);
                strcat(cmd,str);
                break;
            case '%':   /* %% for percent sign */
                strcat(cmd,"%");
                break;
            default:    /* unknown specification */
				printf("ERROR Checking Command Line '%s'\n",instr);
				logprintf("ERROR line %d Checking Command Line '%s'",__LINE__
					,instr);
				bail(1);
                break; }
        j=strlen(cmd); }
    else
        cmd[j++]=instr[i]; }
cmd[j]=0;

return(cmd);
}

/****************************************************************************/
/* Runs an external program directly using spawnvp							*/
/****************************************************************************/
int execute(char *cmdline)
{
	char c,d,e,cmdlen,*arg[30],str[256];
	int i;

	strcpy(str,cmdline);
	arg[0]=str;	/* point to the beginning of the string */
	cmdlen=strlen(str);
	for(c=0,d=1,e=0;c<cmdlen;c++,e++)	/* Break up command line */
		if(str[c]==SP) {
			str[c]=0;			/* insert nulls */
			arg[d++]=str+c+1;	/* point to the beginning of the next arg */
			e=0; }
	arg[d]=0;
	i=spawnvp(P_WAIT,arg[0],arg);
	return(i);
}
/******************************************************************************
 Returns the system address with the same zone as the address passed
******************************************************************************/
faddr_t getsysfaddr(short zone)
{
	int i;
	faddr_t sysfaddr;

	sysfaddr.zone=sysfaddr.net=sysfaddr.node=1;
	sysfaddr.point=0;
	if(!scfg.total_faddrs)
		return(sys_faddr);
	sysfaddr=scfg.faddr[0];
	if(scfg.total_faddrs==1)
		return(sysfaddr);
	for(i=0;i<scfg.total_faddrs;i++)
		if(scfg.faddr[i].zone==zone)
			return(scfg.faddr[i]);
	return(sysfaddr);
}
/******************************************************************************
 This function creates or appends on existing Binkley compatible .?LO file
 attach file.
 Returns 0 on success.
******************************************************************************/
int write_flofile(char *attachment, faddr_t dest)
{
	char fname[256],outbound[128],str[128],ch;
	ushort attr=0;
	int i,file;
	FILE *stream;

	i=matchnode(dest,0);
	if(i<(int)cfg.nodecfgs)
		attr=cfg.nodecfg[i].attr;

rswindell's avatar
rswindell committed
410
411
412
413
	if(attr&ATTR_CRASH) ch='c';
	else if(attr&ATTR_HOLD) ch='h';
	else if(attr&ATTR_DIRECT) ch='d';
	else ch='f';
414
415
416
	if(dest.zone==scfg.faddr[0].zone)		/* Default zone, use default outbound */
		strcpy(outbound,cfg.outbound);
	else								/* Inter-zone outbound is OUTBOUND.XXX */
rswindell's avatar
rswindell committed
417
		sprintf(outbound,"%.*s.%03x\\"
418
419
			,strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
	if(dest.point) {					/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
420
		sprintf(str,"%04x%04x.pnt"
421
422
423
424
			,dest.net,dest.node);
		strcat(outbound,str); }
	if(outbound[strlen(outbound)-1]=='\\')
		outbound[strlen(outbound)-1]=0;
rswindell's avatar
rswindell committed
425
	MKDIR(outbound);
426
427
	strcat(outbound,"\\");
	if(dest.point)
rswindell's avatar
rswindell committed
428
		sprintf(fname,"%s%08x.%clo",outbound,dest.point,ch);
429
	else
rswindell's avatar
rswindell committed
430
		sprintf(fname,"%s%04x%04x.%clo",outbound,dest.net,dest.node,ch);
431
	if((stream=fnopen(&file,fname,O_WRONLY|O_CREAT|O_TRUNC))==NULL) {
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
		printf("\7ERROR line %d opening %s %s\n",__LINE__,fname,sys_errlist[errno]);
		logprintf("ERROR line %d opening %s %s",__LINE__,fname,sys_errlist[errno]);
		return(-1); }

	fseek(stream,0L,SEEK_END);
	fprintf(stream,"^%s\r\n",attachment);
	fclose(stream);
	return(0);
}

/******************************************************************************
 This function will create a netmail message (.MSG format).
 If file is non-zero, will set file attachment bit (for bundles).
 Returns 0 on success.
******************************************************************************/
int create_netmail(char *to,char *subject,char *body,faddr_t dest,int file)
{
	FILE *fstream;
	char str[256],fname[256];
	ushort attr=0;
	int fmsg;
	uint i;
	static uint startmsg;
	time_t t;
	faddr_t	faddr;
	fmsghdr_t hdr;
	struct tm *tm;

if(!startmsg) startmsg=1;
i=matchnode(dest,0);
if(i<cfg.nodecfgs) {
	attr=cfg.nodecfg[i].attr;
	if(!attr) {
		i=matchnode(dest,2);
		if(i<cfg.nodecfgs)
			attr=cfg.nodecfg[i].attr; } }

do {
	for(i=startmsg;i;i++) {
		sprintf(fname,"%s%u.MSG",scfg.netmail_dir,i);
		if(!fexist(fname))
			break; }
	if(!i) {
		printf("\7%s directory full!\n",scfg.netmail_dir);
		logprintf("Directory full: %s",scfg.netmail_dir);
		return(-1); }
	startmsg=i+1;
	strupr(fname);
	if((fstream=fnopen(&fmsg,fname,O_RDWR|O_CREAT))==NULL) {
		printf("\7ERROR line %d opening %s %s\n",__LINE__,fname,sys_errlist[errno]);
		logprintf("ERROR line %d opening %s %s",__LINE__,fname,sys_errlist[errno]);
		return(-1); }

	faddr=getsysfaddr(dest.zone);
	memset(&hdr,0,sizeof(fmsghdr_t));
	hdr.origzone=faddr.zone;
	hdr.orignet=faddr.net;
	hdr.orignode=faddr.node;
	hdr.origpoint=faddr.point;
	hdr.destzone=dest.zone;
	hdr.destnet=dest.net;
	hdr.destnode=dest.node;
	hdr.destpoint=dest.point;

	hdr.attr=(FIDO_PRIVATE|FIDO_KILLSENT|FIDO_LOCAL);
	if(file)
		hdr.attr|=FIDO_FILE;

	if(attr&ATTR_HOLD)
		hdr.attr|=FIDO_HOLD;
	if(attr&ATTR_CRASH)
		hdr.attr|=FIDO_CRASH;

	sprintf(hdr.from,"SBBSecho");

	t=time(NULL);
508
	tm=localtime(&t);
509
510
511
512
513
514
515
516
517
518
519
520
	sprintf(hdr.time,"%02u %3.3s %02u  %02u:%02u:%02u"
		,tm->tm_mday,mon[tm->tm_mon],TM_YEAR(tm->tm_year)
		,tm->tm_hour,tm->tm_min,tm->tm_sec);

	if(to)
		sprintf(hdr.to,"%s",to);
	else
		sprintf(hdr.to,"SYSOP");

	sprintf(hdr.subj,"%.71s",subject);

	fwrite(&hdr,sizeof(fmsghdr_t),1,fstream);
521
	sprintf(str,"\1INTL %hu:%hu/%hu %hu:%hu/%hu\r"
522
523
524
525
526
527
528
529
530
531
		,hdr.destzone,hdr.destnet,hdr.destnode
		,hdr.origzone,hdr.orignet,hdr.orignode);
	fwrite(str,strlen(str),1,fstream);
	if(attr&ATTR_DIRECT) {
		fwrite("\1FLAGS DIR",10,1,fstream);
		if(file)
			fwrite(" KFS\r",5,1,fstream);
		else
			fwrite("\r",1,1,fstream); }
	if(hdr.destpoint) {
532
		sprintf(str,"\1TOPT %hu\r",hdr.destpoint);
533
534
		fwrite(str,strlen(str),1,fstream); }
	if(hdr.origpoint) {
535
		sprintf(str,"\1FMPT %hu\r",hdr.origpoint);
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
		fwrite(str,strlen(str),1,fstream); }
	if(!file || (!(attr&ATTR_DIRECT) && file))
		fwrite(body,strlen(body)+1,1,fstream);	/* Write additional NULL */
	else
		fwrite("\0",1,1,fstream);               /* Write NULL */
	fclose(fstream);
} while(!fexist(fname));
return(0);
}

/******************************************************************************
 This function takes the contents of 'infile' and puts it into a netmail
 message bound for addr.
******************************************************************************/
void file_to_netmail(FILE *infile,char *title,faddr_t addr,char *to)
{
	char *buf,*p;
	long l,m,len;

l=len=ftell(infile);
if(len>8192L)
	len=8192L;
rewind(infile);
if((buf=(char *)MALLOC(len+1))==NULL) {
	printf("ERROR allocating %lu bytes for file to netmail buffer.\n",len);
	logprintf("ERROR line %d allocating %lu for file to netmail buf",__LINE__
		,len);
	return; }
while((m=fread(buf,1,(len>8064L) ? 8064L:len,infile))>0) {
	buf[m]=0;
	if(l>8064L && (p=strrchr(buf,'\n'))!=NULL) {
		p++;
		if(*p) {
			*p=0;
			p++;
			fseek(infile,-1L,SEEK_CUR);
			while(*p) { 			/* Seek back to end of last line */
				p++;
				fseek(infile,-1L,SEEK_CUR); } } }
	if(ftell(infile)<l)
		strcat(buf,"\r\nContinued in next message...\r\n");
	create_netmail(to,title,buf,addr,0); }
FREE(buf);
}
/******************************************************************************
 This function sends a notify list to applicable nodes, this list includes the
 settings configured for the node, as well as a list of areas the node is
 connected to.
******************************************************************************/
void notify_list(void)
{
	FILE *	tmpf;
	char	str[256];
	uint	i,j,k;

	for(k=0;k<cfg.nodecfgs;k++) {

		if(!(cfg.nodecfg[k].attr&SEND_NOTIFY))
			continue;

		if((tmpf=tmpfile())==NULL) {
			printf("\7ERROR couldn't open tmpfile.\n");
			logprintf("ERROR line %d couldn't open tmpfile",__LINE__);
			return; }

		fprintf(tmpf,"Following are the options set for your system and a list "
			"of areas\r\nyou are connected to.  Please make sure everything "
			"is correct.\r\n\r\n");
		fprintf(tmpf,"Packet Type       %s\r\n"
			,cfg.nodecfg[k].pkt_type==PKT_TWO ? "2"
			:cfg.nodecfg[k].pkt_type==PKT_TWO_TWO ? "2.2":"2+");
		fprintf(tmpf,"Archive Type      %s\r\n"
			,(cfg.nodecfg[k].arctype>cfg.arcdefs) ?
			 "None":cfg.arcdef[cfg.nodecfg[k].arctype].name);
		fprintf(tmpf,"Mail Status       %s\r\n"
			,cfg.nodecfg[k].attr&ATTR_CRASH ? "Crash"
			:cfg.nodecfg[k].attr&ATTR_HOLD ? "Hold" : "None");
		fprintf(tmpf,"Direct            %s\r\n"
			,cfg.nodecfg[k].attr&ATTR_DIRECT ? "Yes":"No");
		fprintf(tmpf,"Passive           %s\r\n"
			,cfg.nodecfg[k].attr&ATTR_PASSIVE ? "Yes":"No");
		fprintf(tmpf,"Remote AreaMgr    %s\r\n\r\n"
			,cfg.nodecfg[k].password[0] ? "Yes" : "No");

		fprintf(tmpf,"Connected Areas\r\n---------------\r\n");
		for(i=0;i<cfg.areas;i++) {
			sprintf(str,"%s\r\n",cfg.area[i].name);
			if(str[0]=='*')
				continue;
			for(j=0;j<cfg.area[i].uplinks;j++)
				if(!memcmp(&cfg.nodecfg[k].faddr,&cfg.area[i].uplink[j]
					,sizeof(faddr_t)))
					break;
			if(j<cfg.area[i].uplinks)
				fprintf(tmpf,"%s",str); }

		if(ftell(tmpf))
			file_to_netmail(tmpf,"SBBSecho Notify List",cfg.nodecfg[k].faddr,0);
		fclose(tmpf); }
}
/******************************************************************************
 This function creates a netmail to addr showing a list of available areas (0),
 a list of connected areas (1), or a list of removed areas (2).
******************************************************************************/
void netmail_arealist(char type,faddr_t addr)
{
	FILE *stream,*tmpf;
	char str[256],temp[256],title[81],match,*p;
	int file,i,j,k,x,y;

	if(!type)
		strcpy(title,"List of Available Areas");
	else if(type==1)
		strcpy(title,"List of Connected Areas");
	else
		strcpy(title,"List of Unlinked Areas");

	if((tmpf=tmpfile())==NULL) {
		printf("\7ERROR couldn't open tmpfile.\n");
		logprintf("ERROR line %d couldn't open tmpfile",__LINE__);
		return; }

	if(type==1 || (type!=1 && !(misc&ELIST_ONLY))) {
		for(i=0;i<cfg.areas;i++) {
			sprintf(str,"%s\r\n",cfg.area[i].name);
			if(type) {
				for(j=0;j<cfg.area[i].uplinks;j++)
					if(!memcmp(&addr,&cfg.area[i].uplink[j],sizeof(faddr_t)))
						break;
				if((type==1 && j<cfg.area[i].uplinks) ||
					(type==2 && j==cfg.area[i].uplinks))
						fprintf(tmpf,"%s",str); }
			else
				fprintf(tmpf,"%s",str); } }

	if(!type) {
		i=matchnode(addr,0);
		if(i<cfg.nodecfgs) {
			for(j=0;j<cfg.listcfgs;j++) {
				match=0;
				for(k=0;k<cfg.listcfg[j].numflags;k++) {
					if(match) break;
					for(x=0;x<cfg.nodecfg[i].numflags;x++)
						if(!stricmp(cfg.listcfg[j].flag[k].flag
							,cfg.nodecfg[i].flag[x].flag)) {
							if((stream=fnopen(&file
								,cfg.listcfg[j].listpath,O_RDONLY))==NULL) {
								printf("\7ERROR couldn't open %s.\n"
									,cfg.listcfg[j].listpath);
								logprintf("ERROR line %d couldn't open %s %s"
									,__LINE__,cfg.listcfg[j].listpath
									,sys_errlist[errno]);
								match=1;
								break; }
							while(!feof(stream)) {
								if(!fgets(str,255,stream))
									break;
								truncsp(str);
								strcat(str,"\r\n");
								p=str;
								while(*p && *p<=SP) p++;
								if(*p==';')     /* Ignore Comment Lines */
									continue;
								strcpy(temp,p);
								p=temp;
								while(*p && *p>SP) p++;
								*p=0;
								if(!(misc&ELIST_ONLY)) {
									for(y=0;y<cfg.areas;y++)
										if(!stricmp(cfg.area[y].name,temp))
											break;
									if(y==cfg.areas)
										fprintf(tmpf,"%s",str); }
								else
									fprintf(tmpf,"%s",str); }
							fclose(stream);
							match=1;
							break; } } } } }

	if(!ftell(tmpf))
		create_netmail(0,title,"None.",addr,0);
	else
		file_to_netmail(tmpf,title,addr,0);
	fclose(tmpf);
}
/******************************************************************************
 Imitation of Borland's tempnam function because Watcom doesn't have it
******************************************************************************/
//#ifdef __WATCOMC__
char *tempname(char *dir, char *prefix)
{
	char str[256],*p;
	int i;

for(i=0;i<1000;i++) {
	sprintf(str,"%s%s%03u.$$$",dir,prefix,i);
	if(!fexist(str))
		break; }
if(i>=1000) {
	logprintf("tempnam: too many files");
	return(NULL); }
p=malloc(strlen(str)+1);
if(!p) {
	logprintf("tempnam: couldn't malloc %u",strlen(str)+1);
	return(NULL); }
strcpy(p,str);
return(p);
}
//#endif

char check_elists(char *areatag,faddr_t addr)
{
	FILE *stream;
	char str[1025],quit=0,match=0,*p;
	int i,j,k,x,file;

i=matchnode(addr,0);
if(i<cfg.nodecfgs) {
	for(j=0;j<cfg.listcfgs;j++) {
		quit=0;
		for(k=0;k<cfg.listcfg[j].numflags;k++) {
			if(quit) break;
			for(x=0;x<cfg.nodecfg[i].numflags;x++)
				if(!stricmp(cfg.listcfg[j].flag[k].flag
					,cfg.nodecfg[i].flag[x].flag)) {
					if((stream=fnopen(&file
						,cfg.listcfg[j].listpath,O_RDONLY))==NULL) {
						printf("\7ERROR couldn't open %s.\n"
							,cfg.listcfg[j].listpath);
						logprintf("ERROR line %d opening %s"
							,__LINE__,cfg.listcfg[j].listpath);
						quit=1;
						break; }
					while(!feof(stream)) {
						if(!fgets(str,255,stream))
							break;
						truncsp(str);
						strcat(str,"\r\n");
						p=str;
						while(*p && *p<=SP) p++;
						if(*p==';')     /* Ignore Comment Lines */
							continue;
						strcpy(str,p);
						p=str;
						while(*p && *p>SP) p++;
						*p=0;
						if(!stricmp(areatag,str)) {
							match=1;
							break; } }
					fclose(stream);
					quit=1;
					if(match)
						return(match);
					break; } } } }
return(match);
}
/******************************************************************************
 Used by AREAFIX to add/remove/change areas in the areas file
******************************************************************************/
void alter_areas(area_t add_area,area_t del_area,faddr_t addr)
{
	FILE *nmfile,*afilein,*afileout,*fwdfile;
	char str[1025],fields[1025],field1[81],field2[81],field3[81]
		,drive[3],dir[66],name[9],ext[5],outpath[128]
		,*outname,*p,*tp,nomatch=0,match=0;
	int i,j,k,x,y,file;
	ulong tagcrc;

_splitpath(cfg.areafile,drive,dir,name,ext);
sprintf(outpath,"%s%s",drive,dir);
if((outname=tempname(outpath,"AREAS"))==NULL) {
	printf("\7ERROR creating temp file name for %s.\n",outpath);
	logprintf("ERROR tempnam(%s,AREAS)",outpath);
	return; }
if((nmfile=tmpfile())==NULL) {
	printf("\7ERROR couldn't open NetMail temp file.\n");
	logprintf("ERROR in tmpfile()");
	free(outname);
	return; }
if((afileout=fopen(outname,"w+b"))==NULL) {
	printf("\7ERROR couldn't open %s.\n",outname);
	logprintf("ERROR line %d opening %s %s",__LINE__,outname
		,sys_errlist[errno]);
	fclose(nmfile);
	free(outname);
    return; }
if((afilein=fnopen(&file,cfg.areafile,O_RDONLY))==NULL) {
	printf("\7ERROR couldn't open %s.\n",cfg.areafile);
	logprintf("ERROR line %d opening %s %s",__LINE__,cfg.areafile
		,sys_errlist[errno]);
	fclose(afileout);
	fclose(nmfile);
	free(outname);
	return; }
while(!feof(afilein)) {
	if(!fgets(fields,1024,afilein))
		break;
	truncsp(fields);
	strcat(fields,"\r\n");
	p=fields;
	while(*p && *p<=SP) p++;
	if(*p==';') {    /* Skip Comment Lines */
		fprintf(afileout,"%s",fields);
		continue; }
	sprintf(field1,"%-.81s",p);         /* Internal Code Field */
	tp=field1;
	while(*tp && *tp>SP) tp++;
	*tp=0;
	while(*p && *p>SP) p++;
	while(*p && *p<=SP) p++;
	sprintf(field2,"%-.81s",p);         /* Areatag Field */
	tp=field2;
	while(*tp && *tp>SP) tp++;
	*tp=0;
	while(*p && *p>SP) p++;
	while(*p && *p<=SP) p++;
	if((tp=strchr(p,';'))!=NULL) {
		sprintf(field3,"%-.81s",p);     /* Comment Field (if any) */
		while(*tp && *tp>SP) tp++;
		*tp=0; }
	else
		field3[0]=0;
	if(del_area.tags) { 				/* Check for areas to remove */
		for(i=0;i<del_area.tags;i++) {
			if(!stricmp(del_area.tag[i],field2) ||
				!stricmp(del_area.tag[0],"-ALL"))     /* Match Found */
				break; }
		if(i<del_area.tags) {
			for(i=0;i<cfg.areas;i++) {
				if(!stricmp(field2,cfg.area[i].name)) {
					for(j=0;j<cfg.area[i].uplinks;j++)
						if(!memcmp(&cfg.area[i].uplink[j],&addr
							,sizeof(faddr_t)))
							break;
					if(j==cfg.area[i].uplinks &&
						stricmp(del_area.tag[0],"-ALL")) {
						fprintf(afileout,"%s",fields);
						fprintf(nmfile,"%s not connected.\r\n",field2);
						break; }

					/* Added 12/4/95 to remove uplink from connected uplinks */

					for(k=j;k<cfg.area[i].uplinks-1;k++)
						memcpy(&cfg.area[i].uplink[k],&cfg.area[i].uplink[k+1]
							,sizeof(faddr_t));
					--cfg.area[i].uplinks;
					if((cfg.area[i].uplink=(faddr_t *)
						REALLOC(cfg.area[i].uplink,sizeof(faddr_t)
						*(cfg.area[i].uplinks)))==NULL) {
						printf("ERROR allocating memory for area #%u "
							"uplinks.\n",i+1);
						logprintf("ERROR line %d allocating memory for area "
							"#%u uplinks.\n",__LINE__,i+1);
						bail(1); }

					fprintf(afileout,"%-16s%-23s ",field1,field2);
					for(j=0;j<cfg.area[i].uplinks;j++) {
						if(!memcmp(&cfg.area[i].uplink[j],&addr
							,sizeof(faddr_t)))
							continue;
						fprintf(afileout,"%s "
rswindell's avatar
rswindell committed
897
							,faddrtoa(&cfg.area[i].uplink[j],NULL)); }
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
					if(field3[0])
						fprintf(afileout,"%s",field3);
					fprintf(afileout,"\r\n");
					fprintf(nmfile,"%s removed.\r\n",field2);
					break; } }
			if(i==cfg.areas)			/* Something screwy going on */
				fprintf(afileout,"%s",fields);
			continue; } }				/* Area match so continue on */
	if(add_area.tags) { 				/* Check for areas to add */
		for(i=0;i<add_area.tags;i++)
			if(!stricmp(add_area.tag[i],field2) ||
				!stricmp(add_area.tag[0],"+ALL"))      /* Match Found */
				break;
		if(i<add_area.tags) {
			if(stricmp(add_area.tag[i],"+ALL"))
				add_area.tag[i][0]=0;  /* So we can check other lists */
			for(i=0;i<cfg.areas;i++) {
				if(!stricmp(field2,cfg.area[i].name)) {
					for(j=0;j<cfg.area[i].uplinks;j++)
						if(!memcmp(&cfg.area[i].uplink[j],&addr
							,sizeof(faddr_t)))
							break;
					if(j<cfg.area[i].uplinks) {
						fprintf(afileout,"%s",fields);
						fprintf(nmfile,"%s already connected.\r\n",field2);
						break; }
					if(misc&ELIST_ONLY && !check_elists(field2,addr)) {
						fprintf(afileout,"%s",fields);
						break; }

					/* Added 12/4/95 to add uplink to connected uplinks */

					++cfg.area[i].uplinks;
					if((cfg.area[i].uplink=(faddr_t *)
						REALLOC(cfg.area[i].uplink,sizeof(faddr_t)
						*(cfg.area[i].uplinks)))==NULL) {
						printf("ERROR allocating memory for area #%u "
							"uplinks.\n",i+1);
						logprintf("ERROR line %d allocating memory for area "
							"#%u uplinks.\n",__LINE__,i+1);
                        bail(1); }
					memcpy(&cfg.area[i].uplink[j],&addr,sizeof(faddr_t));

					fprintf(afileout,"%-16s%-23s ",field1,field2);
					for(j=0;j<cfg.area[i].uplinks;j++)
						fprintf(afileout,"%s "
rswindell's avatar
rswindell committed
944
							,faddrtoa(&cfg.area[i].uplink[j],NULL));
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
					if(field3[0])
						fprintf(afileout,"%s",field3);
					fprintf(afileout,"\r\n");
					fprintf(nmfile,"%s added.\r\n",field2);
					break; } }
			if(i==cfg.areas)			/* Something screwy going on */
				fprintf(afileout,"%s",fields);
			continue; } 				/* Area match so continue on */
		nomatch=1; }					/* This area wasn't in there */
	fprintf(afileout,"%s",fields); }    /* No match so write back line */
fclose(afilein);
if(nomatch || (add_area.tags && !stricmp(add_area.tag[0],"+ALL"))) {
	i=matchnode(addr,0);
	if(i<cfg.nodecfgs) {
		for(j=0;j<cfg.listcfgs;j++) {
			match=0;
			for(k=0;k<cfg.listcfg[j].numflags;k++) {
				if(match) break;
				for(x=0;x<cfg.nodecfg[i].numflags;x++)
					if(!stricmp(cfg.listcfg[j].flag[k].flag
						,cfg.nodecfg[i].flag[x].flag)) {
						if((fwdfile=tmpfile())==NULL) {
							printf("\7ERROR couldn't open forwarding temp "
								"file.\n");
							logprintf("ERROR line %d opening forward temp "
								"file",__LINE__);
							match=1;
							break; }
						if((afilein=fnopen(&file
							,cfg.listcfg[j].listpath,O_RDONLY))==NULL) {
							printf("\7ERROR couldn't open %s.\n"
								,cfg.listcfg[j].listpath);
							logprintf("ERROR line %d opening %s"
								,__LINE__,cfg.listcfg[j].listpath);
							fclose(fwdfile);
							match=1;
							break; }
						while(!feof(afilein)) {
							if(!fgets(str,255,afilein))
								break;
							truncsp(str);
							strcat(str,"\r\n");
							p=str;
							while(*p && *p<=SP) p++;
							if(*p==';')     /* Ignore Comment Lines */
								continue;
							strcpy(str,p);
							p=str;
							while(*p && *p>SP) p++;
							*p=0;
							if(!stricmp(add_area.tag[0],"+ALL")) {
								sprintf(fields,"%.1024s",str);
rswindell's avatar
rswindell committed
997
								tagcrc=crc32(strupr(fields),0);
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
								for(y=0;y<cfg.areas;y++)
									if(tagcrc==cfg.area[y].tag)
										break;
								if(y<cfg.areas)
									continue; }
							for(y=0;y<add_area.tags;y++)
								if((!stricmp(add_area.tag[y],str) &&
									add_area.tag[y][0]) ||
									!stricmp(add_area.tag[0],"+ALL"))
									break;
							if(y<add_area.tags) {
								fprintf(afileout,"%-16s%-23s","P",str);
								if(cfg.listcfg[j].forward.zone)
									fprintf(afileout," %s"
rswindell's avatar
rswindell committed
1012
1013
										,faddrtoa(&cfg.listcfg[j].forward,NULL));
								fprintf(afileout," %s\r\n",faddrtoa(&addr,NULL));
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
								fprintf(nmfile,"%s added.\r\n",str);
								if(stricmp(add_area.tag[0],"+ALL"))
									add_area.tag[y][0]=0;
								if(!(cfg.listcfg[j].misc&NOFWD)
									&& cfg.listcfg[j].forward.zone)
									fprintf(fwdfile,"%s\r\n",str); } }
						fclose(afilein);
						if(!(cfg.listcfg[j].misc&NOFWD) && ftell(fwdfile)>0)
							file_to_netmail(fwdfile,cfg.listcfg[j].password
								,cfg.listcfg[j].forward,"Areafix");
						fclose(fwdfile);
						match=1;
                        break; } } } } }
if(add_area.tags && stricmp(add_area.tag[0],"+ALL")) {
	for(i=0;i<add_area.tags;i++)
		if(add_area.tag[i][0])
			fprintf(nmfile,"%s not found.\r\n",add_area.tag[i]); }
if(!ftell(nmfile))
	create_netmail(0,"Area Change Request","No changes made.",addr,0);
else
	file_to_netmail(nmfile,"Area Change Request",addr,0);
fclose(nmfile);
fclose(afileout);
if(delfile(cfg.areafile))					/* Delete AREAS.BBS */
	logprintf("ERROR line %d removing %s %s",__LINE__,cfg.areafile
		,sys_errlist[errno]);
if(rename(outname,cfg.areafile))		   /* Rename new AREAS.BBS file */
	logprintf("ERROR line %d renaming %s to %s",__LINE__,outname,cfg.areafile);
free(outname);
}
/******************************************************************************
 Used by AREAFIX to add/remove/change uplink info in the configuration file
 old = the old setting for this option, new = what the setting is changing to
 option = 0 for compression type change
		  1 for areafix password change
		  2 to set this node to passive
		  3 to set this node to active (remove passive)
******************************************************************************/
void alter_config(faddr_t addr,char *old,char *new,char option)
{
	FILE *outfile,*cfgfile;
	char str[257],outpath[128],tmp[257],tmp2[257],*outname,*p,*tp
		,drive[3],dir[66],name[9],ext[5],match=0;
	int i,j,k,file;
	faddr_t taddr;

i=matchnode(addr,0);				  /* i = config number from here on */
_splitpath(cfg.cfgfile,drive,dir,name,ext);
sprintf(outpath,"%s%s",drive,dir);
if((outname=tempname(outpath,"CFG"))==NULL) {
	printf("\7ERROR creating temporary file name for %s.\n",outpath);
	logprintf("ERROR tempnam(%s,CFG)",outpath);
	return; }
if((outfile=fopen(outname,"w+b"))==NULL) {
	printf("\7ERROR couldn't open %s.\n",outname);
	logprintf("ERROR line %d opening %s %s",__LINE__,outname
		,sys_errlist[errno]);
	free(outname);
    return; }
if((cfgfile=fnopen(&file,cfg.cfgfile,O_RDONLY))==NULL) {
	printf("\7ERROR couldn't open %s.\n",cfg.cfgfile);
	logprintf("ERROR line %d opening %s",__LINE__,cfg.cfgfile
		,sys_errlist[errno]);
	fclose(outfile);
	free(outname);
	return; }

while(!feof(cfgfile)) {
	if(!fgets(str,256,cfgfile))
		break;
	truncsp(str);
	p=str;
	while(*p && *p<=SP) p++;
	if(*p==';') {
		fprintf(outfile,"%s\r\n",str);
        continue; }
	sprintf(tmp,"%-.25s",p);
	tp=strchr(tmp,SP);
	if(tp)
		*tp=0;								/* Chop off at space */
	strupr(tmp);							/* Convert code to uppercase */
	while(*p>SP) p++;						/* Skip code */
    while(*p && *p<=SP) p++;                /* Skip white space */

	if(option==0 && !strcmp(tmp,"USEPACKER")) {     /* Change Compression */
		if(!*p)
			continue;
		strcpy(tmp2,p);
		p=tmp2;
		while(*p && *p>SP) p++;
		*p=0;
		p++;
		if(!stricmp(new,tmp2)) {   /* Add to new definition */
			fprintf(outfile,"%-10s %s %s %s\r\n",tmp,tmp2
rswindell's avatar
rswindell committed
1108
				,faddrtoa(&cfg.nodecfg[i].faddr,NULL)
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
				,(*p) ? p : "");
			match=1;
			continue; }
		else if(!stricmp(old,tmp2)) {	/* Remove from old def */
			for(j=k=0;j<cfg.nodecfgs;j++) {
				if(j==i)
					continue;
				if(!stricmp(cfg.arcdef[cfg.nodecfg[j].arctype].name,tmp2)) {
					if(!k) {
						fprintf(outfile,"%-10s %s",tmp,tmp2);
						k++; }
					fprintf(outfile," %s"
rswindell's avatar
rswindell committed
1121
						,faddrtoa(&cfg.nodecfg[j].faddr,NULL)); } }
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
			fprintf(outfile,"\r\n");
			continue; } }

	if(option==1 && !strcmp(tmp,"AREAFIX")) {       /* Change Password */
		if(!*p)
			continue;
		taddr=atofaddr(p);
		if(!memcmp(&cfg.nodecfg[i].faddr,&taddr,sizeof(faddr_t))) {
			while(*p && *p>SP) p++; 	/* Skip over address */
			while(*p && *p<=SP) p++;	/* Skip over whitespace */
			while(*p && *p>SP) p++; 	/* Skip over password */
			while(*p && *p<=SP) p++;	/* Skip over whitespace */
			fprintf(outfile,"%-10s %s %s %s\r\n",tmp
rswindell's avatar
rswindell committed
1135
				,faddrtoa(&cfg.nodecfg[i].faddr,NULL),new,p);
1136
1137
1138
1139
1140
1141
1142
			continue; } }

	if(option>1 && !strcmp(tmp,"PASSIVE")) {        /* Toggle Passive Areas */
		match=1;
		for(j=k=0;j<cfg.nodecfgs;j++) {
			if(option==2 && j==i) {
				if(!k) fprintf(outfile,"%-10s",tmp);
rswindell's avatar
rswindell committed
1143
				fprintf(outfile," %s",faddrtoa(&cfg.nodecfg[j].faddr,NULL));
1144
1145
1146
1147
1148
1149
				k++;
				continue; }
			if(option==3 && j==i)
				continue;
			if(cfg.nodecfg[j].attr&ATTR_PASSIVE) {
				if(!k) fprintf(outfile,"%-10s",tmp);
rswindell's avatar
rswindell committed
1150
				fprintf(outfile," %s",faddrtoa(&cfg.nodecfg[j].faddr,NULL));
1151
1152
1153
1154
1155
1156
1157
1158
				k++; } }
		if(k) fprintf(outfile,"\r\n");
		continue; }
	fprintf(outfile,"%s\r\n",str); }

if(!match) {
	if(option==0)
		fprintf(outfile,"%-10s %s %s\r\n","USEPACKER",new
rswindell's avatar
rswindell committed
1159
			,faddrtoa(&cfg.nodecfg[i].faddr,NULL));
1160
1161
	if(option==2)
		fprintf(outfile,"%-10s %s\r\n","PASSIVE"
rswindell's avatar
rswindell committed
1162
			,faddrtoa(&cfg.nodecfg[i].faddr,NULL)); }
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184

fclose(cfgfile);
fclose(outfile);
if(delfile(cfg.cfgfile))
	logprintf("ERROR line %d removing %s %s",__LINE__,cfg.cfgfile
		,sys_errlist[errno]);
if(rename(outname,cfg.cfgfile))
	logprintf("ERROR line %d renaming %s to %s",__LINE__,outname,cfg.cfgfile);
free(outname);
}
/******************************************************************************
 Used by AREAFIX to process any '%' commands that come in via netmail
******************************************************************************/
void command(char *instr,faddr_t addr)
{
	FILE *stream,*tmpf;
	char str[256],temp[256],*buf,*p;
	int  file,i,node;
	long l;
	area_t add_area,del_area;

	node=matchnode(addr,0);
1185
	if(node>=cfg.nodecfgs)
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
		return;
	memset(&add_area,0,sizeof(area_t));
	memset(&del_area,0,sizeof(area_t));
	strupr(instr);
	if((p=strstr(instr,"HELP"))!=NULL) {
		sprintf(str,"%s%sAREAMGR.HLP",scfg.exec_dir);
		if(!fexist(str))
			return;
		if((stream=fnopen(&file,str,O_RDONLY))==NULL) {
			printf("\7ERROR couldn't open %s.\n",str);
			logprintf("ERROR line %d opening %s %s",__LINE__,str
				,sys_errlist[errno]);
			return; }
		l=filelength(file);
		if((buf=(char *)LMALLOC(l+1L))==NULL) {
			printf("ERROR line %d allocating %lu bytes for %s\n",__LINE__,l,str);
			return; }
		fread(buf,l,1,stream);
		fclose(stream);
		buf[l]=0;
		create_netmail(0,"Area Manager Help",buf,addr,0);
		LFREE(buf);
		return; }

	if((p=strstr(instr,"LIST"))!=NULL) {
		netmail_arealist(0,addr);
		return; }

	if((p=strstr(instr,"QUERY"))!=NULL) {
		netmail_arealist(1,addr);
		return; }

	if((p=strstr(instr,"UNLINKED"))!=NULL) {
		netmail_arealist(2,addr);
		return; }

	if((p=strstr(instr,"COMPRESSION"))!=NULL) {
		while(*p && *p>SP) p++;
		while(*p && *p<=SP) p++;
		for(i=0;i<cfg.arcdefs;i++)
			if(!stricmp(p,cfg.arcdef[i].name))
				break;
		if(!stricmp(p,"NONE"))
			i=0xffff;
		if(i==cfg.arcdefs) {
			if((tmpf=tmpfile())==NULL) {
				printf("\7ERROR couldn't open tmpfile.\n");
				logprintf("ERROR line %d opening tmpfile()",__LINE__);
				return; }
			fprintf(tmpf,"Compression type unavailable.\r\n\r\n"
				"Available types are:\r\n");
			for(i=0;i<cfg.arcdefs;i++)
				fprintf(tmpf,"                     %s\r\n",cfg.arcdef[i].name);
			file_to_netmail(tmpf,"Compression Type Change",addr,0);
			fclose(tmpf);
			//LFREE(buf);
			return; }
		alter_config(addr,cfg.arcdef[cfg.nodecfg[node].arctype].name
			,cfg.arcdef[i].name,0);
		cfg.nodecfg[node].arctype=i;
		sprintf(str,"Compression type changed to %s.",cfg.arcdef[i].name);
		create_netmail(0,"Compression Type Change",str,addr,0);
		return; }

	if((p=strstr(instr,"PASSWORD"))!=NULL) {
		while(*p && *p>SP) p++;
		while(*p && *p<=SP) p++;
		sprintf(temp,"%-.25s",p);
		p=temp;
		while(*p && *p>SP) p++;
		*p=0;
1257
		if(node>=cfg.nodecfgs)		   /* Should never happen */
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
			return;
		if(!stricmp(temp,cfg.nodecfg[node].password)) {
			sprintf(str,"Your password was already set to %s."
				,cfg.nodecfg[node].password);
			create_netmail(0,"Password Change Request",str,addr,0);
			return; }
		alter_config(addr,cfg.nodecfg[node].password,temp,1);
		sprintf(str,"Your password has been changed from %s to %.25s."
			,cfg.nodecfg[node].password,temp);
		sprintf(cfg.nodecfg[node].password,"%.25s",temp);
		create_netmail(0,"Password Change Request",str,addr,0);
		return; }

	if((p=strstr(instr,"RESCAN"))!=NULL) {
		export_echomail("",addr);
		create_netmail(0,"Rescan Areas"
			,"All connected areas carried by your hub have been rescanned."
			,addr,0);
		return; }

	if((p=strstr(instr,"ACTIVE"))!=NULL) {
		if(!(cfg.nodecfg[node].attr&ATTR_PASSIVE)) {
			create_netmail(0,"Reconnect Disconnected Areas"
				,"Your areas are already connected.",addr,0);
			return; }
		alter_config(addr,0,0,3);
		create_netmail(0,"Reconnect Disconnected Areas"
			,"Temporarily disconnected areas have been reconnected.",addr,0);
		return; }

	if((p=strstr(instr,"PASSIVE"))!=NULL) {
		if(cfg.nodecfg[node].attr&ATTR_PASSIVE) {
			create_netmail(0,"Temporarily Disconnect Areas"
				,"Your areas are already temporarily disconnected.",addr,0);
			return; }
		alter_config(addr,0,0,2);
		create_netmail(0,"Temporarily Disconnect Areas"
			,"Your areas have been temporarily disconnected.",addr,0);
		return; }

	if((p=strstr(instr,"FROM"))!=NULL);

	if((p=strstr(instr,"+ALL"))!=NULL) {
		if((add_area.tag=(char **)REALLOC(add_area.tag
			,sizeof(char *)*add_area.tags+1))==NULL) {
			printf("ERROR allocating memory for add area tag #%u.\n"
				,add_area.tags+1);
			logprintf("ERROR line %d allocating memory for add area tag #%u"
				,__LINE__,add_area.tags+1);
			bail(1); }
		if((add_area.tag[add_area.tags]=(char *)LMALLOC(strlen(instr)+1))==NULL) {
			printf("ERROR allocating memory for add area tag #%u.\n"
				,add_area.tags+1);
			logprintf("ERROR line %d allocating memory for add area tag #%u"
				,__LINE__,add_area.tags+1);
			bail(1); }
		strcpy(add_area.tag[add_area.tags],instr);
		add_area.tags++;
		alter_areas(add_area,del_area,addr);
		for(i=0;i<add_area.tags;i++)
			LFREE(add_area.tag[i]);
		FREE(add_area.tag);
		return; }

	if((p=strstr(instr,"-ALL"))!=NULL) {
		if((del_area.tag=(char **)REALLOC(del_area.tag
			,sizeof(char *)*del_area.tags+1))==NULL) {
			printf("ERROR allocating memory for del area tag #%u.\n"
				,del_area.tags+1);
			logprintf("ERROR line %d allocating memory for del area tag #%u"
				,__LINE__,del_area.tags+1);
			bail(1); }
		if((del_area.tag[del_area.tags]=(char *)LMALLOC(strlen(instr)+1))==NULL) {
			printf("ERROR allocating memory for del area tag #%u.\n"
				,del_area.tags+1);
			logprintf("ERROR line %d allocating memory for del area tag #%u"
				,__LINE__,del_area.tags+1);
			bail(1); }
		strcpy(del_area.tag[del_area.tags],instr);
		del_area.tags++;
		alter_areas(add_area,del_area,addr);
		for(i=0;i<del_area.tags;i++)
			LFREE(del_area.tag[i]);
		FREE(del_area.tag);
		return; }
}
/******************************************************************************
 This is where we're gonna process any netmail that comes in for areafix.
 Returns text for message body for the local sysop if necessary.
******************************************************************************/
char *process_areafix(faddr_t addr,char HUGE16 *inbuf,char *password)
{
	static char body[512],str[81];
	char *p,*tp,action,percent=0;
	int i;
	ulong l,m;
	area_t add_area,del_area;

	p=(char *)inbuf;

	while(*p==1) {				/* Skip kludge lines 11/05/95 */
		while(*p && *p!=CR)
			p++;				/* Skip meat */
		if(*p)
			p++; }				/* Skip CR */

	if(((tp=strstr(p,"---\r"))!=NULL || (tp=strstr(p,"--- "))!=NULL) &&
		(*(tp-1)==10 || *(tp-1)==13))
		*tp=0;

	if(!strnicmp(p,"%FROM",5)) {    /* Remote Remote Maintenance (must be first) */
		sprintf(str,"%.80s",p+6);
		if((tp=strchr(str,CR))!=NULL)
			*tp=0;
rswindell's avatar
rswindell committed
1372
1373
		logprintf("Remote maintenance for %s requested via %s",str
			,faddrtoa(&addr,NULL));
1374
1375
1376
		addr=atofaddr(str); }

	i=matchnode(addr,0);
1377
	if(i>=cfg.nodecfgs) {
1378
1379
1380
1381
		sprintf(body,"Your node is not configured for Areafix, please "
			"contact your hub.\r\n");
		create_netmail(0,"Areafix Request",body,addr,0);
		sprintf(body,"An areafix request was made by node %s.\r\nThis node "
rswindell's avatar
rswindell committed
1382
1383
			"is not currently configured for areafix.\r\n"
			,faddrtoa(&addr,NULL));
1384
1385
1386
1387
1388
1389
		return(body); }

	if(stricmp(cfg.nodecfg[i].password,password)) {
		create_netmail(0,"Areafix Request","Invalid Password.",addr,0);
		sprintf(body,"Node %s attempted an areafix request using an invalid "
			"password.\r\nThe password attempted was %s.\r\nThe correct password "
rswindell's avatar
rswindell committed
1390
			"for this node is %s.\r\n",faddrtoa(&addr,NULL),password
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
			,(cfg.nodecfg[i].password[0]) ? cfg.nodecfg[i].password
			 : "[None Defined]");
		return(body); }

	m=strlen(p);
	add_area.tags=0;
	add_area.tag=NULL;
	del_area.tags=0;
	del_area.tag=NULL;
	for(l=0;l<m;l++) {
		while(*(p+l) && *(p+l)<=SP) l++;
		if(!(*(p+l))) break;
		if(*(p+l)=='+' || *(p+l)=='-' || *(p+l)=='%') {
			action=*(p+l);
			l++; }
		else
			action='+';
		sprintf(str,"%.80s",p+l);
		if((tp=strchr(str,CR))!=NULL)
			*tp=0;
		switch(action) {
			case '+':                       /* Add Area */
				if((add_area.tag=(char **)REALLOC(add_area.tag
					,sizeof(char *)*add_area.tags+1))==NULL) {
					printf("ERROR allocating memory for add area tag #%u.\n"
						,add_area.tags+1);
					logprintf("ERROR line %d allocating memory for add area "
						"tag #%u",__LINE__,add_area.tags+1);
					bail(1); }
				if((add_area.tag[add_area.tags]=(char *)LMALLOC(strlen(str)+1))
					==NULL) {
					printf("ERROR allocating memory for add area tag #%u.\n"
						,add_area.tags+1);
					logprintf("ERROR line %d allocating memory for add area "
						"tag #%u",__LINE__,add_area.tags+1);
					bail(1); }
				strcpy(add_area.tag[add_area.tags],str);
				add_area.tags++;
				break;
			case '-':                       /* Remove Area */
				if((del_area.tag=(char **)REALLOC(del_area.tag
					,sizeof(char *)*del_area.tags+1))==NULL) {
					printf("ERROR allocating memory for del area tag #%u.\n"
						,del_area.tags+1);
					logprintf("ERROR line %d allocating memory for del area "
						"tag #%u",__LINE__,del_area.tags+1);
					bail(1); }
				if((del_area.tag[del_area.tags]=(char *)LMALLOC(strlen(str)+1))
					==NULL) {
					printf("ERROR allocating memory for del area tag #%u.\n"
						,del_area.tags+1);
					logprintf("ERROR line %d allocating memory for del area "
						"tag #%u",__LINE__,del_area.tags+1);
					bail(1); }
				strcpy(del_area.tag[del_area.tags],str);
				del_area.tags++;
				break;
			case '%':                       /* Process Command */
				command(str,addr);
				percent++;
				break; }

		while(*(p+l) && *(p+l)!=CR) l++; }

	if(!percent && !add_area.tags && !del_area.tags) {
		create_netmail(0,"Areafix Request","No commands to process.",addr,0);
		sprintf(body,"Node %s attempted an areafix request with an empty message "
rswindell's avatar
rswindell committed
1458
			"body or with no valid commands.\r\n",faddrtoa(&addr,NULL));
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
		return(body); }
	if(add_area.tags || del_area.tags)
		alter_areas(add_area,del_area,addr);
	if(add_area.tags) {
		for(i=0;i<add_area.tags;i++)
			LFREE(add_area.tag[i]);
		FREE(add_area.tag); }
	if(del_area.tags) {
		for(i=0;i<del_area.tags;i++)
			LFREE(del_area.tag[i]);
		FREE(del_area.tag); }
	return(0);
}
/******************************************************************************
 This function will compare the archive signatures defined in the CFG file and
 extract 'infile' using the appropriate de-archiver.
******************************************************************************/
int unpack(char *infile)
{
	FILE *stream;
	char str[256],tmp[3];
	int i,j,ch,file;

	if((stream=fnopen(&file,infile,O_RDONLY))==NULL) {
		printf("\7ERROR couldn't open %s.\n",infile);
		logprintf("ERROR line %d opening %s %s",__LINE__,infile
			,sys_errlist[errno]);
		bail(1); }
	for(i=0;i<cfg.arcdefs;i++) {
		str[0]=0;
		fseek(stream,cfg.arcdef[i].byteloc,SEEK_SET);
		for(j=0;j<strlen(cfg.arcdef[i].hexid)/2;j++) {
			ch=fgetc(stream);
			if(ch==EOF) {
				i=cfg.arcdefs;
				break; }
			sprintf(tmp,"%02X",ch);
			strcat(str,tmp); }
		if(!stricmp(str,cfg.arcdef[i].hexid))
			break; }
	fclose(stream);

	if(i==cfg.arcdefs) {
		printf("\7ERROR couldn't determine filetype of %s.\n",infile);
		logprintf("ERROR line %d determining filetype of %s",__LINE__,infile);
		return(1); }

	j=execute(cmdstr(&scfg,cfg.arcdef[i].unpack,infile
		,secure ? cfg.secure : cfg.inbound));
	if(j) {
		printf("\7ERROR %d (%d) executing %s\n"
			,j,errno,cmdstr(&scfg,cfg.arcdef[i].unpack,infile
				,secure ? cfg.secure : cfg.inbound));
		logprintf("ERROR %d (%d) line %d executing %s"
			,j,errno,__LINE__,cmdstr(&scfg,cfg.arcdef[i].unpack,infile
				,secure ? cfg.secure : cfg.inbound));
		return(j); }
	return(0);
}
/******************************************************************************
 This function will check the 'dest' for the type of archiver to use (as
 defined in the CFG file) and compress 'srcfile' into 'destfile' using the
 appropriate archive program.
******************************************************************************/
void pack(char *srcfile,char *destfile,faddr_t dest)
{
	int i,j;
	uint use=0;

	i=matchnode(dest,0);
	if(i<cfg.nodecfgs)
		use=cfg.nodecfg[i].arctype;

	j=execute(cmdstr(&scfg,cfg.arcdef[use].pack,destfile,srcfile));
	if(j) {
		printf("\7ERROR %d (%d) executing %s\n"
			,j,errno,cmdstr(&scfg,cfg.arcdef[use].pack,destfile,srcfile));
		logprintf("ERROR %d (%d) line %d executing %s"
			,j,errno,__LINE__,cmdstr(&scfg,cfg.arcdef[use].pack,destfile,srcfile)); }
}

char attachment(char *bundlename,faddr_t dest,char cleanup)
{
	FILE *fidomsg,*stream;
	char str[1025],*path,fname[129],*p;
	int fmsg,file,error=0L;
	long fncrc,*mfncrc=0L,num_mfncrc=0L,crcidx;
    attach_t attach;
	fmsghdr_t hdr;
	size_t		f;
	glob_t		g;

1551
	if(bundlename==NULL && cleanup!=1) {
rswindell's avatar
rswindell committed
1552
1553
1554
		logprintf("ERROR line %d NULL bundlename",__LINE__);
		return(1);
	}
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
	sprintf(fname,"%sBUNDLES.SBE",cfg.outbound);
	if((stream=fnopen(&file,fname,O_RDWR|O_CREAT))==NULL) {
		printf("\7ERROR line %d opening %s %s\n",__LINE__,fname,sys_errlist[errno]);
		logprintf("ERROR line %d opening %s %s",__LINE__,fname,sys_errlist[errno]);
		return(1); }

	if(cleanup==2) {				/* Check for existance in BUNDLES.SBE */
		while(!feof(stream)) {
			if(!fread(&attach,1,sizeof(attach_t),stream))
				break;
			if(!stricmp(attach.fname,bundlename)) {
				fclose(stream);
				return(1); } }
		fclose(stream);
		return(0); }

	if(cleanup==1) {				/* Create netmail attaches */

1573
1574
1575
1576
1577
1578
1579
		if(!filelength(file)) {
			fclose(stream);
			return(0); 
		}
										/* Get attach names from existing MSGs */
		sprintf(str,"%s*.msg",scfg.netmail_dir);
		glob(str,0,NULL,&g);
rswindell's avatar
rswindell committed
1580
		for(f=0;f<g.gl_pathc;f++) {
1581

rswindell's avatar
rswindell committed
1582
			path=g.gl_pathv[f];
1583

rswindell's avatar
rswindell committed
1584
1585
1586
1587
			if((fidomsg=fnopen(&fmsg,path,O_RDWR))==NULL) {
				printf("\7ERROR line %d opening %s\n",__LINE__,path);
				logprintf("ERROR line %d opening %s %s",__LINE__,path
					,sys_errlist[errno]);
1588
1589
				continue; 
			}
rswindell's avatar
rswindell committed
1590
1591
1592
1593
1594
1595
			if(filelength(fmsg)<sizeof(fmsghdr_t)) {
				printf("\7ERROR %s has invalid length of %u bytes\n",path
					,filelength(fmsg));
				logprintf("ERROR line %d %s has invalid length of %u bytes"
					,__LINE__,path,filelength(fmsg));
				fclose(fidomsg);
1596
1597
				continue; 
			}
rswindell's avatar
rswindell committed
1598
1599
1600
1601
1602
1603
			if(fread(&hdr,sizeof(fmsghdr_t),1,fidomsg)!=1) {
				fclose(fidomsg);
				printf("\7ERROR reading %u bytes from %s"
					,sizeof(fmsghdr_t),path);
				logprintf("ERROR line %d reading %u bytes from %s"
					,__LINE__,sizeof(fmsghdr_t),path);
1604
1605
				continue; 
			}
1606
			fclose(fidomsg);
rswindell's avatar
rswindell committed
1607
			if(!(hdr.attr&FIDO_FILE))		/* Not a file attach */
1608
				continue;
rswindell's avatar
rswindell committed
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
			num_mfncrc++;
			if((p=strrchr(hdr.subj,'\\'))!=NULL)
				p++;
			else
				p=hdr.subj;
			if((mfncrc=(long *)REALLOC(mfncrc,num_mfncrc*sizeof(long)))==NULL) {
				printf("ERROR allocating %lu bytes for bundle name crc.\n"
					,num_mfncrc*sizeof(long));
				logprintf("ERROR line %d allocating %lu for bundle name crc"
					,__LINE__,num_mfncrc*sizeof(long));
1619
1620
				continue; 
			}
rswindell's avatar
rswindell committed
1621
			mfncrc[num_mfncrc-1]=crc32(strupr(p),0); 
1622
1623
		}
		globfree(&g);
rswindell's avatar
rswindell committed
1624

1625
1626
1627
1628
1629
1630
		while(!feof(stream)) {
			if(!fread(&attach,1,sizeof(attach_t),stream))
				break;
			sprintf(str,"%s%s",cfg.outbound,attach.fname);
			if(!fexist(str))
				continue;
rswindell's avatar
rswindell committed
1631
			fncrc=crc32(strupr(attach.fname),0);
1632
1633
			for(crcidx=0;crcidx<num_mfncrc;crcidx++)
				if(mfncrc[crcidx]==fncrc)
1634
					break;
1635
1636
1637
			if(crcidx==num_mfncrc)
				if(create_netmail(0,str,"\1FLAGS KFS\r",attach.dest,1))
					error=1; 
rswindell's avatar
rswindell committed
1638
		}
1639
1640
1641
1642
1643
1644
		if(!error)				/* don't truncate if an error occured */
			chsize(file,0L);
		fclose(stream);
		if(num_mfncrc)
			FREE(mfncrc);
		return(0); 
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
	}

	while(!feof(stream)) {
		if(!fread(&attach,1,sizeof(attach_t),stream))
			break;
		if(!stricmp(attach.fname,bundlename)) {
			fclose(stream);
			return(0); } }

	memcpy(&attach.dest,&dest,sizeof(faddr_t));
	strcpy(attach.fname,bundlename);
	fwrite(&attach,sizeof(attach_t),1,stream);
	fclose(stream);
	return(0);
}

/******************************************************************************
 This function is called when a message packet has reached it's maximum size.
 It places packets into a bundle until that bundle is full, at which time the
 last character of the extension increments (1 thru 0 and then A thru Z).  If
 all bundles have reached their maximum size remaining packets are placed into
 the Z bundle.
******************************************************************************/
void pack_bundle(char *infile,faddr_t dest)
{
	char str[256],fname[256],outbound[128],day[3],*p;
	int i,j,file,node;
	time_t now;

1674
1675
1676
1677
1678
	if(infile==NULL || infile[0]==0) {
		logprintf("ERROR line %d invalid filename",__LINE__);
		bail(1);
	}

1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
	node=matchnode(dest,0);
	strcpy(str,infile);
	str[strlen(str)-1]='T';
	if(rename(infile,str))				   /* Change .PK_ file to .PKT file */
		logprintf("ERROR line %d renaming %s to %s",__LINE__,infile,str);
	infile[strlen(infile)-1]='T';
	time(&now);
	sprintf(day,"%-.2s",ctime(&now));
	strupr(day);
	if(misc&FLO_MAILER) {
		if(node<cfg.nodecfgs && cfg.nodecfg[node].route.zone)
			dest=cfg.nodecfg[node].route;
		if(dest.zone==scfg.faddr[0].zone)	/* Default zone, use default outbound */
			strcpy(outbound,cfg.outbound);
		else							/* Inter-zone outbound is OUTBOUND.XXX */
rswindell's avatar
rswindell committed
1694
			sprintf(outbound,"%.*s.%03x\\"
1695
1696
				,strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
		if(dest.point) {				/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
1697
			sprintf(str,"%04x%04x.pnt"
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
				,dest.net,dest.node);
			strcat(outbound,str); }
		}
	else
		strcpy(outbound,cfg.outbound);
	if(outbound[strlen(outbound)-1]=='\\')
		outbound[strlen(outbound)-1]=0;
	mkdir(outbound);
	strcat(outbound,"\\");

	if(node<cfg.nodecfgs)
		if(cfg.nodecfg[node].arctype==0xffff) {    /* Uncompressed! */
			if(misc&FLO_MAILER)
				i=write_flofile(infile,dest);
			else
				i=create_netmail(0,infile,"\1FLAGS KFS\r",dest,1);
			if(i) bail(1);
			return; }

	if(dest.point && !(misc&FLO_MAILER))
rswindell's avatar
rswindell committed
1718
1719
1720
1721
		sprintf(fname,"%s%04hxp%03hx.%s",outbound,0,(short)dest.point,day);
	else
		sprintf(fname,"%s%04hx%04hx.%s",outbound,(short)(scfg.faddr[0].net-dest.net)
			,(short)(scfg.faddr[0].node-dest.node),day);
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
	for(i='0';i<='Z';i++) {
		if(i==':')
			i='A';
		sprintf(str,"%s%c",fname,i);
		if(flength(str)==0)
			if(delfile(str))
				logprintf("ERROR line %d removing %s %s",__LINE__,str
					,sys_errlist[errno]);
		if(fexist(str)) {
			if((p=strrchr(str,'\\'))!=NULL)
				p++;
			else
				p=str;
			if(flength(str)>=cfg.maxbdlsize)
				continue;
			file=sopen(str,O_WRONLY,SH_DENYRW);
			if(file==-1)		/* Can't open?!? Probably being sent */
				continue;
			close(file);
			if(!attachment(p,dest,2))
				attachment(p,dest,0);
			pack(infile,str,dest);
			if(delfile(infile))
				logprintf("ERROR line %d removing %s %s",__LINE__,infile
					,sys_errlist[errno]);
			return; }
		else {
			if(misc&FLO_MAILER)
				j=write_flofile(str,dest);
			else {
				if((p=strrchr(str,'\\'))!=NULL)
					p++;
				else
					p=str;
				j=attachment(p,dest,0); }
			if(j)
				bail(1);
			pack(infile,str,dest);
			if(delfile(infile))
				logprintf("ERROR line %d removing %s %s",__LINE__,infile
					,sys_errlist[errno]);
			return; } }

	pack(infile,str,dest);	/* Won't get here unless all bundles are full */
}

/******************************************************************************
 This function checks the inbound directory for the first bundle it finds, it
 will then unpack and delete the bundle.  If no bundles exist this function
 returns a 0, otherwise a 1 is returned.
******************************************************************************/
int unpack_bundle(void)
{
	char str[256];
	static char fname[256];
	int i;
	struct _finddata_t ff;
	static long find=-1;
	BOOL		found;

	for(i=0;i<7;i++) {
		sprintf(str,"%s*.%s?",secure ? cfg.secure : cfg.inbound
			,(i==0) ? "su" : (i==1) ? "mo" : (i==2) ? "tu" : (i==3) ? "we" : (i==4)
			? "th" : (i==5) ? "fr" : "sa");
		if(find==-1)
			found=(find=_findfirst(str,&ff))!=-1;
		else {
			found=(_findnext(find,&ff)==0);
			if(!found) {
				_findclose(find);
				found=(find=_findfirst(str,&ff))!=-1; 
			} 
		}
		if(found) {
			sprintf(fname,"%s%s",secure ? cfg.secure : cfg.inbound,ff.name);
			if(unpack(fname)) {
				if(ff.time_write+(48L*60L*60L)>time(NULL)) {
					strcpy(str,fname);
					str[strlen(str)-2]='_';
					if(fexist(str))
						str[strlen(str)-2]='-';
					if(fexist(str))
						delfile(str);
					if(rename(fname,str))
						logprintf("ERROR line %d renaming %s to %s"
							,__LINE__,fname,str); } }
			else if(delfile(fname))
				logprintf("ERROR line %d removing %s %s",__LINE__,fname
					,sys_errlist[errno]);
			return(1); } }

	if(find!=-1) {
		_findclose(find);
		find=-1;
	}
	return(0);
}

void remove_re(char *str)
{
	while(!strnicmp(str,"RE:",3)) {
		strcpy(str,str+3);
		while(str[0]==SP)
			strcpy(str,str+1); }
}

/****************************************************************************/
/* Moves or copies a file from one dir to another                           */
/* both 'src' and 'dest' must contain full path and filename                */
/* returns 0 if successful, -1 if error                                     */
/****************************************************************************/
int mv(char *src, char *dest, char copy)
{
	char buf[4096],str[256];
	int  ind,outd;
	long length,chunk=4096,l;
    FILE *inp,*outp;

	if(!strcmp(src,dest))	/* source and destination are the same! */
		return(0);
	if(!fexist(src)) {
		logprintf("MV ERROR: Source doesn't exist '%s",src);
		return(-1); }
	if(!copy && fexist(dest)) {
		logprintf("MV ERROR: Destination already exists '%s'",dest);
		return(-1); }
	if(!copy && ((src[1]!=':' && dest[1]!=':')
		|| (src[1]==':' && dest[1]==':' && toupper(src[0])==toupper(dest[0])))) {
		if(rename(src,dest)) {						/* same drive, so move */
			logprintf("MV ERROR: Error renaming %s to %s",src,dest);
			return(-1); }
		return(0); }
	if((ind=nopen(src,O_RDONLY))==-1) {
		logprintf("MV ERROR: ERR_OPEN %s",src);
		return(-1); }
	if((inp=fdopen(ind,"rb"))==NULL) {
		close(ind);
		logprintf("MV ERROR: ERR_FDOPEN %s",str);
		return(-1); }
	setvbuf(inp,NULL,_IOFBF,8*1024);
	if((outd=nopen(dest,O_WRONLY|O_CREAT|O_TRUNC))==-1) {
		fclose(inp);
		logprintf("MV ERROR: ERR_OPEN %s",dest);
		return(-1); }
	if((outp=fdopen(outd,"wb"))==NULL) {
		close(outd);
		fclose(inp);
		logprintf("MV ERROR: ERR_FDOPEN %s",str);
		return(-1); }
	setvbuf(outp,NULL,_IOFBF,8*1024);
	length=filelength(ind);
	l=0L;
	while(l<length) {
		if(l+chunk>length)
			chunk=length-l;
		fread(buf,chunk,1,inp);
		fwrite(buf,chunk,1,outp);
		l+=chunk; }
	fclose(inp);
	fclose(outp);
	if(!copy && delfile(src)) {
		logprintf("ERROR line %d removing %s %s",__LINE__,src,sys_errlist[errno]);
		return(-1); }
	return(0);
}

/****************************************************************************/
/* Returns the total number of msgs in the sub-board and sets 'ptr' to the  */
/* date of the last message in the sub (0) if no messages.					*/
/****************************************************************************/
ulong getlastmsg(uint subnum, ulong *ptr, time_t *t)
{
	int i;
	smb_t smbfile;

	if(subnum>=scfg.total_subs) {
		printf("\nERROR getlastmsg, subnum=%d\n",subnum);
		logprintf("ERROR line %d getlastmsg %d",__LINE__,subnum);
		bail(1); }
	sprintf(smbfile.file,"%s%s",scfg.sub[subnum]->data_dir,scfg.sub[subnum]->code);
	smbfile.retry_time=scfg.smb_retry_time;
	if((i=smb_open(&smbfile))!=0) {
		printf("ERROR %d opening %s\n",i,smbfile.file);
		logprintf("ERROR %d line %d opening %s",i,__LINE__,smbfile.file);
		return(0); }

	if(!filelength(fileno(smbfile.shd_fp))) {			/* Empty base */
		if(ptr) (*ptr)=0;
		smb_close(&smbfile);
		return(0); }
	smb_close(&smbfile);
	if(ptr) (*ptr)=smbfile.status.last_msg;
	return(smbfile.status.total_msgs);
}


ulong loadmsgs(post_t HUGE16 **post, ulong ptr)
{
	int i;
	long l=0,total;
	idxrec_t idx;


	if((i=smb_locksmbhdr(&smb[cur_smb]))!=0) {
		printf("ERROR %d locking %s\n",i,smb[cur_smb].file);
		logprintf("ERROR %d line %d locking %s",i,__LINE__,smb[cur_smb].file);
		return(0L); }

	/* total msgs in sub */
	total=filelength(fileno(smb[cur_smb].sid_fp))/sizeof(idxrec_t);

	if(!total) {			/* empty */
		smb_unlocksmbhdr(&smb[cur_smb]);
		return(0); }

	if(((*post)=(post_t HUGE16 *)LMALLOC(sizeof(post_t)*total))    /* alloc for max */
		==NULL) {
		smb_unlocksmbhdr(&smb[cur_smb]);
		printf("ERROR allocating %lu bytes for %s\n",sizeof(post_t *)*total
			,smb[cur_smb].file);
		logprintf("ERROR line %d allocating %lu bytes for %s",__LINE__
			,sizeof(post_t *)*total,smb[cur_smb].file);
		return(0); }

	fseek(smb[cur_smb].sid_fp,0L,SEEK_SET);
	while(!feof(smb[cur_smb].sid_fp)) {
1948
		if(smb_fread(&idx,sizeof(idx),smb[cur_smb].sid_fp) != sizeof(idx))
1949
1950
			break;

1951
1952
1953
		if(idx.number==0)	/* invalid message number, ignore */
			continue;

1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
		if(idx.number<=ptr || idx.attr&MSG_DELETE)
			continue;

		if(idx.attr&MSG_MODERATED && !(idx.attr&MSG_VALIDATED))
			break;

		(*post)[l].offset=idx.offset;
		(*post)[l].number=idx.number;
		(*post)[l].to=idx.to;
		(*post)[l].from=idx.from;
		(*post)[l].subj=idx.subj;
		l++; }
	smb_unlocksmbhdr(&smb[cur_smb]);
	if(!l)
		LFREE(*post);
	return(l);
}

void allocfail(uint size)
{
	printf("\7ERROR allocating %u bytes of memory.\n",size);
	bail(1);
}

void bail(int code)
{
1980
1981
	if(code || pause_on_exit) {
		printf("\nHit any key...");
1982
		getch();
1983
	}
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
	exit(code);
}

typedef struct {
	ulong	alias,
			real;
			} username_t;

/****************************************************************************/
/* Note: Wrote another version of this function that read all userdata into */
/****************************************************************************/
/* Looks for a perfect match amoung all usernames (not deleted users)		*/
/* Returns the number of the perfect matched username or 0 if no match		*/
/* Called from functions waitforcall and newuser							*/
/* memory then scanned it from memory... took longer - always.              */
/****************************************************************************/
ulong matchname(char *inname)
{
	static ulong total_users;
	static username_t *username;
	ulong last_user;
	int userdat,i;
	char str[256],name[LEN_NAME+1],alias[LEN_ALIAS+1],c;
	ulong l,crc;

if(!total_users) {		/* Load CRCs */
	fprintf(stderr,"\n%-25s","Loading user names...");
	sprintf(str,"%suser/user.dat",scfg.data_dir);
	if((userdat=nopen(str,O_RDONLY|O_DENYNONE))==-1)
		return(0);
	last_user=filelength(userdat)/U_LEN;
	for(total_users=0;total_users<last_user;total_users++) {
		printf("%5ld\b\b\b\b\b",total_users);
		if((username=(username_t *)REALLOC(username
			,(total_users+1L)*sizeof(username_t)))==NULL)
            break;
		username[total_users].alias=0;