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

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

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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
59
60
61
#ifndef __FreeBSD__
	#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
79
80
81
82
#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;

faddr_t		sys_faddr;
config_t	cfg;
scfg_t		scfg;
83
char		revision[16];
84

85
BOOL pause_on_exit=FALSE;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

#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

120
#if defined(__unix__)	/* borrowed from MSVC */
rswindell's avatar
rswindell committed
121
unsigned _rotr (
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/******************************************************************************
 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);
173
	chcount=vsnprintf(sbuf,sizeof(sbuf),fmat,argptr);
174
	sbuf[sizeof(sbuf)-1]=0;
rswindell's avatar
rswindell committed
175
176
177
178
179
	va_end(argptr);
	lputs(sbuf);
	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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
}

/*****************************************************************************/
/* 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
224
225
226
                if(cfg->temp_dir[0]!='\\' 
					&& cfg->temp_dir[0]!='/' 
					&& cfg->temp_dir[1]!=':') {
227
228
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->temp_dir);
229
                    if(FULLPATH(str2,str,40))
230
231
232
233
234
235
236
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str);}
                else
                    strcat(cmd,cfg->temp_dir);
                break;
            case 'J':
rswindell's avatar
rswindell committed
237
238
239
                if(cfg->data_dir[0]!='\\' 
					&& cfg->data_dir[0]!='/' 
					&& cfg->data_dir[1]!=':') {
240
241
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->data_dir);
242
                    if(FULLPATH(str2,str,40))
243
244
245
246
247
248
249
                        strcpy(str,str2);
                    backslash(str);
                    strcat(cmd,str); }
                else
                    strcat(cmd,cfg->data_dir);
                break;
            case 'K':
rswindell's avatar
rswindell committed
250
251
252
                if(cfg->ctrl_dir[0]!='\\' 
					&& cfg->ctrl_dir[0]!='/' 
					&& cfg->ctrl_dir[1]!=':') {
253
254
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->ctrl_dir);
255
                    if(FULLPATH(str2,str,40))
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
                        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
275
276
277
                if(cfg->exec_dir[0]!='\\' 
					&& cfg->exec_dir[0]!='/' 
					&& cfg->exec_dir[1]!=':') {
278
279
                    strcpy(str,cfg->node_dir);
                    strcat(str,cfg->exec_dir);
280
                    if(FULLPATH(str2,str,40))
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
308
309
310
311
312
313
314
315
316
                        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
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
}
/******************************************************************************
 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)
{
363
364
365
366
367
	char fname[MAX_PATH+1];
	char outbound[MAX_PATH+1];
	char str[MAX_PATH+1];
	char ch;
	char searchstr[MAX_PATH+1];
368
369
370
371
372
373
374
375
	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
376
377
378
379
	if(attr&ATTR_CRASH) ch='c';
	else if(attr&ATTR_HOLD) ch='h';
	else if(attr&ATTR_DIRECT) ch='d';
	else ch='f';
380
381
382
	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
383
		sprintf(outbound,"%.*s.%03x/"
rswindell's avatar
rswindell committed
384
			,(int)strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
385
	if(dest.point) {					/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
386
		sprintf(str,"%04x%04x.pnt"
387
388
			,dest.net,dest.node);
		strcat(outbound,str); }
rswindell's avatar
rswindell committed
389
390
	if(outbound[strlen(outbound)-1]=='\\'
		|| outbound[strlen(outbound)-1]=='/')
391
		outbound[strlen(outbound)-1]=0;
rswindell's avatar
rswindell committed
392
	MKDIR(outbound);
393
	backslash(outbound);
394
	if(dest.point)
rswindell's avatar
rswindell committed
395
		sprintf(fname,"%s%08x.%clo",outbound,dest.point,ch);
396
	else
rswindell's avatar
rswindell committed
397
		sprintf(fname,"%s%04x%04x.%clo",outbound,dest.net,dest.node,ch);
398
399
400
	sprintf(searchstr,"^%s",attachment);
	if(findstr(searchstr,fname))	/* file already in FLO file */
		return(0);
401
	if((stream=fnopen(&file,fname,O_WRONLY|O_APPEND|O_CREAT))==NULL) {
402
403
		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]);
404
405
		return(-1); 
	}
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
	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;

429
430
431
432
433
434
435
436
437
438
439
	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++) {
440
			sprintf(fname,"%s%u.msg",scfg.netmail_dir,i);
441
442
443
444
445
446
447
448
449
450
451
			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;
		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); }
452

453
454
455
456
457
458
459
460
461
462
463
464
465
466
		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;
467

468
469
470
471
		if(attr&ATTR_HOLD)
			hdr.attr|=FIDO_HOLD;
		if(attr&ATTR_CRASH)
			hdr.attr|=FIDO_CRASH;
472

473
474
475
476
477
478
479
480
481
482
		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);
483
		else
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
			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);
		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) {
			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); }
		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);
512
513
514
515
516
517
518
519
520
521
522
}

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

523
524
525
526
527
528
529
530
531
532
533
534
	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) {
535
			p++;
536
537
			if(*p) {
				*p=0;
538
				p++;
539
540
541
542
543
544
545
546
				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);
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
}
/******************************************************************************
 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
******************************************************************************/
char *tempname(char *dir, char *prefix)
{
	char str[256],*p;
	int i;

697
698
699
700
701
702
703
704
705
706
707
708
709
	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);
710
711
}

rswindell's avatar
rswindell committed
712
int check_elists(char *areatag,faddr_t addr)
713
714
{
	FILE *stream;
rswindell's avatar
rswindell committed
715
716
	char str[1025],quit=0,*p;
	int i,j,k,x,file,match=0;
717

rswindell's avatar
rswindell committed
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
	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);
752
						quit=1;
rswindell's avatar
rswindell committed
753
754
755
756
						if(match)
							return(match);
						break; } } } }
	return(match);
757
758
759
760
761
762
763
764
}
/******************************************************************************
 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]
765
		,drive[3],dir[MAX_PATH+1],name[MAX_PATH+1],ext[MAX_PATH+1],outpath[128]
766
767
768
769
		,*outname,*p,*tp,nomatch=0,match=0;
	int i,j,k,x,y,file;
	ulong tagcrc;

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
	_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;
808
		while(*tp && *tp>SP) tp++;
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
		*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; }
841

842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
						/* 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 "
								,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);
						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);
889
							break; }
890
891
						if(misc&ELIST_ONLY && !check_elists(field2,addr)) {
							fprintf(afileout,"%s",fields);
892
							break; }
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
941
942
943
944
945
946
947
948
949

						/* 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))
950
									break;
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
								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);
									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))
		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);
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
}
/******************************************************************************
 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
1022
		,drive[3],dir[MAX_PATH+1],name[MAX_PATH+1],ext[MAX_PATH+1],match=0;
1023
1024
1025
	int i,j,k,file;
	faddr_t taddr;

rswindell's avatar
rswindell committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
	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; }
1046

rswindell's avatar
rswindell committed
1047
1048
1049
1050
1051
1052
1053
1054
	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);
1055
			continue; }
rswindell's avatar
rswindell committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
		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;
1077
				continue; }
rswindell's avatar
rswindell committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
			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)
1093
				continue;
rswindell's avatar
rswindell committed
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
			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);
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
}
/******************************************************************************
 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);
1151
	if(node>=cfg.nodecfgs)
1152
1153
1154
1155
1156
		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
1157
		sprintf(str,"%sAREAMGR.HLP",scfg.exec_dir);
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
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
		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;
1223
		if(node>=cfg.nodecfgs)		   /* Should never happen */
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
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
			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
1338
1339
		logprintf("Remote maintenance for %s requested via %s",str
			,faddrtoa(&addr,NULL));
1340
1341
1342
		addr=atofaddr(str); }

	i=matchnode(addr,0);
1343
	if(i>=cfg.nodecfgs) {
1344
1345
1346
1347
		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
1348
1349
			"is not currently configured for areafix.\r\n"
			,faddrtoa(&addr,NULL));
1350
1351
1352
1353
1354
1355
		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
1356
			"for this node is %s.\r\n",faddrtoa(&addr,NULL),password
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
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
			,(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
1424
			"body or with no valid commands.\r\n",faddrtoa(&addr,NULL));
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
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
		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)); }
}

1506
1507
1508
1509
1510
1511
1512
enum {
	 ATTACHMENT_ADD
	,ATTACHMENT_NETMAIL
	,ATTACHMENT_CHECK
};

int attachment(char *bundlename,faddr_t dest, int mode)
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
{
	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;

1523
	if(bundlename==NULL && mode!=ATTACHMENT_NETMAIL) {
rswindell's avatar
rswindell committed
1524
1525
1526
		logprintf("ERROR line %d NULL bundlename",__LINE__);
		return(1);
	}
1527
1528
1529
1530
	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]);
1531
1532
		return(1); 
	}
1533

1534
	if(mode==ATTACHMENT_CHECK) {				/* Check for existance in BUNDLES.SBE */
1535
1536
1537
1538
1539
		while(!feof(stream)) {
			if(!fread(&attach,1,sizeof(attach_t),stream))
				break;
			if(!stricmp(attach.fname,bundlename)) {
				fclose(stream);
1540
1541
1542
				return(1); 
			} 
		}
1543
		fclose(stream);
1544
1545
		return(0); 
	}
1546

1547
	if(mode=ATTACHMENT_NETMAIL) {				/* Create netmail attaches */
1548

1549
1550
1551
1552
1553
1554
1555
		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
1556
		for(f=0;f<g.gl_pathc;f++) {
1557

rswindell's avatar
rswindell committed
1558
			path=g.gl_pathv[f];
1559

rswindell's avatar
rswindell committed
1560
1561
1562
1563
			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]);
1564
1565
				continue; 
			}
rswindell's avatar
rswindell committed
1566
			if(filelength(fmsg)<sizeof(fmsghdr_t)) {
rswindell's avatar
rswindell committed
1567
1568
1569
1570
1571
1572
				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
1573
1574
					,filelength(fmsg));
				fclose(fidomsg);
1575
1576
				continue; 
			}
rswindell's avatar
rswindell committed
1577
1578
1579
1580
1581
1582
			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);
1583
1584
				continue; 
			}
1585
			fclose(fidomsg);
rswindell's avatar
rswindell committed
1586
			if(!(hdr.attr&FIDO_FILE))		/* Not a file attach */
1587
				continue;
rswindell's avatar
rswindell committed
1588
			num_mfncrc++;
rswindell's avatar
rswindell committed
1589
1590
1591
1592
			p=strrchr(hdr.subj,'/');
			if(p==NULL)
				p=strrchr(hdr.subj,'\\');
			if(p!=NULL)
rswindell's avatar
rswindell committed
1593
1594
1595
1596
1597
1598
1599
1600
				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));
1601
1602
				continue; 
			}
rswindell's avatar
rswindell committed
1603
			mfncrc[num_mfncrc-1]=crc32(strupr(p),0); 
1604
1605
		}
		globfree(&g);
rswindell's avatar
rswindell committed
1606

1607
1608
1609
1610
1611
1612
		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
1613
			fncrc=crc32(strupr(attach.fname),0);
1614
1615
			for(crcidx=0;crcidx<num_mfncrc;crcidx++)
				if(mfncrc[crcidx]==fncrc)
1616
					break;
1617
1618
1619
			if(crcidx==num_mfncrc)
				if(create_netmail(0,str,"\1FLAGS KFS\r",attach.dest,1))
					error=1; 
rswindell's avatar
rswindell committed
1620
		}
1621
		fclose(stream);
1622
1623
		if(!error)			/* remove bundles.sbe if no error occurred */		
			remove(fname);	/* used to truncate here, August-20-2002 */
1624
1625
1626
		if(num_mfncrc)
			FREE(mfncrc);
		return(0); 
1627
1628
1629
1630
1631
1632
1633
	}

	while(!feof(stream)) {
		if(!fread(&attach,1,sizeof(attach_t),stream))
			break;
		if(!stricmp(attach.fname,bundlename)) {
			fclose(stream);
1634
1635
1636
			return(0); 
		} 
	}
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657

	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;

1658
1659
1660
1661
1662
	if(infile==NULL || infile[0]==0) {
		logprintf("ERROR line %d invalid filename",__LINE__);
		bail(1);
	}

1663
1664
	node=matchnode(dest,0);
	strcpy(str,infile);
1665
	str[strlen(str)-1]='t';
1666
1667
	if(rename(infile,str))				   /* Change .PK_ file to .PKT file */
		logprintf("ERROR line %d renaming %s to %s",__LINE__,infile,str);
1668
	infile[strlen(infile)-1]='t';
1669
1670
1671
1672
1673
1674
1675
1676
1677
	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
1678
			sprintf(outbound,"%.*s.%03x/"
rswindell's avatar
rswindell committed
1679
				,(int)strlen(cfg.outbound)-1,cfg.outbound,dest.zone);
1680
		if(dest.point) {				/* Point destination is OUTBOUND\*.PNT */
rswindell's avatar
rswindell committed
1681
			sprintf(str,"%04x%04x.pnt"
1682
1683
1684
1685
1686
				,dest.net,dest.node);
			strcat(outbound,str); }
		}
	else
		strcpy(outbound,cfg.outbound);
rswindell's avatar
rswindell committed
1687
1688
	if(outbound[strlen(outbound)-1]=='\\'
		|| outbound[strlen(outbound)-1]=='/')
1689
		outbound[strlen(outbound)-1]=0;
rswindell's avatar
rswindell committed
1690
	MKDIR(outbound);
1691
	backslash(outbound);
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702

	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
1703
1704
1705
1706
		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);
1707
1708
1709
1710