sbbsecho.c 141 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* 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>
#endif

#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
21
#include <sys/stat.h>
22
23
24
25
26

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

27
28
29
30
#ifndef __FreeBSD__
	#include <malloc.h>
#endif

rswindell's avatar
rswindell committed
31
#include "conwrap.h"		/* getch() */
32
33
34
35
36
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
#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;

66
BOOL pause_on_exit=FALSE;
67
68
69
70
71
72
73
74
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

#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

101
#if defined(__unix__)	/* borrowed from MSVC */
rswindell's avatar
rswindell committed
102
unsigned _rotr (
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
        unsigned val,
        int shift
        )
{
        register unsigned lobit;        /* non-zero means lo bit set */
        register unsigned num = val;    /* number to rotate */

        shift &= 0x1f;                  /* modulo 32 -- this will also make
                                           negative shifts work */

        while (shift--) {
                lobit = num & 1;        /* get high bit */
                num >>= 1;              /* shift right one bit */
                if (lobit)
                        num |= 0x80000000;  /* set hi bit if lo bit was set */
        }

        return num;
}
#endif

rswindell's avatar
rswindell committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/******************************************************************************
 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);
154
	chcount=vsnprintf(sbuf,sizeof(sbuf),fmat,argptr);
155
	sbuf[sizeof(sbuf)-1]=0;
rswindell's avatar
rswindell committed
156
157
158
159
160
	va_end(argptr);
	lputs(sbuf);
	return(chcount);
}

161
162
163
164
165
166
167
168
169
170
/**********************/
/* Log print function */
/**********************/
void logprintf(char *str, ...)
{
    va_list argptr;
    char buf[256];
    time_t now;
    struct tm *gm;

171
172
173
174
175
176
177
178
179
180
181
182
183
	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);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
}

/*****************************************************************************/
/* 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 */
rswindell's avatar
rswindell committed
205
206
207
                if(cfg->temp_dir[0]!='\\' 
					&& cfg->temp_dir[0]!='/' 
					&& cfg->temp_dir[1]!=':') {
208
209
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->temp_dir);
210
                    if(FULLPATH(str2,str,40))
211
212
213
214
215
216
217
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str);}
                else
                    strcat(cmd,cfg->temp_dir);
                break;
            case 'J':
rswindell's avatar
rswindell committed
218
219
220
                if(cfg->data_dir[0]!='\\' 
					&& cfg->data_dir[0]!='/' 
					&& cfg->data_dir[1]!=':') {
221
222
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->data_dir);
223
                    if(FULLPATH(str2,str,40))
224
225
226
227
228
229
230
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str); }
                else
                    strcat(cmd,cfg->data_dir);
                break;
            case 'K':
rswindell's avatar
rswindell committed
231
232
233
                if(cfg->ctrl_dir[0]!='\\' 
					&& cfg->ctrl_dir[0]!='/' 
					&& cfg->ctrl_dir[1]!=':') {
234
235
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->ctrl_dir);
236
                    if(FULLPATH(str2,str,40))
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
                        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 */
rswindell's avatar
rswindell committed
256
257
258
                if(cfg->exec_dir[0]!='\\' 
					&& cfg->exec_dir[0]!='/' 
					&& cfg->exec_dir[1]!=':') {
259
260
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->exec_dir);
261
                    if(FULLPATH(str2,str,40))
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
                        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)
{
rswindell's avatar
rswindell committed
298
#if 1
299
	return system(cmdline);
rswindell's avatar
rswindell committed
300
#else
301
302
303
304
305
306
307
308
309
310
311
312
313
314
	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);
rswindell's avatar
rswindell committed
315
#endif
316
317
318
319
320
321
322
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
}
/******************************************************************************
 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
353
354
355
356
	if(attr&ATTR_CRASH) ch='c';
	else if(attr&ATTR_HOLD) ch='h';
	else if(attr&ATTR_DIRECT) ch='d';
	else ch='f';
357
358
359
	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
360
		sprintf(outbound,"%.*s.%03x/"
rswindell's avatar
rswindell committed
361
			,(int)strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
362
	if(dest.point) {					/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
363
		sprintf(str,"%04x%04x.pnt"
364
365
			,dest.net,dest.node);
		strcat(outbound,str); }
rswindell's avatar
rswindell committed
366
367
	if(outbound[strlen(outbound)-1]=='\\'
		|| outbound[strlen(outbound)-1]=='/')
368
		outbound[strlen(outbound)-1]=0;
rswindell's avatar
rswindell committed
369
	MKDIR(outbound);
370
	backslash(outbound);
371
	if(dest.point)
rswindell's avatar
rswindell committed
372
		sprintf(fname,"%s%08x.%clo",outbound,dest.point,ch);
373
	else
rswindell's avatar
rswindell committed
374
		sprintf(fname,"%s%04x%04x.%clo",outbound,dest.net,dest.node,ch);
375
	if((stream=fnopen(&file,fname,O_WRONLY|O_CREAT|O_TRUNC))==NULL) {
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
		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);
452
	tm=localtime(&t);
453
454
455
456
457
458
459
460
461
462
463
464
	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);
465
	sprintf(str,"\1INTL %hu:%hu/%hu %hu:%hu/%hu\r"
466
467
468
469
470
471
472
473
474
475
		,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) {
476
		sprintf(str,"\1TOPT %hu\r",hdr.destpoint);
477
478
		fwrite(str,strlen(str),1,fstream); }
	if(hdr.origpoint) {
479
		sprintf(str,"\1FMPT %hu\r",hdr.origpoint);
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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
		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

rswindell's avatar
rswindell committed
690
int check_elists(char *areatag,faddr_t addr)
691
692
{
	FILE *stream;
rswindell's avatar
rswindell committed
693
694
	char str[1025],quit=0,*p;
	int i,j,k,x,file,match=0;
695

rswindell's avatar
rswindell committed
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
	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);
730
						quit=1;
rswindell's avatar
rswindell committed
731
732
733
734
						if(match)
							return(match);
						break; } } } }
	return(match);
735
736
737
738
739
740
741
742
}
/******************************************************************************
 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]
743
		,drive[3],dir[MAX_PATH+1],name[MAX_PATH+1],ext[MAX_PATH+1],outpath[128]
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
		,*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
841
							,faddrtoa(&cfg.area[i].uplink[j],NULL)); }
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
					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
888
							,faddrtoa(&cfg.area[i].uplink[j],NULL));
889
890
891
892
893
894
895
896
897
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
					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
941
								tagcrc=crc32(strupr(fields),0);
942
943
944
945
946
947
948
949
950
951
952
953
954
955
								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
956
957
										,faddrtoa(&cfg.listcfg[j].forward,NULL));
								fprintf(afileout," %s\r\n",faddrtoa(&addr,NULL));
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
997
998
999
								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
1000
		,drive[3],dir[MAX_PATH+1],name[MAX_PATH+1],ext[MAX_PATH+1],match=0;
1001
1002
1003
	int i,j,k,file;
	faddr_t taddr;

rswindell's avatar
rswindell committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
	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; }
1024

rswindell's avatar
rswindell committed
1025
1026
1027
1028
1029
1030
1031
1032
	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);
1033
			continue; }
rswindell's avatar
rswindell committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
		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
					,faddrtoa(&cfg.nodecfg[i].faddr,NULL)
					,(*p) ? p : "");
				match=1;
1055
				continue; }
rswindell's avatar
rswindell committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
			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"
							,faddrtoa(&cfg.nodecfg[j].faddr,NULL)); } }
				fprintf(outfile,"\r\n");
				continue; } }

		if(option==1 && !strcmp(tmp,"AREAFIX")) {       /* Change Password */
			if(!*p)
1071
				continue;
rswindell's avatar
rswindell committed
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
1108
1109
1110
1111
1112
1113
1114
1115
			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
					,faddrtoa(&cfg.nodecfg[i].faddr,NULL),new,p);
				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);
					fprintf(outfile," %s",faddrtoa(&cfg.nodecfg[j].faddr,NULL));
					k++;
					continue; }
				if(option==3 && j==i)
					continue;
				if(cfg.nodecfg[j].attr&ATTR_PASSIVE) {
					if(!k) fprintf(outfile,"%-10s",tmp);
					fprintf(outfile," %s",faddrtoa(&cfg.nodecfg[j].faddr,NULL));
					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
				,faddrtoa(&cfg.nodecfg[i].faddr,NULL));
		if(option==2)
			fprintf(outfile,"%-10s %s\r\n","PASSIVE"
				,faddrtoa(&cfg.nodecfg[i].faddr,NULL)); }

	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);
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
}
/******************************************************************************
 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);
1129
	if(node>=cfg.nodecfgs)
1130
1131
1132
1133
1134
		return;
	memset(&add_area,0,sizeof(area_t));
	memset(&del_area,0,sizeof(area_t));
	strupr(instr);
	if((p=strstr(instr,"HELP"))!=NULL) {
rswindell's avatar
rswindell committed
1135
		sprintf(str,"%sAREAMGR.HLP",scfg.exec_dir);
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
		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;
1201
		if(node>=cfg.nodecfgs)		   /* Should never happen */
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
1257
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
			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
1316
1317
		logprintf("Remote maintenance for %s requested via %s",str
			,faddrtoa(&addr,NULL));
1318
1319
1320
		addr=atofaddr(str); }

	i=matchnode(addr,0);
1321
	if(i>=cfg.nodecfgs) {
1322
1323
1324
1325
		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
1326
1327
			"is not currently configured for areafix.\r\n"
			,faddrtoa(&addr,NULL));
1328
1329
1330
1331
1332
1333
		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
1334
			"for this node is %s.\r\n",faddrtoa(&addr,NULL),password
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
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
			,(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
1402
			"body or with no valid commands.\r\n",faddrtoa(&addr,NULL));
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
1458
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
		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)); }
}

rswindell's avatar
rswindell committed
1484
int attachment(char *bundlename,faddr_t dest,char cleanup)
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
{
	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;

1495
	if(bundlename==NULL && cleanup!=1) {
rswindell's avatar
rswindell committed
1496
1497
1498
		logprintf("ERROR line %d NULL bundlename",__LINE__);
		return(1);
	}
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
	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 */

1517
1518
1519
1520
1521
1522
1523
		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
1524
		for(f=0;f<g.gl_pathc;f++) {
1525

rswindell's avatar
rswindell committed
1526
			path=g.gl_pathv[f];
1527

rswindell's avatar
rswindell committed
1528
1529
1530
1531
			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]);
1532
1533
				continue; 
			}
rswindell's avatar
rswindell committed
1534
			if(filelength(fmsg)<sizeof(fmsghdr_t)) {
rswindell's avatar
rswindell committed
1535
1536
1537
1538
1539
1540
				printf("\7ERROR %s has invalid length of %lu bytes\n"
					,path
					,filelength(fmsg));
				logprintf("ERROR line %d %s has invalid length of %lu bytes"
					,__LINE__
					,path
rswindell's avatar
rswindell committed
1541
1542
					,filelength(fmsg));
				fclose(fidomsg);
1543
1544
				continue; 
			}
rswindell's avatar
rswindell committed
1545
1546
1547
1548
1549
1550
			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);
1551
1552
				continue; 
			}
1553
			fclose(fidomsg);
rswindell's avatar
rswindell committed
1554
			if(!(hdr.attr&FIDO_FILE))		/* Not a file attach */
1555
				continue;
rswindell's avatar
rswindell committed
1556
			num_mfncrc++;
rswindell's avatar
rswindell committed
1557
1558
1559
1560
			p=strrchr(hdr.subj,'/');
			if(p==NULL)
				p=strrchr(hdr.subj,'\\');
			if(p!=NULL)
rswindell's avatar
rswindell committed
1561
1562
1563
1564
1565
1566
1567
1568
				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));
1569
1570
				continue; 
			}
rswindell's avatar
rswindell committed
1571
			mfncrc[num_mfncrc-1]=crc32(strupr(p),0); 
1572
1573
		}
		globfree(&g);
rswindell's avatar
rswindell committed
1574

1575
1576
1577
1578
1579
1580
		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
1581
			fncrc=crc32(strupr(attach.fname),0);
1582
1583
			for(crcidx=0;crcidx<num_mfncrc;crcidx++)
				if(mfncrc[crcidx]==fncrc)
1584
					break;
1585
1586
1587
			if(crcidx==num_mfncrc)
				if(create_netmail(0,str,"\1FLAGS KFS\r",attach.dest,1))
					error=1; 
rswindell's avatar
rswindell committed
1588
		}
1589
1590
1591
1592
1593
1594
		if(!error)				/* don't truncate if an error occured */
			chsize(file,0L);
		fclose(stream);
		if(num_mfncrc)
			FREE(mfncrc);
		return(0); 
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
	}

	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;

1624
1625
1626
1627
1628
	if(infile==NULL || infile[0]==0) {
		logprintf("ERROR line %d invalid filename",__LINE__);
		bail(1);
	}

1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
	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
1644
			sprintf(outbound,"%.*s.%03x/"
rswindell's avatar
rswindell committed
1645
				,(int)strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
1646
		if(dest.point) {				/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
1647
			sprintf(str,"%04x%04x.pnt"
1648
1649
1650
1651
1652
				,dest.net,dest.node);
			strcat(outbound,str); }
		}
	else
		strcpy(outbound,cfg.outbound);
rswindell's avatar
rswindell committed
1653
1654
	if(outbound[strlen(outbound)-1]=='\\'
		|| outbound[strlen(outbound)-1]=='/')
1655
		outbound[strlen(outbound)-1]=0;
rswindell's avatar
rswindell committed
1656
	MKDIR(outbound);
1657
	backslash(outbound);
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668

	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
1669
1670
1671
1672
		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);
1673
1674
1675
1676
1677
1678
1679
1680
1681
	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)) {
rswindell's avatar
rswindell committed
1682
1683
1684
1685
			p=strrchr(str,'/');
			if(p==NULL)
				p=strrchr(str,'\\');
			if(p!=NULL)
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
				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 {
rswindell's avatar
rswindell committed
1706
1707
1708
1709
				p=strrchr(str,'/');
				if(p==NULL)
					p=strrchr(str,'\\');
				if(p!=NULL)
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
					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 */
}
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
/******************************************************************************
 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 FALSE, otherwise a TRUE is returned.
 ******************************************************************************/
BOOL unpack_bundle(void)
{
	char*				p;
	char				str[MAX_PATH+1];
	char				fname[MAX_PATH+1];
	int				i;
	static glob_t	g;
	static int		gi;

	for(i=0;i<7;i++) {
		switch(i) {
			case 0:
				p="su";
				break;
			case 1:
				p="mo";
				break;
			case 2:
				p="tu";
				break;
			case 3:
				p="we";
				break;
			case 4:
				p="th";
				break;
			case 5:
				p="fr";
				break;
			default:
				p="sa";
				break;
		}
		sprintf(str,"%s*.%s?",secure ? cfg.secure : cfg.inbound,p);
		if(gi>=g.gl_pathc) {
			gi=0;
			globfree(&g);
			glob(str,0,NULL,&g);
		}
		if(gi<g.gl_pathc) {
			SAFECOPY(fname,g.gl_pathv[gi]);
			if(unpack(fname)) {	/* failure */
				if(fdate(fname)+(48L*60L*60L)>time(NULL)) {
					SAFECOPY(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))	/* successful, so delete bundle */
				logprintf("ERROR line %d removing %s %s",__LINE__,fname
					,sys_errlist[errno]);
			gi++;
			return(TRUE); 
		} 
	}

	return(FALSE);
}
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

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