listfile.cpp 30.2 KB
Newer Older
1 2 3 4 5 6
/* Synchronet file database listing functions */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
23
#include "filedat.h"
24 25 26 27 28 29

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

int extdesclines(char *str);

/*****************************************************************************/
30 31
/* List files in directory 'dir' that match 'filespec'.						 */
/* 'mode' determines other criteria											 */
32 33 34 35
/* 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		 */
/*****************************************************************************/
36
int sbbs_t::listfiles(uint dirnum, const char *filespec, FILE* tofile, long mode)
37
{
38
	char	hdr[256],letter='A',*p;
39
	uchar	flagprompt=0;
40
	int		c, d;
41
	uint	i,j;
42 43 44 45 46
	int		found=0,lastbat=0,disp;
	size_t	m=0;
	long	anchor=0,next;
	file_t* bf[BF_MAX];	/* bf is batch flagged files */
	smb_t	smb;
47
	ulong	file_row[26];
48
	size_t	longest = 0;
49

50 51
	if(!smb_init_dir(&cfg, &smb, dirnum))
		return 0;
52
	if(mode&FL_ULTIME) {
53 54 55
		last_ns_time = now;
		if(!newfiles(&smb, ns_time))	// this is fast
			return 0;
56
	}
57 58 59 60 61 62 63 64 65 66 67 68 69 70
	if(smb_open_dir(&cfg, &smb, dirnum) != SMB_SUCCESS)
		return 0;

	size_t file_count = 0;
	file_t* file_list = loadfiles(&smb
		, (mode&(FL_FINDDESC|FL_EXFIND)) ? NULL : filespec
		, (mode&FL_ULTIME) ? ns_time : 0
		, file_detail_extdesc
		, (enum file_sort)cfg.dir[dirnum]->sort
		, &file_count);
	if(file_list == NULL || file_count < 1) {
		smb_close(&smb);
		free(file_list);
		return 0;
71
	}
72

73 74 75 76 77 78
	for(i = 0; i < file_count; i++) {
		size_t len = strlen(file_list[i].name);
		if(len > longest)
			longest = len;
	}

79 80 81 82
	if(!tofile) {
		action=NODE_LFIL;
		getnodedat(cfg.node_num,&thisnode,0);
		if(thisnode.action!=NODE_LFIL) {	/* was a sync */
83 84 85 86 87 88
			if(getnodedat(cfg.node_num,&thisnode,true)==0) {
				thisnode.action=NODE_LFIL;
				putnodedat(cfg.node_num,&thisnode); 
			} 
		} 
	}
89

90 91 92
	m = 0; // current file index
	file_t* f;
	while(online) {
93 94
		if(found<0)
			found=0;
95
		if(m>=file_count || flagprompt) {		  /* End of list */
96 97 98 99
			if(useron.misc&BATCHFLAG && !tofile && found && found!=lastbat
				&& !(mode&(FL_EXFIND|FL_VIEW))) {
				flagprompt=0;
				lncntr=0;
100
				if((i=batchflagprompt(&smb, bf, file_row, letter-'A', file_count))==2) {
101 102
					m=anchor;
					found-=letter-'A';
103 104
					letter='A'; 
				}
105
				else if(i==3) {
106
					if((long)anchor-(letter-'A')<0) {
107
						m=0;
108 109
						found=0; 
					}
110
					else {
111
						m=anchor-(letter-'A');
112 113 114 115
						found-=letter-'A'; 
					}
					letter='A'; 
				}
116
				else if((int)i==-1) {
117 118
					found = -1;
					break;
119
				}
120 121 122
				else
					break;
				getnodedat(cfg.node_num,&thisnode,0);
123 124
				nodesync(); 
			}
125
			else
126 127
				break; 
		}
128 129
		if(m < file_count)
			f = &file_list[m];
130 131 132 133 134 135 136

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

		if(msgabort()) {		 /* used to be !tofile && msgabort() */
137 138
			found = -1;
			break;
139
		}
140
#if 0 /* unnecessary? */
141 142 143
		if(!(mode&(FL_FINDDESC|FL_EXFIND)) && filespec[0]
			&& !filematch(str,filespec)) {
			m+=11;
144 145
			continue; 
		}
146
#endif
147
		if(mode&(FL_FINDDESC|FL_EXFIND)) {
148
			p = (f->desc == NULL) ? NULL : strcasestr(f->desc, filespec);
149
			if(!(mode&FL_EXFIND) && p==NULL) {
150
				m++;
151 152
				continue; 
			}
153 154 155
			if(mode&FL_EXFIND && f->extdesc != NULL) { /* search extended description */
				if(!strcasestr((char*)f->extdesc, filespec) && p == NULL) {	/* not in description or */
					m++;											 /* extended description */
156 157 158
					continue; 
				}
			}
159 160 161 162
			else if(p == NULL) {			 /* no extended description and not in desc */
				m++;
				continue; 
			} 
163
		}
164
/** necessary?
165 166 167 168
		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;
169 170
				continue; 
			}
171
		}
172
**/
173 174 175 176
		if(useron.misc&BATCHFLAG && letter=='A' && found && !tofile
			&& !(mode&(FL_EXFIND|FL_VIEW))
			&& (!mode || !(useron.misc&EXPERT)))
			bputs(text[FileListBatchCommands]);
177
		m++;
178 179 180 181 182 183 184 185 186
		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,'?')))) {
187
				sprintf(hdr,"%s%s.hdr",cfg.dir[dirnum]->data_dir,cfg.dir[dirnum]->code);
188
				if(fexistcase(hdr))
189 190 191 192 193 194 195 196 197 198 199
					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]);
200
						bputs("\xc9\xcd");            /* use to start with \r\n */
201
						for(c=0;c<d;c++)
202 203
							outchar('\xcd');
						bputs("\xbb\r\n\xba ");
204 205 206
						sprintf(hdr,text[BoxHdrLib],i+1,cfg.lib[usrlib[i]]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
207
							outchar(' ');
208
						attr(cfg.color[clr_filelsthdrbox]);
209
						bputs("\xba\r\n\xba ");
210 211 212
						sprintf(hdr,text[BoxHdrDir],j+1,cfg.dir[dirnum]->lname);
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
213
							outchar(' ');
214
						attr(cfg.color[clr_filelsthdrbox]);
215 216
						bputs("\xba\r\n\xba ");
						sprintf(hdr,text[BoxHdrFiles], file_count);
217 218
						bputs(hdr);
						for(c=bstrlen(hdr);c<d;c++)
219
							outchar(' ');
220
						attr(cfg.color[clr_filelsthdrbox]);
221
						bputs("\xba\r\n\xc8\xcd");
222
						for(c=0;c<d;c++)
223 224
							outchar('\xcd');
						bputs("\xbc\r\n"); 
225 226
					}
				}
227
			}
228 229
			else {					/* short header */
				if(tofile) {
230
					c = fprintf(tofile,"\r\n(%u) %s ",i+1,cfg.lib[usrlib[i]]->sname) - 2;
231
				}
232 233 234
				else {
					sprintf(hdr,text[ShortHdrLib],i+1,cfg.lib[usrlib[i]]->sname);
					bputs("\r\1>\r\n");
235
					bputs(hdr); 
236
					c=bstrlen(hdr);
237
				}
238
				if(tofile) {
239
					c += fprintf(tofile,"(%u) %s",j+1,cfg.dir[dirnum]->lname);
240
				}
241 242
				else {
					sprintf(hdr,text[ShortHdrDir],j+1,cfg.dir[dirnum]->lname);
243
					bputs(hdr); 
244
					c+=bstrlen(hdr);
245
				}
246
				if(tofile) {
247
					fprintf(tofile,"\r\n%.*s\r\n", c, "----------------------------------------------------------------");
248
				}
249 250 251 252
				else {
					CRLF;
					attr(cfg.color[clr_filelstline]);
					while(c--)
rswindell's avatar
rswindell committed
253
						outchar('\xC4');
254 255 256 257
					CRLF; 
				} 
			} 
		}
258
		long currow = row;
259 260 261 262 263
		next=m;
		disp=1;
		if(mode&(FL_EXFIND|FL_VIEW)) {
			if(!found)
				bputs("\r\1>");
264 265 266
			if(!viewfile(f, INT_TO_BOOL(mode&FL_EXFIND))) {
				found = -1;
				break;
267
			}
268
			CRLF;
269
		}
270
		else if(tofile)
271
			listfiletofile(f, tofile);
272
		else if(mode&FL_FINDDESC)
273
			disp=listfile(f, dirnum, filespec, letter, longest);
274
		else
275
			disp=listfile(f, dirnum, nulstr, letter, longest);
276
		if(!disp && letter>'A') {
277
			next=m-1;
278 279
			letter--; 
		}
280 281
		else {
			disp=1;
282 283
			found++; 
		}
284
		if(sys_status&SS_ABORT) {
285 286
			found = -1;
			break;
287
		}
288 289 290 291
		if(mode&(FL_EXFIND|FL_VIEW))
			continue;
		if(useron.misc&BATCHFLAG && !tofile) {
			if(disp) {
292
				bf[letter-'A'] = f;
293
				file_row[letter-'A'] = currow;
294
			}
295
			m++;
296 297 298 299 300 301 302 303
			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;
304
				if((int)(i=batchflagprompt(&smb, bf, file_row, letter-'A'+1, file_count))<1) {
305
					if((int)i==-1)
306 307
						found = -1;
					break;
308
				}
309 310
				if(i==2) {
					next=anchor;
311 312
					found-=(letter-'A')+1; 
				}
313
				else if(i==3) {
314
					if((long)anchor-((letter-'A'+1))<0) {
315
						next=0;
316 317
						found=0; 
					}
318
					else {
319 320 321
						next=anchor-((letter-'A'+1));
						found-=letter-'A'+1; 
					} 
322
				}
323 324
				getnodedat(cfg.node_num,&thisnode,0);
				nodesync();
325 326 327 328
				letter='A';	
			}
			else
				letter++; 
329
		}
330 331 332
		if(useron.misc&BATCHFLAG && !tofile
			&& lncntr>=rows-2) {
			lncntr=0;		/* defeat pause() */
333 334
			flagprompt=1; 
		}
335 336 337
		m=next;
		if(mode&FL_FINDDESC) continue;
		if(filespec[0] && !strchr(filespec,'*') && !strchr(filespec,'?') && m)
338 339
			break; 
	}
340

341 342 343
	freefiles(file_list, file_count);
	smb_close(&smb);
	return found;
344 345 346 347 348 349
}

/****************************************************************************/
/* Prints one file's information on a single line                           */
/* Return 1 if displayed, 0 otherwise										*/
/****************************************************************************/
350
bool sbbs_t::listfile(file_t* f, uint dirnum, const char *search, const char letter, size_t namelen)
351
{
352
	char	*ptr;
353 354
	bool	exist = true;
	char*	ext=NULL;
355
	char	path[MAX_PATH+1];
356
    int		i,j;
357
    off_t	cdt;
358
	int		size_attr=clr_filecdt;
359

360 361 362 363
	if(f->extdesc != NULL && *f->extdesc && (useron.misc&EXTDESC)) {
		ext = f->extdesc;
		if((useron.misc&BATCHFLAG) && lncntr+extdesclines(ext)>=rows-2 && letter!='A')
			return false;
364
	}
365

366
	cond_newline();
367
	attr(cfg.color[clr_filename]);
368 369 370 371 372 373
	char fname[SMB_FILEIDX_NAMELEN + 1];
	if(namelen < 12 || cols < 132)
		namelen = 12;
	else if(namelen > sizeof(fname) - 1)
		namelen = sizeof(fname) - 1;
	bprintf("%-*s", namelen, format_filename(f->name, fname, namelen, /* pad: */TRUE));
374
	getfilepath(&cfg, f, path);
375

376 377
	if(f->extdesc != NULL && *f->extdesc && !(useron.misc&EXTDESC))
		outchar('+');
378
	else
379
		outchar(' '); 
380 381
	if(useron.misc&BATCHFLAG) {
		attr(cfg.color[clr_filedesc]);
382 383
		bprintf("%c",letter); 
	}
384 385 386 387
	cdt = f->cost;
	if(f->size == -1) {
		exist = false;
		size_attr = clr_err; 
388
	}
389 390 391 392 393 394 395 396
	else if((cfg.dir[dirnum]->misc & (DIR_FREE | DIR_FCHK)) == (DIR_FREE | DIR_FCHK))
		cdt = getfilesize(&cfg, f);
	char bytes[32];
	unsigned units = 1;
	do {
		byte_estimate_to_str(cdt, bytes, sizeof(bytes), units, /* precision: */1);
		units *= 1024;
	} while(strlen(bytes) > 6 && units < 1024 * 1024 * 1024);
397
	attr(cfg.color[size_attr]);
398
	if(useron.misc&BATCHFLAG) {
399
		if(!cdt && !(cfg.dir[dirnum]->misc&DIR_FREE)) {
400
			attr(curatr^(HIGH|BLINK));
401 402
			bputs("  FREE"); 
		}
403 404
		else 
			bprintf("%6s", bytes);
405
	}
406
	else {
407
		if(!cdt && !(cfg.dir[dirnum]->misc&DIR_FREE)) {  /* FREE file */
408
			attr(curatr^(HIGH|BLINK));
409 410
			bputs("   FREE"); 
		}
411 412
		else 
			bprintf("%7s", bytes);
413
	}
414
	if(exist)
415
		outchar(' ');
416 417 418
	else
		outchar('-');
	attr(cfg.color[clr_filedesc]);
419

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	if(ext == NULL) {
		char* fdesc = f->desc;
		SKIP_WHITESPACE(fdesc);
		if(fdesc == NULL || *fdesc == '\0')
			bputs(P_TRUNCATE, f->name);
		else if(search[0]) { /* high-light string in string */
			ptr = strcasestr(fdesc, search);
			if(ptr != NULL) {
				i=strlen(search);
				j=ptr - fdesc;
				bprintf("%.*s",j,fdesc);
				attr(cfg.color[clr_filedesc]^HIGH);
				bprintf("%.*s",i,fdesc+j);
				attr(cfg.color[clr_filedesc]);
				bprintf("%.*s",(int)strlen(fdesc)-(j+i),fdesc+j+i);
			}
		}
		else {
			bputs(P_TRUNCATE, fdesc);
439 440
		}
		CRLF; 
441
	} else {
442 443 444 445 446 447 448
		truncsp(ext);
		while(strncmp(ext, "\r\n", 2) == 0
			|| strnicmp(ext, "\001N", 2) == 0
			|| strnicmp(ext, "\0010", 2) == 0
			|| strnicmp(ext, "\001W", 2) == 0)
			ext += 2;
		putmsg(ext, P_INDENT | P_NOATCODES | P_CPM_EOF | P_TRUNCATE);
449
	}
450
	return true;
451 452 453 454 455 456 457
}

/****************************************************************************/
/* 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. 														*/
/****************************************************************************/
458
int sbbs_t::batchflagprompt(smb_t* smb, file_t** bf, ulong* row, uint total
459 460
							,long totalfiles)
{
461
	char	ch,str[256],*p,remcdt=0,remfile=0;
462
	int		c, d;
463
	char 	path[MAX_PATH + 1];
464
	uint	i,j,ml=0,md=0,udir,ulib;
465 466

	for(ulib=0;ulib<usrlibs;ulib++)
467
		if(usrlib[ulib]==cfg.dir[smb->dirnum]->lib)
468 469
			break;
	for(udir=0;udir<usrdirs[ulib];udir++)
470
		if(usrdir[ulib][udir]==smb->dirnum)
471 472 473 474 475 476
			break;

	CRLF;
	while(online) {
		bprintf(text[BatchFlagPrompt]
			,ulib+1
477
			,cfg.lib[cfg.dir[smb->dirnum]->lib]->sname
478
			,udir+1
479
			,cfg.dir[smb->dirnum]->sname
480
			,total, totalfiles);
481 482 483
		ch=getkey(K_UPPER);
		clearline();
		if(ch=='?') {
484
			menu("batflag");
485 486
			if(lncntr)
				pause();
487 488
			return(2); 
		}
489
		if(ch==text[YNQP][2] || sys_status&SS_ABORT)
490 491 492 493 494
			return(-1);
		if(ch=='S')
			return(0);
		if(ch=='P' || ch=='-')
			return(3);
495
		if(ch=='B' || ch=='D') {    /* Flag for batch download */
496 497
			if(useron.rest&FLAG('D')) {
				bputs(text[R_Download]);
498 499
				return(2); 
			}
500
			if(total==1) {
501
				addtobatdl(bf[0]);
502
				if(ch=='D')
503
					start_batch_download();
504
				CRLF;
505 506
				return(2); 
			}
507 508 509 510
			link_list_t saved_hotspots = mouse_hotspots;
			ZERO_VAR(mouse_hotspots);
			for(i=0; i < total; i++)
				add_hotspot((char)('A' + i), /* hungry: */true, -1, -1, row[i]);
511
			bputs(text[BatchDlFlags]);
512
			d=getstr(str, BF_MAX, K_NOCRLF);
513 514
			clear_hotspots();
			mouse_hotspots = saved_hotspots;
515 516 517
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
518 519
			if(d > 0) { 	/* d is string length */
				strupr(str);
520 521 522
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
523
					if(batdn_total() >= cfg.max_batdn) {
524
						bprintf(text[BatchDlQueueIsFull],str+c);
525 526
						break; 
					}
527
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
528
//						f.dir=dirnum;
529
						p=strchr(str+c,' ');
530 531 532
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
533
							if(batdn_total() >= cfg.max_batdn) {
534
								bprintf(text[BatchDlQueueIsFull],str+c);
535 536
								break; 
							}
537 538
							if(filematch(bf[i]->name, str+c)) {
								addtobatdl(bf[i]); 
539 540 541
							} 
						} 
					}
542 543 544
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
545
						addtobatdl(bf[str[c]-'A']);
546
					} 
547
				}
548 549
				if(ch=='D')
					start_batch_download();
550
				CRLF;
551 552
				return(2); 
			}
553
			clearline();
554 555
			continue; 
		}
556 557 558

		if(ch=='E' || ch=='V') {    /* Extended Info */
			if(total==1) {
559
				if(!viewfile(bf[0], ch=='E'))
560
					return(-1);
561 562
				return(2); 
			}
563 564 565 566
			link_list_t saved_hotspots = mouse_hotspots;
			ZERO_VAR(mouse_hotspots);
			for(i=0; i < total; i++)
				add_hotspot((char)('A' + i), /* hungry: */true, -1, -1, row[i]);
567
			bputs(text[BatchDlFlags]);
568
			d=getstr(str, BF_MAX, K_NOCRLF);
569 570
			clear_hotspots();
			mouse_hotspots = saved_hotspots;
571 572 573
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
574 575
			if(d > 0) { 	/* d is string length */
				strupr(str);
576 577 578
				CRLF;
				lncntr=0;
				for(c=0;c<d;c++) {
579
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
580
//						f.dir=dirnum;
581
						p=strchr(str+c,' ');
582 583 584
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
585 586
							if(filematch(bf[i]->name, str+c)) {
								if(!viewfile(bf[i], ch=='E'))
587 588 589 590
									return(-1); 
							} 
						} 
					}
591 592 593
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
594 595 596
						if(!viewfile(bf[str[c]-'A'], ch=='E'))
							return(-1); 
					} 
597
				}
598
				cond_newline();
599 600
				return(2); 
			}
601
			clearline();
602 603
			continue; 
		}
604

605
		if((ch=='R' || ch=='M')     /* Delete or Move */
606
			&& !(useron.rest&FLAG('R'))
607
			&& (dir_op(smb->dirnum) || useron.exempt&FLAG('R'))) {
608 609
			if(total==1) {
				strcpy(str,"A");
610 611
				d=1; 
			}
612
			else {
613 614 615 616
				link_list_t saved_hotspots = mouse_hotspots;
				ZERO_VAR(mouse_hotspots);
				for(i=0; i < total; i++)
					add_hotspot((char)('A' + i), /* hungry: */true, -1, -1, row[i]);
617
				bputs(text[BatchDlFlags]);
618
				d=getstr(str, BF_MAX, K_NOCRLF);
619 620
				clear_hotspots();
				mouse_hotspots = saved_hotspots;
621
			}
622 623 624
			lncntr=0;
			if(sys_status&SS_ABORT)
				return(-1);
625 626 627 628
			if(d > 0) { 	/* d is string length */
				strupr(str);
				if(total > 1)
					newline();
629 630
				if(ch=='R') {
					if(noyes(text[RemoveFileQ]))
631
						return(2);
632 633 634 635
					remcdt = TRUE;
					remfile = TRUE;
					if(dir_op(smb->dirnum)) {
						remfile=!noyes(text[DeleteFileQ]);
636
						remcdt=!noyes(text[RemoveCreditsQ]);
637
					}
638
				}
639 640 641 642 643
				else if(ch=='M') {
					CRLF;
					for(i=0;i<usrlibs;i++)
						bprintf(text[MoveToLibLstFmt],i+1,cfg.lib[usrlib[i]]->lname);
					SYNC;
644
					bprintf(text[MoveToLibPrompt],cfg.dir[smb->dirnum]->lib+1);
645
					if((int)(ml=getnum(usrlibs))==-1)
646 647
						return(2);
					if(!ml)
648
						ml=cfg.dir[smb->dirnum]->lib;
649 650 651 652 653 654 655 656
					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]);
657
					if((int)(md=getnum(usrdirs[ml]))==-1)
658 659 660 661
						return(2);
					if(!md)
						md=usrdirs[ml]-1;
					else md--;
662 663
					CRLF; 
				}
664 665
				lncntr=0;
				for(c=0;c<d;c++) {
666
					if(str[c]=='*' || strchr(str+c,'.')) {     /* filename or spec given */
667
//						f.dir=dirnum;
668
						p=strchr(str+c,' ');
669 670 671
						if(!p) p=strchr(str+c,',');
						if(p) *p=0;
						for(i=0;i<total;i++) {
672
							if(filematch(bf[i]->name, str+c)) {
673
								if(ch=='R') {
674 675 676 677 678 679 680
									if(removefile(smb, bf[i])) {
										if(remfile) {
											if(remove(getfilepath(&cfg, bf[i], path)) != 0)
												errormsg(WHERE, ERR_REMOVE, path);
										}
										if(remcdt)
											removefcdt(bf[i]);
681 682
									}
								}
683
								else if(ch=='M')
684
									movefile(smb, bf[i], usrdir[ml][md]); 
685 686 687
							} 
						} 
					}
688 689 690
					if(strchr(str+c,'.'))
						c+=strlen(str+c);
					else if(str[c]<'A'+(char)total && str[c]>='A') {
691
						file_t* f = bf[str[c]-'A'];
692
						if(ch=='R') {
693 694 695 696 697 698 699
							if(removefile(smb, f)) {
								if(remfile) {
									if(remove(getfilepath(&cfg, f, path)) != 0 && fexist(path))
										errormsg(WHERE, ERR_REMOVE, path);
								}
								if(remcdt)
									removefcdt(f);
700 701
							}
						}
702
						else if(ch=='M')
703 704
							movefile(smb, f, usrdir[ml][md]); 
					} 
705 706 707
				}
				return(2); 
			}
708
			clearline();
709 710
			continue; 
		}
711

712 713
		return(1); 
	}
714 715 716 717 718 719 720 721 722

	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                */
/****************************************************************************/
723
int sbbs_t::listfileinfo(uint dirnum, const char *filespec, long mode)
724
{
725
	char	str[MAX_PATH + 1],path[MAX_PATH + 1],dirpath[MAX_PATH + 1],done=0,ch;
726
	char 	tmp[512];
rswindell's avatar
rswindell committed
727
	int		error;
728 729
	int		found=0;
    uint	i,j;
730
    size_t	m;
731
    time_t	start,end,t;
732
    file_t*	f;
733
	struct	tm tm;
734
	smb_t	smb;
735

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
	if(!smb_init_dir(&cfg, &smb, dirnum))
		return 0;
	if(smb_open_dir(&cfg, &smb, dirnum) != SMB_SUCCESS)
		return 0;

	size_t file_count = 0;
	file_t* file_list = loadfiles(&smb
		, filespec
		, /* time_t */0
		, file_detail_extdesc
		, (enum file_sort)cfg.dir[dirnum]->sort
		, &file_count);
	if(file_list == NULL || file_count < 1) {
		smb_close(&smb);
		free(file_list);
		return 0;
752
	}
753

754
	m=0;
755 756
	while(online && !done && m < file_count) {
		f = &file_list[m];
757 758 759 760 761
		if(mode==FI_REMOVE && dir_op(dirnum))
			action=NODE_SYSP;
		else action=NODE_LFIL;
		if(msgabort()) {
			found=-1;
762 763
			break; 
		}
764 765
		m++;
		if(mode==FI_OLD && f->hdr.last_downloaded > ns_time)
766
			continue;
767
		if((mode==FI_OLDUL || mode==FI_OLD) && f->hdr.when_written.time > ns_time)
768
			continue;
769 770
		curdirnum = dirnum;
		if(mode==FI_OFFLINE && getfilesize(&cfg, f) >= 0)
771
			continue;
772 773 774 775 776 777 778 779 780
		if(mode==FI_USERXFER) {
			str_list_t dest_user_list = strListSplitCopy(NULL, f->to_list, ",");
			char usernum[16];
			SAFEPRINTF(usernum, "%u", useron.number);
			int dest_user = strListFind(dest_user_list, usernum, /* case-sensitive: */true);
			strListFree(&dest_user_list);
			if(dest_user < 0)
				continue;
		}
781 782
		SAFECOPY(dirpath, cfg.dir[f->dir]->path);
		if((mode==FI_REMOVE) && (!dir_op(dirnum) && stricmp(f->from
783 784 785 786
			,useron.alias) && !(useron.exempt&FLAG('R'))))
			continue;
		found++;
		if(mode==FI_INFO) {
787 788 789 790 791 792 793 794 795 796 797
			switch(viewfile(f, true)) {
				case 0:
					done=1;
					found=-1;
					break;
				case -2:
					m--;
					if(m)
						m--;
					break;
			} 
798
		}
799 800 801
		else {
			showfileinfo(f, /* show_extdesc: */mode != FI_DOWNLOAD);
//			newline();
802
		}
803
		if(mode==FI_REMOVE || mode==FI_OLD || mode==FI_OLDUL
804 805
			|| mode==FI_OFFLINE) {
			SYNC;
806 807 808
//			CRLF;
			SAFECOPY(str, "VEQRNP\b-\r");
			if(dir_op(dirnum)) {
809
				mnemonics(text[SysopRemoveFilePrompt]);
810
				SAFECAT(str,"FMC");
811
			}
812 813
			else if(useron.exempt&FLAG('R')) {
				mnemonics(text[RExemptRemoveFilePrompt]);
814
				SAFECAT(str,"M");
815
			}