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

	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';
136
137
					letter='A'; 
				}
138
139
140
				else if(i==3) {
					if((long)anchor-((letter-'A')*F_IXBSIZE)<0) {
						m=0;
141
142
						found=0; 
					}
143
144
					else {
						m=anchor-((letter-'A')*F_IXBSIZE);
145
146
147
148
						found-=letter-'A'; 
					}
					letter='A'; 
				}
149
				else if((int)i==-1) {
deuce's avatar
deuce committed
150
151
					free((char *)ixbbuf);
					free((char *)datbuf);
152
153
					return(-1); 
				}
154
155
156
				else
					break;
				getnodedat(cfg.node_num,&thisnode,0);
157
158
				nodesync(); 
			}
159
			else
160
161
				break; 
		}
162
163
164
165
166
167
168

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

		if(msgabort()) {		 /* used to be !tofile && msgabort() */
deuce's avatar
deuce committed
169
170
			free((char *)ixbbuf);
			free((char *)datbuf);
171
172
			return(-1); 
		}
173
		for(j=0;j<12 && m<l;j++)
174
175
			if(j==8)
				str[j]=ixbbuf[m]>' ' ? '.' : ' ';
176
177
178
179
180
181
			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;
182
183
			continue; 
		}
184
185
186
		n=ixbbuf[m]|((long)ixbbuf[m+1]<<8)|((long)ixbbuf[m+2]<<16);
		if(n>=datbuflen) {	/* out of bounds */
			m+=11;
187
188
			continue; 
		}
189
190
191
192
193
194
		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;
195
196
				continue; 
			}
197
198
			getrec((char *)&datbuf[n],F_MISC,1,tmp);
			j=tmp[0];  /* misc bits */
199
			if(j) j-=' ';
200
			if(mode&FL_EXFIND && j&FM_EXTDESC) { /* search extended description */
201
				getextdesc(&cfg,dirnum,n,ext);
202
203
204
				strupr(ext);
				if(!strstr(ext,filespec) && !p) {	/* not in description or */
					m+=11;						 /* extended description */
205
206
207
					continue; 
				}
			}
208
209
			else if(!p) {			 /* no extended description and not in desc */
				m+=11;
210
211
				continue; } 
		}
212
213
214
215
		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;
216
217
				continue; } 
		}
218
219
220
221
222
223
224
225
226
227
228
229
230
231
		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,'?')))) {
232
				sprintf(hdr,"%s%s.hdr",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
233
				if(fexistcase(hdr))
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
					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++)
252
							outchar(' ');
253
						attr(cfg.color[clr_filelsthdrbox]);
254
255
256
257
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrDir],j+1,cfg.dir[dirnum]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
258
							outchar(' ');
259
						attr(cfg.color[clr_filelsthdrbox]);
260
261
262
263
						bputs("\r\n ");
						sprintf(hdr,text[BoxHdrFiles],l/F_IXBSIZE);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
264
							outchar(' ');
265
						attr(cfg.color[clr_filelsthdrbox]);
266
267
268
						bputs("\r\n");
						for(c=0;c<d;c++)
							outchar('');
269
						bputs("\r\n"); 
270
271
					}
				}
272
			}
273
274
275
276
			else {					/* short header */
				if(tofile) {
					sprintf(hdr,"(%u) %s ",i+1,cfg.lib[usrlib[i]]->sname);
					write(tofile,crlf,2);
277
278
					write(tofile,hdr,strlen(hdr)); 
				}
279
280
281
				else {
					sprintf(hdr,text[ShortHdrLib],i+1,cfg.lib[usrlib[i]]->sname);
					bputs("\r\1>\r\n");
282
283
					bputs(hdr); 
				}
284
285
286
				c=bstrlen(hdr);
				if(tofile) {
					sprintf(hdr,"(%u) %s",j+1,cfg.dir[dirnum]->lname);
287
288
					write(tofile,hdr,strlen(hdr)); 
				}
289
290
				else {
					sprintf(hdr,text[ShortHdrDir],j+1,cfg.dir[dirnum]->lname);
291
292
					bputs(hdr); 
				}
293
294
295
296
				c+=bstrlen(hdr);
				if(tofile) {
					write(tofile,crlf,2);
					sprintf(hdr,"%*s",c,nulstr);
297
					memset(hdr,'',c);
298
					strcat(hdr,crlf);
299
300
					write(tofile,hdr,strlen(hdr)); 
				}
301
302
303
304
305
				else {
					CRLF;
					attr(cfg.color[clr_filelstline]);
					while(c--)
						outchar('');
306
307
308
309
					CRLF; 
				} 
			} 
		}
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
		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
328
329
					free((char *)ixbbuf);
					free((char *)datbuf);
330
331
					return(-1); } 
			}
332
333
			else {
				if(!viewfile(&f,0)) {
deuce's avatar
deuce committed
334
335
					free((char *)ixbbuf);
					free((char *)datbuf);
336
337
338
339
					return(-1); 
				} 
			} 
		}
340
341
342
343
344
345
346
347
348

		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;
349
350
			letter--; 
		}
351
352
		else {
			disp=1;
353
354
			found++; 
		}
355
		if(sys_status&SS_ABORT) {
deuce's avatar
deuce committed
356
357
			free((char *)ixbbuf);
			free((char *)datbuf);
358
359
			return(-1); 
		}
360
361
362
363
364
365
366
367
368
369
		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)
370
371
					|((long)ixbbuf[m+9]<<16)|((long)ixbbuf[m+10]<<24); 
			}
372
373
374
375
376
377
378
379
380
381
			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
382
383
					free((char *)ixbbuf);
					free((char *)datbuf);
384
					if((int)i==-1)
385
386
						return(-1);
					else
387
388
						return(found); 
				}
389
390
				if(i==2) {
					next=anchor;
391
392
					found-=(letter-'A')+1; 
				}
393
394
395
				else if(i==3) {
					if((long)anchor-((letter-'A'+1)*F_IXBSIZE)<0) {
						next=0;
396
397
						found=0; 
					}
398
399
					else {
						next=anchor-((letter-'A'+1)*F_IXBSIZE);
400
401
						found-=letter-'A'+1; } 
				}
402
403
404
				getnodedat(cfg.node_num,&thisnode,0);
				nodesync();
				letter='A';	}
405
406
			else letter++; 
		}
407
408
409
		if(useron.misc&BATCHFLAG && !tofile
			&& lncntr>=rows-2) {
			lncntr=0;		/* defeat pause() */
410
411
			flagprompt=1; 
		}
412
413
414
		m=next;
		if(mode&FL_FINDDESC) continue;
		if(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?') && m)
415
416
			break; 
	}
417

deuce's avatar
deuce committed
418
419
	free((char *)ixbbuf);
	free((char *)datbuf);
420
421
422
423
424
425
426
	return(found);
}

/****************************************************************************/
/* Prints one file's information on a single line                           */
/* Return 1 if displayed, 0 otherwise										*/
/****************************************************************************/
427
428
bool sbbs_t::listfile(const char *fname, const char *buf, uint dirnum
	, const char *search, const char letter, ulong datoffset)
429
{
430
	char	str[256],ext[513]="",*ptr,*cr,*lf,exist=1;
431
	char	path[MAX_PATH+1];
432
433
434
435
	char 	tmp[512];
    uchar	alt;
    int		i,j;
    ulong	cdt;
436

437
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC && useron.misc&EXTDESC) {
438
		getextdesc(&cfg,dirnum,datoffset,ext);
439
		if(useron.misc&BATCHFLAG && lncntr+extdesclines(ext)>=rows-2 && letter!='A')
440
441
			return(false); 
	}
442

443
444
	attr(cfg.color[clr_filename]);
	bputs(fname);
445

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

451
	if(buf[F_MISC]!=ETX && (buf[F_MISC]-' ')&FM_EXTDESC) {
452
453
454
		if(!(useron.misc&EXTDESC))
			outchar('+');
		else
455
456
			outchar(' '); 
	}
457
	else
458
		outchar(' ');
459
460
	if(useron.misc&BATCHFLAG) {
		attr(cfg.color[clr_filedesc]);
461
462
		bprintf("%c",letter); 
	}
463
	if(cfg.dir[dirnum]->misc&DIR_FCHK && !fexistcase(path)) {
464
		exist=0;
465
466
		attr(cfg.color[clr_err]); 
	}
467
468
	else
		attr(cfg.color[clr_filecdt]);
469
	getrec(buf,F_CDT,LEN_FCDT,str);
470
471
472
473
	cdt=atol(str);
	if(useron.misc&BATCHFLAG) {
		if(!cdt) {
			attr(curatr^(HIGH|BLINK));
474
475
			bputs("  FREE"); 
		}
476
477
478
		else {
			if(cdt<1024)    /* 1k is smallest size */
				cdt=1024;
479
480
481
			if(cdt>(99999*1024))
				bprintf("%5luM",cdt/(1024*1024));
			else
482
483
				bprintf("%5luk",cdt/1024L); } 
	}
484
485
486
	else {
		if(!cdt) {  /* FREE file */
			attr(curatr^(HIGH|BLINK));
487
488
			bputs("   FREE"); 
		}
489
490
491
		else if(cdt>9999999L)
			bprintf("%6luk",cdt/1024L);
		else
492
493
			bprintf("%7lu",cdt); 
	}
494
	if(exist)
495
		outchar(' ');
496
497
	else
		outchar('-');
498
	getrec(buf,F_DESC,LEN_FDESC,str);
499
	attr(cfg.color[clr_filedesc]);
500
501
502

#ifdef _WIN32
 
503
	if(exist && !(cfg.file_misc&FM_NO_LFN)) {
504
505
		fexistcase(path);	/* Get real (long?) filename */
		ptr=getfname(path);
506
		if(stricmp(ptr,tmp) && stricmp(ptr,str))
507
			bprintf("%.*s\r\n%21s",LEN_FDESC,ptr,"");
508
	}
509

510
#endif
511

512
513
514
515
516
517
518
519
520
521
522
	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]);
523
524
			bprintf("%.*s",strlen(str)-(j+i),str+j+i); 
		}
525
526
		else
			bputs(str);
527
528
		CRLF; 
	}
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
	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
544
545
				break; 
		}
546
547
548
549
550
		if(!(*(cr+1)) || !(*(cr+2)))
			break;
		bprintf("%21s",nulstr);
		ptr=cr;
		if(!(*ptr)) ptr++;
551
552
		while(*ptr==LF || *ptr==CR) ptr++; 
	}
553
554
555
556
557
558
559
560
	return(true);
}

/****************************************************************************/
/* Remove credits from uploader of file 'f'                                 */
/****************************************************************************/
bool sbbs_t::removefcdt(file_t* f)
{
561
562
563
564
	char	str[128];
	char 	tmp[512];
	int		u;
	long	cdt;
565

566
	if((u=matchuser(&cfg,f->uler,TRUE /*sysop_alias*/))==0) {
567
	   bputs(text[UnknownUser]);
568
569
	   return(false); 
	}
570
571
572
573
574
575
576
577
578
579
580
581
	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]);
582
583
		putsmsg(&cfg,u,tmp); 
	}
584
585
586
587
588
589
590
591
592
593
	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]);
594
595
		putsmsg(&cfg,u,tmp); 
	}
596
597
598
599
600
601

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

602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
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);
}

619
620
621
622
623
/****************************************************************************/
/* Move file 'f' from f.dir to newdir                                       */
/****************************************************************************/
bool sbbs_t::movefile(file_t* f, int newdir)
{
624
	char str[MAX_PATH+1],path[MAX_PATH+1],fname[128],ext[1024];
625
626
627
628
	int olddir=f->dir;

	if(findfile(&cfg,newdir,f->name)) {
		bprintf(text[FileAlreadyThere],f->name);
629
630
		return(false); 
	}
631
	getextdesc(&cfg,olddir,f->datoffset,ext);
632
	if(cfg.dir[olddir]->misc&DIR_MOVENEW)
633
		f->dateuled=time32(NULL);
634
635
636
637
638
639
	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);
640
641
	sprintf(str,"%s moved %s to %s %s",f->name
		,useron.alias
642
643
644
645
		,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);
646
		if(fexistcase(str)) {
647
648
649
650
			sprintf(path,"%s%s",cfg.dir[f->dir]->path,getfname(str));
			mv(str,path,0); 
		} 
	}
651
	if(f->misc&FM_EXTDESC)
652
		putextdesc(&cfg,f->dir,f->datoffset,ext);
653
654
655
656
657
658
659
660
661
662
663
	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)
{
664
	char	ch,c,d,str[256],fname[128],*p,remcdt=0,remfile=0;
665
	char 	tmp[512];
666
	uint	i,j,ml=0,md=0,udir,ulib;
667
	file_t	f;
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686

	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=='?') {
687
			menu("batflag");
688
689
			if(lncntr)
				pause();
690
691
			return(2); 
		}
692
693
694
695
696
697
698
699
700
		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]);
701
702
				return(2); 
			}
703
704
705
706
707
708
709
710
			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;
711
712
				return(2); 
			}
713
714
715
716
717
718
719
720
721
722
723
			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);
724
725
						break; 
					}
726
727
					if(strchr(str+c,'.')) {     /* filename or spec given */
						f.dir=dirnum;
728
						p=strchr(str+c,' ');
729
730
731
732
733
						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);
734
735
								break; 
							}
736
737
738
739
740
741
							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);
742
743
744
745
								addtobatdl(&f); 
							} 
						} 
					}
746
747
748
749
750
751
752
753
					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);
754
755
						addtobatdl(&f); } 
				}
756
				CRLF;
757
758
				return(2); 
			}
759
			clearline();
760
761
			continue; 
		}
762
763
764
765
766
767
768
769
770
771
772
773

		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);
774
775
				return(2); 
			}
776
777
778
779
780
781
782
783
784
785
786
			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;
787
						p=strchr(str+c,' ');
788
789
790
791
792
793
794
795
796
797
798
799
						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'))
800
801
802
803
									return(-1); 
							} 
						} 
					}
804
805
806
807
808
809
810
811
812
813
814
					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'))
815
816
817
818
							return(-1); } 
				}
				return(2); 
			}
819
			clearline();
820
821
			continue; 
		}
822
823
824
825
826
827

		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");
828
829
				d=1; 
			}
830
831
			else {
				bputs(text[BatchDlFlags]);
832
833
				d=getstr(str,BF_MAX,K_UPPER|K_LOWPRIO|K_NOCRLF); 
			}
834
835
836
837
838
839
840
841
842
843
844
			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]);
845
846
						remfile=!noyes(text[DeleteFileQ]); } 
				}
847
848
849
850
851
852
				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);
853
					if((int)(ml=getnum(usrlibs))==-1)
854
855
856
857
858
859
860
861
862
863
864
						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]);
865
					if((int)(md=getnum(usrdirs[ml]))==-1)
866
867
868
869
						return(2);
					if(!md)
						md=usrdirs[ml]-1;
					else md--;
870
871
					CRLF; 
				}
872
873
874
875
				lncntr=0;
				for(c=0;c<d;c++) {
					if(strchr(str+c,'.')) {     /* filename or spec given */
						f.dir=dirnum;
876
						p=strchr(str+c,' ');
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
						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);
892
893
									continue; 
								}
894
								if(ch=='D') {
895
									removefile(&f);
896
897
									if(remfile) {
										sprintf(tmp,"%s%s",cfg.dir[f.dir]->path,fname);
898
899
										remove(tmp); 
									}
900
									if(remcdt)
901
902
										removefcdt(&f); 
								}
903
								else if(ch=='M')
904
905
906
907
									movefile(&f,usrdir[ml][md]); 
							} 
						} 
					}
908
909
910
911
912
913
914
915
916
917
918
919
920
921
					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);
922
923
							continue; 
						}
924
						if(ch=='D') {
925
							removefile(&f);
926
927
							if(remfile) {
								sprintf(tmp,"%s%s",cfg.dir[f.dir]->path,fname);
928
929
								remove(tmp); 
							}
930
							if(remcdt)
931
932
								removefcdt(&f); 
						}
933
						else if(ch=='M')
934
935
936
937
							movefile(&f,usrdir[ml][md]); } 
				}
				return(2); 
			}
938
			clearline();
939
940
			continue; 
		}
941

942
943
		return(1); 
	}
944
945
946
947
948
949
950
951
952
953
954
955

	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];
956
957
	char 	tmp[512];
	uchar	*ixbbuf,*usrxfrbuf=NULL,*p;
958
	int		file;
rswindell's avatar
rswindell committed
959
	int		error;
960
961
	int		found=0;
    uint	i,j;
962
	long	usrxfrlen=0;
963
964
965
966
    long	m,l;
	long	usrcdt;
    time_t	start,end,t;
    file_t	f;
967
	struct	tm tm;
968

969
	sprintf(str,"%sxfer.ixt",cfg.data_dir);
970
971
972
	if(mode==FI_USERXFER) {
		if(flength(str)<1L)
			return(0);
973
974
		if((file=nopen(str,O_RDONLY))==-1) {
			errormsg(WHERE,ERR_OPEN,str,O_RDONLY);
975
976
			return(0); 
		}
977
		usrxfrlen=(long)filelength(file);
deuce's avatar
deuce committed
978
		if((usrxfrbuf=(uchar *)malloc(usrxfrlen))==NULL) {
979
980
			close(file);
			errormsg(WHERE,ERR_ALLOC,str,usrxfrlen);
981
982
			return(0); 
		}
983
984
		if(read(file,usrxfrbuf,usrxfrlen)!=usrxfrlen) {
			close(file);
deuce's avatar
deuce committed
985
			free(usrxfrbuf);
986
			errormsg(WHERE,ERR_READ,str,usrxfrlen);
987
988
989
990
			return(0); 
		}
		close(file); 
	}
991
	sprintf(str,"%s%s.ixb",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
992
993
	if((file=nopen(str,O_RDONLY))==-1)
		return(0);
994
	l=(long)filelength(file);
995
996
	if(!l) {
		close(file);
997
998
		return(0); 
	}
deuce's avatar
deuce committed
999
	if((ixbbuf=(uchar *)malloc(l))==NULL) {
1000
1001
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,l);
1002
1003
		return(0); 
	}
1004
1005
1006
	if(lread(file,ixbbuf,l)!=l) {
		close(file);
		errormsg(WHERE,ERR_READ,str,l);
deuce's avatar
deuce committed
1007
		free((char *)ixbbuf);
1008
		if(usrxfrbuf)
deuce's avatar
deuce committed
1009
			free(usrxfrbuf);
1010
1011
		return(0); 
	}
1012
	close(file);
1013
	sprintf(str,"%s%s.dat",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
1014
1015
	if((file=nopen(str,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_READ,str,O_RDONLY);
deuce's avatar
deuce committed
1016
		free((char *)ixbbuf);
1017
		if(usrxfrbuf)
deuce's avatar
deuce committed
1018
			free(usrxfrbuf);
1019
1020
		return(0); 
	}
1021
1022
1023
1024
1025
1026
1027
1028
	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;
1029
1030
			break; 
		}
1031
		for(i=0;i<12 && m<l;i++)
1032
1033
			if(i==8)
				str[i]=ixbbuf[m]>' ' ? '.' : ' ';
1034
1035
1036
1037
1038
1039
			else
				str[i]=ixbbuf[m++];     /* Turns FILENAMEEXT into FILENAME.EXT */
		str[i]=0;
		unpadfname(str,fname);
		if(filespec[0] && !filematch(str,filespec)) {
			m+=11;
1040
1041
			continue; 
		}
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
		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))
1068
1069
					break; 
			}
1070
			if(p>=usrxfrbuf+usrxfrlen) /* file wasn't found */
1071
1072
				continue; 
		}
1073
		if((mode==FI_REMOVE) && (!dir_op(dirnum) && stricmp(f.uler
1074
1075
1076
1077
1078
1079
			,useron.alias) && !(useron.exempt&FLAG('R'))))
			continue;
		found++;
		if(mode==FI_INFO) {
			if(!viewfile(&f,1)) {
				done=1;
1080
1081
				found=-1; } 
		}
1082
1083
1084
1085
1086
		else
			fileinfo(&f);
		if(mode==FI_CLOSE) {
			if(!noyes(text[CloseFileRecordQ])) {
				f.opencount=0;
1087
1088
				putfiledat(&cfg,&f); } 
		}
1089
1090
1091
1092
1093
1094
		else if(mode==FI_REMOVE || mode==FI_OLD || mode==FI_OLDUL
			|| mode==FI_OFFLINE) {
			SYNC;
			CRLF;
			if(f.opencount) {
				mnemonics(text[QuitOrNext]);
1095
1096
				strcpy(str,"Q\r"); 
			}
1097
1098
			else if(dir_op(dirnum)) {
				mnemonics(text[SysopRemoveFilePrompt]);
1099
1100
				strcpy(str,"VEFMCQR\r"); 
			}
1101
1102
			else if(useron.exempt&FLAG('R')) {
				mnemonics(text[RExemptRemoveFilePrompt]);
1103
1104
				strcpy(str,"VEMQR\r"); 
			}
1105
1106
			else {
				mnemonics(text[UserRemoveFilePrompt]);
1107
1108
				strcpy(str,"VEQR\r"); 
			}
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
			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);
1121
1122
						if(!getstr(str,12,K_EDIT|K_AUTODEL))
							break;
1123
1124
						if(strcmp(str,fname)) { /* rename */
							padfname(str,path);
1125
1126
							if(stricmp(str,fname)
								&& findfile(&cfg,f.dir,path))
1127
1128
1129
1130
								bprintf(text[FileAlreadyThere],path);
							else {
								sprintf(path,"%s%s",dirpath,fname);
								sprintf(tmp,"%s%s",dirpath,str);
1131
								if(fexistcase(path) && rename