addfiles.c 24.5 KB
Newer Older
1
2
3
4
5
6
7
8
/* Program to add files to a Synchronet file database */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
9
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *																			*
 * 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"
37
#include <stdbool.h>
38

39
#define ADDFILES_VER "3.04"
40
41
42
43
44
45
46

scfg_t scfg;

int cur_altpath=0;

long files=0,removed=0,mode=0;

47
48
char lib[LEN_GSNAME+1];

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define DEL_LIST	(1L<<0)
#define NO_EXTEND	(1L<<1)
#define FILE_DATE	(1L<<2)
#define TODAYS_DATE (1L<<3)
#define FILE_ID 	(1L<<4)
#define NO_UPDATE	(1L<<5)
#define NO_NEWDATE	(1L<<6)
#define ASCII_ONLY	(1L<<7)
#define UL_STATS	(1L<<8)
#define ULDATE_ONLY (1L<<9)
#define KEEP_DESC	(1L<<10)
#define AUTO_ADD	(1L<<11)
#define SEARCH_DIR	(1L<<12)
#define SYNC_LIST	(1L<<13)
#define KEEP_SPACE	(1L<<14)
rswindell's avatar
rswindell committed
64
#define CHECK_DATE	(1L<<15)
65
66

/****************************************************************************/
67
/* This is needed by load_cfg.c												*/
68
/****************************************************************************/
69
int lprintf(int level, const char *fmat, ...)
70
71
{
	va_list argptr;
72
	char sbuf[512];
73
74
75
76
77
	int chcount;

	va_start(argptr,fmat);
	chcount=vsprintf(sbuf,fmat,argptr);
	va_end(argptr);
78
79
	truncsp(sbuf);
	printf("%s\n",sbuf);
80
81
82
	return(chcount);
}

83
void prep_desc(char *str)
84
85
86
87
88
{
	char tmp[1024];
	int i,j;

	for(i=j=0;str[i] && j < sizeof(tmp)-1;i++) {
89
		if(j && str[i]==' ' && tmp[j-1]==' ' && (mode&KEEP_SPACE))
90
			tmp[j++]=str[i];
91
		else if(j && str[i]<=' ' && str[i] > 0&& tmp[j-1]==' ')
92
			continue;
93
		else if(i && !isalnum((uchar)str[i]) && str[i]==str[i-1])
94
			continue;
95
		else if(str[i]>=' ' || str[i]<0)
96
97
			tmp[j++]=str[i];
		else if(str[i]==TAB || (str[i]==CR && str[i+1]==LF))
98
			tmp[j++]=' ';
99
100
101
102
103
	}
	tmp[j]=0;
	strcpy(str,tmp);
}

104
105
106
/*****************************************************************************/
/* Returns command line generated from instr with %c replacments             */
/*****************************************************************************/
107
char *mycmdstr(const char *instr, const char *fpath, const char *fspec, char *outstr)
108
109
110
111
{
    static char cmd[MAX_PATH+1];
    char str[MAX_PATH+1];
    int i,j,len;
rswindell's avatar
rswindell committed
112
113
114
#ifdef _WIN32
	char sfpath[MAX_PATH+1];
#endif
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

	len=strlen(instr);
	for(i=j=0;i<len && j<128;i++) {
		if(instr[i]=='%') {
			i++;
			cmd[j]=0;
			switch(toupper(instr[i])) {
				case 'F':   /* File path */
					strcat(cmd,fpath);
					break;
				case '~':	/* DOS-compatible (8.3) filename */
#ifdef _WIN32
					SAFECOPY(sfpath,fpath);
					GetShortPathName(fpath,sfpath,sizeof(sfpath));
					strcat(cmd,sfpath);
#else
                    strcat(cmd,fpath);
132
#endif
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
					break;
				case 'G':   /* Temp directory */
					strcat(cmd,scfg.temp_dir);
					break;
                case 'J':
                    strcat(cmd,scfg.data_dir);
                    break;
                case 'K':
                    strcat(cmd,scfg.ctrl_dir);
                    break;
				case 'N':   /* Node Directory (same as SBBSNODE environment var) */
					strcat(cmd,scfg.node_dir);
					break;
				case 'S':   /* File Spec */
					strcat(cmd,fspec);
					break;
                case 'Z':
                    strcat(cmd,scfg.text_dir);
                    break;
				case '!':   /* EXEC Directory */
					strcat(cmd,scfg.exec_dir);
					break;
155
156
157
158
159
                case '@':   /* EXEC Directory for DOS/OS2/Win32, blank for Unix */
#ifndef __unix__
                    strcat(cmd,scfg.exec_dir);
#endif
                    break;
160
161
162
163
164
165
166
167
				case '#':   /* Node number (same as SBBSNNUM environment var) */
					sprintf(str,"%d",scfg.node_num);
					strcat(cmd,str);
					break;
				case '%':   /* %% for percent sign */
					strcat(cmd,"%");
					break;
				default:    /* unknown specification */
168
					break;
169
			}
170
			j=strlen(cmd);
171
172
		}
		else
173
			cmd[j++]=instr[i];
174
175
176
177
178
179
180
181
182
183
184
185
186
	}
	cmd[j]=0;

	return(cmd);
}

/****************************************************************************/
/* Updates dstst.dab file													*/
/****************************************************************************/
void updatestats(ulong size)
{
    char	str[MAX_PATH+1];
    int		file;
deuce's avatar
64-bit    
deuce committed
187
	uint32_t	l;
188

189
	sprintf(str,"%sdsts.dab",scfg.ctrl_dir);
190
	if((file=nopen(str,O_RDWR|O_BINARY))==-1) {
191
		printf("ERR_OPEN %s\n",str);
192
		return;
193
194
195
196
197
198
199
200
201
202
203
204
205
	}
	lseek(file,20L,SEEK_SET);	/* Skip timestamp, logons and logons today */
	read(file,&l,4);			/* Uploads today		 */
	l++;
	lseek(file,-4L,SEEK_CUR);
	write(file,&l,4);
	read(file,&l,4);			/* Upload bytes today	 */
	l+=size;
	lseek(file,-4L,SEEK_CUR);
	write(file,&l,4);
	close(file);
}

206
bool get_file_diz(file_t* f, const char* filepath, char* ext)
207
208
209
{
	int i,file;
	char tmp[MAX_PATH+1];
210
	char tmpext[513];
211
212

	for(i=0;i<scfg.total_fextrs;i++)
213
214
		if(!stricmp(scfg.fextr[i]->ext,f->name+9)
			&& chk_ar(&scfg,scfg.fextr[i]->ar,/* user: */NULL, /* client: */NULL))
215
			break;
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	// If we could not find an extractor which matches our requirements, use any
	if(i >= scfg.total_fextrs) {
		for(i=0;i<scfg.total_fextrs;i++)
			if(!stricmp(scfg.fextr[i]->ext,f->name+9))
				break;
	}
	if(i >= scfg.total_fextrs)
		return false;

	SAFEPRINTF(tmp,"%sFILE_ID.DIZ",scfg.temp_dir);
	removecase(tmp);
	system(mycmdstr(scfg.fextr[i]->cmd,filepath,"FILE_ID.DIZ",NULL));
	if(!fexistcase(tmp)) {
		SAFEPRINTF(tmp,"%sDESC.SDI",scfg.temp_dir);
230
		removecase(tmp);
231
232
		system(mycmdstr(scfg.fextr[i]->cmd,filepath,"DESC.SDI",NULL));
		fexistcase(tmp);
233
	}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

	if((file=nopen(tmp,O_RDONLY|O_BINARY)) == -1)
		return false;

	memset(ext,0,513);
	read(file,ext,512);
	for(i=512;i;i--)
		if(ext[i-1]>' ' || ext[i-1]<0)
			break;
	ext[i]=0;
	if(mode&ASCII_ONLY)
		strip_exascii(ext, ext);
	if(!(mode&KEEP_DESC)) {
		sprintf(tmpext,"%.256s",ext);
		prep_desc(tmpext);
		for(i=0;tmpext[i];i++)
			if(isalpha((uchar)tmpext[i]))
				break;
		sprintf(f->desc,"%.*s",LEN_FDESC,tmpext+i);
		for(i=0;(f->desc[i]>=' ' || f->desc[i]<0) && i<LEN_FDESC;i++)
			;
		f->desc[i]=0; }
	close(file);
	f->misc|=FM_EXTDESC;
	return true;
259
260
}

261
262
void addlist(char *inpath, file_t f, uint dskip, uint sskip)
{
263
264
265
266
267
268
	char str[MAX_PATH+1];
	char tmp[MAX_PATH+1];
	char fname[MAX_PATH+1];
	char listpath[MAX_PATH+1];
	char filepath[MAX_PATH+1];
	char curline[256],nextline[256];
269
	char *p;
270
271
	char ext[1024];
	int i;
272
	long l;
273
	BOOL exist;
274
275
276
277
278
	FILE *stream;
	DIR*	dir;
	DIRENT*	dirent;

	if(mode&SEARCH_DIR) {
279
		SAFECOPY(str,cur_altpath ? scfg.altpath[cur_altpath-1] : scfg.dir[f.dir]->path);
280
281
282
283
		printf("Searching %s\n\n",str);
		dir=opendir(str);

		while(dir!=NULL && (dirent=readdir(dir))!=NULL) {
rswindell's avatar
rswindell committed
284
285
286
			sprintf(tmp,"%s%s"
				,cur_altpath ? scfg.altpath[cur_altpath-1] : scfg.dir[f.dir]->path
				,dirent->d_name);
287
288
289
290
291
			if(isdir(tmp))
				continue;
#ifdef _WIN32
			GetShortPathName(tmp, filepath, sizeof(filepath));
#else
292
			SAFECOPY(filepath,tmp);
293
#endif
294
295
			f.misc=0;
			f.desc[0]=0;
296
			memset(ext, 0, sizeof(ext));
297
			f.cdt=flength(filepath);
rswindell's avatar
rswindell committed
298
			time_t file_timestamp = fdate(filepath);
299
			padfname(getfname(filepath),f.name);
deuce's avatar
deuce committed
300
			printf("%s  %10"PRIu32"  %s\n"
rswindell's avatar
rswindell committed
301
				,f.name,f.cdt,unixtodstr(&scfg,(time32_t)file_timestamp,str));
302
303
304
305
			exist=findfile(&scfg,f.dir,f.name);
			if(exist) {
				if(mode&NO_UPDATE)
					continue;
306
307
308
309
310
311
				if(!getfileixb(&scfg,&f)) {
					fprintf(stderr, "!ERROR reading index of directory %u\n", f.dir);
					continue;
				}
				if((mode&CHECK_DATE) && file_timestamp <= f.dateuled)
					continue;
312
				if(mode&ULDATE_ONLY) {
313
					f.dateuled=time32(NULL);
314
					update_uldate(&scfg, &f);
315
316
					continue;
				}
317
318
				if(f.misc & FM_EXTDESC)
					getextdesc(&scfg, f.dir, f.datoffset, ext);
319
320
321
			}

			if(mode&FILE_DATE) {		/* get the file date and put into desc */
rswindell's avatar
rswindell committed
322
				unixtodstr(&scfg,(time32_t)file_timestamp,f.desc);
323
				strcat(f.desc,"  ");
324
325
326
			}

			if(mode&TODAYS_DATE) {		/* put today's date in desc */
327
				unixtodstr(&scfg,time32(NULL),f.desc);
328
				strcat(f.desc,"  ");
329
330
			}

331
			if(mode&FILE_ID)
332
				get_file_diz(&f, filepath, ext);
333

334
			f.dateuled=time32(NULL);
335
			f.altpath=cur_altpath;
336
			prep_desc(f.desc);
337
			if(mode&ASCII_ONLY)
338
				strip_exascii(f.desc, f.desc);
339
340
341
			if(exist) {
				putfiledat(&scfg,&f);
				if(!(mode&NO_NEWDATE))
342
					update_uldate(&scfg, &f);
343
			}
344
345
346
347
			else
				addfiledat(&scfg,&f);
			if(f.misc&FM_EXTDESC) {
				truncsp(ext);
348
				putextdesc(&scfg,f.dir,f.datoffset,ext);
349
			}
350
351
			if(mode&UL_STATS)
				updatestats(f.cdt);
352
			files++;
353
354
355
		}
		if(dir!=NULL)
			closedir(dir);
356
		return;
357
358
359
	}


360
	SAFECOPY(listpath,inpath);
361
	fexistcase(listpath);
362
	if((stream=fopen(listpath,"r"))==NULL) {
363
364
		fprintf(stderr,"Error %d (%s) opening %s\n"
			,errno,strerror(errno),listpath);
365
366
		sprintf(listpath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[f.dir]->path,inpath);
367
		fexistcase(listpath);
368
		if((stream=fopen(listpath,"r"))==NULL) {
369
370
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
371
372
			return;
		}
373
	}
374
375
376
377
378

	printf("Adding %s to %s %s\n\n"
		,listpath,scfg.lib[scfg.dir[f.dir]->lib]->sname,scfg.dir[f.dir]->sname);

	fgets(nextline,255,stream);
379
	do {
380
381
		f.misc=0;
		f.desc[0]=0;
382
		memset(ext, 0, sizeof(ext));
383
		SAFECOPY(curline,nextline);
384
385
386
		nextline[0]=0;
		fgets(nextline,255,stream);
		truncsp(curline);
387
		if(curline[0]<=' ' || (mode&ASCII_ONLY && (uchar)curline[0]>=0x7e))
388
389
			continue;
		printf("%s\n",curline);
390
		SAFECOPY(fname,curline);
391

392
#if 0	/* Files without dots are valid on modern systems */
393
394
395
		p=strchr(fname,'.');
		if(!p || p==fname || p>fname+8)    /* no dot or invalid dot location */
			continue;
396
397
#endif
		p=strchr(fname,' ');
398
399
400
401
		if(p) *p=0;
		else				   /* no space after filename? */
			continue;

402
403
404
		if(!isalnum(*fname)) {	// filename doesn't begin with an alpha-numeric char?
			continue;
		}
405
406
407
408
409
410
411
412
413
414
415
		sprintf(filepath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
			: scfg.dir[f.dir]->path,fname);

#ifdef _WIN32
		{
			char shortpath[MAX_PATH+1];
			GetShortPathName(filepath, shortpath, sizeof(shortpath));
			SAFECOPY(fname, getfname(shortpath));
		}
#endif

416
417
418
419
420
		padfname(fname,f.name);
		if(strcspn(f.name,"\\/|<>+[]:=\";,")!=strlen(f.name))
			continue;

		for(i=0;i<12;i++)
421
			if(f.name[i]<' ' || (mode&ASCII_ONLY && (uchar)f.name[i]>0x7e))
422
423
424
425
				break;

		if(i<12)					/* Ctrl chars or EX-ASCII in filename? */
			continue;
rswindell's avatar
rswindell committed
426
		time_t file_timestamp = fdate(filepath);
427
428
429
430
		exist=findfile(&scfg,f.dir,f.name);
		if(exist) {
			if(mode&NO_UPDATE)
				continue;
431
432
433
434
435
436
			if(!getfileixb(&scfg,&f)) {
				fprintf(stderr, "!ERROR reading index of directory %u\n", f.dir);
				continue;
			}
			if((mode&CHECK_DATE) && file_timestamp <= f.dateuled)
				continue;
437
			if(mode&ULDATE_ONLY) {
438
				f.dateuled=time32(NULL);
439
				update_uldate(&scfg, &f);
440
441
				continue;
			}
442
443
			if (f.misc & FM_EXTDESC)
				getextdesc(&scfg, f.dir, f.datoffset, ext);
444
		}
445
446

		if(mode&FILE_DATE) {		/* get the file date and put into desc */
rswindell's avatar
rswindell committed
447
			unixtodstr(&scfg,(time32_t)file_timestamp,f.desc);
448
			strcat(f.desc,"  ");
449
		}
450
451

		if(mode&TODAYS_DATE) {		/* put today's date in desc */
452
			l=time32(NULL);
453
			unixtodstr(&scfg,l,f.desc);
454
			strcat(f.desc,"  ");
455
		}
456
457
458

		if(dskip && strlen(curline)>=dskip) p=curline+dskip;
		else {
459
460
			p = curline;
			FIND_WHITESPACE(p);
461
			SKIP_WHITESPACE(p);
462
		}
463
464
465
		SAFECOPY(tmp,p);
		prep_desc(tmp);
		sprintf(f.desc+strlen(f.desc),"%.*s",(int)(LEN_FDESC-strlen(f.desc)),tmp);
466

467
		if(nextline[0]==' ' || strlen(p)>LEN_FDESC) {	/* ext desc */
468
			if(!(mode&NO_EXTEND)) {
469
				memset(ext, 0, sizeof(ext));
470
				f.misc|=FM_EXTDESC;
471
				sprintf(ext,"%s\r\n",p);
472
			}
473

474
			if(nextline[0]==' ') {
475
				SAFECOPY(str,nextline);				   /* tack on to end of desc */
476
				p=str+dskip;
477
				while(*p>0 && *p<=' ') p++;
478
479
480
481
482
483
				i=LEN_FDESC-strlen(f.desc);
				if(i>1) {
					p[i-1]=0;
					truncsp(p);
					if(p[0]) {
						strcat(f.desc," ");
484
485
486
						strcat(f.desc,p);
					}
				}
487
			}
488
489

			while(!feof(stream) && !ferror(stream) && strlen(ext)<512) {
490
				if(nextline[0]!=' ')
491
492
493
494
495
496
					break;
				truncsp(nextline);
				printf("%s\n",nextline);
				if(!(mode&NO_EXTEND)) {
					f.misc|=FM_EXTDESC;
					p=nextline+dskip;
497
					while(*p==' ') p++;
498
					strcat(ext,p);
499
					strcat(ext,"\r\n");
500
				}
501
				nextline[0]=0;
502
503
				fgets(nextline,255,stream);
			}
504
		}
505
506


507
508
509
510
		l=flength(filepath);
		if(l<0L) {
			printf("%s not found.\n",filepath);
			continue;
511
		}
512
513
514
515
516
		if(l == 0L) {
			printf("%s is a zero length file.\n",filepath);
			continue;
		}
		if(sskip) l=atol(fname+sskip);
517

518
		if(mode&FILE_ID)
519
			get_file_diz(&f, filepath, ext);
520
521

		f.cdt=l;
522
		f.dateuled=time32(NULL);
523
		f.altpath=cur_altpath;
524
		prep_desc(f.desc);
525
		if(mode&ASCII_ONLY)
526
			strip_exascii(f.desc, f.desc);
527
528
529
		if(exist) {
			putfiledat(&scfg,&f);
			if(!(mode&NO_NEWDATE))
530
				update_uldate(&scfg, &f);
531
		}
532
533
534
535
		else
			addfiledat(&scfg,&f);
		if(f.misc&FM_EXTDESC) {
			truncsp(ext);
536
			putextdesc(&scfg,f.dir,f.datoffset,ext);
537
		}
538
539
540

		if(mode&UL_STATS)
			updatestats(l);
541
		files++;
542
	} while(!feof(stream) && !ferror(stream));
543
544
545
	fclose(stream);
	if(mode&DEL_LIST && !(mode&SYNC_LIST)) {
		printf("\nDeleting %s\n",listpath);
546
		remove(listpath);
547
	}
548
549
550
551
552

}

void synclist(char *inpath, int dirnum)
{
553
	char	str[1024];
554
555
556
	char	fname[MAX_PATH+1];
	char	listpath[MAX_PATH+1];
	uchar*	ixbbuf;
557
	char*	p;
558
559
560
561
	int		i,file,found;
	long	l,m,length;
	FILE*	stream;
	file_t	f;
562

563
	sprintf(str,"%s%s.ixb",scfg.dir[dirnum]->data_dir,scfg.dir[dirnum]->code);
564
	if((file=nopen(str,O_RDONLY|O_BINARY))==-1) {
565
		printf("ERR_OPEN %s\n",str);
566
		return;
567
	}
568
569
570
	length=filelength(file);
	if(length%F_IXBSIZE) {
		close(file);
rswindell's avatar
rswindell committed
571
		printf("ERR_LEN (%ld) of %s\n",length,str);
572
		return;
573
	}
deuce's avatar
deuce committed
574
	if((ixbbuf=(uchar *)malloc(length))==NULL) {
575
576
		close(file);
		printf("ERR_ALLOC %s\n",str);
577
		return;
578
	}
579
580
	if(lread(file,ixbbuf,length)!=length) {
		close(file);
deuce's avatar
deuce committed
581
		free((char *)ixbbuf);
582
		printf("ERR_READ %s\n",str);
583
		return;
584
	}
585
586
	close(file);

587
	SAFECOPY(listpath,inpath);
588
	if((stream=fopen(listpath,"r"))==NULL) {
589
590
		sprintf(listpath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[dirnum]->path,inpath);
591
		if((stream=fopen(listpath,"r"))==NULL) {
592
593
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
594
595
			return;
		}
596
	}
597
598
599
600
601
602
603
604

	printf("\nSynchronizing %s with %s %s\n\n"
		,listpath,scfg.lib[scfg.dir[dirnum]->lib]->sname,scfg.dir[dirnum]->sname);

	for(l=0;l<length;l+=F_IXBSIZE) {
		m=l;
		for(i=0;i<12 && l<length;i++)
			if(i==8)
605
				str[i]=ixbbuf[m]>' ' ? '.' : ' ';
606
607
608
609
610
611
612
613
614
615
			else
				str[i]=ixbbuf[m++]; 	/* Turns FILENAMEEXT into FILENAME.EXT */
		str[i]=0;
		unpadfname(str,fname);
		rewind(stream);
		found=0;
		while(!found) {
			if(!fgets(str,1000,stream))
				break;
			truncsp(str);
616
			p=strchr(str,' ');
617
618
			if(p) *p=0;
			if(!stricmp(str,fname))
619
				found=1;
620
		}
621
622
623
624
625
626
		if(found)
			continue;
		padfname(fname,f.name);
		printf("%s not found in list - ",f.name);
		f.dir=dirnum;
		f.datoffset=ixbbuf[m]|((long)ixbbuf[m+1]<<8)|((long)ixbbuf[m+2]<<16);
627
628
629
630
		if(!getfiledat(&scfg,&f)) {
			fprintf(stderr, "!ERROR reading index of directory %u\n", f.dir);
			continue;
		}
631
632
		if(f.opencount) {
			printf("currently OPEN by %u users\n",f.opencount);
633
			continue;
634
		}
635
		removefiledat(&scfg,&f);
636
		if(remove(getfilepath(&scfg,&f,str)))
637
638
			printf("Error removing %s\n",str);
		removed++;
639
		printf("Removed from database\n");
640
	}
641
642
643

	if(mode&DEL_LIST) {
		printf("\nDeleting %s\n",listpath);
644
		remove(listpath);
645
	}
646
647
}

648
char *usage="\nusage: addfiles code [.alt_path] [-opts] +list "
649
			 "[desc_off] [size_off]"
650
	"\n   or: addfiles code [.alt_path] [-opts] file "
651
652
		"\"description\"\n"
	"\navailable opts:"
653
654
655
656
657
658
659
660
661
662
	"\n      -a         import ASCII only (no extended ASCII)"
	"\n      -b         synchronize database with file list (use with caution)"
	"\n      -c         do not remove extra spaces from file description"
	"\n      -d         delete list after import"
	"\n      -e         do not import extended descriptions"
	"\n      -f         include file date in descriptions"
	"\n      -t         include today's date in descriptions"
	"\n      -i         include added files in upload statistics"
	"\n      -n         do not update information for existing files"
	"\n      -o         update upload date only for existing files"
rswindell's avatar
rswindell committed
663
	"\n      -p         compare file date with upload date for existing files"
664
665
666
667
668
669
	"\n      -u         do not update upload date for existing files"
	"\n      -z         check for and import FILE_ID.DIZ and DESC.SDI"
	"\n      -k         keep original short description (not DIZ)"
	"\n      -s         search directory for files (no file list)"
	"\n      -l <lib>   specify library (short name) to Auto-ADD"
	"\n      -x <name>  specify uploader's user name (may require quotes)"
670
	"\n"
671
672
	"\nAuto-ADD:   use - in place of code for Auto-ADD of FILES.BBS"
	"\n            use -filename to Auto-ADD a different filename"
673
	"\n            use -l \"libname\" to only Auto-ADD files to a specific library"
674
675
676
677
678
679
680
681
	;

/*********************/
/* Entry point (duh) */
/*********************/
int main(int argc, char **argv)
{
	char error[512];
682
	char revision[16];
683
684
	char str[MAX_PATH+1];
	char tmp[MAX_PATH+1];
685
686
	char *p;
	char exist,listgiven=0,namegiven=0,ext[513]
687
		,auto_name[MAX_PATH+1]="FILES.BBS";
688
	int i,j;
689
	uint desc_offset=0, size_offset=0;
690
691
692
	long l;
	file_t	f;

693
	sscanf("$Revision$", "%*s %s", revision);
694

695
	fprintf(stderr,"\nADDFILES v%s-%s (rev %s) - Adds Files to Synchronet "
696
697
698
		"Filebase\n"
		,ADDFILES_VER
		,PLATFORM_DESC
699
		,revision
700
701
702
		);

	if(argc<2) {
703
		puts(usage);
704
		return(1);
705
	}
706
707
708
709
710

	p=getenv("SBBSCTRL");
	if(p==NULL) {
		printf("\nSBBSCTRL environment variable not set.\n");
		printf("\nExample: SET SBBSCTRL=/sbbs/ctrl\n");
711
		exit(1);
712
713
	}

714
715
	memset(&scfg,0,sizeof(scfg));
	scfg.size=sizeof(scfg);
716
	SAFECOPY(scfg.ctrl_dir,p);
717
718
719
720
721

	if(chdir(scfg.ctrl_dir)!=0)
		fprintf(stderr,"!ERROR changing directory to: %s", scfg.ctrl_dir);

	printf("\nLoading configuration files from %s\n",scfg.ctrl_dir);
722
723
724
725
	if(!load_cfg(&scfg,NULL,TRUE,error)) {
		fprintf(stderr,"!ERROR loading configuration files: %s\n",error);
		exit(1);
	}
rswindell's avatar
rswindell committed
726
727
	SAFECOPY(scfg.temp_dir,"../temp");
	prep_dir(scfg.ctrl_dir, scfg.temp_dir, sizeof(scfg.temp_dir));
728

729
730
	if(argv[1][0]=='*' || argv[1][0]=='-') {
		if(argv[1][1]=='?') {
731
			puts(usage);
732
733
			exit(0);
		}
734
		if(argv[1][1])
735
			SAFECOPY(auto_name,argv[1]+1);
736
		mode|=AUTO_ADD;
737
		i=0;
738
	} else {
739
		if(!isalnum((uchar)argv[1][0]) && argc==2) {
740
			puts(usage);
741
			return(1);
742
743
		}

744
		for(i=0;i<scfg.total_dirs;i++)
745
			if(!stricmp(scfg.dir[i]->code,argv[1])) /* use matchmatchi() instead? */
746
747
748
749
				break;

		if(i>=scfg.total_dirs) {
			printf("Directory code '%s' not found.\n",argv[1]);
750
751
			exit(1);
		}
752
	}
753
754
755

	memset(&f,0,sizeof(file_t));
	f.dir=i;
756
	SAFECOPY(f.uler,"-> ADDFILES <-");
757
758

	for(j=2;j<argc;j++) {
759
760
		if(argv[j][0]=='*')     /* set the uploader name (legacy) */
			SAFECOPY(f.uler,argv[j]+1);
761
		else
762
763
764
			if(argv[j][0]=='-'
			|| argv[j][0]=='/'
			) {      /* options */
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
			for(i=1;argv[j][i];i++)
				switch(toupper(argv[j][i])) {
					case 'A':
						mode|=ASCII_ONLY;
						break;
					case 'B':
						mode|=(SYNC_LIST|NO_UPDATE);
						break;
					case 'C':
						mode|=KEEP_SPACE;
						break;
					case 'D':
						mode|=DEL_LIST;
						break;
					case 'E':
						mode|=NO_EXTEND;
						break;
					case 'I':
						mode|=UL_STATS;
						break;
785
786
787
					case 'L':
						j++;
						if(argv[j]==NULL) {
788
							puts(usage);
789
790
791
792
793
							return(-1);
						}
						SAFECOPY(lib,argv[j]);
						i=strlen(argv[j])-1;
						break;
794
795
796
					case 'X':
						j++;
						if(argv[j]==NULL) {
797
							puts(usage);
798
799
800
801
802
							return(-1);
						}
						SAFECOPY(f.uler,argv[j]);
						i=strlen(argv[j])-1;
						break;
803
804
805
806
807
808
809
810
811
812
813
814
					case 'Z':
						mode|=FILE_ID;
						break;
					case 'K':
						mode|=KEEP_DESC;	 /* Don't use DIZ for short desc */
						break;
					case 'N':
						mode|=NO_UPDATE;
						break;
					case 'O':
						mode|=ULDATE_ONLY;
						break;
rswindell's avatar
rswindell committed
815
816
817
					case 'P':
						mode|=CHECK_DATE;
						break;
818
819
820
821
822
823
824
825
826
827
828
829
830
					case 'U':
						mode|=NO_NEWDATE;
						break;
					case 'F':
						mode|=FILE_DATE;
						break;
					case 'T':
						mode|=TODAYS_DATE;
						break;
					case 'S':
						mode|=SEARCH_DIR;
						break;
					default:
831
						puts(usage);
832
833
						return(1);
			}
834
		}
835
		else if(isdigit((uchar)argv[j][0])) {
836
837
838
839
840
841
			if(desc_offset==0)
				desc_offset=atoi(argv[j]);
			else
				size_offset=atoi(argv[j]);
			continue;
		}
842
843
		else if(argv[j][0]=='+') {      /* filelist - FILES.BBS */
			listgiven=1;
rswindell's avatar
rswindell committed
844
			if(argc > j+1
845
				&& isdigit((uchar)argv[j+1][0])) { /* skip x characters before description */
rswindell's avatar
rswindell committed
846
				if(argc > j+2
847
					&& isdigit((uchar)argv[j+2][0])) { /* skip x characters before size */
848
					addlist(argv[j]+1,f,atoi(argv[j+1]),atoi(argv[j+2]));
849
					j+=2;
850
				}
851
852
				else {
					addlist(argv[j]+1,f,atoi(argv[j+1]),0);
853
854
					j++;
				}
855
			}
856
857
858
			else
				addlist(argv[j]+1,f,0,0);
			if(mode&SYNC_LIST)
859
				synclist(argv[j]+1,f.dir);
860
		}
861
862
863
864
		else if(argv[j][0]=='.') {      /* alternate file path */
			cur_altpath=atoi(argv[j]+1);
			if(cur_altpath>scfg.altpaths) {
				printf("Invalid alternate path.\n");
865
866
				exit(1);
			}
867
		}
868
869
870
871
		else {
			namegiven=1;
			padfname(argv[j],f.name);
			f.desc[0]=0;
872
			memset(ext, 0, sizeof(ext));
873
#if 0
874
			strupr(f.name);
875
#endif
876
877
			if(j+1==argc) {
				printf("%s no description given.\n",f.name);
878
				continue;
879
			}
880
881
882
			sprintf(str,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[f.dir]->path,argv[j]);
			if(mode&FILE_DATE)
883
				sprintf(f.desc,"%s  ",unixtodstr(&scfg,(time32_t)fdate(str),tmp));
884
			if(mode&TODAYS_DATE)
885
				sprintf(f.desc,"%s  ",unixtodstr(&scfg,time32(NULL),tmp));
rswindell's avatar
rswindell committed
886
			sprintf(tmp,"%.*s",(int)(LEN_FDESC-strlen(f.desc)),argv[++j]);
887
			SAFECOPY(f.desc,tmp);
888
889
890
			l=flength(str);
			if(l==-1) {
				printf("%s not found.\n",str);
891
				continue;
892
			}
893
894
895
896
			exist=findfile(&scfg,f.dir,f.name);
			if(exist) {
				if(mode&NO_UPDATE)
					continue;
897
898
899
900
901
902
				if(!getfileixb(&scfg,&f)) {
					fprintf(stderr, "!ERROR reading index of directory %u\n", f.dir);
					continue;
				}
				if((mode&CHECK_DATE) && fdate(str) <= f.dateuled)
					continue;
903
				if(mode&ULDATE_ONLY) {
904
					f.dateuled=time32(NULL);
905
					update_uldate(&scfg, &f);
906
907
					continue;
				}
908
909
				if (f.misc & FM_EXTDESC)
					getextdesc(&scfg, f.dir, f.datoffset, ext);
910
			}
911
			f.cdt=l;
912
			f.dateuled=time32(NULL);
913
			f.altpath=cur_altpath;
914
			prep_desc(f.desc);
915
			if(mode&ASCII_ONLY)
916
				strip_exascii(f.desc, f.desc);
deuce's avatar
deuce committed
917
			printf("%s %7"PRIu32" %s\n",f.name,f.cdt,f.desc);
918
			if(mode&FILE_ID)
919
				get_file_diz(&f, str, ext);
920

921
922
923
			if(exist) {
				putfiledat(&scfg,&f);
				if(!(mode&NO_NEWDATE))
924
					update_uldate(&scfg, &f);
925
			}
926
927
928
929
930
931
932
933
			else
				addfiledat(&scfg,&f);

			if(f.misc&FM_EXTDESC)
				putextdesc(&scfg,f.dir,f.datoffset,ext);

			if(mode&UL_STATS)
				updatestats(l);
934
935
			files++;
		}
936
	}
937
938
939

	if(mode&AUTO_ADD) {
		for(i=0;i<scfg.total_dirs;i++) {
940
941
			if(lib[0] && stricmp(scfg.lib[scfg.dir[i]->lib]->sname,lib))
				continue;
942
943
944
945
			if(scfg.dir[i]->misc&DIR_NOAUTO)
				continue;
			f.dir=i;
			if(mode&SEARCH_DIR) {
946
				addlist("",f,desc_offset,size_offset);
947
				continue;
948
			}
949
			sprintf(str,"%s%s.lst",scfg.dir[f.dir]->path,scfg.dir[f.dir]->code);
950
			if(fexistcase(str) && flength(str)>0L) {
951
				printf("Auto-adding %s\n",str);
952
				addlist(str,f,desc_offset,size_offset);
953
954
				if(mode&SYNC_LIST)
					synclist(str,i);
955
				continue;
956
			}
957
			sprintf(str,"%s%s",scfg.dir[f.dir]->path,auto_name);
958
			if(fexistcase(str) && flength(str)>0L) {
959
				printf("Auto-adding %s\n",str);
960
				addlist(str,f,desc_offset,size_offset);
961
962
				if(mode&SYNC_LIST)
					synclist(str,i);
963
964
965
				continue;
			}
		}
966
	}
967
968
969

	else {
		if(!listgiven && !namegiven) {
970
			sprintf(str,"%s%s.lst",scfg.dir[f.dir]->path, scfg.dir[f.dir]->code);