pack_qwk.cpp 18.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* pack_qwk.cpp */

/* Synchronet pack QWK packet routine */

/* $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.	*
 ****************************************************************************/

#include "sbbs.h"
#include "qwk.h"
#include "post.h"

/****************************************************************************/
/* Creates QWK packet, returning 1 if successful, 0 if not. 				*/
/****************************************************************************/
bool sbbs_t::pack_qwk(char *packet, ulong *msgcnt, bool prepack)
{
	char	str[256],tmp2[256],ch,*p;
48
	char 	tmp[512];
49
	char*	fname;
50
51
52
	int 	file,mode;
	uint	i,j,k,conf;
	long	l,size,msgndx,posts,ex;
53
	ulong	mailmsgs=0;
54
55
	ulong	totalcdt,totaltime,lastmsg
			,files,submsgs,msgs,netfiles=0,preqwk=0;
56
	ulong	subs_scanned=0;
57
58
59
60
61
	float	f;	/* Sparky is responsible */
	time_t	start;
	node_t	node;
	mail_t	*mail;
	post_t	HUGE16 *post;
62
	glob_t	g;
63
	FILE	*stream,*qwk,*personal,*ndx;
64
65
	DIR*	dir;
	DIRENT*	dirent;
66
67
68
	struct	tm* tm;
	smbmsg_t msg;

69

70
	ex=EX_OUTL|EX_OUTR;	/* Need sh for wildcard expansion */
71
72
73
	if(prepack)
		ex|=EX_OFFLINE;

74
	delfiles(cfg.temp_dir,ALLFILES);
75
	sprintf(str,"%sfile/%04u.qwk",cfg.data_dir,useron.number);
76
77
78
79
80
81
82
	if(fexist(str)) {
		for(k=0;k<cfg.total_fextrs;k++)
			if(!stricmp(cfg.fextr[k]->ext,useron.tmpext)
				&& chk_ar(cfg.fextr[k]->ar,&useron))
				break;
		if(k>=cfg.total_fextrs)
			k=0;
83
		i=external(cmdstr(cfg.fextr[k]->cmd,str,ALLFILES,NULL),ex);
84
		if(!i)
85
86
			preqwk=1; 
	}
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

	if(useron.rest&FLAG('Q') && useron.qwk&QWK_RETCTLA)
		useron.qwk|=(QWK_NOINDEX|QWK_NOCTRL|QWK_VIA|QWK_TZ);

	if(useron.qwk&QWK_EXPCTLA)
		mode=A_EXPAND;
	else if(useron.qwk&QWK_RETCTLA)
		mode=A_LEAVE;
	else mode=0;
	if(useron.qwk&QWK_TZ)
		mode|=TZ;
	if(useron.qwk&QWK_VIA)
		mode|=VIA;
	(*msgcnt)=0L;
	if(/* !prepack && */ !(useron.qwk&QWK_NOCTRL)) {
		/***************************/
		/* Create CONTROL.DAT file */
		/***************************/
		sprintf(str,"%sCONTROL.DAT",cfg.temp_dir);
		if((stream=fnopen(&file,str,O_WRONLY|O_CREAT|O_TRUNC))==NULL) {
			errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_CREAT|O_TRUNC);
			return(false); }

		now=time(NULL);
		tm=gmtime(&now);
		if(tm==NULL)
			return(false);

		fprintf(stream,"%s\r\n%s\r\n%s\r\n%s, Sysop\r\n0000,%s\r\n"
			"%02u-%02u-%u,%02u:%02u:%02u\r\n"
			,cfg.sys_name
			,cfg.sys_location
			,cfg.node_phone
			,cfg.sys_op
			,cfg.sys_id
			,tm->tm_mon+1,tm->tm_mday,tm->tm_year+1900
			,tm->tm_hour,tm->tm_min,tm->tm_sec);
		k=0;
		for(i=0;i<usrgrps;i++)
			for(j=0;j<usrsubs[i];j++)
				k++;	/* k is how many subs */
		fprintf(stream,"%s\r\n\r\n0\r\n0\r\n%u\r\n",useron.alias,k);
		fprintf(stream,"0\r\nE-mail\r\n");   /* first conference is e-mail */
		for(i=0;i<usrgrps;i++)
			for(j=0;j<usrsubs[i];j++)
				fprintf(stream,"%u\r\n%s\r\n"
					,cfg.sub[usrsub[i][j]]->qwkconf ? cfg.sub[usrsub[i][j]]->qwkconf
					: ((i+1)*1000)+j+1,cfg.sub[usrsub[i][j]]->qwkname);
		fprintf(stream,"HELLO\r\nBBSNEWS\r\nGOODBYE\r\n");
		fclose(stream);
		/***********************/
		/* Create DOOR.ID File */
		/***********************/
		sprintf(str,"%sDOOR.ID",cfg.temp_dir);
		if((stream=fnopen(&file,str,O_WRONLY|O_CREAT|O_TRUNC))==NULL) {
			errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_CREAT|O_TRUNC);
			return(false); }
		p="CONTROLTYPE = ";
145
		fprintf(stream,"DOOR = %.10s\r\nVERSION = %s%c\r\n"
146
			"SYSTEM = %s\r\n"
147
148
149
150
151
152
153
154
155
			"CONTROLNAME = SBBS\r\n"
			"%sADD\r\n"
			"%sDROP\r\n"
			"%sYOURS\r\n"
			"%sRESET\r\n"
			"%sRESETALL\r\n"
			"%sFILES\r\n"
			"%sATTACH\r\n"
			"%sOWN\r\n"
156
			"%smail\r\n"
157
158
159
160
161
162
163
164
			"%sDELMAIL\r\n"
			"%sCTRL-A\r\n"
			"%sFREQ\r\n"
			"%sNDX\r\n"
			"%sTZ\r\n"
			"%sVIA\r\n"
			"%sCONTROL\r\n"
			"MIXEDCASE = YES\r\n"
165
			,VERSION_NOTICE
166
			,VERSION,REVISION
167
			,VERSION_NOTICE
168
169
170
171
172
173
174
175
176
177
178
			,p,p,p,p
			,p,p,p,p
			,p,p,p,p
			,p,p,p,p
			);
		fclose(stream);
		if(useron.rest&FLAG('Q')) {
			/***********************/
			/* Create NETFLAGS.DAT */
			/***********************/
			sprintf(str,"%sNETFLAGS.DAT",cfg.temp_dir);
179
180
181
182
			if((stream=fopen(str,"w"))==NULL) {
				errormsg(WHERE,ERR_CREATE,str,0);
				return(false); 
			}
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
			ch=1;						/* Net enabled */
			if(usrgrps)
				for(i=0;i<(usrgrps*1000)+usrsubs[usrgrps-1];i++)
					fputc(ch,stream);
			fclose(stream); }
		} /* !prepack */

	/****************************************************/
	/* Create MESSAGES.DAT, write header and leave open */
	/****************************************************/
	sprintf(str,"%sMESSAGES.DAT",cfg.temp_dir);
	if((qwk=fnopen(&file,str,O_CREAT|O_WRONLY|O_TRUNC))==NULL) {
		errormsg(WHERE,ERR_OPEN,str,O_CREAT|O_WRONLY|O_TRUNC);
		return(false); }
	l=filelength(file);
198
	if(l<1) {
199
		fprintf(qwk,"%-128.128s","Produced by " VERSION_NOTICE "  " COPYRIGHT_NOTICE);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
		msgndx=1; }
	else
		msgndx=l/128L;
	fseek(qwk,0,SEEK_END);
	sprintf(str,"%sNEWFILES.DAT",cfg.temp_dir);
	remove(str);
	if(!(useron.rest&FLAG('T')) && useron.qwk&QWK_FILES)
		files=create_filelist("NEWFILES.DAT",FL_ULTIME);
	else
		files=0;

	start=time(NULL);

	if(useron.rest&FLAG('Q'))
		useron.qwk|=(QWK_EMAIL|QWK_ALLMAIL|QWK_DELMAIL);

	if(!(useron.qwk&QWK_NOINDEX)) {
		sprintf(str,"%sPERSONAL.NDX",cfg.temp_dir);
		if((personal=fnopen(&file,str,O_CREAT|O_WRONLY|O_APPEND))==NULL) {
			fclose(qwk);
			errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_CREAT|O_APPEND);
			return(false); }
		fseek(personal,0L,SEEK_END); }
	else
		personal=NULL;

	if(useron.qwk&(QWK_EMAIL|QWK_ALLMAIL) /* && !prepack */) {
227
		sprintf(smb.file,"%smail",cfg.data_dir);
228
		smb.retry_time=cfg.smb_retry_time;
229
		smb.subnum=INVALID_SUB;
230
231
232
233
234
235
236
237
238
239
240
		if((i=smb_open(&smb))!=0) {
			fclose(qwk);
			if(personal)
				fclose(personal);
			errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
			return(false); }

		/***********************/
		/* Pack E-mail, if any */
		/***********************/
		qwkmail_time=time(NULL);
241
		mail=loadmail(&smb,&mailmsgs,useron.number,0,useron.qwk&QWK_ALLMAIL ? 0
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
			: LM_UNREAD);
		if(mailmsgs && !(sys_status&SS_ABORT)) {
			bputs(text[QWKPackingEmail]);
			if(!(useron.qwk&QWK_NOINDEX)) {
				sprintf(str,"%s000.NDX",cfg.temp_dir);
				if((ndx=fnopen(&file,str,O_CREAT|O_WRONLY|O_APPEND))==NULL) {
					fclose(qwk);
					if(personal)
						fclose(personal);
					smb_close(&smb);
					errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_CREAT|O_APPEND);
					FREE(mail);
					return(false); }
				fseek(ndx,0L,SEEK_END); }
			else
				ndx=NULL;

			if(useron.rest&FLAG('Q'))
				mode|=TO_QNET;
			else
				mode&=~TO_QNET;

264
			for(l=0;(ulong)l<mailmsgs;l++) {
265
266
267
268
269
270
271
272
				bprintf("\b\b\b\b\b\b\b\b\b\b\b\b%4lu of %-4lu"
					,l+1,mailmsgs);

				msg.idx.offset=mail[l].offset;
				if(!loadmsg(&msg,mail[l].number))
					continue;

				if(msg.hdr.auxattr&MSG_FILEATTACH && useron.qwk&QWK_ATTACH) {
273
					sprintf(str,"%sfile/%04u.in/%s"
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
						,cfg.data_dir,useron.number,msg.subj);
					sprintf(tmp,"%s%s",cfg.temp_dir,msg.subj);
					if(fexist(str) && !fexist(tmp))
						mv(str,tmp,1); }

				size=msgtoqwk(&msg,qwk,mode,INVALID_SUB,0);
				smb_unlockmsghdr(&smb,&msg);
				smb_freemsgmem(&msg);
				if(ndx) {
					msgndx++;
					f=ltomsbin(msgndx); 	/* Record number */
					ch=0;					/* Sub number, not used */
					if(personal) {
						fwrite(&f,4,1,personal);
						fwrite(&ch,1,1,personal); }
					fwrite(&f,4,1,ndx);
					fwrite(&ch,1,1,ndx);
291
292
293
294
					msgndx+=size/128L; 
				} 
				mswait(1);	/* yield */
			}
295
296
297
298
299
300
301
302
303
304
305
306
			bprintf(text[QWKPackedEmail],mailmsgs);
			if(ndx)
				fclose(ndx); }
		smb_close(&smb);					/* Close the e-mail */
		if(mailmsgs)
			FREE(mail);
		}

	/*********************/
	/* Pack new messages */
	/*********************/
	for(i=0;i<usrgrps;i++) {
307
		for(j=0;j<usrsubs[i] && !msgabort();j++) {
308
			if(subscan[usrsub[i][j]].cfg&SUB_CFG_NSCAN
309
310
311
312
313
314
315
316
				|| (!(useron.rest&FLAG('Q'))
				&& cfg.sub[usrsub[i][j]]->misc&SUB_FORCED)) {
				if(!chk_ar(cfg.sub[usrsub[i][j]]->read_ar,&useron))
					continue;
				lncntr=0;						/* defeat pause */
				if(useron.rest&FLAG('Q') && !(cfg.sub[usrsub[i][j]]->misc&SUB_QNET))
					continue;	/* QWK Net Node and not QWK networked, so skip */

317
				subs_scanned++;
318
				msgs=getlastmsg(usrsub[i][j],&lastmsg,0);
319
320
				if(!msgs || lastmsg<=subscan[usrsub[i][j]].ptr) { /* no msgs */
					if(subscan[usrsub[i][j]].ptr>lastmsg)	{ /* corrupted ptr */
321
						outchar('*');
322
323
						subscan[usrsub[i][j]].ptr=lastmsg; /* so fix automatically */
						subscan[usrsub[i][j]].last=lastmsg; }
324
325
326
327
328
329
330
331
					bprintf(text[NScanStatusFmt]
						,cfg.grp[cfg.sub[usrsub[i][j]]->grp]->sname
						,cfg.sub[usrsub[i][j]]->lname,0L,msgs);
					continue; }

				sprintf(smb.file,"%s%s"
					,cfg.sub[usrsub[i][j]]->data_dir,cfg.sub[usrsub[i][j]]->code);
				smb.retry_time=cfg.smb_retry_time;
332
				smb.subnum=usrsub[i][j];
333
334
335
336
337
338
339
				if((k=smb_open(&smb))!=0) {
					errormsg(WHERE,ERR_OPEN,smb.file,k,smb.last_error);
					continue; }

				k=0;
				if(useron.qwk&QWK_BYSELF)
					k|=LP_BYSELF;
340
				if(!(subscan[usrsub[i][j]].cfg&SUB_CFG_YSCAN))
341
					k|=LP_OTHERS;
342
				post=loadposts(&posts,usrsub[i][j],subscan[usrsub[i][j]].ptr,k);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

				bprintf(text[NScanStatusFmt]
					,cfg.grp[cfg.sub[usrsub[i][j]]->grp]->sname
					,cfg.sub[usrsub[i][j]]->lname,posts,msgs);
				if(!posts)	{ /* no new messages */
					smb_close(&smb);
					continue; }
				bputs(text[QWKPackingSubboard]);	
				submsgs=0;
				conf=cfg.sub[usrsub[i][j]]->qwkconf;
				if(!conf)
					conf=((i+1)*1000)+j+1;

				if(!(useron.qwk&QWK_NOINDEX)) {
					sprintf(str,"%s%u.NDX",cfg.temp_dir,conf);
					if((ndx=fnopen(&file,str,O_CREAT|O_WRONLY|O_APPEND))==NULL) {
						fclose(qwk);
						if(personal)
							fclose(personal);
						smb_close(&smb);
						errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_CREAT|O_APPEND);
						LFREE(post);
						return(false); }
					fseek(ndx,0L,SEEK_END); }
				else
					ndx=NULL;

				for(l=0;l<posts && !msgabort();l++) {
					bprintf("\b\b\b\b\b%-5lu",l+1);

373
374
					subscan[usrsub[i][j]].ptr=post[l].number;	/* set ptr */
					subscan[usrsub[i][j]].last=post[l].number; /* set last read */
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

					msg.idx.offset=post[l].offset;
					if(!loadmsg(&msg,post[l].number))
						continue;

					if(useron.rest&FLAG('Q')) {
						if(msg.from_net.type && msg.from_net.type!=NET_QWK &&
							!(cfg.sub[usrsub[i][j]]->misc&SUB_GATE)) { /* From other */
							smb_freemsgmem(&msg);			 /* net, don't gate */
							smb_unlockmsghdr(&smb,&msg);
							continue; }
						mode|=(TO_QNET|TAGLINE);
						if(msg.from_net.type==NET_QWK) {
							mode&=~TAGLINE;
							if(route_circ((char *)msg.from_net.addr,useron.alias)
								|| !strnicmp(msg.subj,"NE:",3)) {
								smb_freemsgmem(&msg);
								smb_unlockmsghdr(&smb,&msg);
								continue; } } }
					else
						mode&=~(TAGLINE|TO_QNET);

					size=msgtoqwk(&msg,qwk,mode,usrsub[i][j],conf);
					smb_unlockmsghdr(&smb,&msg);

					if(ndx) {
						msgndx++;
						f=ltomsbin(msgndx); 	/* Record number */
						ch=0;					/* Sub number, not used */
						if(personal
							&& (!stricmp(msg.to,useron.alias)
								|| !stricmp(msg.to,useron.name))) {
							fwrite(&f,4,1,personal);
							fwrite(&ch,1,1,personal); }
						fwrite(&f,4,1,ndx);
						fwrite(&ch,1,1,ndx);
						msgndx+=size/128L; }

					smb_freemsgmem(&msg);
					(*msgcnt)++;
					submsgs++;
					if(cfg.max_qwkmsgs
417
						&& !(useron.exempt&FLAG('O')) && (*msgcnt)>=cfg.max_qwkmsgs) {
418
						bputs(text[QWKmsgLimitReached]);
419
420
						break; 
					} 
421
422
					if(!(l%50))
						mswait(1);	/* yield */
423
				}
424
425
426
427
428
429
				if(!(sys_status&SS_ABORT))
					bprintf(text[QWKPackedSubboard],submsgs,(*msgcnt));
				if(ndx) {
					fclose(ndx);
					sprintf(str,"%s%u.NDX",cfg.temp_dir,conf);
					if(!flength(str))
430
431
						remove(str); 
				}
432
433
434
				smb_close(&smb);
				LFREE(post);
				if(l<posts)
435
436
437
438
					break; 
				mswait(1);	/* yield */
			}
		}
439
		if(j<usrsubs[i]) /* if sub aborted, abort all */
440
441
442
443
			break; 
	}

	if(online==ON_LOCAL) /* event */
444
445
		eprintf("%s scanned %lu sub-boards for new messages"
			,useron.alias,subs_scanned);
446
	else
447
448
		lprintf("Node %d %s scanned %lu sub-boards for new messages"
			,cfg.node_num,useron.alias,subs_scanned);
449
450

	if((*msgcnt)+mailmsgs && time(NULL)-start) {
451
		bprintf("\r\n\r\n\1n\1hPacked %lu messages (%lu bytes) in %lu seconds "
452
			"(%lu messages/second)."
453
454
455
			,(*msgcnt)+mailmsgs
			,ftell(qwk)
			,time(NULL)-start
456
			,((*msgcnt)+mailmsgs)/(time(NULL)-start));
457
458
459
460
		sprintf(str,"Packed %lu messages (%lu bytes) in %lu seconds (%lu msgs/sec)"
			,(*msgcnt)+mailmsgs
			,ftell(qwk)
			,time(NULL)-start
461
			,((*msgcnt)+mailmsgs)/(time(NULL)-start));
462
463
464
465
		if(online==ON_LOCAL) /* event */
			eprintf(str);
		else
			lprintf(str);
466
467
468
469
470
471
472
	}

	fclose(qwk);			/* close MESSAGE.DAT */
	if(personal) {
		fclose(personal);		 /* close PERSONAL.NDX */
		sprintf(str,"%sPERSONAL.NDX",cfg.temp_dir);
		if(!flength(str))
473
474
			remove(str); 
	}
475
476
477
478
479
480
	CRLF;

	if(!prepack && (sys_status&SS_ABORT || !online))
		return(false);

	if(/*!prepack && */ useron.rest&FLAG('Q')) { /* If QWK Net node, check for files */
481
		sprintf(str,"%sqnet/%s.out/",cfg.data_dir,useron.alias);
482
		dir=opendir(str);
483
		while(dir!=NULL && (dirent=readdir(dir))!=NULL) {    /* Move files into temp dir */
484
			sprintf(str,"%sqnet/%s.out/%s",cfg.data_dir,useron.alias,dirent->d_name);
485
486
487
488
			if(isdir(str))
				continue;
			sprintf(tmp2,"%s%s",cfg.temp_dir,dirent->d_name);
			lncntr=0;	/* Default pause */
489
490
491
492
			if(online==ON_LOCAL)
				eprintf("Including %s in packet",str);
			else
				lprintf("Including %s in packet",str);
493
494
495
496
			bprintf(text[RetrievingFile],str);
			if(!mv(str,tmp2,1))
				netfiles++;
		}
497
498
		if(dir!=NULL)
			closedir(dir);
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
		if(netfiles)
			CRLF; }

	if(batdn_total) {
		for(i=0,totalcdt=0;i<batdn_total;i++)
				totalcdt+=batdn_cdt[i];
		if(!(useron.exempt&FLAG('D'))
			&& totalcdt>useron.cdt+useron.freecdt) {
			bprintf(text[YouOnlyHaveNCredits]
				,ultoac(useron.cdt+useron.freecdt,tmp)); }
		else {
			for(i=0,totaltime=0;i<batdn_total;i++) {
				if(!(cfg.dir[batdn_dir[i]]->misc&DIR_TFREE) && cur_cps)
					totaltime+=batdn_size[i]/(ulong)cur_cps; }
			if(!(useron.exempt&FLAG('T')) && !SYSOP && totaltime>timeleft)
				bputs(text[NotEnoughTimeToDl]);
			else {
				for(i=0;i<batdn_total;i++) {
					lncntr=0;
					unpadfname(batdn_name[i],tmp);
					sprintf(tmp2,"%s%s",cfg.temp_dir,tmp);
					if(!fexist(tmp2)) {
						seqwait(cfg.dir[batdn_dir[i]]->seqdev);
						bprintf(text[RetrievingFile],tmp);
						sprintf(str,"%s%s"
							,batdn_alt[i]>0 && batdn_alt[i]<=cfg.altpaths
							? cfg.altpath[batdn_alt[i]-1]
							: cfg.dir[batdn_dir[i]]->path
							,tmp);
						mv(str,tmp2,1); /* copy the file to temp dir */
						getnodedat(cfg.node_num,&thisnode,1);
						thisnode.aux=0xfe;
						putnodedat(cfg.node_num,&thisnode);
						CRLF; } } } } }

	if(!(*msgcnt) && !mailmsgs && !files && !netfiles && !batdn_total
		&& (prepack || !preqwk)) {
		bputs(text[QWKNoNewMessages]);
		return(false); }

	if(!prepack && !(useron.rest&FLAG('Q'))) {      /* Don't include in network */
		/***********************/					/* packets */
		/* Copy QWK Text files */
		/***********************/
		sprintf(str,"%sQWK/HELLO",cfg.text_dir);
		if(fexist(str)) {
			sprintf(tmp2,"%sHELLO",cfg.temp_dir);
			mv(str,tmp2,1); }
		sprintf(str,"%sQWK/BBSNEWS",cfg.text_dir);
		if(fexist(str)) {
			sprintf(tmp2,"%sBBSNEWS",cfg.temp_dir);
			mv(str,tmp2,1); }
		sprintf(str,"%sQWK/GOODBYE",cfg.text_dir);
		if(fexist(str)) {
			sprintf(tmp2,"%sGOODBYE",cfg.temp_dir);
			mv(str,tmp2,1); }
555
		sprintf(str,"%sQWK/BLT-*",cfg.text_dir);
556
557
558
559
		glob(str,0,NULL,&g);
		for(i=0;i<g.gl_pathc;i++) { 			/* Copy BLT-*.* files */
			fname=getfname(g.gl_pathv[i]);
			padfname(fname,str);
560
			if(isdigit(str[4]) && isdigit(str[9])) {
561
562
563
564
565
566
567
				sprintf(str,"%sQWK/%s",cfg.text_dir,fname);
				sprintf(tmp2,"%s%s",cfg.temp_dir,fname);
				mv(str,tmp2,1); 
			}
		}
		globfree(&g);
	}
568
569
570
571
572
573
574
575
576
577
578
579
580

	if(prepack) {
		for(i=1;i<=cfg.sys_nodes;i++) {
			getnodedat(i,&node,0);
			if((node.status==NODE_INUSE || node.status==NODE_QUIET
				|| node.status==NODE_LOGON) && node.useron==useron.number)
				break; }
		if(i<=cfg.sys_nodes)	/* Don't pre-pack with user online */
			return(false); }

	/*******************/
	/* Compress Packet */
	/*******************/
581
	sprintf(tmp2,"%s%s",cfg.temp_dir,ALLFILES);
582
583
	i=external(cmdstr(temp_cmd(),packet,tmp2,NULL)
		,ex|EX_SH);	/* Needs sh for wildcard expansion */
584
585
586
587
588
589
590
591
592
593
594
595
	if(!fexist(packet)) {
		bputs(text[QWKCompressionFailed]);
		if(i)
			errormsg(WHERE,ERR_EXEC,cmdstr(temp_cmd(),packet,tmp2,NULL),i);
		else
			errorlog("Couldn't compress QWK packet");
		return(false); }

	if(prepack) 		/* Early return if pre-packing */
		return(true);

	l=flength(packet);
596
	sprintf(str,"%s.qwk",cfg.sys_id);
597
598
599
600
601
602
603
604
605
606
	bprintf(text[FiFilename],str);
	bprintf(text[FiFileSize],ultoac(l,tmp));
	if(l>0L && cur_cps)
		i=l/(ulong)cur_cps;
	else
		i=0;
	bprintf(text[FiTransferTime],sectostr(i,tmp));
	CRLF;
	if(!(useron.exempt&FLAG('T')) && i>timeleft) {
		bputs(text[NotEnoughTimeToDl]);
607
608
		return(false); 
	}
609
610

	if(useron.rest&FLAG('Q')) {
611
		sprintf(str,"%s.qwk",cfg.sys_id);
612
		dir=opendir(cfg.temp_dir);
613
		while(dir!=NULL && (dirent=readdir(dir))!=NULL) {
614
615
616
617
618
619
			if(!stricmp(str,dirent->d_name))	/* QWK packet */
				continue;
			sprintf(tmp,"%s%s",cfg.temp_dir,dirent->d_name);
			if(!isdir(tmp))
				remove(tmp); 
		}
620
621
		if(dir!=NULL)
			closedir(dir);
622
	}
623
624
625

	return(true);
}