Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

file.cpp 8.77 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

34
	current_file = f;
35 36 37 38
	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);
39

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

	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);
56
		}
57 58 59 60
		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));
61
	}
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	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);
	char* p = f->hdr.attr&MSG_ANONYMOUS ? text[UNKNOWN_USER] : f->from;
	if(p != NULL && *p != '\0')
		bprintf(P_TRUNCATE, text[FiUploadedBy], p);
	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);
		long p_mode = P_NOATCODES;
		if(!(console&CON_RAW_IN))
			p_mode |= P_WORDWRAP;
		putmsg(p, p_mode);
		newline();
87
	}
88
	if(f->size == -1) {
89
		bprintf(text[FileIsNotOnline],f->name);
90
		if(SYSOP)
91
			bprintf("%s\r\n",path);
92
	}
93
	current_file = NULL;
94 95 96
}

/****************************************************************************/
97
/* Prompts user for file specification. <CR> is *							*/
98 99 100 101 102 103
/* Returns padded file specification.                                       */
/* Returns NULL if input was aborted.                                       */
/****************************************************************************/
char * sbbs_t::getfilespec(char *str)
{
	bputs(text[FileSpecStarDotStar]);
104 105
	if(!getstr(str, MAX_FILENAME_LEN, K_NONE))
		strcpy(str, ALLFILES);
106 107 108 109 110 111 112 113
	if(sys_status&SS_ABORT)
		return(0);
	return(str);
}

/****************************************************************************/
/* Checks to see if filename matches filespec. Returns 1 if yes, 0 if no    */
/****************************************************************************/
114
extern "C" BOOL filematch(const char *filename, const char *filespec)
115
{
116
	return wildmatchi(filename, filespec, /* path: */FALSE);
117 118 119 120 121 122 123 124 125
}

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

126
	if(fname[0]=='-'
127 128
		|| strcspn(fname,ILLEGAL_FILENAME_CHARS)!=strlen(fname)
		|| strstr(fname, "..") != NULL) {
129
		lprintf(LOG_WARNING,"Suspicious filename attempt: '%s'",fname);
deuce's avatar
deuce committed
130
		hacklog((char *)"Filename", fname);
131 132
		return(false); 
	}
133 134
	d=strlen(fname);
	while(c<d) {
135
		if(fname[c]<=' ' || fname[c]&0x80)
136
			return(false);
137 138
		c++; 
	}
139 140
	return(true);
}
141 142 143 144 145 146 147 148

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;
}
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

/****************************************************************************/
/* 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
172 173 174 175 176
		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);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	}
	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
193 194 195 196
		adjustuserrec(&cfg,u,U_CDT,10,-cdt);
		sprintf(tmp,text[FileRemovedUserMsg]
			,f->name,cdt ? ultoac(cdt,str) : text[No]);
		putsmsg(&cfg,u,tmp);
197 198 199 200 201 202 203 204 205 206 207 208 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 244 245 246 247 248 249 250 251 252 253
	}

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

	if(!addfile(&cfg, newdir, f, f->extdesc))
		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;
}