file.cpp 9.25 KB
Newer Older
1
/* Synchronet file transfer-related sbbs_t class methods */
2
3
4
5
6

/****************************************************************************
 * @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

/****************************************************************************/
/* Prints all information of file in file_t structure 'f'					*/
/****************************************************************************/
28
void sbbs_t::showfileinfo(file_t* f, bool show_extdesc)
29
{
30
	char 	tmp[512];
31
	char	tmp2[64];
32
	char	path[MAX_PATH+1];
33
	bool	is_op = dir_op(f->dir);
34

35
	current_file = f;
36
37
38
39
	getfilepath(&cfg, f, path);
	bprintf(P_TRUNCATE, text[FiLib], getusrlib(f->dir), cfg.lib[cfg.dir[f->dir]->lib]->lname);
	bprintf(P_TRUNCATE, text[FiDir], getusrdir(f->dir), cfg.dir[f->dir]->lname);
	bprintf(P_TRUNCATE, text[FiFilename],f->name);
40

41
42
	if(getfilesize(&cfg, f) >= 0)
		bprintf(P_TRUNCATE, text[FiFileSize], ultoac((ulong)f->size,tmp)
43
			, byte_estimate_to_str(f->size, tmp2, sizeof(tmp2), /* units: */1024, /* precision: */1));
44
45
46
47
48
49
50
51
52
53
54
55
56

	bprintf(P_TRUNCATE, text[FiCredits]
		,(cfg.dir[f->dir]->misc&DIR_FREE || !f->cost) ? "FREE" : ultoac((ulong)f->cost,tmp));
	if(getfilesize(&cfg, f) > 0 &&  f->size == f->file_idx.idx.size) {
#if 0 // I don't think anyone cares about the CRC-16 checksum value of a file
		if(f->file_idx.hash.flags & SMB_HASH_CRC16) {
			SAFEPRINTF(tmp, "%04x", f->file_idx.hash.data.crc16);
			bprintf(P_TRUNCATE, text[FiChecksum], "CRC-16", tmp);
		}
#endif
		if(f->file_idx.hash.flags & SMB_HASH_CRC32) {
			SAFEPRINTF(tmp, "%08x", f->file_idx.hash.data.crc32);
			bprintf(P_TRUNCATE, text[FiChecksum], "CRC-32", tmp);
57
		}
58
59
60
61
		if(f->file_idx.hash.flags & SMB_HASH_MD5)
			bprintf(P_TRUNCATE, text[FiChecksum], "MD5", MD5_hex(tmp, f->file_idx.hash.data.md5));
		if(f->file_idx.hash.flags & SMB_HASH_SHA1)
			bprintf(P_TRUNCATE, text[FiChecksum], "SHA-1", SHA1_hex(tmp, f->file_idx.hash.data.sha1));
62
	}
63
64
65
66
	if(f->desc && f->desc[0])
		bprintf(P_TRUNCATE, text[FiDescription],f->desc);
	if(f->tags && f->tags[0])
		bprintf(P_TRUNCATE, text[FiTags], f->tags);
67
68
69
70
	if(f->author)
		bprintf(P_TRUNCATE, text[FiAuthor], f->author);
	if(f->author_org)
		bprintf(P_TRUNCATE, text[FiGroup], f->author_org);
71
72
73
	char* p = f->hdr.attr&MSG_ANONYMOUS ? text[UNKNOWN_USER] : f->from;
	if(p != NULL && *p != '\0')
		bprintf(P_TRUNCATE, text[FiUploadedBy], p);
74
75
76
77
78
79
80
81
	if(is_op) {
		if(f->from_ip != NULL)
			bprintf(P_TRUNCATE, text[FiUploadedBy], f->from_ip);
		if(f->from_host != NULL)
			bprintf(P_TRUNCATE, text[FiUploadedBy], f->from_host);
		if(f->from_prot != NULL)
			bprintf(P_TRUNCATE, text[FiUploadedBy], f->from_prot);
	}
82
83
	if(f->to_list != NULL && *f->to_list != '\0')
		bprintf(P_TRUNCATE, text[FiUploadedTo], f->to_list);
84
85
86
87
88
89
90
91
92
93
94
95
96
	bprintf(P_TRUNCATE, text[FiDateUled],timestr(f->hdr.when_imported.time));
	if(getfiletime(&cfg, f) > 0)
		bprintf(P_TRUNCATE, text[FiFileDate],timestr(f->time));
	bprintf(P_TRUNCATE, text[FiDateDled],f->hdr.last_downloaded ? timestr(f->hdr.last_downloaded) : "Never");
	bprintf(P_TRUNCATE, text[FiTimesDled],f->hdr.times_downloaded);
	ulong timetodl = gettimetodl(&cfg, f, cur_cps);
	if(timetodl > 0)
		bprintf(text[FiTransferTime],sectostr(timetodl,tmp));
	bputs(P_TRUNCATE, text[FileHdrDescSeparator]);
	if(show_extdesc && f->extdesc != NULL && *f->extdesc) {
		char* p = f->extdesc;
		SKIP_CRLF(p);
		truncsp(p);
97
		putmsg(p, P_NOATCODES | P_CPM_EOF);
98
		newline();
99
	}
100
	if(f->size == -1) {
101
		bprintf(text[FileIsNotOnline],f->name);
102
		if(SYSOP)
103
			bprintf("%s\r\n",path);
104
	}
105
	current_file = NULL;
106
107
108
}

/****************************************************************************/
109
/* Prompts user for file specification. <CR> is *							*/
110
111
112
113
114
115
/* Returns padded file specification.                                       */
/* Returns NULL if input was aborted.                                       */
/****************************************************************************/
char * sbbs_t::getfilespec(char *str)
{
	bputs(text[FileSpecStarDotStar]);
116
117
	if(!getstr(str, MAX_FILENAME_LEN, K_NONE))
		strcpy(str, ALLFILES);
118
119
120
121
122
123
124
125
	if(sys_status&SS_ABORT)
		return(0);
	return(str);
}

/****************************************************************************/
/* Checks to see if filename matches filespec. Returns 1 if yes, 0 if no    */
/****************************************************************************/
126
extern "C" BOOL filematch(const char *filename, const char *filespec)
127
{
128
	return wildmatchi(filename, filespec, /* path: */FALSE);
129
130
131
132
133
134
135
136
137
}

/*****************************************************************************/
/* Checks the filename 'fname' for invalid symbol or character sequences     */
/*****************************************************************************/
bool sbbs_t::checkfname(char *fname)
{
    int		c=0,d;

138
	if(fname[0]=='-'
139
140
		|| strcspn(fname,ILLEGAL_FILENAME_CHARS)!=strlen(fname)
		|| strstr(fname, "..") != NULL) {
141
		lprintf(LOG_WARNING,"Suspicious filename attempt: '%s'",fname);
deuce's avatar
deuce committed
142
		hacklog((char *)"Filename", fname);
143
144
		return(false); 
	}
145
146
	d=strlen(fname);
	while(c<d) {
147
		if(fname[c]<=' ' || fname[c]&0x80)
148
			return(false);
149
150
		c++; 
	}
151
152
	return(true);
}
153
154
155
156
157
158
159
160

long sbbs_t::delfiles(const char *inpath, const char *spec, size_t keep)
{
	long result = ::delfiles(inpath, spec, keep);
	if(result < 0)
		errormsg(WHERE, ERR_REMOVE, inpath, result, spec);
	return result;
}
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

/****************************************************************************/
/* Remove credits or minutes and adjust statistics of uploader of file 'f'	*/
/****************************************************************************/
bool sbbs_t::removefcdt(file_t* f)
{
	char	str[128];
	char 	tmp[512];
	int		u;
	long	cdt;

	if((u=matchuser(&cfg,f->from,TRUE /*sysop_alias*/))==0) {
	   bputs(text[UnknownUser]);
	   return(false); 
	}
	cdt=0L;
	if(cfg.dir[f->dir]->misc&DIR_CDTMIN && cur_cps) {
		if(cfg.dir[f->dir]->misc&DIR_CDTUL)
			cdt=((ulong)(f->cost*(cfg.dir[f->dir]->up_pct/100.0))/cur_cps)/60;
		if(cfg.dir[f->dir]->misc&DIR_CDTDL
			&& f->hdr.times_downloaded)  /* all downloads */
			cdt+=((ulong)((long)f->hdr.times_downloaded
				*f->cost*(cfg.dir[f->dir]->dn_pct/100.0))/cur_cps)/60;
Rob Swindell's avatar
Rob Swindell committed
184
185
186
187
188
		adjustuserrec(&cfg,u,U_MIN,10,-cdt);
		sprintf(str,"%lu minute",cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? str : text[No]);
		putsmsg(&cfg,u,tmp);
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
	}
	else {
		if(cfg.dir[f->dir]->misc&DIR_CDTUL)
			cdt=(ulong)(f->cost*(cfg.dir[f->dir]->up_pct/100.0));
		if(cfg.dir[f->dir]->misc&DIR_CDTDL
			&& f->hdr.times_downloaded)  /* all downloads */
			cdt+=(ulong)((long)f->hdr.times_downloaded
				*f->cost*(cfg.dir[f->dir]->dn_pct/100.0));
		if(dir_op(f->dir)) {
			ultoa(cdt, str, 10);
			bputs(text[CreditsToRemove]);
			getstr(str, 10, K_NUMBER|K_LINE|K_EDIT|K_AUTODEL);
			if(sys_status&SS_ABORT)
				return false;
			cdt = atol(str); 
		}
Rob Swindell's avatar
Rob Swindell committed
205
206
207
208
		adjustuserrec(&cfg,u,U_CDT,10,-cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? ultoac(cdt,str) : text[No]);
		putsmsg(&cfg,u,tmp);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
	}

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

/****************************************************************************/
/****************************************************************************/
bool sbbs_t::removefile(smb_t* smb, file_t* f)
{
	char str[256];
	int result;

	if((result = smb_removefile(smb ,f)) == SMB_SUCCESS) {
		SAFEPRINTF4(str,"%s removed %s from %s %s"
			,useron.alias
			,f->name
			,cfg.lib[cfg.dir[smb->dirnum]->lib]->sname,cfg.dir[smb->dirnum]->sname);
		logline("U-",str);
		return true;
	}
	errormsg(WHERE, ERR_REMOVE, f->name, result, smb->last_error);
	return false;
}

/****************************************************************************/
/****************************************************************************/
bool sbbs_t::movefile(smb_t* smb, file_t* f, int newdir)
{
	if(findfile(&cfg, newdir, f->name, NULL)) {
		bprintf(text[FileAlreadyThere], f->name);
		return false; 
	}

244
	if(!addfile(&cfg, newdir, f, f->extdesc, /* client: */NULL))
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
		return false;
	removefile(smb, f);
	bprintf(text[MovedFile],f->name
		,cfg.lib[cfg.dir[newdir]->lib]->sname,cfg.dir[newdir]->sname);
	char str[MAX_PATH+1];
	SAFEPRINTF4(str, "%s moved %s to %s %s",f->name
		,useron.alias
		,cfg.lib[cfg.dir[newdir]->lib]->sname
		,cfg.dir[newdir]->sname);
	logline(nulstr,str);

	/* move actual file */
	char oldpath[MAX_PATH + 1];
	getfilepath(&cfg, f, oldpath);
	f->dir = newdir;
	char newpath[MAX_PATH + 1];
	getfilepath(&cfg, f, newpath);
	mv(oldpath, newpath, /* copy */false); 
	
	return true;
}