listfile.cpp 38.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* listfile.cpp */

/* Synchronet file database listing functions */

/* $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 2008 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 *																			*
 * 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"

#define BF_MAX	26	/* Batch Flag max: A-Z */	

int extdesclines(char *str);

/*****************************************************************************/
/* List files in directory 'dir' that match 'filespec'. Filespec must be 	 */
/* padded. ex: FILE*   .EXT, not FILE*.EXT. 'mode' determines other critiria */
/* the files must meet before they'll be listed. 'mode' bit FL_NOHDR doesn't */
/* list the directory header.                                                */
/* Returns -1 if the listing was aborted, otherwise total files listed		 */
/*****************************************************************************/
int sbbs_t::listfiles(uint dirnum, char *filespec, int tofile, long mode)
{
	char	str[256],hdr[256],c,d,letter='A',*p,*datbuf,ext[513];
54
	char 	tmp[512];
55
56
57
58
	uchar*	ixbbuf;
	uchar	flagprompt=0;
	uint	i,j;
	int		file,found=0,lastbat=0,disp;
deuce's avatar
64-bit    
deuce committed
59
60
	long	m=0,n,anchor=0,next,datbuflen;
	int32_t	l;
61
62
63
64
	file_t	f,bf[26];	/* bf is batch flagged files */

	if(mode&FL_ULTIME) {
		last_ns_time=now;
65
		sprintf(str,"%s%s.dab",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
66
67
68
69
70
		if((file=nopen(str,O_RDONLY))!=-1) {
			read(file,&l,4);
			close(file);
			if(ns_time>(time_t)l)
				return(0); } }
71
	sprintf(str,"%s%s.ixb",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
72
73
74
75
76
77
	if((file=nopen(str,O_RDONLY))==-1)
		return(0);
	l=filelength(file);
	if(!l) {
		close(file);
		return(0); }
deuce's avatar
deuce committed
78
	if((ixbbuf=(uchar *)malloc(l))==NULL) {
79
80
81
82
83
84
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,l);
		return(0); }
	if(lread(file,ixbbuf,l)!=l) {
		close(file);
		errormsg(WHERE,ERR_READ,str,l);
deuce's avatar
deuce committed
85
		free((char *)ixbbuf);
86
87
		return(0); }
	close(file);
88
	sprintf(str,"%s%s.dat",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
89
90
	if((file=nopen(str,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
deuce's avatar
deuce committed
91
		free((char *)ixbbuf);
92
93
		return(0); }
	datbuflen=filelength(file);
deuce's avatar
deuce committed
94
	if((datbuf=(char *)malloc(datbuflen))==NULL) {
95
96
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,datbuflen);
deuce's avatar
deuce committed
97
		free((char *)ixbbuf);
98
99
100
101
		return(0); }
	if(lread(file,datbuf,datbuflen)!=datbuflen) {
		close(file);
		errormsg(WHERE,ERR_READ,str,datbuflen);
deuce's avatar
deuce committed
102
103
		free((char *)datbuf);
		free((char *)ixbbuf);
104
105
106
107
108
109
		return(0); }
	close(file);
	if(!tofile) {
		action=NODE_LFIL;
		getnodedat(cfg.node_num,&thisnode,0);
		if(thisnode.action!=NODE_LFIL) {	/* was a sync */
110
111
112
113
114
115
			if(getnodedat(cfg.node_num,&thisnode,true)==0) {
				thisnode.action=NODE_LFIL;
				putnodedat(cfg.node_num,&thisnode); 
			} 
		} 
	}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

	while(online && found<MAX_FILES) {
		if(found<0)
			found=0;
		if(m>=l || flagprompt) {		  /* End of list */
			if(useron.misc&BATCHFLAG && !tofile && found && found!=lastbat
				&& !(mode&(FL_EXFIND|FL_VIEW))) {
				flagprompt=0;
				lncntr=0;
				if((i=batchflagprompt(dirnum,bf,letter-'A',l/F_IXBSIZE))==2) {
					m=anchor;
					found-=letter-'A';
					letter='A'; }
				else if(i==3) {
					if((long)anchor-((letter-'A')*F_IXBSIZE)<0) {
						m=0;
						found=0; }
					else {
						m=anchor-((letter-'A')*F_IXBSIZE);
						found-=letter-'A'; }
					letter='A'; }
				else if((int)i==-1) {
deuce's avatar
deuce committed
138
139
					free((char *)ixbbuf);
					free((char *)datbuf);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
					return(-1); }
				else
					break;
				getnodedat(cfg.node_num,&thisnode,0);
				nodesync(); }
			else
				break; }

		if(letter>'Z')
			letter='A';
		if(letter=='A')
			anchor=m;

		if(msgabort()) {		 /* used to be !tofile && msgabort() */
deuce's avatar
deuce committed
154
155
			free((char *)ixbbuf);
			free((char *)datbuf);
156
157
			return(-1); }
		for(j=0;j<12 && m<l;j++)
158
159
			if(j==8)
				str[j]=ixbbuf[m]>' ' ? '.' : ' ';
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
			else
				str[j]=ixbbuf[m++];		/* Turns FILENAMEEXT into FILENAME.EXT */
		str[j]=0;
		if(!(mode&(FL_FINDDESC|FL_EXFIND)) && filespec[0]
			&& !filematch(str,filespec)) {
			m+=11;
			continue; }
		n=ixbbuf[m]|((long)ixbbuf[m+1]<<8)|((long)ixbbuf[m+2]<<16);
		if(n>=datbuflen) {	/* out of bounds */
			m+=11;
			continue; }
		if(mode&(FL_FINDDESC|FL_EXFIND)) {
			getrec((char *)&datbuf[n],F_DESC,LEN_FDESC,tmp);
			strupr(tmp);
			p=strstr(tmp,filespec);
			if(!(mode&FL_EXFIND) && p==NULL) {
				m+=11;
				continue; }
			getrec((char *)&datbuf[n],F_MISC,1,tmp);
			j=tmp[0];  /* misc bits */
180
			if(j) j-=' ';
181
			if(mode&FL_EXFIND && j&FM_EXTDESC) { /* search extended description */
182
				getextdesc(&cfg,dirnum,n,ext);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
				strupr(ext);
				if(!strstr(ext,filespec) && !p) {	/* not in description or */
					m+=11;						 /* extended description */
					continue; } }
			else if(!p) {			 /* no extended description and not in desc */
				m+=11;
				continue; } }
		if(mode&FL_ULTIME) {
			if(ns_time>(ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)|((long)ixbbuf[m+5]<<16)
				|((long)ixbbuf[m+6]<<24))) {
				m+=11;
				continue; } }
		if(useron.misc&BATCHFLAG && letter=='A' && found && !tofile
			&& !(mode&(FL_EXFIND|FL_VIEW))
			&& (!mode || !(useron.misc&EXPERT)))
			bputs(text[FileListBatchCommands]);
		m+=11;
		if(!found && !(mode&(FL_EXFIND|FL_VIEW))) {
			for(i=0;i<usrlibs;i++)
				if(usrlib[i]==cfg.dir[dirnum]->lib)
					break;
			for(j=0;j<usrdirs[i];j++)
				if(usrdir[i][j]==dirnum)
					break;						/* big header */
			if((!mode || !(useron.misc&EXPERT)) && !tofile && (!filespec[0]
				|| (strchr(filespec,'*') || strchr(filespec,'?')))) {
209
				sprintf(hdr,"%s%s.hdr",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
210
				if(fexistcase(hdr))
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
					printfile(hdr,0);	/* Use DATA\DIRS\<CODE>.HDR */
				else {
					if(useron.misc&BATCHFLAG)
						bputs(text[FileListBatchCommands]);
					else {
						CLS;
						d=strlen(cfg.lib[usrlib[i]]->lname)>strlen(cfg.dir[dirnum]->lname) ?
							strlen(cfg.lib[usrlib[i]]->lname)+17
							: strlen(cfg.dir[dirnum]->lname)+17;
						if(i>8 || j>8) d++;
						attr(cfg.color[clr_filelsthdrbox]);
						bputs("");            /* use to start with \r\n */
						for(c=0;c<d;c++)
							outchar('');
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrLib],i+1,cfg.lib[usrlib[i]]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
229
							outchar(' ');
230
231
232
233
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrDir],j+1,cfg.dir[dirnum]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
234
							outchar(' ');
235
236
237
238
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrFiles],l/F_IXBSIZE);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
239
							outchar(' ');
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
						bputs("\r\n");
						for(c=0;c<d;c++)
							outchar('');
						bputs("\r\n"); } } }
			else {					/* short header */
				if(tofile) {
					sprintf(hdr,"(%u) %s ",i+1,cfg.lib[usrlib[i]]->sname);
					write(tofile,crlf,2);
					write(tofile,hdr,strlen(hdr)); }
				else {
					sprintf(hdr,text[ShortHdrLib],i+1,cfg.lib[usrlib[i]]->sname);
					bputs("\r\1>\r\n");
					bputs(hdr); }
				c=bstrlen(hdr);
				if(tofile) {
					sprintf(hdr,"(%u) %s",j+1,cfg.dir[dirnum]->lname);
					write(tofile,hdr,strlen(hdr)); }
				else {
					sprintf(hdr,text[ShortHdrDir],j+1,cfg.dir[dirnum]->lname);
					bputs(hdr); }
				c+=bstrlen(hdr);
				if(tofile) {
					write(tofile,crlf,2);
					sprintf(hdr,"%*s",c,nulstr);
264
					memset(hdr,'',c);
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
					strcat(hdr,crlf);
					write(tofile,hdr,strlen(hdr)); }
				else {
					CRLF;
					attr(cfg.color[clr_filelstline]);
					while(c--)
						outchar('');
					CRLF; } } }
		next=m;
		disp=1;
		if(mode&(FL_EXFIND|FL_VIEW)) {
			f.dir=dirnum;
			strcpy(f.name,str);
			m-=11;
			f.datoffset=n;
			f.dateuled=ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)
				|((long)ixbbuf[m+5]<<16)|((long)ixbbuf[m+6]<<24);
			f.datedled=ixbbuf[m+7]|((long)ixbbuf[m+8]<<8)
				|((long)ixbbuf[m+9]<<16)|((long)ixbbuf[m+10]<<24);
			m+=11;
			f.size=0;
			getfiledat(&cfg,&f);
			if(!found)
				bputs("\r\1>");
			if(mode&FL_EXFIND) {
				if(!viewfile(&f,1)) {
deuce's avatar
deuce committed
291
292
					free((char *)ixbbuf);
					free((char *)datbuf);
293
294
295
					return(-1); } }
			else {
				if(!viewfile(&f,0)) {
deuce's avatar
deuce committed
296
297
					free((char *)ixbbuf);
					free((char *)datbuf);
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
					return(-1); } } }

		else if(tofile)
			listfiletofile(str,&datbuf[n],dirnum,tofile);
		else if(mode&FL_FINDDESC)
			disp=listfile(str,&datbuf[n],dirnum,filespec,letter,n);
		else
			disp=listfile(str,&datbuf[n],dirnum,nulstr,letter,n);
		if(!disp && letter>'A') {
			next=m-F_IXBSIZE;
			letter--; }
		else {
			disp=1;
			found++; }
		if(sys_status&SS_ABORT) {
deuce's avatar
deuce committed
313
314
			free((char *)ixbbuf);
			free((char *)datbuf);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
			return(-1); }
		if(mode&(FL_EXFIND|FL_VIEW))
			continue;
		if(useron.misc&BATCHFLAG && !tofile) {
			if(disp) {
				strcpy(bf[letter-'A'].name,str);
				m-=11;
				bf[letter-'A'].datoffset=n;
				bf[letter-'A'].dateuled=ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)
					|((long)ixbbuf[m+5]<<16)|((long)ixbbuf[m+6]<<24);
				bf[letter-'A'].datedled=ixbbuf[m+7]|((long)ixbbuf[m+8]<<8)
					|((long)ixbbuf[m+9]<<16)|((long)ixbbuf[m+10]<<24); }
			m+=11;
			if(flagprompt || letter=='Z' || !disp ||
				(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?')
				&& !(mode&FL_FINDDESC))
				|| (useron.misc&BATCHFLAG && !tofile && lncntr>=rows-2)
				) {
				flagprompt=0;
				lncntr=0;
				lastbat=found;
				if((int)(i=batchflagprompt(dirnum,bf,letter-'A'+1,l/F_IXBSIZE))<1) {
deuce's avatar
deuce committed
337
338
					free((char *)ixbbuf);
					free((char *)datbuf);
339
					if((int)i==-1)
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
						return(-1);
					else
						return(found); }
				if(i==2) {
					next=anchor;
					found-=(letter-'A')+1; }
				else if(i==3) {
					if((long)anchor-((letter-'A'+1)*F_IXBSIZE)<0) {
						next=0;
						found=0; }
					else {
						next=anchor-((letter-'A'+1)*F_IXBSIZE);
						found-=letter-'A'+1; } }
				getnodedat(cfg.node_num,&thisnode,0);
				nodesync();
				letter='A';	}
			else letter++; }
		if(useron.misc&BATCHFLAG && !tofile
			&& lncntr>=rows-2) {
			lncntr=0;		/* defeat pause() */
			flagprompt=1; }
		m=next;
		if(mode&FL_FINDDESC) continue;
		if(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?') && m)
			break; }

deuce's avatar
deuce committed
366
367
	free((char *)ixbbuf);
	free((char *)datbuf);
368
369
370
371
372
373
374
	return(found);
}

/****************************************************************************/
/* Prints one file's information on a single line                           */
/* Return 1 if displayed, 0 otherwise										*/
/****************************************************************************/
deuce's avatar
deuce committed
375
bool sbbs_t::listfile(char *fname, char *buf, uint dirnum
376
377
	, char *search, char letter, ulong datoffset)
{
378
	char	str[256],ext[513]="",*ptr,*cr,*lf,exist=1;
379
	char	path[MAX_PATH+1];
380
381
382
383
	char 	tmp[512];
    uchar	alt;
    int		i,j;
    ulong	cdt;
384

385
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC && useron.misc&EXTDESC) {
386
		getextdesc(&cfg,dirnum,datoffset,ext);
387
388
		if(useron.misc&BATCHFLAG && lncntr+extdesclines(ext)>=rows-2 && letter!='A')
			return(false); }
389

390
391
	attr(cfg.color[clr_filename]);
	bputs(fname);
392
393
394
395
396
397

	getrec((char *)buf,F_ALTPATH,2,str);
	alt=(uchar)ahtoul(str);
	sprintf(path,"%s%s",alt>0 && alt<=cfg.altpaths ? cfg.altpath[alt-1]:cfg.dir[dirnum]->path
		,unpadfname(fname,tmp));

398
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC) {
399
400
401
		if(!(useron.misc&EXTDESC))
			outchar('+');
		else
402
			outchar(' '); }
403
	else
404
		outchar(' ');
405
406
407
	if(useron.misc&BATCHFLAG) {
		attr(cfg.color[clr_filedesc]);
		bprintf("%c",letter); }
408
	if(cfg.dir[dirnum]->misc&DIR_FCHK && !fexistcase(path)) {
409
410
411
412
413
414
415
416
417
418
419
420
421
		exist=0;
		attr(cfg.color[clr_err]); }
	else
		attr(cfg.color[clr_filecdt]);
	getrec((char *)buf,F_CDT,LEN_FCDT,str);
	cdt=atol(str);
	if(useron.misc&BATCHFLAG) {
		if(!cdt) {
			attr(curatr^(HIGH|BLINK));
			bputs("  FREE"); }
		else {
			if(cdt<1024)    /* 1k is smallest size */
				cdt=1024;
422
423
424
425
			if(cdt>(99999*1024))
				bprintf("%5luM",cdt/(1024*1024));
			else
				bprintf("%5luk",cdt/1024L); } }
426
427
428
429
430
431
432
433
434
	else {
		if(!cdt) {  /* FREE file */
			attr(curatr^(HIGH|BLINK));
			bputs("   FREE"); }
		else if(cdt>9999999L)
			bprintf("%6luk",cdt/1024L);
		else
			bprintf("%7lu",cdt); }
	if(exist)
435
		outchar(' ');
436
437
438
439
	else
		outchar('-');
	getrec((char *)buf,F_DESC,LEN_FDESC,str);
	attr(cfg.color[clr_filedesc]);
440
441
442

#ifdef _WIN32
 
443
	if(exist && !(cfg.file_misc&FM_NO_LFN)) {
444
445
		fexistcase(path);	/* Get real (long?) filename */
		ptr=getfname(path);
446
		if(stricmp(ptr,tmp) && stricmp(ptr,str))
447
			bprintf("%.*s\r\n%21s",LEN_FDESC,ptr,"");
448
	}
449

450
#endif
451

452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
	if(!ext[0]) {
		if(search[0]) { /* high-light string in string */
			strcpy(tmp,str);
			strupr(tmp);
			ptr=strstr(tmp,search);
			i=strlen(search);
			j=ptr-tmp;
			bprintf("%.*s",j,str);
			attr(cfg.color[clr_filedesc]^HIGH);
			bprintf("%.*s",i,str+j);
			attr(cfg.color[clr_filedesc]);
			bprintf("%.*s",strlen(str)-(j+i),str+j+i); }
		else
			bputs(str);
		CRLF; }
	ptr=ext;
	while(*ptr && ptr<ext+512 && !msgabort()) {
		cr=strchr(ptr,CR);
		lf=strchr(ptr,LF);
		if(lf && (lf<cr || !cr)) cr=lf;
		if(cr>ptr+LEN_FDESC)
			cr=ptr+LEN_FDESC;
		else if(cr)
			*cr=0;
		sprintf(str,"%.*s\r\n",LEN_FDESC,ptr);
		putmsg(str,P_NOATCODES|P_SAVEATR);
		if(!cr) {
			if(strlen(ptr)>LEN_FDESC)
				cr=ptr+LEN_FDESC;
			else
				break; }
		if(!(*(cr+1)) || !(*(cr+2)))
			break;
		bprintf("%21s",nulstr);
		ptr=cr;
		if(!(*ptr)) ptr++;
		while(*ptr==LF || *ptr==CR) ptr++; }
	return(true);
}

/****************************************************************************/
/* Remove credits from uploader of file 'f'                                 */
/****************************************************************************/
bool sbbs_t::removefcdt(file_t* f)
{
497
498
499
500
	char	str[128];
	char 	tmp[512];
	int		u;
	long	cdt;
501

502
	if((u=matchuser(&cfg,f->uler,TRUE /*sysop_alias*/))==0) {
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
	   bputs(text[UnknownUser]);
	   return(false); }
	cdt=0L;
	if(cfg.dir[f->dir]->misc&DIR_CDTMIN && cur_cps) {
		if(cfg.dir[f->dir]->misc&DIR_CDTUL)
			cdt=((ulong)(f->cdt*(cfg.dir[f->dir]->up_pct/100.0))/cur_cps)/60;
		if(cfg.dir[f->dir]->misc&DIR_CDTDL
			&& f->timesdled)  /* all downloads */
			cdt+=((ulong)((long)f->timesdled
				*f->cdt*(cfg.dir[f->dir]->dn_pct/100.0))/cur_cps)/60;
		adjustuserrec(&cfg,u,U_MIN,10,-cdt);
		sprintf(str,"%lu minute",cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? str : text[No]);
		putsmsg(&cfg,u,tmp); }
	else {
		if(cfg.dir[f->dir]->misc&DIR_CDTUL)
			cdt=(ulong)(f->cdt*(cfg.dir[f->dir]->up_pct/100.0));
		if(cfg.dir[f->dir]->misc&DIR_CDTDL
			&& f->timesdled)  /* all downloads */
			cdt+=(ulong)((long)f->timesdled
				*f->cdt*(cfg.dir[f->dir]->dn_pct/100.0));
		adjustuserrec(&cfg,u,U_CDT,10,-cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? ultoac(cdt,str) : text[No]);
		putsmsg(&cfg,u,tmp); }

	adjustuserrec(&cfg,u,U_ULB,10,-f->size);
	adjustuserrec(&cfg,u,U_ULS,5,-1);
	return(true);
}

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
bool sbbs_t::removefile(file_t* f)
{
	char str[256];

	if(removefiledat(&cfg,f)) {
		SAFEPRINTF4(str,"%s removed %s from %s %s"
			,useron.alias
			,f->name
			,cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
		logline("U-",str);
		return(true);
	}
	SAFEPRINTF2(str,"%s %s",cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
	errormsg(WHERE, ERR_REMOVE, f->name, 0, str);
	return(false);
}

552
553
554
555
556
/****************************************************************************/
/* Move file 'f' from f.dir to newdir                                       */
/****************************************************************************/
bool sbbs_t::movefile(file_t* f, int newdir)
{
557
	char str[MAX_PATH+1],path[MAX_PATH+1],fname[128],ext[1024];
558
559
560
561
	int olddir=f->dir;

	if(findfile(&cfg,newdir,f->name)) {
		bprintf(text[FileAlreadyThere],f->name);
562
563
		return(false); 
	}
564
	getextdesc(&cfg,olddir,f->datoffset,ext);
565
566
567
568
569
570
571
572
	if(cfg.dir[olddir]->misc&DIR_MOVENEW)
		f->dateuled=time(NULL);
	unpadfname(f->name,fname);
	removefiledat(&cfg,f);
	f->dir=newdir;
	addfiledat(&cfg,f);
	bprintf(text[MovedFile],f->name
		,cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
573
574
	sprintf(str,"%s moved %s to %s %s",f->name
		,useron.alias
575
576
577
578
		,cfg.lib[cfg.dir[f->dir]->lib]->sname,cfg.dir[f->dir]->sname);
	logline(nulstr,str);
	if(!f->altpath) {	/* move actual file */
		sprintf(str,"%s%s",cfg.dir[olddir]->path,fname);
579
		if(fexistcase(str)) {
580
581
582
583
			sprintf(path,"%s%s",cfg.dir[f->dir]->path,getfname(str));
			mv(str,path,0); 
		} 
	}
584
	if(f->misc&FM_EXTDESC)
585
		putextdesc(&cfg,f->dir,f->datoffset,ext);
586
587
588
589
590
591
592
593
594
595
596
	return(true);
}

/****************************************************************************/
/* Batch flagging prompt for download, extended info, and archive viewing	*/
/* Returns -1 if 'Q' or Ctrl-C, 0 if skip, 1 if [Enter], 2 otherwise        */
/* or 3, backwards. 														*/
/****************************************************************************/
int sbbs_t::batchflagprompt(uint dirnum, file_t* bf, uint total
							,long totalfiles)
{
597
	char	ch,c,d,str[256],fname[128],*p,remcdt=0,remfile=0;
598
	char 	tmp[512];
599
	uint	i,j,ml=0,md=0,udir,ulib;
600
	file_t	f;
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619

	for(ulib=0;ulib<usrlibs;ulib++)
		if(usrlib[ulib]==cfg.dir[dirnum]->lib)
			break;
	for(udir=0;udir<usrdirs[ulib];udir++)
		if(usrdir[ulib][udir]==dirnum)
			break;

	CRLF;
	while(online) {
		bprintf(text[BatchFlagPrompt]
			,ulib+1
			,cfg.lib[cfg.dir[dirnum]->lib]->sname
			,udir+1
			,cfg.dir[dirnum]->sname
			,totalfiles);
		ch=getkey(K_UPPER);
		clearline();
		if(ch=='?') {
620
			menu("batflag");
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
			if(lncntr)
				pause();
			return(2); }
		if(ch=='Q' || sys_status&SS_ABORT)
			return(-1);
		if(ch=='S')
			return(0);
		if(ch=='P' || ch=='-')
			return(3);
		if(ch=='B') {    /* Flag for batch download */
			if(useron.rest&FLAG('D')) {
				bputs(text[R_Download]);
				return(2); }
			if(total==1) {
				f.dir=dirnum;
				strcpy(f.name,bf[0].name);
				f.datoffset=bf[0].datoffset;
				f.size=0;
				getfiledat(&cfg,&f);
				addtobatdl(&f);
				CRLF;
				return(2); }
			bputs(text[BatchDlFlags]);
			d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF);
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
			if(d) { 	/* d is string length */
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
					if(batdn_total>=cfg.max_batdn) {
						bprintf(text[BatchDlQueueIsFull],str+c);
						break; }
					if(strchr(str+c,'.')) {     /* filename or spec given */
						f.dir=dirnum;
657
						p=strchr(str+c,' ');
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
							if(batdn_total>=cfg.max_batdn) {
								bprintf(text[BatchDlQueueIsFull],str+c);
								break; }
							padfname(str+c,tmp);
							if(filematch(bf[i].name,tmp)) {
								strcpy(f.name,bf[i].name);
								f.datoffset=bf[i].datoffset;
								f.size=0;
								getfiledat(&cfg,&f);
								addtobatdl(&f); } } }
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
						f.dir=dirnum;
						strcpy(f.name,bf[str[c]-'A'].name);
						f.datoffset=bf[str[c]-'A'].datoffset;
						f.size=0;
						getfiledat(&cfg,&f);
						addtobatdl(&f); } }
				CRLF;
				return(2); }
			clearline();
			continue; }

		if(ch=='E' || ch=='V') {    /* Extended Info */
			if(total==1) {
				f.dir=dirnum;
				strcpy(f.name,bf[0].name);
				f.datoffset=bf[0].datoffset;
				f.dateuled=bf[0].dateuled;
				f.datedled=bf[0].datedled;
				f.size=0;
				getfiledat(&cfg,&f);
				if(!viewfile(&f,ch=='E'))
					return(-1);
				return(2); }
			bputs(text[BatchDlFlags]);
			d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF);
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
			if(d) { 	/* d is string length */
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
					if(strchr(str+c,'.')) {     /* filename or spec given */
						f.dir=dirnum;
708
						p=strchr(str+c,' ');
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
							padfname(str+c,tmp);
							if(filematch(bf[i].name,tmp)) {
								strcpy(f.name,bf[i].name);
								f.datoffset=bf[i].datoffset;
								f.dateuled=bf[i].dateuled;
								f.datedled=bf[i].datedled;
								f.size=0;
								getfiledat(&cfg,&f);
								if(!viewfile(&f,ch=='E'))
									return(-1); } } }
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
						f.dir=dirnum;
						strcpy(f.name,bf[str[c]-'A'].name);
						f.datoffset=bf[str[c]-'A'].datoffset;
						f.dateuled=bf[str[c]-'A'].dateuled;
						f.datedled=bf[str[c]-'A'].datedled;
						f.size=0;
						getfiledat(&cfg,&f);
						if(!viewfile(&f,ch=='E'))
							return(-1); } }
				return(2); }
			clearline();
			continue; }

		if((ch=='D' || ch=='M')     /* Delete or Move */
			&& !(useron.rest&FLAG('R'))
			&& (dir_op(dirnum) || useron.exempt&FLAG('R'))) {
			if(total==1) {
				strcpy(str,"A");
				d=1; }
			else {
				bputs(text[BatchDlFlags]);
				d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF); }
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
			if(d) { 	/* d is string length */
				CRLF;
				if(ch=='D') {
					if(noyes(text[AreYouSureQ]))
						return(2);
					remcdt=remfile=1;
					if(dir_op(dirnum)) {
						remcdt=!noyes(text[RemoveCreditsQ]);
						remfile=!noyes(text[DeleteFileQ]); } }
				else if(ch=='M') {
					CRLF;
					for(i=0;i<usrlibs;i++)
						bprintf(text[MoveToLibLstFmt],i+1,cfg.lib[usrlib[i]]->lname);
					SYNC;
					bprintf(text[MoveToLibPrompt],cfg.dir[dirnum]->lib+1);
765
					if((int)(ml=getnum(usrlibs))==-1)
766
767
768
769
770
771
772
773
774
775
776
						return(2);
					if(!ml)
						ml=cfg.dir[dirnum]->lib;
					else
						ml--;
					CRLF;
					for(j=0;j<usrdirs[ml];j++)
						bprintf(text[MoveToDirLstFmt]
							,j+1,cfg.dir[usrdir[ml][j]]->lname);
					SYNC;
					bprintf(text[MoveToDirPrompt],usrdirs[ml]);
777
					if((int)(md=getnum(usrdirs[ml]))==-1)
778
779
780
781
782
783
784
785
786
						return(2);
					if(!md)
						md=usrdirs[ml]-1;
					else md--;
					CRLF; }
				lncntr=0;
				for(c=0;c<d;c++) {
					if(strchr(str+c,'.')) {     /* filename or spec given */
						f.dir=dirnum;
787
						p=strchr(str+c,' ');
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
							padfname(str+c,tmp);
							if(filematch(bf[i].name,tmp)) {
								strcpy(f.name,bf[i].name);
								unpadfname(f.name,fname);
								f.datoffset=bf[i].datoffset;
								f.dateuled=bf[i].dateuled;
								f.datedled=bf[i].datedled;
								f.size=0;
								getfiledat(&cfg,&f);
								if(f.opencount) {
									bprintf(text[FileIsOpen]
										,f.opencount,f.opencount>1 ? "s":nulstr);
									continue; }
								if(ch=='D') {
805
									removefile(&f);
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
									if(remfile) {
										sprintf(tmp,"%s%s",cfg.dir[f.dir]->path,fname);
										remove(tmp); }
									if(remcdt)
										removefcdt(&f); }
								else if(ch=='M')
									movefile(&f,usrdir[ml][md]); } } }
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
						f.dir=dirnum;
						strcpy(f.name,bf[str[c]-'A'].name);
						unpadfname(f.name,fname);
						f.datoffset=bf[str[c]-'A'].datoffset;
						f.dateuled=bf[str[c]-'A'].dateuled;
						f.datedled=bf[str[c]-'A'].datedled;
						f.size=0;
						getfiledat(&cfg,&f);
						if(f.opencount) {
							bprintf(text[FileIsOpen]
								,f.opencount,f.opencount>1 ? "s":nulstr);
							continue; }
						if(ch=='D') {
829
							removefile(&f);
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
							if(remfile) {
								sprintf(tmp,"%s%s",cfg.dir[f.dir]->path,fname);
								remove(tmp); }
							if(remcdt)
								removefcdt(&f); }
						else if(ch=='M')
							movefile(&f,usrdir[ml][md]); } }
				return(2); }
			clearline();
			continue; }

		return(1); }

	return(-1);
}

/****************************************************************************/
/* List detailed information about the files in 'filespec'. Prompts for     */
/* action depending on 'mode.'                                              */
/* Returns number of files matching filespec that were found                */
/****************************************************************************/
int sbbs_t::listfileinfo(uint dirnum, char *filespec, long mode)
{
	char	str[258],path[258],dirpath[256],done=0,ch,fname[13],ext[513];
854
855
	char 	tmp[512];
	uchar	*ixbbuf,*usrxfrbuf=NULL,*p;
856
	int		file;
rswindell's avatar
rswindell committed
857
	int		error;
858
859
	int		found=0;
    uint	i,j;
860
	long	usrxfrlen=0;
861
862
863
864
    long	m,l;
	long	usrcdt;
    time_t	start,end,t;
    file_t	f;
865
	struct	tm tm;
866

867
	sprintf(str,"%sxfer.ixt",cfg.data_dir);
868
869
870
	if(mode==FI_USERXFER) {
		if(flength(str)<1L)
			return(0);
871
872
873
874
		if((file=nopen(str,O_RDONLY))==-1) {
			errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
			return(0); }
		usrxfrlen=filelength(file);
deuce's avatar
deuce committed
875
		if((usrxfrbuf=(uchar *)malloc(usrxfrlen))==NULL) {
876
877
878
879
880
			close(file);
			errormsg(WHERE,ERR_ALLOC,str,usrxfrlen);
			return(0); }
		if(read(file,usrxfrbuf,usrxfrlen)!=usrxfrlen) {
			close(file);
deuce's avatar
deuce committed
881
			free(usrxfrbuf);
882
883
884
			errormsg(WHERE,ERR_READ,str,usrxfrlen);
			return(0); }
		close(file); }
885
	sprintf(str,"%s%s.ixb",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
886
887
888
889
890
891
	if((file=nopen(str,O_RDONLY))==-1)
		return(0);
	l=filelength(file);
	if(!l) {
		close(file);
		return(0); }
deuce's avatar
deuce committed
892
	if((ixbbuf=(uchar *)malloc(l))==NULL) {
893
894
895
896
897
898
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,l);
		return(0); }
	if(lread(file,ixbbuf,l)!=l) {
		close(file);
		errormsg(WHERE,ERR_READ,str,l);
deuce's avatar
deuce committed
899
		free((char *)ixbbuf);
900
		if(usrxfrbuf)
deuce's avatar
deuce committed
901
			free(usrxfrbuf);
902
903
		return(0); }
	close(file);
904
	sprintf(str,"%s%s.dat",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
905
906
	if((file=nopen(str,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_READ,str,O_RDONLY);
deuce's avatar
deuce committed
907
		free((char *)ixbbuf);
908
		if(usrxfrbuf)
deuce's avatar
deuce committed
909
			free(usrxfrbuf);
910
911
912
913
914
915
916
917
918
919
920
		return(0); }
	close(file);
	m=0;
	while(online && !done && m<l) {
		if(mode==FI_REMOVE && dir_op(dirnum))
			action=NODE_SYSP;
		else action=NODE_LFIL;
		if(msgabort()) {
			found=-1;
			break; }
		for(i=0;i<12 && m<l;i++)
921
922
			if(i==8)
				str[i]=ixbbuf[m]>' ' ? '.' : ' ';
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
950
951
952
953
954
955
956
957
958
			else
				str[i]=ixbbuf[m++];     /* Turns FILENAMEEXT into FILENAME.EXT */
		str[i]=0;
		unpadfname(str,fname);
		if(filespec[0] && !filematch(str,filespec)) {
			m+=11;
			continue; }
		f.datoffset=ixbbuf[m]|((long)ixbbuf[m+1]<<8)|((long)ixbbuf[m+2]<<16);
		f.dateuled=ixbbuf[m+3]|((long)ixbbuf[m+4]<<8)
			|((long)ixbbuf[m+5]<<16)|((long)ixbbuf[m+6]<<24);
		f.datedled=ixbbuf[m+7]|((long)ixbbuf[m+8]<<8)
			|((long)ixbbuf[m+9]<<16)|((long)ixbbuf[m+10]<<24);
		m+=11;
		if(mode==FI_OLD && f.datedled>ns_time)
			continue;
		if((mode==FI_OLDUL || mode==FI_OLD) && f.dateuled>ns_time)
			continue;
		f.dir=curdirnum=dirnum;
		strcpy(f.name,str);
		f.size=0;
		getfiledat(&cfg,&f);
		if(mode==FI_OFFLINE && f.size>=0)
			continue;
		if(f.altpath>0 && f.altpath<=cfg.altpaths)
			strcpy(dirpath,cfg.altpath[f.altpath-1]);
		else
			strcpy(dirpath,cfg.dir[f.dir]->path);
		if(mode==FI_CLOSE && !f.opencount)
			continue;
		if(mode==FI_USERXFER) {
			for(p=usrxfrbuf;p<usrxfrbuf+usrxfrlen;p+=24) {
				sprintf(str,"%17.17s",p);   /* %4.4u %12.12s */
				if(!strcmp(str+5,f.name) && useron.number==atoi(str))
					break; }
			if(p>=usrxfrbuf+usrxfrlen) /* file wasn't found */
				continue; }
959
		if((mode==FI_REMOVE) && (!dir_op(dirnum) && stricmp(f.uler
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
			,useron.alias) && !(useron.exempt&FLAG('R'))))
			continue;
		found++;
		if(mode==FI_INFO) {
			if(!viewfile(&f,1)) {
				done=1;
				found=-1; } }
		else
			fileinfo(&f);
		if(mode==FI_CLOSE) {
			if(!noyes(text[CloseFileRecordQ])) {
				f.opencount=0;
				putfiledat(&cfg,&f); } }
		else if(mode==FI_REMOVE || mode==FI_OLD || mode==FI_OLDUL
			|| mode==FI_OFFLINE) {
			SYNC;
			CRLF;
			if(f.opencount) {
				mnemonics(text[QuitOrNext]);
				strcpy(str,"Q\r"); }
			else if(dir_op(dirnum)) {
				mnemonics(text[SysopRemoveFilePrompt]);
				strcpy(str,"VEFMCQR\r"); }
			else if(useron.exempt&FLAG('R')) {
				mnemonics(text[RExemptRemoveFilePrompt]);
				strcpy(str,"VEMQR\r"); }
			else {
				mnemonics(text[UserRemoveFilePrompt]);
				strcpy(str,"VEQR\r"); }
			switch(getkeys(str,0)) {
				case 'V':
					viewfilecontents(&f);
					CRLF;
					ASYNC;
					pause();
					m-=F_IXBSIZE;
					continue;
				case 'E':   /* edit file information */
					if(dir_op(dirnum)) {
						bputs(text[EditFilename]);
						strcpy(str,fname);
1001
1002
						if(!getstr(str,12,K_EDIT|K_AUTODEL))
							break;
1003
1004
						if(strcmp(str,fname)) { /* rename */
							padfname(str,path);
1005
1006
							if(stricmp(str,fname)
								&& findfile(&cfg,f.dir,path))
1007
1008
1009
1010
								bprintf(text[FileAlreadyThere],path);
							else {
								sprintf(path,"%s%s",dirpath,fname);
								sprintf(tmp,"%s%s",dirpath,str);
1011
								if(fexistcase(path) && rename(path,tmp))
1012
1013
1014
1015
1016
1017
									bprintf(text[CouldntRenameFile],path,tmp);
								else {
									bprintf(text[FileRenamed],path,tmp);
									strcpy(fname,str);
									removefiledat(&cfg,&f);
									strcpy(f.name,padfname(str,tmp));
1018
1019
1020
1021
1022
									addfiledat(&cfg,&f); 
								} 
							} 
						} 
					}
1023
1024
					bputs(text[EditDescription]);
					getstr(f.desc,LEN_FDESC,K_LINE|K_EDIT|K_AUTODEL);
1025
1026
					if(sys_status&SS_ABORT)
						break;
1027
1028
1029
1030
1031
1032
1033
1034
					if(f.misc&FM_EXTDESC) {
						if(!noyes(text[DeleteExtDescriptionQ])) {
							remove(str);
							f.misc&=~FM_EXTDESC; } }
					if(!dir_op(dirnum)) {
						putfiledat(&cfg,&f);
						break; }
					bputs(text[EditUploader]);
1035
1036
					if(!getstr(f.uler,LEN_ALIAS,K_EDIT|K_AUTODEL))
						break;
1037
1038
					ultoa(f.cdt,str,10);
					bputs(text[EditCreditValue]);
1039
					getstr(str,10,K_NUMBER|K_EDIT|K_AUTODEL);
1040
1041
					if(sys_status&SS_ABORT)
						break;
1042
					f.cdt=atol(str);
1043
					ultoa(f.timesdled,str,10);
1044
1045
					bputs(text[EditTimesDownloaded]);
					getstr(str,5,K_NUMBER|K_EDIT|K_AUTODEL);
1046
1047
					if(sys_status&SS_ABORT)
						break;
1048
1049
					f.timesdled=atoi(str);
					if(f.opencount) {
1050
						ultoa(f.opencount,str,10);
1051
1052
1053
1054
						bputs(text[EditOpenCount]);
						getstr(str,3,K_NUMBER|K_EDIT|K_AUTODEL);
						f.opencount=atoi(str); }
					if(cfg.altpaths || f.altpath) {
1055
						ultoa(f.altpath,str,10);
1056
1057
1058
1059
1060
						bputs(text[EditAltPath]);
						getstr(str,3,K_NUMBER|K_EDIT|K_AUTODEL);
						f.altpath=atoi(str);
						if(f.altpath>cfg.altpaths)
							f.altpath=0; }
1061
1062
					if(sys_status&SS_ABORT)
						break;
1063
					putfiledat(&cfg,&f);
deuce's avatar
deuce committed
1064
					inputnstime32(&f.dateuled);
rswindell's avatar
rswindell committed
1065
					update_uldate(&cfg, &f);
1066
1067
1068
					break;
				case 'F':   /* delete file only */
					sprintf(str,"%s%s",dirpath,fname);
1069
					if(!fexistcase(str))
1070
						bprintf(text[FileDoesNotExist],str);
1071
1072
1073
1074
1075
					else {
						if(!noyes(text[AreYouSureQ])) {
							if(remove(str))
								bprintf(text[CouldntRemoveFile],str);
							else {
1076
1077
1078
1079
1080
1081
1082
								sprintf(tmp,"%s deleted %s"
									,useron.alias
									,str);
								logline(nulstr,tmp); 
							} 
						} 
					}
1083
1084
1085
1086
					break;
				case 'R':   /* remove file from database */
					if(noyes(text[AreYouSureQ]))
						break;
1087
					removefile(&f);
1088
					sprintf(str,"%s%s",dirpath,fname);
1089
					if(fexistcase(str)) {
1090
1091
1092
1093
1094
						if(dir_op(dirnum)) {
							if(!noyes(text[DeleteFileQ])) {
								if(remove(str))
									bprintf(text[CouldntRemoveFile],str);
								else {
1095
1096
1097
1098
1099
1100
1101
									sprintf(tmp,"%s deleted %s"
										,useron.alias
										,str);
									logline(nulstr,tmp); 
								} 
							} 
						}
1102
1103
1104
1105
1106
1107
1108
1109
1110
						else if(remove(str))    /* always remove if not sysop */
							bprintf(text[CouldntRemoveFile],str); }
					if(dir_op(dirnum) || useron.exempt&FLAG('R')) {
						i=cfg.lib[cfg.dir[f.dir]->lib]->offline_dir;
						if(i!=dirnum && i!=INVALID_DIR
							&& !findfile(&cfg,i,f.name)) {
							sprintf(str,text[AddToOfflineDirQ]
								,fname,cfg.lib[cfg.dir[i]->lib]->sname,cfg.dir[i]->sname);
							if(yesno(str)) {
1111
								getextdesc(&cfg,f.dir,f.datoffset,ext);
1112
1113
1114
								f.dir=i;
								addfiledat(&cfg,&f);
								if(f.misc&FM_EXTDESC)
1115
									putextdesc(&cfg,f.dir,f.datoffset,ext); } } }
1116
					if(dir_op(dirnum) || stricmp(f.uler,useron.alias)) {
1117
1118
1119
						if(noyes(text[RemoveCreditsQ]))
	/* Fall through */      break; }
				case 'C':   /* remove credits only */
1120
					if((i=matchuser(&cfg,f.uler,TRUE /*sysop_alias*/))==0) {
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
						bputs(text[UnknownUser]);
						break; }
					if(dir_op(dirnum)) {
						usrcdt=(ulong)(f.cdt*(cfg.dir[f.dir]->up_pct/100.0));
						if(f.timesdled)     /* all downloads */
							usrcdt+=(ulong)((long)f.timesdled
								*f.cdt*(cfg.dir[f.dir]->dn_pct/100.0));
						ultoa(usrcdt,str,10);
						bputs(text[CreditsToRemove]);
						getstr(str,10,K_NUMBER|K_LINE|K_EDIT|K_AUTODEL);
						f.cdt=atol(str); }
					usrcdt=adjustuserrec(&cfg,i,U_CDT,10,-(long)f.cdt);
					if(i==useron.number)
						useron.cdt=usrcdt;
					sprintf(str,text[FileRemovedUserMsg]
						,f.name,f.cdt ? ultoac(f.cdt,tmp) : text[No]);
					putsmsg(&cfg,i,str);
					usrcdt=adjustuserrec(&cfg,i,U_ULB,10,-f.size);
					if(i==useron.number)
						useron.ulb=usrcdt;
					usrcdt=adjustuserrec(&cfg,i,U_ULS,5,-1);
					if(i==useron.number)
						useron.uls=(ushort)usrcdt;
					break;
				case 'M':   /* move the file to another dir */
					CRLF;
					for(i=0;i<usrlibs;i++)
						bprintf(text[MoveToLibLstFmt],i+1,cfg.lib[usrlib[i]]->lname);
					SYNC;
					bprintf(text[MoveToLibPrompt],cfg.dir[dirnum]->lib+1);
1151
					if((int)(i=getnum(usrlibs))==-1)
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
						continue;
					if(!i)
						i=cfg.dir[dirnum]->lib;
					else
						i--;
					CRLF;
					for(j=0;j<usrdirs[i];j++)
						bprintf(text[MoveToDirLstFmt]
							,j+1,cfg.dir[usrdir[i][j]]->lname);
					SYNC;
					bprintf(text[MoveToDirPrompt],usrdirs[i]);
1163
					if((int)(j=getnum(usrdirs[i]))==-1)
1164
1165
1166
1167
1168
						continue;
					if(!j)
						j=usrdirs[i]-1;
					else j--;
					CRLF;
1169
					movefile(&f,usrdir[i][j]);
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
					break;
				case 'Q':   /* quit */
					found=-1;
					done=1;
					break; } }
		else if(mode==FI_DOWNLOAD || mode==FI_USERXFER) {
			sprintf(path,"%s%s",dirpath,fname);
			if(f.size<1L) { /* getfiledat will set this to -1 if non-existant */
				SYNC;       /* and 0 byte files shouldn't be d/led */
				mnemonics(text[QuitOrNext]);
				if(getkeys("\rQ",0)=='Q') {
					found=-1;
					break; }
				continue; }
1184
			if(!is_download_free(&cfg,f.dir,&useron)
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
				&& f.cdt>(useron.cdt+useron.freecdt)) {
				SYNC;
				bprintf(text[YouOnlyHaveNCredits]
					,ultoac(useron.cdt+useron.freecdt,tmp));
				mnemonics(text[QuitOrNext]);
				if(getkeys("\rQ",0)=='Q') {
					found=-1;
					break; }
				continue; }
			if(!chk_ar(cfg.dir[f.dir]->dl_ar,&useron)) {
				SYNC;
				bputs(text[CantDownloadFromDir]);
				mnemonics(text[QuitOrNext]);
				if(getkeys("\rQ",0)=='Q') {
					found=-1;
					break; }
				continue; }
			if(!(cfg.dir[f.dir]->misc&DIR_TFREE) && f.timetodl>timeleft && !dir_op(dirnum)
				&& !(useron.exempt&FLAG('T'))) {
				SYNC;
				bputs(text[NotEnoughTimeToDl]);
				mnemonics(text[QuitOrNext]);
				if(getkeys("\rQ",0)=='Q') {
					found=-1;
					break; }
				continue; }
1211
			xfer_prot_menu(XFER_DOWNLOAD);
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
			openfile(&f);
			SYNC;
			mnemonics(text[ProtocolBatchQuitOrNext]);
			strcpy(str,"BQ\r");
			for(i=0;i<cfg.total_prots;i++)
				if(cfg.prot[i]->dlcmd[0]
					&& chk_ar(cfg.prot[i]->ar,&useron)) {
					sprintf(tmp,"%c",cfg.prot[i]->mnemonic);
					strcat(str,tmp); }
	//		  ungetkey(useron.prot);
			ch=(char)getkeys(str,0);
			if(ch=='Q') {
				found=-1;
				done=1; }
			else if(ch=='B') {
				if(!addtobatdl(&f)) {
					closefile(&f);
					break; } }
			else if(ch!=CR) {
				for(i=0;i<cfg.total_prots;i++)
					if(cfg.prot[i]->dlcmd[0] && cfg.prot[i]->mnemonic==ch
						&& chk_ar(cfg.prot[i]->ar,&useron))
						break;
				if(i<cfg.total_prots) {
rswindell's avatar
rswindell committed
1236
#if 0	/* no such thing as local logon */
1237
1238
					if(online==ON_LOCAL) {
						bputs(text[EnterPath]);
1239
						if(getstr(path,60,K_LINE)) {
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
							backslash(path);
							strcat(path,fname);
							sprintf(str,"%s%s",dirpath,fname);
							if(!mv(str,path,1))
								downloadfile(&f);
							for(j=0;j<cfg.total_dlevents;j++)
								if(!stricmp(cfg.dlevent[j]->ext,f.name+9)
									&& chk_ar(cfg.dlevent[j]->ar,&useron)) {
									bputs(cfg.dlevent[j]->workstr);
									external(cmdstr(cfg.dlevent[j]->cmd,path,nulstr
										,NULL)
										,EX_OUTL);
									CRLF; }
								} }
rswindell's avatar
rswindell committed
1254
1255
1256
					else 
#endif
					{
1257
						delfiles(cfg.temp_dir,ALLFILES);
1258
1259
1260
1261
1262
1263
1264
						if(cfg.dir[f.dir]->seqdev) {
							lncntr=0;
							seqwait(cfg.dir[f.dir]->seqdev);
							bprintf(text[RetrievingFile],fname);
							sprintf(str,"%s%s",dirpath,fname);
							sprintf(path,"%s%s",cfg.temp_dir,fname);
							mv(str,path,1); /* copy the file to temp dir */
1265
1266
1267
1268
1269
1270
							if(getnodedat(cfg.node_num,&thisnode,true)==0) {
								thisnode.aux=0xf0;
								putnodedat(cfg.node_num,&thisnode);
							}
							CRLF; 
						}
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
						for(j=0;j<cfg.total_dlevents;j++)
							if(!stricmp(cfg.dlevent[j]->ext,f.name+9)
								&& chk_ar(cfg.dlevent[j]->ar,&useron)) {
								bputs(cfg.dlevent[j]->workstr);
								external(cmdstr(cfg.dlevent[j]->cmd,path,nulstr,NULL)
									,EX_OUTL);
								CRLF; }
						getnodedat(cfg.node_num,&thisnode,1);
						action=NODE_DLNG;
						t=now+f.timetodl;
1281
1282
						localtime_r(&t,&tm);
						thisnode.aux=(tm.tm_hour*60)+tm.tm_min;
1283
1284
						putnodedat(cfg.node_num,&thisnode); /* calculate ETA */
						start=time(NULL);
1285
						error=protocol(cfg.prot[i],XFER_DOWNLOAD,path,nulstr,false);
1286
1287
1288
						end=time(NULL);
						if(cfg.dir[f.dir]->misc&DIR_TFREE)
							starttime+=end-start;
1289
1290
1291
1292
						if(checkprotresult(cfg.prot[i],error,&f))
							downloadfile(&f);
						else
							notdownloaded(f.size,start,end); 
1293
						delfiles(cfg.temp_dir,ALLFILES);
1294
1295
1296
1297
						autohangup(); 
					} 
				} 
			}
1298
			closefile(&f); }
1299
1300
1301
		if(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?')) 
			break; 
	}
deuce's avatar
deuce committed
1302
	free((char *)ixbbuf);
1303
	if(usrxfrbuf)
deuce's avatar
deuce committed
1304
		free(usrxfrbuf);
1305
1306
1307
1308
1309
1310
	return(found);
}

/****************************************************************************/
/* Prints one file's information on a single line to a file 'file'          */
/****************************************************************************/
deuce's avatar
deuce committed
1311
void sbbs_t::listfiletofile(char *fname, char *buf, uint dirnum, int file)
1312
{
1313
1314
1315
1316
1317
    char	str[256];
	char 	tmp[512];
    uchar	alt;
    ulong	cdt;
	bool	exist=true;
1318
1319

	strcpy(str,fname);
1320
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC)
1321
1322
1323
1324
1325
1326
1327
1328
		strcat(str,"+");
	else
		strcat(str," ");
	write(file,str,13);
	getrec((char *)buf,F_ALTPATH,2,str);
	alt=(uchar)ahtoul(str);
	sprintf(str,"%s%s",alt>0 && alt<=cfg.altpaths ? cfg.altpath[alt-1]
		: cfg.dir[dirnum]->path,unpadfname(fname,tmp));
1329
	if(cfg.dir[dirnum]->misc&DIR_FCHK && !fexistcase(str))
1330
		exist=false;
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
	getrec((char *)buf,F_CDT,LEN_FCDT,str);
	cdt=atol(str);
	if(!cdt)
		strcpy(str,"   FREE");
	else
		sprintf(str,"%7lu",cdt);
	if(exist)
		strcat(str," ");
	else
		strcat(str,"-");
	write(file,str,8);
	getrec((char *)buf,F_DESC,LEN_FDESC,str);
	write(file,str,strlen(str));
	write(file,crlf,2);
}

int extdesclines(char *str)
{
	int i,lc,last;

	for(i=lc=last=0;str[i];i++)
		if(str[i]==LF || i-last>LEN_FDESC) {
			lc++;
			last=i; }
	return(lc);
}