netmail.cpp 40.1 KB
Newer Older
1 2 3 4 5 6
/* Synchronet network mail-related functions */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
rswindell's avatar
rswindell committed
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 23 24
 *																			*
 * 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"
#include "qwk.h"

Rob Swindell's avatar
Rob Swindell committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
static void pt_zone_kludge(const fmsghdr_t* hdr,int fido)
{
	char str[256];

	sprintf(str,"\1INTL %hu:%hu/%hu %hu:%hu/%hu\r"
		,hdr->destzone,hdr->destnet,hdr->destnode
		,hdr->origzone,hdr->orignet,hdr->orignode);
	write(fido,str,strlen(str));

	if(hdr->destpoint) {
		sprintf(str,"\1TOPT %hu\r"
			,hdr->destpoint);
		write(fido,str,strlen(str)); 
	}

	if(hdr->origpoint) {
		sprintf(str,"\1FMPT %hu\r"
			,hdr->origpoint);
		write(fido,str,strlen(str)); 
	}
}
46

47 48 49 50 51 52 53 54
/****************************************************************************/
/* Returns the FidoNet address (struct) parsed from str (in ASCII text).    */
/****************************************************************************/
static faddr_t atofaddr(scfg_t* cfg, const char *str)
{
	return smb_atofaddr(&cfg->faddr[0], str);
}

55 56 57
/****************************************************************************/
/* Send FidoNet/QWK/Internet NetMail from BBS								*/
/****************************************************************************/
58
bool sbbs_t::netmail(const char *into, const char *title, long mode, smb_t* resmb, smbmsg_t* remsg, str_list_t cc_list)
59 60 61 62 63 64 65 66 67 68 69 70 71 72
{
	char	str[256],fname[128],*buf,*p,ch;
	char	to[256] = "";
	char	from[FIDO_NAME_LEN]= "";
	char	subj[FIDO_SUBJ_LEN]= "";
	char	msgpath[MAX_PATH+1];
	char 	tmp[512];
	const char*	editor=NULL;
	const char*	charset=NULL;
	int		file,x;
	uint	i;
	long	length,l;
	faddr_t src_addr;
	faddr_t dest_addr;
73 74
	char*	net_addr;
	uint16_t net_type = NET_NONE;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	smbmsg_t msg;
	memset(&msg, 0, sizeof(msg));

	if(useron.etoday>=cfg.level_emailperday[useron.level] && !SYSOP && !(useron.exempt&FLAG('M'))) {
		bputs(text[TooManyEmailsToday]);
		return false; 
	}

	if(useron.rest&FLAG('M')) {
		bputs(text[NoNetMailAllowed]);
		return false;
	}
	
	if(title != NULL)
		SAFECOPY(subj, title);
	if(into != NULL)
		SAFECOPY(to, into);
	if(remsg != NULL) {
		if(subj[0] == 0 && remsg->subj != NULL)
			SAFECOPY(subj, remsg->subj);
95
		if(to[0] == 0 && strListCount(cc_list) < 1) {
96 97 98 99 100 101 102 103 104 105 106 107
			if((p = smb_netaddrstr(&remsg->from_net, tmp)) != NULL) {
				if(strchr(p, '@')) {
					SAFECOPY(to, p);
				} else {
					SAFEPRINTF2(to, "%s@%s", remsg->from, p);
				}
			} else {
				SAFECOPY(to, remsg->from);
			}
		}
	}

108 109 110 111
	if(*to == '\0' && cc_list != NULL && (*cc_list) != NULL) {
		SAFECOPY(to, cc_list[0]);
		cc_list++;
	}
112 113
	lookup_netuser(to);

114 115 116 117
	net_addr = to;
	net_type = smb_netaddr_type(net_addr);

	lprintf(LOG_DEBUG, "parsed net type of '%s' is %s", net_addr, smb_nettype((enum smb_net_type)net_type));
118 119 120 121 122
	if(net_type == NET_QWK) {
		if(mode&WM_FILE) {
			bputs(text[EmailFilesNotAllowed]);
			mode&=~WM_FILE;
		}
123
		return qnetmail(net_addr, subj, mode, resmb, remsg);
124 125 126 127 128 129 130 131 132 133
	}
	if(net_type == NET_INTERNET) {
		if(!(cfg.inetmail_misc&NMAIL_ALLOW)) {
			bputs(text[NoNetMailAllowed]);
			return false;
		}
		if(mode&WM_FILE && !SYSOP && !(cfg.inetmail_misc&NMAIL_FILE)) {
			bputs(text[EmailFilesNotAllowed]);
			mode&=~WM_FILE;
		}
134
		return inetmail(to, subj, mode, resmb, remsg, cc_list);
135
	}
136
	SAFECOPY(to, net_addr);
137 138 139
	p=strrchr(to,'@');      /* Find '@' in name@addr */
	if(p==NULL || net_type != NET_FIDO) {
		if(!(sys_status&SS_ABORT))
140
			bprintf(text[InvalidNetMailAddr], net_addr);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
		return false; 
	}
	if(!cfg.total_faddrs || (!SYSOP && !(cfg.netmail_misc&NMAIL_ALLOW))) {
		bputs(text[NoNetMailAllowed]);
		return false; 
	}
	*p=0;					/* Chop off address */
	p++;
	SKIP_WHITESPACE(p);
	dest_addr=atofaddr(&cfg,p); 	/* Get fido address */

	if((mode&WM_FILE) && !SYSOP && !(cfg.netmail_misc&NMAIL_FILE)) {
		bputs(text[EmailFilesNotAllowed]);
		mode&=~WM_FILE;
	}

	truncsp(to);				/* Truncate off space */

159
	SAFECOPY(from, (cfg.netmail_misc&NMAIL_ALIAS) || (useron.rest&FLAG('O')) ? useron.alias : useron.name);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	/* Look-up in nodelist? */

	if(cfg.netmail_cost && !(useron.exempt&FLAG('S'))) {
		if(useron.cdt+useron.freecdt<cfg.netmail_cost) {
			bputs(text[NotEnoughCredits]);
			return(false); 
		}
		sprintf(str,text[NetMailCostContinueQ],cfg.netmail_cost);
		if(noyes(str))
			return(false); 
	}

	for(i=0;i<cfg.total_faddrs;i++)
		if(dest_addr.zone==cfg.faddr[i].zone && dest_addr.net==cfg.faddr[i].net)
			break;
	if(i==cfg.total_faddrs) {
		for(i=0;i<cfg.total_faddrs;i++)
			if(dest_addr.zone==cfg.faddr[i].zone)
				break; 
	}
	if(i >= cfg.total_faddrs)
		i=0;

	if((cfg.netmail_misc&NMAIL_CHSRCADDR) && cfg.total_faddrs > 1) {
		for(uint j=0; j < cfg.total_faddrs; j++)
			uselect(/* add: */TRUE, j, text[OriginFidoAddr], smb_faddrtoa(&cfg.faddr[j], tmp), /* ar: */NULL);
		int choice = uselect(/* add: */FALSE, /* default: */i, NULL, NULL, NULL);
		if(choice < 0)
			return false;
		i = choice;
	}
	src_addr = cfg.faddr[i];

	smb_faddrtoa(&cfg.faddr[i], str);
	bprintf(text[NetMailing], to, smb_faddrtoa(&dest_addr,tmp), from, str);

	if(cfg.netmail_misc&NMAIL_CRASH) msg.hdr.netattr |= MSG_CRASH;
	if(cfg.netmail_misc&NMAIL_HOLD)  msg.hdr.netattr |= MSG_HOLD;
	if(cfg.netmail_misc&NMAIL_KILL)  msg.hdr.netattr |= MSG_KILLSENT;
	if(mode&WM_FILE) msg.hdr.auxattr |= (MSG_FILEATTACH | MSG_KILLFILE); 

	if(remsg != NULL && resmb != NULL && !(mode&WM_QUOTE)) {
		if(quotemsg(resmb, remsg, /* include tails: */true))
			mode |= WM_QUOTE;
	}

	msg_tmp_fname(useron.xedit, msgpath, sizeof(msgpath));
	if(!writemsg(msgpath,nulstr,subj,WM_NETMAIL|mode,INVALID_SUB, to, from, &editor, &charset)) {
		bputs(text[Aborted]);
		return(false); 
	}

	if(mode&WM_FILE) {
		SAFECOPY(fname, subj);
		sprintf(str,"%sfile/%04u.out", cfg.data_dir, useron.number);
216
		(void)MKDIR(str);
217 218 219
		SAFECOPY(tmp, cfg.data_dir);
		if(tmp[0]=='.')    /* Relative path */
			sprintf(tmp,"%s%s", cfg.node_dir, cfg.data_dir);
220
		SAFEPRINTF3(str,"%sfile/%04u.out/%s",tmp,useron.number,fname);
221 222 223 224 225 226 227 228
		SAFECOPY(subj, str);
		if(fexistcase(str)) {
			bputs(text[FileAlreadyThere]);
			return(false); 
		}
		{ /* Remote */
			xfer_prot_menu(XFER_UPLOAD);
			mnemonics(text[ProtocolOrQuit]);
229
			sprintf(str,"%c",quit_key());
230 231 232 233 234 235
			for(x=0;x<cfg.total_prots;x++)
				if(cfg.prot[x]->ulcmd[0] && chk_ar(cfg.prot[x]->ar,&useron,&client)) {
					sprintf(tmp,"%c",cfg.prot[x]->mnemonic);
					strcat(str,tmp); 
				}
			ch=(char)getkeys(str,0);
236
			if(ch==quit_key() || sys_status&SS_ABORT) {
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
				bputs(text[Aborted]);
				return(false); 
			}
			for(x=0;x<cfg.total_prots;x++)
				if(cfg.prot[x]->ulcmd[0] && cfg.prot[x]->mnemonic==ch
					&& chk_ar(cfg.prot[x]->ar,&useron,&client))
					break;
			if(x<cfg.total_prots)	/* This should be always */
				protocol(cfg.prot[x],XFER_UPLOAD,subj,nulstr,true); 
		}
		sprintf(tmp,"%s%s",cfg.temp_dir,subj);
		if(!fexistcase(subj) && fexistcase(tmp))
			mv(tmp,subj,0);
		l=(long)flength(subj);
		if(l>0)
			bprintf(text[FileNBytesReceived],fname,ultoac(l,tmp));
		else {
			bprintf(text[FileNotReceived],fname);
			return(false); 
		} 
	}

259
	msg.hdr.netattr |= MSG_LOCAL; 
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	lprintf(LOG_DEBUG, "NetMail subject: %s", subj);
	p=subj;
	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"CR:",3)) {     /* Crash over-ride by sysop */
		p+=3;				/* skip CR: */
		SKIP_WHITESPACE(p);
		msg.hdr.netattr |= MSG_CRASH; 
	}

	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"FR:",3)) {     /* File request */
		p+=3;				/* skip FR: */
		SKIP_WHITESPACE(p);
		msg.hdr.auxattr |= MSG_FILEREQUEST; 
	}

	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"RR:",3)) {     /* Return receipt request */
		p+=3;				/* skip RR: */
		SKIP_WHITESPACE(p);
		msg.hdr.auxattr |= MSG_RECEIPTREQ; 
	}

	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"FA:",3)) {     /* File Attachment */
		p+=3;				/* skip FA: */
		SKIP_WHITESPACE(p);
//		hdr.attr|=FIDO_FILE;	TODO
	}

	if(p != subj)
		SAFECOPY(subj, p);

	if((file=nopen(msgpath,O_RDONLY))==-1) {
		errormsg(WHERE,ERR_OPEN,msgpath,O_RDONLY);
		return(false); 
	}
	length=(long)filelength(file);
298 299 300 301 302
	if(length < 0) {
		close(file);
		errormsg(WHERE, ERR_LEN, msgpath, length);
		return false;
	}
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	if((buf=(char *)calloc(1, length+1))==NULL) {
		close(file);
		errormsg(WHERE,ERR_ALLOC,str,length);
		return(false); 
	}
	read(file,buf,length);
	close(file);

	smb_net_type_t nettype = NET_FIDO;
	smb_hfield_str(&msg,SENDER, from);
	sprintf(str,"%u",useron.number);
	smb_hfield_str(&msg,SENDEREXT,str);
	smb_hfield(&msg,SENDERNETTYPE, sizeof(nettype), &nettype); 
	smb_hfield(&msg,SENDERNETADDR, sizeof(dest_addr), &src_addr); 

	smb_hfield_str(&msg,RECIPIENT, to);
	smb_hfield(&msg,RECIPIENTNETTYPE, sizeof(nettype), &nettype); 
	smb_hfield(&msg,RECIPIENTNETADDR, sizeof(dest_addr), &dest_addr); 

	smb_hfield_str(&msg,SUBJECT, subj);

	editor_info_to_msg(&msg, editor, charset);

	if(cfg.netmail_misc&NMAIL_DIRECT)
		msg.hdr.netattr |= MSG_DIRECT;

	smb_t smb;
	memset(&smb, 0, sizeof(smb));
	smb.subnum = INVALID_SUB;
332
	int result = savemsg(&cfg, &smb, &msg, &client, server_host_name(), buf, remsg);
333 334 335 336 337 338 339 340 341 342 343 344 345
	free(buf);
	smb_close(&smb);
	smb_freemsgmem(&msg);
	if(result != SMB_SUCCESS) {
		errormsg(WHERE, ERR_WRITE, smb.file, result, smb.last_error);
		return false;
	}

	useron.emails = (ushort)adjustuserrec(&cfg, useron.number, U_EMAILS, 0, 1);
	logon_emails++;
	useron.etoday = (ushort)adjustuserrec(&cfg, useron.number, U_ETODAY, 0, 1);
	if(!(useron.exempt&FLAG('S')))
		subtract_cdt(&cfg,&useron,cfg.netmail_cost);
346 347

	bprintf(text[FidoNetMailSent], to, smb_faddrtoa(&dest_addr,tmp));
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	if(mode&WM_FILE)
		SAFEPRINTF2(str,"sent NetMail file attachment to %s (%s)"
			,to, smb_faddrtoa(&dest_addr,tmp));
	else
		SAFEPRINTF2(str,"sent NetMail to %s (%s)"
			,to, smb_faddrtoa(&dest_addr,tmp));
	logline("EN",str);

	return true;
}

/****************************************************************************/
/* Send NetMail from QWK REP Packet 										*/
/****************************************************************************/
void sbbs_t::qwktonetmail(FILE *rep, char *block, char *into, uchar fromhub)
{
	char	*qwkbuf,to[129],name[129],sender[129],senderaddr[129]
			   ,str[256],*p,*cp,*addr,fulladdr[129],ch;
	char*	sender_id = fromhub ? cfg.qhub[fromhub-1]->id : useron.alias;
	char*	subject = NULL;
	char 	tmp[512];
	int 	i,fido,inet=0,qnet=0;
	uint16_t net;
	uint16_t xlat;
372 373
	long	l,length,m,n;
	off_t offset;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	faddr_t fidoaddr;
    fmsghdr_t hdr;
	smbmsg_t msg;
	struct	tm tm;

	if(useron.rest&FLAG('M')) { 
		bputs(text[NoNetMailAllowed]);
		return; 
	}

	to[0]=0;
	name[0]=0;
	sender[0]=0;
	senderaddr[0]=0;
	fulladdr[0]=0;

	sprintf(str,"%.6s",block+116);
	n=atol(str);	  /* number of 128 byte records */

	if(n<2L || n>999999L) {
		errormsg(WHERE,ERR_CHK,"QWK blocks",n);
		return; 
	}
	// Allocate/zero an extra block of NULs for strchr() usage and other ASCIIZ goodness
	if((qwkbuf=(char *)calloc(n + 1, QWK_BLOCK_LEN))==NULL) {
		errormsg(WHERE,ERR_ALLOC,nulstr,n*QWK_BLOCK_LEN);
		return; 
	}
	memcpy((char *)qwkbuf,block,QWK_BLOCK_LEN);
403
	if(fread(qwkbuf+QWK_BLOCK_LEN, QWK_BLOCK_LEN, n-1, rep) != (size_t)n-1) {
404
		errormsg(WHERE, ERR_READ, "QWK block", n-1);
Rob Swindell's avatar
Rob Swindell committed
405
		free(qwkbuf);
406 407
		return;
	}
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

	size_t kludge_hdrlen = 0;
	char* beg = qwkbuf + QWK_BLOCK_LEN;
	char* end = qwkbuf + (n * QWK_BLOCK_LEN);
	p = beg;
	if(into==NULL) {
		SAFECOPY(to, p);  /* To user on first line */
		char* tp = strchr(to, QWK_NEWLINE);		/* chop off at first CR */
		if(tp != NULL)
			*tp = 0;
		p += strlen(to) + 1;
	}
	else
		SAFECOPY(to, into);

	// Parse QWKE Kludge Lines here:
	while(p < end && *p != QWK_NEWLINE) {
		if(strncmp(p, "To:", 3) == 0) {
			p += 3;
			SKIP_WHITESPACE(p);
			char* tp = strchr(p, QWK_NEWLINE);		/* chop off at first CR */
			if(tp != NULL)
				*tp = 0;
			SAFECOPY(to, p);
			p += strlen(p) + 1;
			continue;
		}
		if(strncmp(p, "Subject:", 8) == 0) {
			p += 8;
			SKIP_WHITESPACE(p);
			char* tp = strchr(p, QWK_NEWLINE);		/* chop off at first CR */
			if(tp != NULL)
				*tp = 0;
			subject = p;
			p += strlen(p) + 1;
			continue;
		}
		break;
	}
	kludge_hdrlen += (p - beg) + 1;

	SAFECOPY(name,to);
	p=strchr(name,'@');
	if(p) *p=0;
	truncsp(name);


	p=strrchr(to,'@');       /* Find '@' in name@addr */
456
	if(p && !IS_DIGIT(*(p+1)) && !strchr(p,'.') && !strchr(p,':')) { /* QWKnet */
457 458 459
		qnet=1;
		*p=0; 
	}
460
	else if(p==NULL || !IS_DIGIT(*(p+1)) || !cfg.total_faddrs) {
461 462 463
		if(cfg.inetmail_misc&NMAIL_ALLOW) {	/* Internet */
			inet=1;
		} else {
464
			bprintf(text[InvalidNetMailAddr], to);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
			free(qwkbuf);
			return; 
		} 
	}
	else {
		fidoaddr=atofaddr(&cfg,p+1); 	/* Get fido address */
		*p=0;					/* Chop off address */
	}


	if(!inet && !qnet &&		/* FidoNet */
		((!SYSOP && !(cfg.netmail_misc&NMAIL_ALLOW)) || !cfg.total_faddrs)) {
		bputs(text[NoNetMailAllowed]);
		free(qwkbuf);
		return; 
	}

	truncsp(to);			/* Truncate off space */

	if(!stricmp(to,"SBBS") && !SYSOP && qnet) {
		free(qwkbuf);
		return; 
	}

	l = QWK_BLOCK_LEN + kludge_hdrlen;		/* Start of message text */

	if(qnet || inet) {

		memset(&msg,0,sizeof(smbmsg_t));
		msg.hdr.version=smb_ver();
		msg.hdr.when_imported.time=time32(NULL);
		msg.hdr.when_imported.zone=sys_timezone(&cfg);

		if(fromhub || useron.rest&FLAG('Q')) {
			net=NET_QWK;
			smb_hfield(&msg,SENDERNETTYPE,sizeof(net),&net);
			if(!strncmp(qwkbuf+l,"@VIA:",5)) {
				sprintf(str,"%.128s",qwkbuf+l+5);
				cp=strchr(str,QWK_NEWLINE);
				if(cp) *cp=0;
				l+=strlen(str)+1;
				cp=str;
				while(*cp && *cp<=' ') cp++;
Rob Swindell's avatar
Rob Swindell committed
508
				safe_snprintf(senderaddr, sizeof(senderaddr), "%s/%s",sender_id,cp);
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
				strupr(senderaddr);
				smb_hfield(&msg,SENDERNETADDR,strlen(senderaddr),senderaddr); 
			}
			else {
				SAFECOPY(senderaddr, sender_id);
				strupr(senderaddr);
				smb_hfield(&msg,SENDERNETADDR,strlen(senderaddr),senderaddr); 
			}
			sprintf(sender,"%.25s",block+46);     /* From name */
		}
		else {	/* Not Networked */
			msg.hdr.when_written.zone=sys_timezone(&cfg);
			sprintf(str,"%u",useron.number);
			smb_hfield(&msg,SENDEREXT,strlen(str),str);
			SAFECOPY(sender,(qnet || cfg.inetmail_misc&NMAIL_ALIAS)
				? useron.alias : useron.name);
			}
		truncsp(sender);
		smb_hfield(&msg,SENDER,strlen(sender),sender);
		if(fromhub)
			msg.idx.from=0;
		else
			msg.idx.from=useron.number;
		if(!strncmp(qwkbuf+l,"@TZ:",4)) {
			sprintf(str,"%.128s",qwkbuf+l);
			cp=strchr(str,QWK_NEWLINE);
			if(cp) *cp=0;
			l+=strlen(str)+1;
			cp=str+4;
			while(*cp && *cp<=' ') cp++;
			msg.hdr.when_written.zone=(short)ahtoul(cp); 
		}
		else
			msg.hdr.when_written.zone=sys_timezone(&cfg);
		memset(&tm,0,sizeof(tm));
		tm.tm_mon=((qwkbuf[8]&0xf)*10)+(qwkbuf[9]&0xf);
		if(tm.tm_mon) tm.tm_mon--;	/* 0 based */
		tm.tm_mday=((qwkbuf[11]&0xf)*10)+(qwkbuf[12]&0xf);
		tm.tm_year=((qwkbuf[14]&0xf)*10)+(qwkbuf[15]&0xf);
		if(tm.tm_year<Y2K_2DIGIT_WINDOW)
			tm.tm_year+=100;
		tm.tm_hour=((qwkbuf[16]&0xf)*10)+(qwkbuf[17]&0xf);
		tm.tm_min=((qwkbuf[19]&0xf)*10)+(qwkbuf[20]&0xf);  /* From QWK time */
		tm.tm_sec=0;

		tm.tm_isdst=-1;	/* Do not adjust for DST */
		msg.hdr.when_written.time=mktime32(&tm);

		if(subject == NULL) {
			sprintf(str, "%.25s", block+71);
			subject = str;
		}
		smb_hfield_str(&msg, SUBJECT, subject);
	}

	if(qnet) {

		p++;
		addr=p;
		msg.idx.to=qwk_route(&cfg,addr,fulladdr, sizeof(fulladdr)-1);
		if(!fulladdr[0]) {		/* Invalid address, so BOUNCE it */
		/**
			errormsg(WHERE,ERR_CHK,addr,0);
			free(qwkbuf);
			smb_freemsgmem(msg);
			return;
		**/
			smb_hfield(&msg,SENDER,strlen(cfg.sys_id),cfg.sys_id);
			msg.idx.from=0;
			msg.idx.to=useron.number;
			SAFECOPY(to,sender);
			SAFECOPY(fulladdr,senderaddr);
			SAFEPRINTF(str,"BADADDR: %s",addr);
			smb_hfield(&msg,SUBJECT,strlen(str),str);
			net=NET_NONE;
			smb_hfield(&msg,SENDERNETTYPE,sizeof(net),&net);
		}
		/* This is required for fixsmb to be able to rebuild the index */
		SAFEPRINTF(str,"%u",msg.idx.to);
		smb_hfield_str(&msg,RECIPIENTEXT,str);

		smb_hfield(&msg,RECIPIENT,strlen(name),name);
		net=NET_QWK;
		smb_hfield(&msg,RECIPIENTNETTYPE,sizeof(net),&net);

		truncsp(fulladdr);
		if(fulladdr[0])
			smb_hfield(&msg,RECIPIENTNETADDR,strlen(fulladdr),fulladdr);

		bprintf(text[NetMailing],to,fulladdr,sender,cfg.sys_id); 
	}

	if(inet) {				/* Internet E-mail */

		if(cfg.inetmail_cost && !(useron.exempt&FLAG('S'))) {
			if(useron.cdt+useron.freecdt<cfg.inetmail_cost) {
				bputs(text[NotEnoughCredits]);
				free(qwkbuf);
				smb_freemsgmem(&msg);
				return; 
			}
			sprintf(str,text[NetMailCostContinueQ],cfg.inetmail_cost);
			if(noyes(str)) {
				free(qwkbuf);
				smb_freemsgmem(&msg);
				return; 
			} 
		}

		net=NET_INTERNET;
		smb_hfield(&msg,RECIPIENT,strlen(name),name);
		msg.idx.to=0;   /* Out-bound NetMail set to 0 */
		smb_hfield(&msg,RECIPIENTNETTYPE,sizeof(net),&net);
		smb_hfield(&msg,RECIPIENTNETADDR,strlen(to),to);

		bprintf(text[NetMailing],name,to
625
			,(cfg.inetmail_misc&NMAIL_ALIAS) || (useron.rest&FLAG('O')) ? useron.alias : useron.name
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
			,cfg.sys_inetaddr); 
	}

	if(qnet || inet) {

		bputs(text[WritingIndx]);

		if((i=smb_stack(&smb,SMB_STACK_PUSH))!=0) {
			errormsg(WHERE,ERR_OPEN,"MAIL",i);
			free(qwkbuf);
			smb_freemsgmem(&msg);
			return; 
		}
		sprintf(smb.file,"%smail", cfg.data_dir);
		smb.retry_time=cfg.smb_retry_time;
		smb.subnum=INVALID_SUB;
		if((i=smb_open(&smb))!=0) {
			smb_stack(&smb,SMB_STACK_POP);
			errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
			free(qwkbuf);
			smb_freemsgmem(&msg);
			return; 
		}
		add_msg_ids(&cfg, &smb, &msg, /* remsg: */NULL);

		if(smb_fgetlength(smb.shd_fp)<1L) {   /* Create it if it doesn't exist */
			smb.status.max_crcs=cfg.mail_maxcrcs;
			smb.status.max_msgs=0;
			smb.status.max_age=cfg.mail_maxage;
			smb.status.attr=SMB_EMAIL;
			if((i=smb_create(&smb))!=0) {
				smb_close(&smb);
				smb_stack(&smb,SMB_STACK_POP);
				errormsg(WHERE,ERR_CREATE,smb.file,i,smb.last_error);
				free(qwkbuf);
				smb_freemsgmem(&msg);
				return; 
			} 
		}

		length=n*256L;	// Extra big for CRLF xlat, was (n-1L)*256L (03/16/96)


		if(length&0xfff00000UL || !length) {
			smb_close(&smb);
			smb_stack(&smb,SMB_STACK_POP);
			sprintf(str,"REP msg (%ld)",n);
			errormsg(WHERE,ERR_LEN,str,length);
			free(qwkbuf);
			smb_freemsgmem(&msg);
			return; 
		}

		if((i=smb_open_da(&smb))!=0) {
			smb_close(&smb);
			smb_stack(&smb,SMB_STACK_POP);
			errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
			free(qwkbuf);
			smb_freemsgmem(&msg);
			return; 
		}
		if(cfg.sys_misc&SM_FASTMAIL)
			offset=smb_fallocdat(&smb,length,1);
		else
			offset=smb_allocdat(&smb,length,1);
		smb_close_da(&smb);

		smb_fseek(smb.sdt_fp,offset,SEEK_SET);
		xlat=XLAT_NONE;
		smb_fwrite(&smb,&xlat,2,smb.sdt_fp);
		m=2;
		for(;l<n*QWK_BLOCK_LEN && m<length;l++) {
			if(qwkbuf[l]==0 || qwkbuf[l]==LF)
				continue;
			if(qwkbuf[l]==QWK_NEWLINE) {
				if(m <= 2)	/* Ignore blank lines at top of message */
					continue;
				smb_fwrite(&smb,crlf,2,smb.sdt_fp);
				m+=2;
				continue; 
			}
			smb_fputc(qwkbuf[l],smb.sdt_fp);
			m++; 
		}

		for(ch=0;m<length;m++)			/* Pad out with NULLs */
			smb_fputc(ch,smb.sdt_fp);
		smb_fflush(smb.sdt_fp);

715
		msg.hdr.offset=(uint32_t)offset;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741

		smb_dfield(&msg,TEXT_BODY,length);

		i=smb_addmsghdr(&smb,&msg,smb_storage_mode(&cfg, &smb));
		smb_close(&smb);
		smb_stack(&smb,SMB_STACK_POP);

		smb_freemsgmem(&msg);
		if(i!=SMB_SUCCESS) {
			errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error); 
			smb_freemsgdat(&smb,offset,length,1);
		}
		else {		/* Successful */
			if(inet) {
				if(cfg.inetmail_sem[0]) 	 /* update semaphore file */
					ftouch(cmdstr(cfg.inetmail_sem,nulstr,nulstr,NULL));
				if(!(useron.exempt&FLAG('S')))
					subtract_cdt(&cfg,&useron,cfg.inetmail_cost); 
			}

			useron.emails++;
			logon_emails++;
			putuserrec(&cfg,useron.number,U_EMAILS,5,ultoa(useron.emails,tmp,10)); 
			useron.etoday++;
			putuserrec(&cfg,useron.number,U_ETODAY,5,ultoa(useron.etoday,tmp,10));

742 743 744 745
			if(qnet)
				bprintf(text[QWKNetMailSent], name, fulladdr);
			else
				bprintf(text[InternetMailSent], name, to);
746 747 748 749 750 751 752 753 754 755 756 757 758 759
			safe_snprintf(str,sizeof(str), "%s (%s) sent %s NetMail to %s (%s) via QWK"
				,sender, sender_id
				,qnet ? "QWK":"Internet",name,qnet ? fulladdr : to);
			logline("EN",str); 
		}

		free((char *)qwkbuf);
		return; 
	}


	/****************************** FidoNet **********************************/

	if(!fidoaddr.zone || !cfg.netmail_dir[0]) {  // No fido netmail allowed
760
		bprintf(text[InvalidNetMailAddr], to);
761 762 763 764 765 766 767 768 769 770 771 772 773 774
		free(qwkbuf);
		return; 
	}

	memset(&hdr,0,sizeof(hdr));   /* Initialize header to null */

	if(fromhub || useron.rest&FLAG('Q')) {
		sprintf(str,"%.25s",block+46);              /* From */
		truncsp(str);
		sprintf(tmp,"@%s",sender_id);
		strupr(tmp);
		strcat(str,tmp); 
	}
	else
775
		SAFECOPY(str,(cfg.netmail_misc&NMAIL_ALIAS) || (useron.rest&FLAG('O')) ? useron.alias : useron.name);
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	SAFECOPY(hdr.from,str);

	SAFECOPY(hdr.to,to);

	/* Look-up in nodelist? */

	if(cfg.netmail_cost && !(useron.exempt&FLAG('S'))) {
		if(useron.cdt+useron.freecdt<cfg.netmail_cost) {
			bputs(text[NotEnoughCredits]);
			free(qwkbuf);
			return; 
		}
		sprintf(str,text[NetMailCostContinueQ],cfg.netmail_cost);
		if(noyes(str)) {
			free(qwkbuf);
			return; 
		} 
	}

	hdr.destzone	=fidoaddr.zone;
	hdr.destnet 	=fidoaddr.net;
	hdr.destnode	=fidoaddr.node;
	hdr.destpoint	=fidoaddr.point;

	for(i=0;i<cfg.total_faddrs;i++)
		if(fidoaddr.zone==cfg.faddr[i].zone && fidoaddr.net==cfg.faddr[i].net)
			break;
	if(i==cfg.total_faddrs) {
		for(i=0;i<cfg.total_faddrs;i++)
			if(fidoaddr.zone==cfg.faddr[i].zone)
				break; 
	}
	if(i==cfg.total_faddrs)
		i=0;
	hdr.origzone	=cfg.faddr[i].zone;
	hdr.orignet 	=cfg.faddr[i].net;
	hdr.orignode	=cfg.faddr[i].node;
	hdr.origpoint   =cfg.faddr[i].point;

	smb_faddrtoa(&cfg.faddr[i],str);
	bprintf(text[NetMailing],hdr.to,smb_faddrtoa(&fidoaddr,tmp),hdr.from,str);
	tm.tm_mon=((qwkbuf[8]&0xf)*10)+(qwkbuf[9]&0xf);
	if (tm.tm_mon) tm.tm_mon--;
	tm.tm_mday=((qwkbuf[11]&0xf)*10)+(qwkbuf[12]&0xf);
	tm.tm_year=((qwkbuf[14]&0xf)*10)+(qwkbuf[15]&0xf)+1900;
	tm.tm_hour=((qwkbuf[16]&0xf)*10)+(qwkbuf[17]&0xf);
	tm.tm_min=((qwkbuf[19]&0xf)*10)+(qwkbuf[20]&0xf);		/* From QWK time */
	tm.tm_sec=0;
	sprintf(hdr.time,"%02u %3.3s %02u  %02u:%02u:%02u"          /* To FidoNet */
		,tm.tm_mday,mon[tm.tm_mon],TM_YEAR(tm.tm_year)
		,tm.tm_hour,tm.tm_min,tm.tm_sec);
	hdr.attr=(FIDO_LOCAL|FIDO_PRIVATE);

	if(cfg.netmail_misc&NMAIL_CRASH) hdr.attr|=FIDO_CRASH;
	if(cfg.netmail_misc&NMAIL_HOLD)  hdr.attr|=FIDO_HOLD;
	if(cfg.netmail_misc&NMAIL_KILL)  hdr.attr|=FIDO_KILLSENT;

	sprintf(str,"%.25s",block+71);      /* Title */
	truncsp(str);
	p=str;
	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"CR:",3)) {     /* Crash over-ride by sysop */
		p+=3;               /* skip CR: */
		if(*p==' ') p++;     /* skip extra space if it exists */
		hdr.attr|=FIDO_CRASH; 
	}

	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"FR:",3)) {     /* File request */
		p+=3;               /* skip FR: */
		if(*p==' ') p++;
		hdr.attr|=FIDO_FREQ; 
	}

	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"RR:",3)) {     /* Return receipt request */
		p+=3;               /* skip RR: */
		if(*p==' ') p++;
		hdr.attr|=FIDO_RRREQ; 
	}

	if((SYSOP || useron.exempt&FLAG('F'))
		&& !strnicmp(p,"FA:",3)) {     /* File attachment */
		p+=3;				/* skip FA: */
		if(*p==' ') p++;
		hdr.attr|=FIDO_FILE; 
	}

	if(subject != NULL)
		SAFECOPY(hdr.subj, subject);
	else
		SAFECOPY(hdr.subj, p);

	md(cfg.netmail_dir);
	for(i=1;i;i++) {
		sprintf(str,"%s%u.msg", cfg.netmail_dir,i);
		if(!fexistcase(str))
			break; 
	}
	if(!i) {
		bputs(text[TooManyEmailsToday]);
		return; 
	}
	if((fido=nopen(str,O_WRONLY|O_CREAT|O_EXCL))==-1) {
		free(qwkbuf);
		errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_CREAT|O_EXCL);
		return; 
	}
	write(fido,&hdr,sizeof(hdr));

Rob Swindell's avatar
Rob Swindell committed
886
	pt_zone_kludge(&hdr,fido);
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

	if(cfg.netmail_misc&NMAIL_DIRECT) {
		sprintf(str,"\1FLAGS DIR\r\n");
		write(fido,str,strlen(str)); 
	}

	l = QWK_BLOCK_LEN + kludge_hdrlen;

	length=n*QWK_BLOCK_LEN;
	while(l<length) {
		if(qwkbuf[l]==CTRL_A) {   /* Ctrl-A, so skip it and the next char */
			l++;
			if(l>=length)
				break;
		}
		else if(qwkbuf[l]!=LF) {
			if(qwkbuf[l]==QWK_NEWLINE) /* QWK cr/lf char converted to hard CR */
				qwkbuf[l]=CR;
			write(fido,(char *)qwkbuf+l,1); 
		}
		l++;
	}
	l=0;
910
	write(fido,(BYTE*)&l,sizeof(BYTE));	/* Null terminator */
911 912 913 914 915 916 917 918 919 920 921 922 923
	close(fido);
	free((char *)qwkbuf);
	if(cfg.netmail_sem[0])		/* update semaphore file */
		ftouch(cmdstr(cfg.netmail_sem,nulstr,nulstr,NULL));
	if(!(useron.exempt&FLAG('S')))
		subtract_cdt(&cfg,&useron,cfg.netmail_cost);

	useron.emails++;
	logon_emails++;
	putuserrec(&cfg,useron.number,U_EMAILS,5,ultoa(useron.emails,tmp,10)); 
	useron.etoday++;
	putuserrec(&cfg,useron.number,U_ETODAY,5,ultoa(useron.etoday,tmp,10));

924
	bprintf(text[FidoNetMailSent], hdr.to, smb_faddrtoa(&fidoaddr,tmp));
925 926 927 928 929 930
	sprintf(str,"%s sent NetMail to %s @%s via QWK"
		,sender_id
		,hdr.to,smb_faddrtoa(&fidoaddr,tmp));
	logline("EN",str);
}

931
/****************************************************************************/
932
/* Internet mail															*/
933
/****************************************************************************/
934
bool sbbs_t::inetmail(const char *into, const char *subj, long mode, smb_t* resmb, smbmsg_t* remsg, str_list_t cc_list)
935
{
936
	char	str[256],str2[256],msgpath[256],ch, *p;
937
	char 	tmp[512];
938 939 940
	char	title[256] = "";
	char	name[256] = "";
	char	addr[256] = "";
941 942
	const char*	editor=NULL;
	const char*	charset=NULL;
943
	char	your_addr[128];
944
	int 	i,x,file;
945 946 947 948
	long	l;
	FILE	*instream;
	smbmsg_t msg;

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	if((!SYSOP && !(cfg.inetmail_misc&NMAIL_ALLOW)) || useron.rest&FLAG('M')) {
		bputs(text[NoNetMailAllowed]);
		return false;
	}

	str_list_t rcpt_list = strListInit();
	if(into != NULL && *into)
		strListPush(&rcpt_list, into);
	for(i = 0; cc_list != NULL && cc_list[i] != NULL; i++) {
		char* p = cc_list[i];
		char* at = strchr(p, '@');
		if(at == NULL) {
			bprintf(text[InvalidNetMailAddr], p);
			continue;
		}
		while(at > p && *at > ' ')
			at--;
		p = at;
		SKIP_WHITESPACE(p);
		uint16_t net_type = smb_netaddr_type(p);
		if(net_type != NET_INTERNET) {
			bprintf(text[InvalidNetMailAddr], p);
			break;
		}
		strListPush(&rcpt_list, p);
974
	}
975 976
	strListStripStrings(rcpt_list, "<>");
	size_t rcpt_count = strListDedupe(&rcpt_list, /* case-sensitive */FALSE);
977

978 979 980 981 982
	if(useron.etoday + rcpt_count > cfg.level_emailperday[useron.level] && !SYSOP && !(useron.exempt&FLAG('M'))) {
		strListFree(&rcpt_list);
		bputs(text[TooManyEmailsToday]);
		return false; 
	}
983

984 985 986 987 988
	if(subj != NULL)
		SAFECOPY(title,subj);
	if(remsg != NULL) {
		if(title[0] == 0 && remsg->subj != NULL)
			SAFECOPY(title, remsg->subj);
989 990 991 992
		if(remsg->from_net.addr != NULL && rcpt_count < 1) {
			strListPush(&rcpt_list, smb_netaddrstr(&remsg->from_net, addr));
			rcpt_count = 1;
		}
993
	}
994

995 996
	/* Get this user's Internet mailing address */
	usermailaddr(&cfg,your_addr
997
		,(cfg.inetmail_misc&NMAIL_ALIAS) || (useron.rest&FLAG('O')) ? useron.alias : useron.name);
998 999 1000 1001 1002 1003 1004

	if(rcpt_count > 1) { /* remove "self" from reply-all list */
		int found = strListFind(rcpt_list, your_addr, /* case_sensitive */FALSE);
		if(found >= 0) {
			strListDelete(&rcpt_list, found);
			rcpt_count--;
		}
rswindell's avatar
rswindell committed
1005
	}
1006

1007
	if(rcpt_count < 1) {
1008 1009
		bprintf(text[InvalidNetMailAddr], into);
		strListFree(&rcpt_list);
1010 1011 1012
		return false;
	}

1013
	if(cfg.inetmail_cost && !(useron.exempt&FLAG('S'))) {
1014 1015
		if(useron.cdt+useron.freecdt < cfg.inetmail_cost * rcpt_count) {
			strListFree(&rcpt_list);
1016
			bputs(text[NotEnoughCredits]);
rswindell's avatar
rswindell committed
1017 1018
			return(false); 
		}
1019 1020 1021 1022 1023
		SAFEPRINTF(str, text[NetMailCostContinueQ], cfg.inetmail_cost * rcpt_count);
		if(noyes(str)) {
			strListFree(&rcpt_list);
			return(false);
		}
rswindell's avatar
rswindell committed
1024
	}
1025

1026 1027
	char to_list[512];
	strListJoin(rcpt_list, to_list, sizeof(to_list), ", ");
1028

1029
	bprintf(text[InternetMailing], to_list, your_addr);
1030 1031 1032
	action=NODE_SMAL;
	nodesync();

1033 1034 1035 1036 1037
	if(remsg != NULL && resmb != NULL && !(mode&WM_QUOTE)) {
		if(quotemsg(resmb, remsg, /* include tails: */true))
			mode |= WM_QUOTE;
	}

1038
	SAFEPRINTF(msgpath,"%snetmail.msg",cfg.node_dir);
1039 1040
	if(!writemsg(msgpath,nulstr,title,WM_NETMAIL|mode,INVALID_SUB, to_list, /* from: */your_addr, &editor, &charset)) {
		strListFree(&rcpt_list);
1041
		bputs(text[Aborted]);
rswindell's avatar
rswindell committed
1042 1043
		return(false); 
	}
1044 1045

	if(mode&WM_FILE) {
1046
		SAFEPRINTF2(str2,"%sfile/%04u.out",cfg.data_dir,useron.number);
1047
		(void)MKDIR(str2);
1048
		SAFEPRINTF3(str2,"%sfile/%04u.out/%s",cfg.data_dir,useron.number,title);
1049
		if(fexistcase(str2)) {
1050
			strListFree(&rcpt_list);
1051 1052
			bputs(text[FileAlreadyThere]);
			remove(msgpath);
rswindell's avatar
rswindell committed
1053 1054
			return(false); 
		}
rswindell's avatar
rswindell committed
1055
		{ /* Remote */
1056
			xfer_prot_menu(XFER_UPLOAD);
1057
			mnemonics(text[ProtocolOrQuit]);
1058
			SAFEPRINTF(str,"%c",quit_key());
1059
			for(x=0;x<cfg.total_prots;x++)
rswindell's avatar
rswindell committed
1060
				if(cfg.prot[x]->ulcmd[0] && chk_ar(cfg.prot[x]->ar,&useron,&client)) {
1061
					SAFEPRINTF(tmp,"%c",cfg.prot[x]->mnemonic);
rswindell's avatar
rswindell committed
1062
					SAFECAT(str,tmp); 
rswindell's avatar
rswindell committed
1063
				}
1064
			ch=(char)getkeys(str,0);
1065
			if(ch==quit_key() || sys_status&SS_ABORT) {
1066
				bputs(text[Aborted]);
1067
				strListFree(&rcpt_list);
1068
				remove(msgpath);
rswindell's avatar
rswindell committed
1069 1070
				return(false); 
			}
1071 1072
			for(x=0;x<cfg.total_prots;x++)
				if(cfg.prot[x]->ulcmd[0] && cfg.prot[x]->mnemonic==ch
rswindell's avatar
rswindell committed
1073
					&& chk_ar(cfg.prot[x]->ar,&useron,&client))
1074 1075
					break;
			if(x<cfg.total_prots)	/* This should be always */
1076
				protocol(cfg.prot[x],XFER_UPLOAD,str2,nulstr,true); 
1077
		}
1078
		SAFEPRINTF2(tmp,"%s%s",cfg.temp_dir,title);
1079 1080
		if(!fexistcase(str2) && fexistcase(tmp))
			mv(tmp,str2,0);
1081
		l=(long)flength(str2);
1082 1083 1084 1085
		if(l>0)
			bprintf(text[FileNBytesReceived],title,ultoac(l,tmp));
		else {
			bprintf(text[FileNotReceived],title);
1086
			strListFree(&rcpt_list);
1087
			remove(msgpath);
rswindell's avatar
rswindell committed
1088 1089 1090
			return(false); 
		} 
	}
1091

1092
	smb_t smb;
1093
	if((i=smb_open_sub(&cfg, &smb, INVALID_SUB))!=SMB_SUCCESS) {
1094
		strListFree(&rcpt_list);
1095
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
rswindell's avatar
rswindell committed
1096 1097
		return(false); 
	}
1098

1099
	if((instream=fnopen(&file,msgpath,O_RDONLY|O_BINARY))==NULL) {
1100
		strListFree(&rcpt_list);
1101
		smb_close(&smb);
1102
		errormsg(WHERE,ERR_OPEN,msgpath,O_RDONLY|O_BINARY);
rswindell's avatar
rswindell committed
1103 1104
		return(false); 
	}
1105
	long length = (long)filelength(file);
1106
	if(length < 1) {
1107
		strListFree(&rcpt_list);
1108
		fclose(instream);
1109
		smb_close(&smb);
1110
		errormsg(WHERE, ERR_LEN, msgpath, length);
rswindell's avatar
rswindell committed
1111 1112
		return(false); 
	}
1113 1114
	char* msgbuf;
	if((msgbuf = (char*)malloc(length + 1)) == NULL) {
1115
		strListFree(&rcpt_list);
1116
		fclose(instream);
1117
		smb_close(&smb);
1118
		errormsg(WHERE, ERR_ALLOC, msgpath, length);
rswindell's avatar
rswindell committed
1119 1120
		return(false); 
	}
1121
	if(fread(msgbuf, sizeof(char), length, instream) != (size_t)length) {
1122
		strListFree(&rcpt_list);
1123 1124
		fclose(instream);
		free(msgbuf);
1125
		smb_close(&smb);
1126
		errormsg(WHERE, ERR_READ, msgpath, length);
rswindell's avatar
rswindell committed
1127 1128
		return(false); 
	}
1129
	msgbuf[length] = 0;
1130 1131 1132 1133 1134
	fclose(instream);

	memset(&msg,0,sizeof(smbmsg_t));
	msg.hdr.version=smb_ver();
	if(mode&WM_FILE)
1135
		msg.hdr.auxattr |= (MSG_FILEATTACH | MSG_KILLFILE);
1136
	msg.hdr.when_written.time=msg.hdr.when_imported.time=time32(NULL);
1137
	msg.hdr.when_written.zone=msg.hdr.when_imported.zone=sys_timezone(&cfg);
1138

1139 1140 1141 1142
	msg.hdr.netattr |= MSG_LOCAL;
	if(cfg.inetmail_misc&NMAIL_KILL)
		msg.hdr.netattr |= MSG_KILLSENT;

1143 1144
	if(rcpt_count > 1)
		smb_hfield_str(&msg, RECIPIENTLIST, to_list);
1145

1146
	smb_hfield_str(&msg,SENDER,(cfg.inetmail_misc&NMAIL_ALIAS) || (useron.rest&FLAG('O')) ? useron.alias : useron.name);
1147

1148
	SAFEPRINTF(str,"%u",useron.number);
1149
	smb_hfield_str(&msg,SENDEREXT,str);
1150

1151
	/* Security logging */
1152
	msg_client_hfields(&msg,&client);
1153
	smb_hfield_str(&msg,SENDERSERVER, server_host_name());
1154

1155
	smb_hfield_str(&msg,SUBJECT,title);
1156

1157
	editor_info_to_msg(&msg, editor, charset);
1158

1159
	i = savemsg(&cfg, &smb, &msg, &client, server_host_name(), msgbuf, remsg);
1160
	free(msgbuf);
1161

1162
	if(i!=SMB_SUCCESS) {
1163 1164 1165
		smb_close(&smb);
		smb_freemsgmem(&msg);
		strListFree(&rcpt_list);
1166
		errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
1167 1168
		return(false); 
	}
1169

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	for(rcpt_count = 0; rcpt_list[rcpt_count] != NULL; rcpt_count++) {

		uint16_t net_type = smb_netaddr_type(rcpt_list[rcpt_count]);
		if(net_type != NET_INTERNET) {
			bprintf(text[InvalidNetMailAddr], rcpt_list[rcpt_count]);
			break;
		}

		/* Get destination user address */
		if((p = strrchr(rcpt_list[rcpt_count], '<')) != NULL) {
			p++;
			SKIP_WHITESPACE(p);
			SAFECOPY(addr, p);
			p = strrchr(addr, '>');
			if(p == NULL) {
				bprintf(text[InvalidNetMailAddr], rcpt_list[rcpt_count]);
				break;
			}
			*p = 0;
		} else {
			p = rcpt_list[rcpt_count];
			SKIP_WHITESPACE(p);
			SAFECOPY(addr, p);
		}
		truncsp(addr);

		/* Get destination user name */
		p = rcpt_list[rcpt_count];
		SKIP_WHITESPACE(p);
		SAFECOPY(name, p);
		p=strrchr(name,'<');
		if(!p)
			p=strrchr(name,'@');
		if(!p)
			p=strrchr(name,'!');
		if(p) {
			*p=0;
			if(name[0] == 0)
				SAFECOPY(name, addr);
		}
		truncsp(name);

		smbmsg_t newmsg;
		if((i = smb_copymsgmem(&smb, &newmsg, &msg)) != SMB_SUCCESS) {
			errormsg(WHERE, "copying", "message header", i, smb.last_error);
			break;
		}

		smb_hfield_str(&newmsg, RECIPIENT, name);
		smb_hfield(&newmsg, RECIPIENTNETTYPE, sizeof(net_type), &net_type);
		smb_hfield_str(&newmsg, RECIPIENTNETADDR, addr);
		i=smb_addmsghdr(&smb, &newmsg, smb_storage_mode(&cfg, &smb));
		smb_freemsgmem(&newmsg);
		if(i != SMB_SUCCESS) {
			errormsg(WHERE, "adding", "message header", i, smb.last_error);
			break;
		}
	}

	if(rcpt_count > 1)
		smb_incmsg_dfields(&smb, &msg, (uint16_t)(rcpt_count - 1));

	smb_close(&smb);

	smb_freemsgmem(&msg);
	strListFree(&rcpt_list);

1237 1238 1239 1240
	if(mode&WM_FILE && online==ON_REMOTE)
		autohangup();

	if(cfg.inetmail_sem[0]) 	 /* update semaphore file */
1241
		ftouch(cmdstr(cfg.inetmail_sem,nulstr,nulstr,NULL));
1242

1243
	if(!(useron.exempt&FLAG('S')))
1244
		subtract_cdt(&cfg,&useron,cfg.inetmail_cost * rcpt_count);
1245

1246 1247
	useron.emails += (ushort)rcpt_count;
	logon_emails += rcpt_count;
1248
	putuserrec(&cfg,useron.number,U_EMAILS,5,ultoa(useron.emails,tmp,10)); 
1249
	useron.etoday += (ushort)rcpt_count;
1250
	putuserrec(&cfg,useron.number,U_ETODAY,5,ultoa(useron.etoday,tmp,10));
1251