sbbsecho.c 150 KB
Newer Older
1
/* sbbsecho.c */
2

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

5
6
7
8
9
10
/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html		*
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/
37

38
/* Portions written by Allen Christiansen 1994-1996 						*/
39
40
41
42
43
44
45
46
47
48
49
50
51

#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>
52
#include <sys/stat.h>
53
54
55
56
57

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

58
#ifndef __unix__
59
60
61
	#include <malloc.h>
#endif

rswindell's avatar
rswindell committed
62
#include "conwrap.h"		/* getch() */
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "sbbs.h"			/* load_cfg() */
#include "sbbsdefs.h"
#include "smblib.h"
#include "scfglib.h"
#include "lzh.h"
#include "sbbsecho.h"

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;
79
BOOL twit_list;
80
81
82
83

faddr_t		sys_faddr;
config_t	cfg;
scfg_t		scfg;
84
char		revision[16];
85
char		compiler[32];
86

87
BOOL pause_on_exit=FALSE;
88
89
90
91
92
93
94
95
96

#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;

97
98
99
100
101
102
103
104
105
	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));
106
107
108
109
110
111
112
113
114
115
}
#endif

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

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

122
#if defined(__unix__)	/* borrowed from MSVC */
rswindell's avatar
rswindell committed
123
unsigned _rotr (
124
125
126
127
        unsigned val,
        int shift
        )
{
128
129
130
131
132
133
134
135
136
137
138
    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 */
	}
139

140
    return num;
141
142
143
}
#endif

rswindell's avatar
rswindell committed
144
145
146
/******************************************************************************
 Displays characters locally
******************************************************************************/
147
static int lputs(int level, char *str)
rswindell's avatar
rswindell committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
{
    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));
}

/****************************************************************************/
164
/* This is needed by load_cfg.c												*/
rswindell's avatar
rswindell committed
165
/****************************************************************************/
166
int lprintf(int level, char *fmat, ...)
rswindell's avatar
rswindell committed
167
168
169
170
171
172
{
	va_list argptr;
	char sbuf[256];
	int chcount;

	va_start(argptr,fmat);
173
	chcount=vsnprintf(sbuf,sizeof(sbuf),fmat,argptr);
174
	sbuf[sizeof(sbuf)-1]=0;
rswindell's avatar
rswindell committed
175
	va_end(argptr);
176
	lputs(level, sbuf);
rswindell's avatar
rswindell committed
177
178
179
	return(chcount);
}

180
181
182
183
184
185
186
187
188
189
/**********************/
/* Log print function */
/**********************/
void logprintf(char *str, ...)
{
    va_list argptr;
    char buf[256];
    time_t now;
    struct tm *gm;

190
191
192
193
194
195
196
197
198
199
200
201
202
	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);
203
204
205
206
207
}

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

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
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
298
299
300
301
302
303
304
305
306
307
	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[0]!='/' 
						&& cfg->temp_dir[1]!=':') {
						strcpy(str,cfg->node_dir);
						strcat(str,cfg->temp_dir);
						if(FULLPATH(str2,str,40))
							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[0]!='/' 
						&& cfg->data_dir[1]!=':') {
						strcpy(str,cfg->node_dir);
						strcat(str,cfg->data_dir);
						if(FULLPATH(str2,str,40))
							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[0]!='/' 
						&& cfg->ctrl_dir[1]!=':') {
						strcpy(str,cfg->node_dir);
						strcat(str,cfg->ctrl_dir);
						if(FULLPATH(str2,str,40))
							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[0]!='/' 
						&& cfg->exec_dir[1]!=':') {
						strcpy(str,cfg->node_dir);
						strcat(str,cfg->exec_dir);
						if(FULLPATH(str2,str,40))
							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;
308

309
	return(cmd);
310
311
312
313
314
315
316
}

/****************************************************************************/
/* Runs an external program directly using spawnvp							*/
/****************************************************************************/
int execute(char *cmdline)
{
rswindell's avatar
rswindell committed
317
#if 1
318
	return system(cmdline);
rswindell's avatar
rswindell committed
319
#else
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	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
334
#endif
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
/******************************************************************************
 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)
{
364
365
366
367
368
	char fname[MAX_PATH+1];
	char outbound[MAX_PATH+1];
	char str[MAX_PATH+1];
	char ch;
	char searchstr[MAX_PATH+1];
369
370
371
372
373
374
375
376
	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
377
378
379
380
	if(attr&ATTR_CRASH) ch='c';
	else if(attr&ATTR_HOLD) ch='h';
	else if(attr&ATTR_DIRECT) ch='d';
	else ch='f';
381
382
	if(dest.zone==scfg.faddr[0].zone)		/* Default zone, use default outbound */
		strcpy(outbound,cfg.outbound);
383
384
	else {								/* Inter-zone outbound is OUTBOUND.XXX */
		sprintf(outbound,"%.*s.%03x"
rswindell's avatar
rswindell committed
385
			,(int)strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
386
387
388
		MKDIR(outbound);
		backslash(outbound);
	}
389
	if(dest.point) {					/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
390
		sprintf(str,"%04x%04x.pnt"
391
392
			,dest.net,dest.node);
		strcat(outbound,str); }
rswindell's avatar
rswindell committed
393
394
	if(outbound[strlen(outbound)-1]=='\\'
		|| outbound[strlen(outbound)-1]=='/')
395
		outbound[strlen(outbound)-1]=0;
rswindell's avatar
rswindell committed
396
	MKDIR(outbound);
397
	backslash(outbound);
398
	if(dest.point)
rswindell's avatar
rswindell committed
399
		sprintf(fname,"%s%08x.%clo",outbound,dest.point,ch);
400
	else
rswindell's avatar
rswindell committed
401
		sprintf(fname,"%s%04x%04x.%clo",outbound,dest.net,dest.node,ch);
402
403
404
	sprintf(searchstr,"^%s",attachment);
	if(findstr(searchstr,fname))	/* file already in FLO file */
		return(0);
405
	if((stream=fnopen(&file,fname,O_WRONLY|O_APPEND|O_CREAT))==NULL) {
406
407
		printf("\7ERROR line %d opening %s %s\n",__LINE__,fname,strerror(errno));
		logprintf("ERROR line %d opening %s %s",__LINE__,fname,strerror(errno));
408
409
		return(-1); 
	}
410
411
412
413
414
	fprintf(stream,"^%s\r\n",attachment);
	fclose(stream);
	return(0);
}

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* Writes text buffer to file, expanding sole LFs to CRLFs */
size_t fwrite_crlf(char* buf, size_t len, FILE* fp)
{
	char	ch,last_ch=0;
	size_t	i;
	size_t	wr=0;	/* total chars written (may be > len) */

	for(i=0;i<len;i++) {
		ch=*buf++;
		if(ch=='\n' && last_ch!='\r') {
			if(fputc('\r',fp)==EOF)
				return(wr);
			wr++;
		}
		if(fputc(ch,fp)==EOF)
			return(wr);
		wr++;
		last_ch=ch;
	}

	return(wr);
}

438
439
440
441
442
/******************************************************************************
 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.
******************************************************************************/
443
int create_netmail(char *to, char *subject, char *body, faddr_t dest, BOOL file_attached)
444
445
446
447
448
449
450
451
452
453
454
455
{
	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;

456
457
458
459
460
461
462
463
464
465
466
	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++) {
467
			sprintf(fname,"%s%u.msg",scfg.netmail_dir,i);
468
469
470
			if(!fexistcase(fname))
				break; 
		}
471
472
473
474
475
476
		if(!i) {
			printf("\7%s directory full!\n",scfg.netmail_dir);
			logprintf("Directory full: %s",scfg.netmail_dir);
			return(-1); }
		startmsg=i+1;
		if((fstream=fnopen(&fmsg,fname,O_RDWR|O_CREAT))==NULL) {
477
478
			printf("\7ERROR line %d opening %s %s\n",__LINE__,fname,strerror(errno));
			logprintf("ERROR line %d opening %s %s",__LINE__,fname,strerror(errno));
479
			return(-1); }
480

481
482
483
484
485
486
487
488
489
490
491
492
		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);
493
		if(file_attached)
494
			hdr.attr|=FIDO_FILE;
495

496
497
498
499
		if(attr&ATTR_HOLD)
			hdr.attr|=FIDO_HOLD;
		if(attr&ATTR_CRASH)
			hdr.attr|=FIDO_CRASH;
500

501
502
503
504
505
506
507
508
509
510
		sprintf(hdr.from,"SBBSecho");

		t=time(NULL);
		tm=localtime(&t);
		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);
511
		else
512
513
514
515
516
517
518
519
520
			sprintf(hdr.to,"SYSOP");

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

		fwrite(&hdr,sizeof(fmsghdr_t),1,fstream);
		sprintf(str,"\1INTL %hu:%hu/%hu %hu:%hu/%hu\r"
			,hdr.destzone,hdr.destnet,hdr.destnode
			,hdr.origzone,hdr.orignet,hdr.orignode);
		fwrite(str,strlen(str),1,fstream);
521
522
523
524
525
526
527
528

		/* Add FSC-53 FLAGS kludge */
		fprintf(fstream,"\1FLAGS");
		if(attr&ATTR_DIRECT)
			fprintf(fstream," DIR");
		if(file_attached) {
			if(misc&TRUNC_BUNDLES)
				fprintf(fstream," TFS");
529
			else
530
531
532
533
				fprintf(fstream," KFS");
		}
		fprintf(fstream,"\r");

534
535
536
537
538
539
		if(hdr.destpoint) {
			sprintf(str,"\1TOPT %hu\r",hdr.destpoint);
			fwrite(str,strlen(str),1,fstream); }
		if(hdr.origpoint) {
			sprintf(str,"\1FMPT %hu\r",hdr.origpoint);
			fwrite(str,strlen(str),1,fstream); }
540
		if(!file_attached || (!(attr&ATTR_DIRECT) && file_attached))
541
			fwrite_crlf(body,strlen(body)+1,fstream);	/* Write additional NULL */
542
543
544
		else
			fwrite("\0",1,1,fstream);               /* Write NULL */
		fclose(fstream);
545
	} while(!fexistcase(fname));
546
	return(0);
547
548
549
550
551
552
553
554
555
556
557
}

/******************************************************************************
 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;

558
559
560
561
562
563
564
565
566
567
568
569
	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) {
570
			p++;
571
572
			if(*p) {
				*p=0;
573
				p++;
574
575
576
577
578
579
				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");
580
581
		create_netmail(to,title,buf,addr,FALSE); 
	}
582
	FREE(buf);
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
}
/******************************************************************************
 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
691
									,strerror(errno));
692
693
694
695
696
697
698
699
								match=1;
								break; }
							while(!feof(stream)) {
								if(!fgets(str,255,stream))
									break;
								truncsp(str);
								strcat(str,"\r\n");
								p=str;
700
								SKIP_WHITESPACE(p);
701
702
703
704
								if(*p==';')     /* Ignore Comment Lines */
									continue;
								strcpy(temp,p);
								p=temp;
705
								FIND_WHITESPACE(p);
706
707
708
709
710
711
712
713
714
715
716
717
718
719
								*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))
720
		create_netmail(NULL,title,"None.",addr,FALSE);
721
722
723
724
725
726
727
728
729
730
731
732
	else
		file_to_netmail(tmpf,title,addr,0);
	fclose(tmpf);
}
/******************************************************************************
 Imitation of Borland's tempnam function because Watcom doesn't have it
******************************************************************************/
char *tempname(char *dir, char *prefix)
{
	char str[256],*p;
	int i;

733
734
735
736
737
738
739
740
741
742
743
744
745
	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);
746
747
}

rswindell's avatar
rswindell committed
748
int check_elists(char *areatag,faddr_t addr)
749
750
{
	FILE *stream;
rswindell's avatar
rswindell committed
751
752
	char str[1025],quit=0,*p;
	int i,j,k,x,file,match=0;
753

rswindell's avatar
rswindell committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
	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;
777
							SKIP_WHITESPACE(p);
rswindell's avatar
rswindell committed
778
779
780
781
							if(*p==';')     /* Ignore Comment Lines */
								continue;
							strcpy(str,p);
							p=str;
782
							FIND_WHITESPACE(p);
rswindell's avatar
rswindell committed
783
784
785
786
787
							*p=0;
							if(!stricmp(areatag,str)) {
								match=1;
								break; } }
						fclose(stream);
788
						quit=1;
rswindell's avatar
rswindell committed
789
790
791
792
						if(match)
							return(match);
						break; } } } }
	return(match);
793
794
795
796
797
798
799
800
}
/******************************************************************************
 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]
801
		,drive[3],dir[MAX_PATH+1],name[MAX_PATH+1],ext[MAX_PATH+1],outpath[128]
802
803
804
805
		,*outname,*p,*tp,nomatch=0,match=0;
	int i,j,k,x,y,file;
	ulong tagcrc;

806
807
808
809
810
811
812
813
814
815
816
817
818
819
	_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
820
			,strerror(errno));
821
822
823
824
825
826
		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
827
			,strerror(errno));
828
829
830
831
832
833
834
835
836
837
		fclose(afileout);
		fclose(nmfile);
		free(outname);
		return; }
	while(!feof(afilein)) {
		if(!fgets(fields,1024,afilein))
			break;
		truncsp(fields);
		strcat(fields,"\r\n");
		p=fields;
838
		SKIP_WHITESPACE(p);
839
840
841
		if(*p==';') {    /* Skip Comment Lines */
			fprintf(afileout,"%s",fields);
			continue; }
842
		SAFECOPY(field1,p);         /* Internal Code Field */
843
		tp=field1;
844
		FIND_WHITESPACE(tp);
845
		*tp=0;
846
847
848
		FIND_WHITESPACE(p);
		SKIP_WHITESPACE(p);
		SAFECOPY(field2,p);         /* Areatag Field */
849
		tp=field2;
850
		FIND_WHITESPACE(tp);
851
		*tp=0;
852
853
		FIND_WHITESPACE(p);
		SKIP_WHITESPACE(p);
854
		if((tp=strchr(p,';'))!=NULL) {
855
856
			SAFECOPY(field3,p);     /* Comment Field (if any) */
			FIND_WHITESPACE(tp);
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
			*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; }
877

878
879
880
881
882
883
						/* 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;
884
885
886
887
888
889
890
891
892
893
894
895
						if(cfg.area[i].uplinks==0) {
							FREE_AND_NULL(cfg.area[i].uplink);
						} else
							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); 
							}
896
897
898
899
900
901
902
903
904
905
906
907

						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 "
								,faddrtoa(&cfg.area[i].uplink[j],NULL)); }
						if(field3[0])
							fprintf(afileout,"%s",field3);
						fprintf(afileout,"\r\n");
						fprintf(nmfile,"%s removed.\r\n",field2);
908
909
910
						break; 
					} 
				}
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
				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);
931
							break; }
932
933
						if(misc&ELIST_ONLY && !check_elists(field2,addr)) {
							fprintf(afileout,"%s",fields);
934
							break; }
935
936
937
938
939
940
941
942
943
944
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

						/* 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 "
								,faddrtoa(&cfg.area[i].uplink[j],NULL));
						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))
992
									break;
993
994
995
								truncsp(str);
								strcat(str,"\r\n");
								p=str;
996
								SKIP_WHITESPACE(p);
997
998
999
1000
								if(*p==';')     /* Ignore Comment Lines */
									continue;
								strcpy(str,p);
								p=str;
1001
								FIND_WHITESPACE(p);
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
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
								*p=0;
								if(!stricmp(add_area.tag[0],"+ALL")) {
									sprintf(fields,"%.1024s",str);
									tagcrc=crc32(strupr(fields),0);
									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"
											,faddrtoa(&cfg.listcfg[j].forward,NULL));
									fprintf(afileout," %s\r\n",faddrtoa(&addr,NULL));
									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))
1040
		create_netmail(NULL,"Area Change Request","No changes made.",addr,FALSE);
1041
1042
1043
1044
1045
1046
	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
1047
			,strerror(errno));
1048
1049
1050
	if(rename(outname,cfg.areafile))		   /* Rename new AREAS.BBS file */
		logprintf("ERROR line %d renaming %s to %s",__LINE__,outname,cfg.areafile);
	free(outname);
1051
1052
1053
1054
1055
1056
1057
1058
1059
}
/******************************************************************************
 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)
******************************************************************************/
1060
void alter_config(faddr_t addr, char *old, char *new, int option)
1061
1062
1063
{
	FILE *outfile,*cfgfile;
	char str[257],outpath[128],tmp[257],tmp2[257],*outname,*p,*tp
1064
		,drive[3],dir[MAX_PATH+1],name[MAX_PATH+1],ext[MAX_PATH+1],match=0;
1065
1066
1067
	int i,j,k,file;
	faddr_t taddr;

rswindell's avatar
rswindell committed
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
	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
1078
			,strerror(errno));
rswindell's avatar
rswindell committed
1079
1080
1081
1082
1083
		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
1084
			,strerror(errno));
rswindell's avatar
rswindell committed
1085
1086
1087
		fclose(outfile);
		free(outname);
		return; }
1088

rswindell's avatar
rswindell committed
1089
1090
1091
1092
1093
	while(!feof(cfgfile)) {
		if(!fgets(str,256,cfgfile))
			break;
		truncsp(str);
		p=str;
1094
		SKIP_WHITESPACE(p);
rswindell's avatar
rswindell committed
1095
1096
		if(*p==';') {
			fprintf(outfile,"%s\r\n",str);
1097
			continue; }
rswindell's avatar
rswindell committed
1098
1099
1100
1101
1102
		sprintf(tmp,"%-.25s",p);
		tp=strchr(tmp,SP);
		if(tp)
			*tp=0;								/* Chop off at space */
		strupr(tmp);							/* Convert code to uppercase */
1103
1104
		FIND_WHITESPACE(p);						/* Skip code */
		SKIP_WHITESPACE(p);						/* Skip white space */
rswindell's avatar
rswindell committed
1105
1106
1107
1108
1109
1110

		if(option==0 && !strcmp(tmp,"USEPACKER")) {     /* Change Compression */
			if(!*p)
				continue;
			strcpy(tmp2,p);
			p=tmp2;
1111
			FIND_WHITESPACE(p);
rswindell's avatar
rswindell committed
1112
1113
1114
1115
1116
1117
1118
			*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;
1119
				continue; }
rswindell's avatar
rswindell committed
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
			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)
1135
				continue;
rswindell's avatar
rswindell committed
1136
1137
			taddr=atofaddr(p);
			if(!memcmp(&cfg.nodecfg[i].faddr,&taddr,sizeof(faddr_t))) {
1138
1139
1140
1141
				FIND_WHITESPACE(p); 	/* Skip over address */
				SKIP_WHITESPACE(p);	/* Skip over whitespace */
				FIND_WHITESPACE(p); 	/* Skip over password */
				SKIP_WHITESPACE(p);	/* Skip over whitespace */
rswindell's avatar
rswindell committed
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
				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
1176
			,strerror(errno));
rswindell's avatar
rswindell committed
1177
1178
1179
	if(rename(outname,cfg.cfgfile))
		logprintf("ERROR line %d renaming %s to %s",__LINE__,outname,cfg.cfgfile);
	free(outname);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
}
/******************************************************************************
 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);
1193
	if(node>=cfg.nodecfgs)
1194
1195
1196
1197
1198
		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
1199
		sprintf(str,"%sAREAMGR.HLP",scfg.exec_dir);
1200
		if(!fexistcase(str))
1201
1202
1203
1204
			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
1205
				,strerror(errno));
1206
1207
			return; 
		}
1208
1209
1210
		l=filelength(file);
		if((buf=(char *)LMALLOC(l+1L))==NULL) {
			printf("ERROR line %d allocating %lu bytes for %s\n",__LINE__,l,str);
1211
1212
			return; 
		}
1213
1214
1215
		fread(buf,l,1,stream);
		fclose(stream);
		buf[l]=0;
1216
		create_netmail(NULL,"Area Manager Help",buf,addr,FALSE);
1217
		LFREE(buf);
1218
1219
		return; 
	}
1220
1221
1222

	if((p=strstr(instr,"LIST"))!=NULL) {
		netmail_arealist(0,addr);
1223
1224
		return; 
	}
1225
1226
1227

	if((p=strstr(instr,"QUERY"))!=NULL) {
		netmail_arealist(1,addr);
1228
1229
		return; 
	}
1230
1231
1232

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

	if((p=strstr(instr,"COMPRESSION"))!=NULL) {
1237
1238
		FIND_WHITESPACE(p);
		SKIP_WHITESPACE(p);
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
		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);
1256
1257
			return; 
		}
1258
1259
1260
1261
		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);
1262
		create_netmail(NULL,"Compression Type Change",str,addr,FALSE);
1263
1264
		return; 
	}
1265
1266

	if((p=strstr(instr,"PASSWORD"))!=NULL) {
1267
1268
		FIND_WHITESPACE(p);
		SKIP_WHITESPACE(p);
1269
1270
		sprintf(temp,"%-.25s",p);
		p=temp;
1271
		FIND_WHITESPACE(p);
1272
		*p=0;
1273
		if(node>=cfg.nodecfgs)		   /* Should never happen */
1274
1275
1276
1277
			return;
		if(!stricmp(temp,cfg.nodecfg[node].password)) {
			sprintf(str,"Your password was already set to %s."
				,cfg.nodecfg[node].password);
1278
			create_netmail(NULL,"Password Change Request",str,addr,FALSE);
1279
1280
1281
1282
1283
			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);
1284
		create_netmail(NULL,"Password Change Request",str,addr,FALSE);
1285
1286
		return; 
	}
1287
1288
1289

	if((p=strstr(instr,"RESCAN"))!=NULL) {
		export_echomail("",addr);
1290
		create_netmail(NULL,"Rescan Areas"
1291
			,"All connected areas carried by your hub have been rescanned."
1292
1293
1294
			,addr,FALSE);
		return; 
	}
1295
1296
1297

	if((p=strstr(instr,"ACTIVE"))!=NULL) {
		if(!(cfg.nodecfg[node].attr&ATTR_PASSIVE)) {
1298
1299
			create_netmail(NULL,"Reconnect Disconnected Areas"
				,"Your areas are already connected.",addr,FALSE);
1300
1301
			return; }
		alter_config(addr,0,0,3);
1302
1303
1304
1305
		create_netmail(NULL,"Reconnect Disconnected Areas"
			,"Temporarily disconnected areas have been reconnected.",addr,FALSE);
		return; 
	}
1306
1307
1308

	if((p=strstr(instr,"PASSIVE"))!=NULL) {
		if(cfg.nodecfg[node].attr&ATTR_PASSIVE) {
1309
1310
1311
1312
			create_netmail(NULL,"Temporarily Disconnect Areas"
				,"Your areas are already temporarily disconnected.",addr,FALSE);
			return; 
		}
1313
		alter_config(addr,0,0,2);
1314
1315
1316
1317
		create_netmail(NULL,"Temporarily Disconnect Areas"
			,"Your areas have been temporarily disconnected.",addr,FALSE);
		return; 
	}
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340

	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);
1341
1342
		return; 
	}
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363

	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);
1364
1365
		return; 
	}
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
}
/******************************************************************************
 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 */
1382
		FIND_CHAR(p,'\n');
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
		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
1394
1395
		logprintf("Remote maintenance for %s requested via %s",str
			,faddrtoa(&addr,NULL));
1396
1397
1398
		addr=atofaddr(str); }

	i=matchnode(addr,0);
1399
	if(i>=cfg.nodecfgs) {
1400
1401
		sprintf(body,"Your node is not configured for Areafix, please "
			"contact your hub.\r\n");
1402
		create_netmail(NULL,"Areafix Request",body,addr,FALSE);
1403
		sprintf(body,"An areafix request was made by node %s.\r\nThis node "
rswindell's avatar
rswindell committed
1404
1405
			"is not currently configured for areafix.\r\n"
			,faddrtoa(&addr,NULL));
1406
1407
1408
		return(body); }

	if(stricmp(cfg.nodecfg[i].password,password)) {
1409
		create_netmail(NULL,"Areafix Request","Invalid Password.",addr,FALSE);
1410
1411
		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
1412
			"for this node is %s.\r\n",faddrtoa(&addr,NULL),password
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
			,(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 */