addfiles.c 25.8 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
char lib[LEN_GSNAME+1];
48
const char* datefmt = NULL;
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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
65
#define CHECK_DATE	(1L<<15)
66
67

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

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

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

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

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

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

190
	sprintf(str,"%sdsts.dab",scfg.ctrl_dir);
191
	if((file=nopen(str,O_RDWR|O_BINARY))==-1) {
192
		printf("ERR_OPEN %s\n",str);
193
		return;
194
195
196
197
198
199
200
201
202
203
204
205
206
	}
	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);
}

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

	for(i=0;i<scfg.total_fextrs;i++)
214
215
		if(!stricmp(scfg.fextr[i]->ext,f->name+9)
			&& chk_ar(&scfg,scfg.fextr[i]->ar,/* user: */NULL, /* client: */NULL))
216
			break;
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	// 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);
231
		removecase(tmp);
232
233
		system(mycmdstr(scfg.fextr[i]->cmd,filepath,"DESC.SDI",NULL));
		fexistcase(tmp);
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
259

	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;
260
261
}

262
263
void addlist(char *inpath, file_t f, uint dskip, uint sskip)
{
264
265
266
267
268
269
	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];
270
	char *p;
271
272
	char ext[1024];
	int i;
273
	long l;
274
	BOOL exist;
275
276
277
278
279
	FILE *stream;
	DIR*	dir;
	DIRENT*	dirent;

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

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

			if(mode&TODAYS_DATE) {		/* put today's date in desc */
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
				time_t now = time(NULL);
				if(datefmt) {
					struct tm tm = {0};
					localtime_r(&now, &tm);
					strftime(f.desc, sizeof(f.desc), datefmt, &tm);
				} else
					unixtodstr(&scfg, (time32_t)now, f.desc);
				SAFECAT(f.desc,"  ");
			}
			else if(mode&FILE_DATE) {		/* get the file date and put into desc */
				if(datefmt) {
					struct tm tm = {0};
					localtime_r(&file_timestamp, &tm);
					strftime(f.desc, sizeof(f.desc), datefmt, &tm);
				} else
					unixtodstr(&scfg,(time32_t)file_timestamp,f.desc);
				SAFECAT(f.desc,"  ");
340
341
			}

342
			if(mode&FILE_ID)
343
				get_file_diz(&f, filepath, ext);
344

345
			f.dateuled=time32(NULL);
346
			f.altpath=cur_altpath;
347
			prep_desc(f.desc);
348
			if(mode&ASCII_ONLY)
349
				strip_exascii(f.desc, f.desc);
350
351
352
			if(exist) {
				putfiledat(&scfg,&f);
				if(!(mode&NO_NEWDATE))
353
					update_uldate(&scfg, &f);
354
			}
355
356
357
358
			else
				addfiledat(&scfg,&f);
			if(f.misc&FM_EXTDESC) {
				truncsp(ext);
359
				putextdesc(&scfg,f.dir,f.datoffset,ext);
360
			}
361
362
			if(mode&UL_STATS)
				updatestats(f.cdt);
363
			files++;
364
365
366
		}
		if(dir!=NULL)
			closedir(dir);
367
		return;
368
369
370
	}


371
	SAFECOPY(listpath,inpath);
372
	fexistcase(listpath);
373
	if((stream=fopen(listpath,"r"))==NULL) {
374
375
		fprintf(stderr,"Error %d (%s) opening %s\n"
			,errno,strerror(errno),listpath);
376
377
		sprintf(listpath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[f.dir]->path,inpath);
378
		fexistcase(listpath);
379
		if((stream=fopen(listpath,"r"))==NULL) {
380
381
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
382
383
			return;
		}
384
	}
385
386
387
388
389

	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);
390
	do {
391
392
		f.misc=0;
		f.desc[0]=0;
393
		memset(ext, 0, sizeof(ext));
394
		SAFECOPY(curline,nextline);
395
396
397
		nextline[0]=0;
		fgets(nextline,255,stream);
		truncsp(curline);
398
		if(curline[0]<=' ' || (mode&ASCII_ONLY && (uchar)curline[0]>=0x7e))
399
400
			continue;
		printf("%s\n",curline);
401
		SAFECOPY(fname,curline);
402

403
#if 0	/* Files without dots are valid on modern systems */
404
405
406
		p=strchr(fname,'.');
		if(!p || p==fname || p>fname+8)    /* no dot or invalid dot location */
			continue;
407
408
#endif
		p=strchr(fname,' ');
409
410
411
412
		if(p) *p=0;
		else				   /* no space after filename? */
			continue;

413
414
415
		if(!isalnum(*fname)) {	// filename doesn't begin with an alpha-numeric char?
			continue;
		}
rswindell's avatar
rswindell committed
416
		SAFEPRINTF2(filepath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
417
418
419
420
421
422
423
424
			: scfg.dir[f.dir]->path,fname);

#ifdef _WIN32
		{
			char shortpath[MAX_PATH+1];
			GetShortPathName(filepath, shortpath, sizeof(shortpath));
			SAFECOPY(fname, getfname(shortpath));
		}
rswindell's avatar
rswindell committed
425
426
427
#else
		fexistcase(filepath);
		SAFECOPY(fname, getfname(filepath));
428
429
#endif

430
431
432
433
434
		padfname(fname,f.name);
		if(strcspn(f.name,"\\/|<>+[]:=\";,")!=strlen(f.name))
			continue;

		for(i=0;i<12;i++)
435
			if(f.name[i]<' ' || (mode&ASCII_ONLY && (uchar)f.name[i]>0x7e))
436
437
438
439
				break;

		if(i<12)					/* Ctrl chars or EX-ASCII in filename? */
			continue;
rswindell's avatar
rswindell committed
440
		time_t file_timestamp = fdate(filepath);
441
442
443
444
		exist=findfile(&scfg,f.dir,f.name);
		if(exist) {
			if(mode&NO_UPDATE)
				continue;
445
446
447
448
449
450
			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;
451
			if(mode&ULDATE_ONLY) {
452
				f.dateuled=time32(NULL);
453
				update_uldate(&scfg, &f);
454
455
				continue;
			}
456
457
			if (f.misc & FM_EXTDESC)
				getextdesc(&scfg, f.dir, f.datoffset, ext);
458
		}
459
460

		if(mode&TODAYS_DATE) {		/* put today's date in desc */
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
			time_t now = time(NULL);
			if(datefmt) {
				struct tm tm = {0};
				localtime_r(&now, &tm);
				strftime(f.desc, sizeof(f.desc), datefmt, &tm);
			} else
				unixtodstr(&scfg, (time32_t)now, f.desc);
			SAFECAT(f.desc,"  ");
		}
		else if(mode&FILE_DATE) {		/* get the file date and put into desc */
			if(datefmt) {
				struct tm tm = {0};
				localtime_r(&file_timestamp, &tm);
				strftime(f.desc, sizeof(f.desc), datefmt, &tm);
			} else
				unixtodstr(&scfg,(time32_t)file_timestamp,f.desc);
			SAFECAT(f.desc,"  ");
478
		}
479
480
481

		if(dskip && strlen(curline)>=dskip) p=curline+dskip;
		else {
482
483
			p = curline;
			FIND_WHITESPACE(p);
484
			SKIP_WHITESPACE(p);
485
		}
486
487
488
		SAFECOPY(tmp,p);
		prep_desc(tmp);
		sprintf(f.desc+strlen(f.desc),"%.*s",(int)(LEN_FDESC-strlen(f.desc)),tmp);
489

490
		if(nextline[0]==' ' || strlen(p)>LEN_FDESC) {	/* ext desc */
491
			if(!(mode&NO_EXTEND)) {
492
				memset(ext, 0, sizeof(ext));
493
				f.misc|=FM_EXTDESC;
494
				sprintf(ext,"%s\r\n",p);
495
			}
496

497
			if(nextline[0]==' ') {
498
				SAFECOPY(str,nextline);				   /* tack on to end of desc */
499
				p=str+dskip;
500
				while(*p>0 && *p<=' ') p++;
501
502
503
504
505
				i=LEN_FDESC-strlen(f.desc);
				if(i>1) {
					p[i-1]=0;
					truncsp(p);
					if(p[0]) {
506
507
						SAFECAT(f.desc," ");
						SAFECAT(f.desc,p);
508
509
					}
				}
510
			}
511
512

			while(!feof(stream) && !ferror(stream) && strlen(ext)<512) {
513
				if(nextline[0]!=' ')
514
515
516
517
518
519
					break;
				truncsp(nextline);
				printf("%s\n",nextline);
				if(!(mode&NO_EXTEND)) {
					f.misc|=FM_EXTDESC;
					p=nextline+dskip;
520
					while(*p==' ') p++;
521
522
					SAFECAT(ext,p);
					SAFECAT(ext,"\r\n");
523
				}
524
				nextline[0]=0;
525
526
				fgets(nextline,255,stream);
			}
527
		}
528
529


530
531
532
533
		l=flength(filepath);
		if(l<0L) {
			printf("%s not found.\n",filepath);
			continue;
534
		}
535
536
537
538
539
		if(l == 0L) {
			printf("%s is a zero length file.\n",filepath);
			continue;
		}
		if(sskip) l=atol(fname+sskip);
540

541
		if(mode&FILE_ID)
542
			get_file_diz(&f, filepath, ext);
543
544

		f.cdt=l;
545
		f.dateuled=time32(NULL);
546
		f.altpath=cur_altpath;
547
		prep_desc(f.desc);
548
		if(mode&ASCII_ONLY)
549
			strip_exascii(f.desc, f.desc);
550
551
552
		if(exist) {
			putfiledat(&scfg,&f);
			if(!(mode&NO_NEWDATE))
553
				update_uldate(&scfg, &f);
554
		}
555
556
557
558
		else
			addfiledat(&scfg,&f);
		if(f.misc&FM_EXTDESC) {
			truncsp(ext);
559
			putextdesc(&scfg,f.dir,f.datoffset,ext);
560
		}
561
562
563

		if(mode&UL_STATS)
			updatestats(l);
564
		files++;
565
	} while(!feof(stream) && !ferror(stream));
566
567
568
	fclose(stream);
	if(mode&DEL_LIST && !(mode&SYNC_LIST)) {
		printf("\nDeleting %s\n",listpath);
569
		remove(listpath);
570
	}
571
572
573
574
575

}

void synclist(char *inpath, int dirnum)
{
576
	char	str[1024];
577
578
579
	char	fname[MAX_PATH+1];
	char	listpath[MAX_PATH+1];
	uchar*	ixbbuf;
580
	char*	p;
581
582
583
584
	int		i,file,found;
	long	l,m,length;
	FILE*	stream;
	file_t	f;
585

586
	sprintf(str,"%s%s.ixb",scfg.dir[dirnum]->data_dir,scfg.dir[dirnum]->code);
587
	if((file=nopen(str,O_RDONLY|O_BINARY))==-1) {
588
		printf("ERR_OPEN %s\n",str);
589
		return;
590
	}
591
592
593
	length=filelength(file);
	if(length%F_IXBSIZE) {
		close(file);
rswindell's avatar
rswindell committed
594
		printf("ERR_LEN (%ld) of %s\n",length,str);
595
		return;
596
	}
deuce's avatar
deuce committed
597
	if((ixbbuf=(uchar *)malloc(length))==NULL) {
598
599
		close(file);
		printf("ERR_ALLOC %s\n",str);
600
		return;
601
	}
602
603
	if(lread(file,ixbbuf,length)!=length) {
		close(file);
deuce's avatar
deuce committed
604
		free((char *)ixbbuf);
605
		printf("ERR_READ %s\n",str);
606
		return;
607
	}
608
609
	close(file);

610
	SAFECOPY(listpath,inpath);
611
	if((stream=fopen(listpath,"r"))==NULL) {
612
613
		sprintf(listpath,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[dirnum]->path,inpath);
614
		if((stream=fopen(listpath,"r"))==NULL) {
615
616
			printf("Can't open: %s\n"
				   "        or: %s\n",inpath,listpath);
617
618
			return;
		}
619
	}
620
621
622
623
624
625
626
627

	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)
628
				str[i]=ixbbuf[m]>' ' ? '.' : ' ';
629
630
631
632
633
634
635
636
637
638
			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);
639
			p=strchr(str,' ');
640
641
			if(p) *p=0;
			if(!stricmp(str,fname))
642
				found=1;
643
		}
644
645
646
647
648
649
		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);
650
651
652
653
		if(!getfiledat(&scfg,&f)) {
			fprintf(stderr, "!ERROR reading index of directory %u\n", f.dir);
			continue;
		}
654
655
		if(f.opencount) {
			printf("currently OPEN by %u users\n",f.opencount);
656
			continue;
657
		}
658
		removefiledat(&scfg,&f);
659
		if(remove(getfilepath(&scfg,&f,str)))
660
661
			printf("Error removing %s\n",str);
		removed++;
662
		printf("Removed from database\n");
663
	}
664
665
666

	if(mode&DEL_LIST) {
		printf("\nDeleting %s\n",listpath);
667
		remove(listpath);
668
	}
669
670
}

671
char *usage="\nusage: addfiles code [.alt_path] [-opts] +list "
672
			 "[desc_off] [size_off]"
673
	"\n   or: addfiles code [.alt_path] [-opts] file "
674
675
		"\"description\"\n"
	"\navailable opts:"
676
677
678
679
680
681
	"\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"
682
	"\n      -F <fmt>   include file date in descriptions, using strftime format"
683
684
685
686
	"\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
687
	"\n      -p         compare file date with upload date for existing files"
688
689
690
691
692
693
	"\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)"
694
	"\n"
695
696
	"\nAuto-ADD:   use - in place of code for Auto-ADD of FILES.BBS"
	"\n            use -filename to Auto-ADD a different filename"
697
	"\n            use -l \"libname\" to only Auto-ADD files to a specific library"
698
699
700
701
702
703
704
705
	;

/*********************/
/* Entry point (duh) */
/*********************/
int main(int argc, char **argv)
{
	char error[512];
706
	char revision[16];
707
708
	char str[MAX_PATH+1];
	char tmp[MAX_PATH+1];
709
710
	char *p;
	char exist,listgiven=0,namegiven=0,ext[513]
711
		,auto_name[MAX_PATH+1]="FILES.BBS";
712
	int i,j;
713
	uint desc_offset=0, size_offset=0;
714
715
716
	long l;
	file_t	f;

717
	sscanf("$Revision$", "%*s %s", revision);
718

719
	fprintf(stderr,"\nADDFILES v%s-%s (rev %s) - Adds Files to Synchronet "
720
721
722
		"Filebase\n"
		,ADDFILES_VER
		,PLATFORM_DESC
723
		,revision
724
725
726
		);

	if(argc<2) {
727
		puts(usage);
728
		return(1);
729
	}
730

731
	p = get_ctrl_dir();
732

733
734
	memset(&scfg,0,sizeof(scfg));
	scfg.size=sizeof(scfg);
735
	SAFECOPY(scfg.ctrl_dir,p);
736
737
738
739
740

	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);
741
742
743
744
	if(!load_cfg(&scfg,NULL,TRUE,error)) {
		fprintf(stderr,"!ERROR loading configuration files: %s\n",error);
		exit(1);
	}
rswindell's avatar
rswindell committed
745
746
	SAFECOPY(scfg.temp_dir,"../temp");
	prep_dir(scfg.ctrl_dir, scfg.temp_dir, sizeof(scfg.temp_dir));
747

748
749
	if(argv[1][0]=='*' || argv[1][0]=='-') {
		if(argv[1][1]=='?') {
750
			puts(usage);
751
752
			exit(0);
		}
753
		if(argv[1][1])
754
			SAFECOPY(auto_name,argv[1]+1);
755
		mode|=AUTO_ADD;
756
		i=0;
757
	} else {
758
		if(!isalnum((uchar)argv[1][0]) && argc==2) {
759
			puts(usage);
760
			return(1);
761
762
		}

763
		for(i=0;i<scfg.total_dirs;i++)
764
			if(!stricmp(scfg.dir[i]->code,argv[1])) /* use matchmatchi() instead? */
765
766
767
768
				break;

		if(i>=scfg.total_dirs) {
			printf("Directory code '%s' not found.\n",argv[1]);
769
770
			exit(1);
		}
771
	}
772
773
774

	memset(&f,0,sizeof(file_t));
	f.dir=i;
775
	SAFECOPY(f.uler,"-> ADDFILES <-");
776
777

	for(j=2;j<argc;j++) {
778
779
		if(argv[j][0]=='*')     /* set the uploader name (legacy) */
			SAFECOPY(f.uler,argv[j]+1);
780
		else
781
782
783
			if(argv[j][0]=='-'
			|| argv[j][0]=='/'
			) {      /* options */
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
			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;
804
805
806
					case 'L':
						j++;
						if(argv[j]==NULL) {
807
							puts(usage);
808
809
810
811
812
							return(-1);
						}
						SAFECOPY(lib,argv[j]);
						i=strlen(argv[j])-1;
						break;
813
814
815
					case 'X':
						j++;
						if(argv[j]==NULL) {
816
							puts(usage);
817
818
819
820
821
							return(-1);
						}
						SAFECOPY(f.uler,argv[j]);
						i=strlen(argv[j])-1;
						break;
822
823
824
825
826
827
828
829
830
831
832
833
					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
834
835
836
					case 'P':
						mode|=CHECK_DATE;
						break;
837
838
839
840
841
					case 'U':
						mode|=NO_NEWDATE;
						break;
					case 'F':
						mode|=FILE_DATE;
842
843
844
845
846
847
848
849
850
851
						if(argv[j][i] == 'F') {
							j++;
							if(argv[j]==NULL) {
								puts(usage);
								return(-1);
							}
							datefmt = argv[j];
							i=strlen(argv[j]) - 1;
							break;
						}
852
853
854
855
856
857
858
859
						break;
					case 'T':
						mode|=TODAYS_DATE;
						break;
					case 'S':
						mode|=SEARCH_DIR;
						break;
					default:
860
						puts(usage);
861
862
						return(1);
			}
863
		}
864
		else if(isdigit((uchar)argv[j][0])) {
865
866
867
868
869
870
			if(desc_offset==0)
				desc_offset=atoi(argv[j]);
			else
				size_offset=atoi(argv[j]);
			continue;
		}
871
872
		else if(argv[j][0]=='+') {      /* filelist - FILES.BBS */
			listgiven=1;
rswindell's avatar
rswindell committed
873
			if(argc > j+1
874
				&& isdigit((uchar)argv[j+1][0])) { /* skip x characters before description */
rswindell's avatar
rswindell committed
875
				if(argc > j+2
876
					&& isdigit((uchar)argv[j+2][0])) { /* skip x characters before size */
877
					addlist(argv[j]+1,f,atoi(argv[j+1]),atoi(argv[j+2]));
878
					j+=2;
879
				}
880
881
				else {
					addlist(argv[j]+1,f,atoi(argv[j+1]),0);
882
883
					j++;
				}
884
			}
885
886
887
			else
				addlist(argv[j]+1,f,0,0);
			if(mode&SYNC_LIST)
888
				synclist(argv[j]+1,f.dir);
889
		}
890
891
892
893
		else if(argv[j][0]=='.') {      /* alternate file path */
			cur_altpath=atoi(argv[j]+1);
			if(cur_altpath>scfg.altpaths) {
				printf("Invalid alternate path.\n");
894
895
				exit(1);
			}
896
		}
897
898
899
900
		else {
			namegiven=1;
			padfname(argv[j],f.name);
			f.desc[0]=0;
901
			memset(ext, 0, sizeof(ext));
902
#if 0
903
			strupr(f.name);
904
#endif
905
906
			if(j+1==argc) {
				printf("%s no description given.\n",f.name);
907
				continue;
908
			}
909
910
			sprintf(str,"%s%s",cur_altpath ? scfg.altpath[cur_altpath-1]
				: scfg.dir[f.dir]->path,argv[j]);
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
			if(mode&TODAYS_DATE) {
				time_t now = time(NULL);
				if(datefmt) {
					struct tm tm = {0};
					localtime_r(&now, &tm);
					strftime(f.desc, sizeof(f.desc), datefmt, &tm);
				} else
					SAFECOPY(f.desc, unixtodstr(&scfg, (time32_t)now, tmp));
				SAFECAT(f.desc, "  ");
			}
			else if(mode&FILE_DATE) {
				time_t file_timestamp = fdate(str);
				if(datefmt) {
					struct tm tm = {0};
					localtime_r(&file_timestamp, &tm);
					strftime(f.desc, sizeof(f.desc), datefmt, &tm);
				} else
					SAFECOPY(f.desc, unixtodstr(&scfg,(time32_t)file_timestamp,tmp));
				SAFECAT(f.desc, "  ");
			}
			j++;
			SAFECAT(f.desc, argv[j]);
933
934
935
			l=flength(str);
			if(l==-1) {
				printf("%s not found.\n",str);
936
				continue;
937
			}
938
939
940
941
			exist=findfile(&scfg,f.dir,f.name);
			if(exist) {
				if(mode&NO_UPDATE)
					continue;
942
943
944
945
946
947
				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;
948
				if(mode&ULDATE_ONLY) {
949
					f.dateuled=time32(NULL);
950
					update_uldate(&scfg, &f);
951
952
					continue;
				}
953
954
				if (f.misc & FM_EXTDESC)
					getextdesc(&scfg, f.dir, f.datoffset, ext);
955
			}
956
			f.cdt=l;
957
			f.dateuled=time32(NULL);
958
			f.altpath=cur_altpath;
959
			prep_desc(f.desc);
960
			if(mode&ASCII_ONLY)
961
				strip_exascii(f.desc, f.desc);
deuce's avatar
deuce committed
962
			printf("%s %7"PRIu32" %s\n",f.name,f.cdt,f.desc);
963
			if(mode&FILE_ID)
964
				get_file_diz(&f, str, ext);
965

966
967
968
			if(exist) {
				putfiledat(&scfg,&f);
				if(!(mode&NO_NEWDATE))
969
					update_uldate(&scfg, &f);
970
			}
971
972
973
974
975
976
977
978
			else
				addfiledat(&scfg,&f);

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

			if(mode&UL_STATS)
				updatestats(l);
979
980
			files++;
		}