tmp_xfer.cpp 14.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
45
/* tmp_xfer.cpp */

/* Synchronet temp directory file transfer routines */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * 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"

/*****************************************************************************/
/* Temp directory section. Files must be extracted here and both temp_uler   */
/* and temp_uler fields should be filled before entrance.                    */
/*****************************************************************************/
void sbbs_t::temp_xfer()
{
46
47
    char	str[256],tmp2[256],done=0,ch;
	char 	tmp[512];
rswindell's avatar
rswindell committed
48
49
	int		error;
    uint	i,dirnum=cfg.total_dirs,files;
50
51
52
53
54
    ulong	bytes;
	ulong	space;
    time_t	start,end,t;
    file_t	f;
	glob_t	g;
55
	struct	tm tm;
56
57
58
59
60
61
62
63
64
65
66
67
68

	if(!usrlibs)
		return;
	if(useron.rest&FLAG('D')) {
		bputs(text[R_Download]);
		return; }
	/*************************************/
	/* Create TEMP directory information */
	/*************************************/
	if((cfg.dir[dirnum]=(dir_t *)MALLOC(sizeof(dir_t)))==0) {
		errormsg(WHERE,ERR_ALLOC,"temp_dir",sizeof(dir_t));
		return; }
	memset(cfg.dir[dirnum],0,sizeof(dir_t));
69
70
	strcpy(cfg.dir[dirnum]->lname,"Temporary");
	strcpy(cfg.dir[dirnum]->sname,"Temp");
71
	strcpy(cfg.dir[dirnum]->code,"TEMP");
72
73
	strcpy(cfg.dir[dirnum]->path,cfg.temp_dir);
	strcpy(cfg.dir[dirnum]->data_dir,cfg.dir[0]->data_dir);
74
75
76
77
78
79
80
81
82
	cfg.dir[dirnum]->maxfiles=MAX_FILES;
	cfg.dir[dirnum]->op_ar=(uchar *)nulstr;
	temp_dirnum=curdirnum=usrdir[curlib][curdir[curlib]];
	cfg.total_dirs++;

	/****************************/
	/* Fill filedat information */
	/****************************/
	memset(&f,0,sizeof(f));
83
	sprintf(f.name,"temp_%3.3d.%s",cfg.node_num,useron.tmpext);
84
85
86
	strcpy(f.desc,"Temp File");
	f.dir=dirnum;

87
	if(useron.misc&(RIP|WIP|HTML) && !(useron.misc&EXPERT))
88
		menu("tempxfer");
89
90
	lncntr=0;
	while(online && !done) {
91
		if(!(useron.misc&(EXPERT|RIP|WIP|HTML))) {
92
93
94
95
96
97
			sys_status&=~SS_ABORT;
			if(lncntr) {
				SYNC;
				CRLF;
				if(lncntr)          /* CRLF or SYNC can cause pause */
					pause(); }
98
			menu("tempxfer"); }
99
100
101
102
103
104
105
106
107
		ASYNC;
		bputs(text[TempDirPrompt]);
		strcpy(f.uler,temp_uler);
		ch=(char)getkeys("ADEFNILQRVX?\r",0);
		if(ch>SP)
			logch(ch,0);
		switch(ch) {
			case 'A':   /* add to temp file */
				/* free disk space */
108
				space=getfreediskspace(cfg.temp_dir,0);
109
110
				if(space<(ulong)cfg.min_dspace*1024L) {
					bputs(text[LowDiskSpace]);
111
112
					sprintf(str,"Diskspace is low: %s (%lu bytes)"
						,cfg.temp_dir,space);
113
114
115
116
117
118
119
120
					errorlog(str);
					if(!dir_op(dirnum))
						break; }
				bprintf(text[DiskNBytesFree],ultoac(space,tmp));
				if(!getfilespec(str))
					break;
				if(!checkfname(str))
					break;
121
122
				sprintf(tmp2,"%s added %s to %s"
					,useron.alias,str,f.name);
123
124
125
				logline(nulstr,tmp2);
				sprintf(tmp2,"%s%s",cfg.temp_dir,str);
				sprintf(str,"%s%s",cfg.temp_dir,f.name);
126
				external(cmdstr(temp_cmd(),str,tmp2,NULL),EX_WILDCARD|EX_OUTL|EX_OUTR);
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
				break;
			case 'D':   /* download from temp dir */
				sprintf(str,"%s%s",cfg.temp_dir,f.name);
				if(!fexist(str)) {
					bprintf(text[TempFileNotCreatedYet],f.name);
					break; }
				f.size=f.cdt=flength(str);
				f.opencount=0;
				if(temp_cdt)    /* if file was not free */
					f.cdt=f.size;
				else
					f.cdt=0;
				if(!(useron.exempt&FLAG('D'))
					&& f.cdt>useron.cdt+useron.freecdt) {
					bprintf(text[YouOnlyHaveNCredits]
						,ultoac(useron.cdt+useron.freecdt,tmp));
					break; }    /* f.cdt must equal size here */
				if(!(useron.exempt&FLAG('T')) && !dir_op(dirnum)
					&& !(cfg.dir[temp_dirnum]->misc&DIR_TFREE) && cur_cps
					&& f.size/(ulong)cur_cps>timeleft) {
					bputs(text[NotEnoughTimeToDl]);
					break; }
				if(!chk_ar(cfg.dir[temp_dirnum]->dl_ar,&useron)) {
					bputs(text[CantDownloadFromDir]);
					break; }
				addfiledat(&cfg,&f);
153
				menu("dlprot");
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
				SYNC;
				mnemonics(text[ProtocolOrQuit]);
				strcpy(tmp2,"Q");
				for(i=0;i<cfg.total_prots;i++)
					if(cfg.prot[i]->dlcmd[0] && chk_ar(cfg.prot[i]->ar,&useron)) {
						sprintf(tmp,"%c",cfg.prot[i]->mnemonic);
						strcat(tmp2,tmp); }
				ungetkey(useron.prot);
				ch=(char)getkeys(tmp2,0);
				for(i=0;i<cfg.total_prots;i++)
					if(cfg.prot[i]->dlcmd[0] && cfg.prot[i]->mnemonic==ch
						&& chk_ar(cfg.prot[i]->ar,&useron))
						break;
				if(i<cfg.total_prots) {
					getnodedat(cfg.node_num,&thisnode,1);
					action=NODE_DLNG;
					t=now;
					if(cur_cps) 
						t+=(f.size/(ulong)cur_cps);
173
					if(localtime_r(&t,&tm)==NULL)
174
						break;
175
					thisnode.aux=(tm.tm_hour*60)+tm.tm_min;
176
177
178

					putnodedat(cfg.node_num,&thisnode); /* calculate ETA */
					start=time(NULL);
rswindell's avatar
rswindell committed
179
					error=protocol(cmdstr(cfg.prot[i]->dlcmd,str,nulstr,NULL),false);
180
181
182
183
184
185
186
187
188
					end=time(NULL);
					if(cfg.dir[temp_dirnum]->misc&DIR_TFREE)
						starttime+=end-start;
					if(cfg.prot[i]->misc&PROT_DSZLOG) {
						if(checkprotlog(&f))
							downloadfile(&f);
						else
							notdownloaded(f.size,start,end); }
					else {
rswindell's avatar
rswindell committed
189
						if(!error)
190
191
192
193
194
195
196
197
198
199
200
201
							downloadfile(&f);
						else {
							bprintf(text[FileNotSent],f.name);
							notdownloaded(f.size,start,end); } }
					autohangup(); }
				removefiledat(&cfg,&f);
				break;
			case 'E':
				extract(usrdir[curlib][curdir[curlib]]);
				sys_status&=~SS_ABORT;
				break;
			case 'F':   /* Create a file list */
202
				delfiles(cfg.temp_dir,ALLFILES);
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
				create_filelist("FILELIST.TXT",0);
				if(!(sys_status&SS_ABORT))
					logline(nulstr,"Created list of all files");
				CRLF;
				sys_status&=~SS_ABORT;
				break;
			case 'I':   /* information on what's here */
				bprintf(text[TempFileInfo],f.uler,temp_file);
				break;
			case 'L':   /* list files in dir */
				if(!getfilespec(str))
					break;
				if(!checkfname(str))
					break;
				bytes=files=0L;
				CRLF;
				sprintf(tmp2,"%s%s",cfg.temp_dir,str);
220
				glob(tmp2,0,NULL,&g);
rswindell's avatar
rswindell committed
221
				for(i=0;i<(uint)g.gl_pathc && !msgabort();i++) {
222
223
					if(isdir(g.gl_pathv[i]))
						continue;
224
					t=fdate(g.gl_pathv[i]);
225
					bprintf("%-25s %15s   %s\r\n",getfname(g.gl_pathv[i])
226
227
						,ultoac(flength(g.gl_pathv[i]),tmp)
						,timestr(&t));
228
229
230
231
					files++;
					bytes+=flength(g.gl_pathv[i]);
				}
				globfree(&g);
232
233
234
235
236
237
				if(!files)
					bputs(text[EmptyDir]);
				else if(files>1)
					bprintf(text[TempDirTotal],ultoac(bytes,tmp),files);
				break;
			case 'N':   /* Create a list of new files */
238
				delfiles(cfg.temp_dir,ALLFILES);
239
240
241
242
243
244
245
				create_filelist("NEWFILES.TXT",FL_ULTIME);
				if(!(sys_status&SS_ABORT))
					logline(nulstr,"Created list of new files");
				CRLF;
				sys_status&=~SS_ABORT;
				break;
			case 'R':   /* Remove files from dir */
246
				if(!getfilespec(str) || !checkfname(str))
247
					break;
248
				bprintf(text[NFilesRemoved],delfiles(cfg.temp_dir,str));
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
				break;
			case 'V':   /* view files in dir */
				bputs(text[FileSpec]);
				if(!getstr(str,12,K_UPPER) || !checkfname(str))
					break;
				viewfiles(dirnum,str);
				break;
			case CR:
			case 'Q':   /* quit */
				done=1;
				break;
			case 'X':   /* extract from archive in temp dir */
				extract(dirnum);
				sys_status&=~SS_ABORT;
				break;
			case '?':   /* menu */
265
				if(useron.misc&(EXPERT|RIP|WIP|HTML))
266
					menu("tempxfer");
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
				break; }
		if(sys_status&SS_ABORT)
			break; }
	FREE(cfg.dir[dirnum]);
	cfg.total_dirs--;
}

/*****************************************************************************/
/* Handles extraction from a normal transfer file to the temp directory      */
/*****************************************************************************/
void sbbs_t::extract(uint dirnum)
{
    char	fname[13],str[256],excmd[256],path[256],done
				,tmp[256],intmp=0;
    uint	i,j;
	ulong	space;
    file_t	f;
284
285
	DIR*	dir;
	DIRENT*	dirent;
286
287
288
289
290
291

	temp_dirnum=curdirnum=dirnum;
	if(!strcmp(cfg.dir[dirnum]->code,"TEMP"))
		intmp=1;

	/* get free disk space */
292
	space=getfreediskspace(cfg.temp_dir,0);
293
294
	if(space<(ulong)cfg.min_dspace*1024L) {
		bputs(text[LowDiskSpace]);
295
		sprintf(str,"Diskspace is low: %s (%lu bytes)",cfg.temp_dir,space);
296
297
298
299
300
301
302
303
		errorlog(str);
		if(!dir_op(dirnum))
			return; }
	else if(!intmp) {   /* not in temp dir */
		CRLF; }
	bprintf(text[DiskNBytesFree],ultoac(space,tmp));

	if(!intmp) {    /* not extracting FROM temp directory */
304
		sprintf(str,"%s%s",cfg.temp_dir,ALLFILES);
305
306
		if(fexist(str)) {
			bputs(text[RemovingTempFiles]);
307
			dir=opendir(cfg.temp_dir);
308
			while(dir!=NULL && (dirent=readdir(dir))!=NULL) {
309
310
				sprintf(str,"%s%s",cfg.temp_dir,dirent->d_name);
        		if(!isdir(str))
311
					remove(str);
312
			}
313
314
			if(dir!=NULL)
				closedir(dir);
315
316
317
			CRLF; 
		} 
	}
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
	bputs(text[ExtractFrom]);
	if(!getstr(fname,12,K_UPPER) || !checkfname(fname) || strchr(fname,'*')
		|| strchr(fname,'?'))
		return;
	padfname(fname,f.name);
	strcpy(str,f.name);
	truncsp(str);
	for(i=0;i<cfg.total_fextrs;i++)
		if(!strcmp(str+9,cfg.fextr[i]->ext) && chk_ar(cfg.fextr[i]->ar,&useron)) {
			strcpy(excmd,cfg.fextr[i]->cmd);
			break; }
	if(i==cfg.total_fextrs) {
		bputs(text[UnextractableFile]);
		return; }
	if(!intmp && !findfile(&cfg,dirnum,f.name)) {    /* not temp dir */
		bputs(text[SearchingAllDirs]);
		for(i=0;i<usrdirs[curlib] && !msgabort();i++) {
			if(i==dirnum) continue;
			if(findfile(&cfg,usrdir[curlib][i],f.name))
				break; }
		if(i==usrdirs[curlib]) { /* not found in cur lib */
			bputs(text[SearchingAllLibs]);
340
			for(i=j=0;i<usrlibs;i++) {
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
				if(i==curlib) continue;
				for(j=0;j<usrdirs[i] && !msgabort();j++)
					if(findfile(&cfg,usrdir[i][j],f.name))
						break;
				if(j<usrdirs[i])
					break; }
			if(i==usrlibs) {
				bputs(text[FileNotFound]);  /* not in database */
				return; }
			dirnum=usrdir[i][j]; }
		else
			dirnum=usrdir[curlib][i]; }
	if(sys_status&SS_ABORT)
		return;
	sprintf(path,"%s%s",cfg.dir[dirnum]->path,fname);
	if(!intmp) {    /* not temp dir, so get temp_file info */
		f.datoffset=f.dateuled=f.datedled=0L;
		f.dir=dirnum;
		getfileixb(&cfg,&f);
		if(!f.datoffset && !f.dateuled && !f.datedled)  /* error reading ixb */
			return;
		f.size=0;
		getfiledat(&cfg,&f);
		fileinfo(&f);
		if(f.altpath>0 && f.altpath<=cfg.altpaths)
			sprintf(path,"%s%s",cfg.altpath[f.altpath-1],fname);
		temp_dirnum=dirnum;
		if(cfg.dir[f.dir]->misc&DIR_FREE)
			temp_cdt=0L;
		else
			temp_cdt=f.cdt;
		strcpy(temp_uler,f.uler);
		strcpy(temp_file,f.name); }     /* padded filename */
	if(!fexist(path)) {
		bputs(text[FileNotThere]);  /* not on disk */
		return; }
	done=0;
	while(online && !done) {
		mnemonics(text[ExtractFilesPrompt]);
		switch(getkeys("EVQ",0)) {
			case 'E':
				if(!getfilespec(str))
					break;
				if(!checkfname(str))
					break;
				if((i=external(cmdstr(excmd,path,str,NULL)
387
					,EX_INR|EX_OUTL|EX_OUTR))!=0) {
388
389
					errormsg(WHERE,ERR_EXEC,cmdstr(excmd,path,str,NULL),i);
					return; }
390
				sprintf(tmp,"%s extracted %s from %s",useron.alias,str,path);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
				logline(nulstr,tmp);
				CRLF;
				break;
			case 'V':
				viewfiles(dirnum,fname);
				break;
			default:
				done=1;
				break; } }
}

/****************************************************************************/
/* Creates a text file named NEWFILES.DAT in the temp directory that        */
/* all new files since p-date. Returns number of files in list.             */
/****************************************************************************/
ulong sbbs_t::create_filelist(char *name, long mode)
{
    char	str[256];
	int		file;
	uint	i,j,d;
	ulong	l,k;

	bprintf(text[CreatingFileList],name);
	sprintf(str,"%s%s",cfg.temp_dir,name);
	if((file=nopen(str,O_CREAT|O_WRONLY|O_APPEND))==-1) {
		errormsg(WHERE,ERR_OPEN,str,O_CREAT|O_WRONLY|O_APPEND);
		return(0); }
	k=0;
	if(mode&FL_ULTIME) {
		sprintf(str,"New files since: %s\r\n",timestr(&ns_time));
		write(file,str,strlen(str)); }
422
	for(i=j=d=0;i<usrlibs;i++) {
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
		for(j=0;j<usrdirs[i];j++,d++) {
			outchar('.');
			if(d && !(d%5))
				bputs("\b\b\b\b\b     \b\b\b\b\b");
			if(mode&FL_ULTIME /* New-scan */
				&& (cfg.lib[usrlib[i]]->offline_dir==usrdir[i][j]
				|| cfg.dir[usrdir[i][j]]->misc&DIR_NOSCAN))
				continue;
			l=listfiles(usrdir[i][j],nulstr,file,mode);
			if((long)l==-1)
				break;
			k+=l; }
		if(j<usrdirs[i])
			break; }
	if(k>1) {
rswindell's avatar
rswindell committed
438
		sprintf(str,"\r\n%ld Files Listed.\r\n",k);
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
		write(file,str,strlen(str)); }
	close(file);
	if(k)
		bprintf(text[CreatedFileList],name);
	else {
		bputs(text[NoFiles]);
		sprintf(str,"%s%s",cfg.temp_dir,name);
		remove(str); }
	strcpy(temp_file,name);
	strcpy(temp_uler,"File List");
	return(k);
}

/****************************************************************************/
/* This function returns the command line for the temp file extension for	*/
/* current user online. 													*/
/****************************************************************************/
char * sbbs_t::temp_cmd(void)
{
	int i;

	if(!cfg.total_fcomps) {
461
		errormsg(WHERE,ERR_CHK,"compressible file types",0);
462
463
464
465
466
467
468
		return(nulstr); }
	for(i=0;i<cfg.total_fcomps;i++)
		if(!stricmp(useron.tmpext,cfg.fcomp[i]->ext)
			&& chk_ar(cfg.fcomp[i]->ar,&useron))
			return(cfg.fcomp[i]->cmd);
	return(cfg.fcomp[0]->cmd);
}