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

netmail.cpp 39.2 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"

25 26
void pt_zone_kludge(fmsghdr_t hdr,int fido);

27 28 29 30 31 32 33 34
/****************************************************************************/
/* 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);
}

35 36 37
/****************************************************************************/
/* Send FidoNet/QWK/Internet NetMail from BBS								*/
/****************************************************************************/
38
bool sbbs_t::netmail(const char *into, const char *title, long mode, smb_t* resmb, smbmsg_t* remsg, str_list_t cc_list)
39 40 41 42 43 44 45 46 47 48 49 50 51 52
{
	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;
53 54
	char*	net_addr;
	uint16_t net_type = NET_NONE;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	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);
75
		if(to[0] == 0 && strListCount(cc_list) < 1) {
76 77 78 79 80 81 82 83 84 85 86 87 88 89
			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);
			}
		}
	}

	lookup_netuser(to);

90 91 92 93 94 95
	net_addr = to;
	if(*net_addr == '\0' && cc_list != NULL)
		net_addr = cc_list[0];
	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));
96 97 98 99 100
	if(net_type == NET_QWK) {
		if(mode&WM_FILE) {
			bputs(text[EmailFilesNotAllowed]);
			mode&=~WM_FILE;
		}
101
		return qnetmail(net_addr, subj, mode, resmb, remsg);
102 103 104 105 106 107 108 109 110 111
	}
	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;
		}
112
		return inetmail(to, subj, mode, resmb, remsg, cc_list);
113
	}
114
	SAFECOPY(to, net_addr);
115 116 117
	p=strrchr(to,'@');      /* Find '@' in name@addr */
	if(p==NULL || net_type != NET_FIDO) {
		if(!(sys_status&SS_ABORT))
118
			bprintf(text[InvalidNetMailAddr], net_addr);
119 120 121 122 123 124 125 126 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 153 154 155 156 157 158 159 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
		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 */

	SAFECOPY(from, cfg.netmail_misc&NMAIL_ALIAS ? useron.alias : useron.name);

	/* 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);
194
		(void)MKDIR(str);
195 196 197
		SAFECOPY(tmp, cfg.data_dir);
		if(tmp[0]=='.')    /* Relative path */
			sprintf(tmp,"%s%s", cfg.node_dir, cfg.data_dir);
198
		SAFEPRINTF3(str,"%sfile/%04u.out/%s",tmp,useron.number,fname);
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
		SAFECOPY(subj, str);
		if(fexistcase(str)) {
			bputs(text[FileAlreadyThere]);
			return(false); 
		}
		{ /* Remote */
			xfer_prot_menu(XFER_UPLOAD);
			mnemonics(text[ProtocolOrQuit]);
			sprintf(str,"%c",text[YNQP][2]);
			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);
			if(ch==text[YNQP][2] || sys_status&SS_ABORT) {
				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); 
		} 
	}

237
	msg.hdr.netattr |= MSG_LOCAL; 
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	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);
276 277 278 279 280
	if(length < 0) {
		close(file);
		errormsg(WHERE, ERR_LEN, msgpath, length);
		return false;
	}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	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;
310
	int result = savemsg(&cfg, &smb, &msg, &client, server_host_name(), buf, remsg);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 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
	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);
	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;
	long	l,offset,length,m,n;
	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);
378
	if(fread(qwkbuf+QWK_BLOCK_LEN, QWK_BLOCK_LEN, n-1, rep) != (size_t)n-1) {
379 380 381
		errormsg(WHERE, ERR_READ, "QWK block", n-1);
		return;
	}
382 383 384 385 386 387 388 389 390 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 422 423 424 425 426 427 428 429

	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 */
430
	if(p && !IS_DIGIT(*(p+1)) && !strchr(p,'.') && !strchr(p,':')) { /* QWKnet */
431 432 433
		qnet=1;
		*p=0; 
	}
434
	else if(p==NULL || !IS_DIGIT(*(p+1)) || !cfg.total_faddrs) {
435 436 437
		if(cfg.inetmail_misc&NMAIL_ALLOW) {	/* Internet */
			inet=1;
		} else {
438
			bprintf(text[InvalidNetMailAddr], to);
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
			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
482
				safe_snprintf(senderaddr, sizeof(senderaddr), "%s/%s",sender_id,cp);
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 508 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 625 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 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
				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
			,cfg.inetmail_misc&NMAIL_ALIAS ? useron.alias : useron.name
			,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);

		msg.hdr.offset=offset;

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

			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
730
		bprintf(text[InvalidNetMailAddr], to);
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 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
		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
		SAFECOPY(str,cfg.netmail_misc&NMAIL_ALIAS ? useron.alias : useron.name);
	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));

	pt_zone_kludge(hdr,fido);

	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;
880
	write(fido,(BYTE*)&l,sizeof(BYTE));	/* Null terminator */
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	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));

	sprintf(str,"%s sent NetMail to %s @%s via QWK"
		,sender_id
		,hdr.to,smb_faddrtoa(&fidoaddr,tmp));
	logline("EN",str);
}

900
/****************************************************************************/
901
/* Internet mail															*/
902
/****************************************************************************/
903
bool sbbs_t::inetmail(const char *into, const char *subj, long mode, smb_t* resmb, smbmsg_t* remsg, str_list_t cc_list)
904
{
905
	char	str[256],str2[256],msgpath[256],ch, *p;
906
	char 	tmp[512];
907 908 909
	char	title[256] = "";
	char	name[256] = "";
	char	addr[256] = "";
910 911
	const char*	editor=NULL;
	const char*	charset=NULL;
912
	char	your_addr[128];
913
	int 	i,x,file;
914 915 916 917
	long	l;
	FILE	*instream;
	smbmsg_t msg;

918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
	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);
943
	}
944 945
	strListStripStrings(rcpt_list, "<>");
	size_t rcpt_count = strListDedupe(&rcpt_list, /* case-sensitive */FALSE);
946

947 948 949 950 951
	if(useron.etoday + rcpt_count > cfg.level_emailperday[useron.level] && !SYSOP && !(useron.exempt&FLAG('M'))) {
		strListFree(&rcpt_list);
		bputs(text[TooManyEmailsToday]);
		return false; 
	}
952

953 954 955 956 957
	if(subj != NULL)
		SAFECOPY(title,subj);
	if(remsg != NULL) {
		if(title[0] == 0 && remsg->subj != NULL)
			SAFECOPY(title, remsg->subj);
958 959 960 961
		if(remsg->from_net.addr != NULL && rcpt_count < 1) {
			strListPush(&rcpt_list, smb_netaddrstr(&remsg->from_net, addr));
			rcpt_count = 1;
		}
962
	}
963

964 965 966 967 968 969 970 971 972 973
	/* Get this user's Internet mailing address */
	usermailaddr(&cfg,your_addr
		,cfg.inetmail_misc&NMAIL_ALIAS ? useron.alias : useron.name);

	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
974
	}
975

976
	if(rcpt_count < 1) {
977 978
		bprintf(text[InvalidNetMailAddr], into);
		strListFree(&rcpt_list);
979 980 981
		return false;
	}

982
	if(cfg.inetmail_cost && !(useron.exempt&FLAG('S'))) {
983 984
		if(useron.cdt+useron.freecdt < cfg.inetmail_cost * rcpt_count) {
			strListFree(&rcpt_list);
985
			bputs(text[NotEnoughCredits]);
rswindell's avatar
rswindell committed
986 987
			return(false); 
		}
988 989 990 991 992
		SAFEPRINTF(str, text[NetMailCostContinueQ], cfg.inetmail_cost * rcpt_count);
		if(noyes(str)) {
			strListFree(&rcpt_list);
			return(false);
		}
rswindell's avatar
rswindell committed
993
	}
994

995 996
	char to_list[512];
	strListJoin(rcpt_list, to_list, sizeof(to_list), ", ");
997

998
	bprintf(text[InternetMailing], to_list, your_addr);
999 1000 1001
	action=NODE_SMAL;
	nodesync();

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

1007
	SAFEPRINTF(msgpath,"%snetmail.msg",cfg.node_dir);
1008 1009
	if(!writemsg(msgpath,nulstr,title,WM_NETMAIL|mode,INVALID_SUB, to_list, /* from: */your_addr, &editor, &charset)) {
		strListFree(&rcpt_list);
1010
		bputs(text[Aborted]);
rswindell's avatar
rswindell committed
1011 1012
		return(false); 
	}
1013 1014

	if(mode&WM_FILE) {
1015
		SAFEPRINTF2(str2,"%sfile/%04u.out",cfg.data_dir,useron.number);
1016
		(void)MKDIR(str2);
1017
		SAFEPRINTF3(str2,"%sfile/%04u.out/%s",cfg.data_dir,useron.number,title);
1018
		if(fexistcase(str2)) {
1019
			strListFree(&rcpt_list);
1020 1021
			bputs(text[FileAlreadyThere]);
			remove(msgpath);
rswindell's avatar
rswindell committed
1022 1023
			return(false); 
		}
rswindell's avatar
rswindell committed
1024
		{ /* Remote */
1025
			xfer_prot_menu(XFER_UPLOAD);
1026
			mnemonics(text[ProtocolOrQuit]);
1027
			SAFEPRINTF(str,"%c",text[YNQP][2]);
1028
			for(x=0;x<cfg.total_prots;x++)
rswindell's avatar
rswindell committed
1029
				if(cfg.prot[x]->ulcmd[0] && chk_ar(cfg.prot[x]->ar,&useron,&client)) {
1030
					SAFEPRINTF(tmp,"%c",cfg.prot[x]->mnemonic);
rswindell's avatar
rswindell committed
1031
					SAFECAT(str,tmp); 
rswindell's avatar
rswindell committed
1032
				}
1033
			ch=(char)getkeys(str,0);
1034
			if(ch==text[YNQP][2] || sys_status&SS_ABORT) {
1035
				bputs(text[Aborted]);
1036
				strListFree(&rcpt_list);
1037
				remove(msgpath);
rswindell's avatar
rswindell committed
1038 1039
				return(false); 
			}
1040 1041
			for(x=0;x<cfg.total_prots;x++)
				if(cfg.prot[x]->ulcmd[0] && cfg.prot[x]->mnemonic==ch
rswindell's avatar
rswindell committed
1042
					&& chk_ar(cfg.prot[x]->ar,&useron,&client))
1043 1044
					break;
			if(x<cfg.total_prots)	/* This should be always */
1045
				protocol(cfg.prot[x],XFER_UPLOAD,str2,nulstr,true); 
1046
		}
1047
		SAFEPRINTF2(tmp,"%s%s",cfg.temp_dir,title);
1048 1049
		if(!fexistcase(str2) && fexistcase(tmp))
			mv(tmp,str2,0);
1050
		l=(long)flength(str2);
1051 1052 1053 1054
		if(l>0)
			bprintf(text[FileNBytesReceived],title,ultoac(l,tmp));
		else {
			bprintf(text[FileNotReceived],title);
1055
			strListFree(&rcpt_list);
1056
			remove(msgpath);
rswindell's avatar
rswindell committed
1057 1058 1059
			return(false); 
		} 
	}
1060

1061
	smb_t smb;
1062
	if((i=smb_open_sub(&cfg, &smb, INVALID_SUB))!=SMB_SUCCESS) {
1063
		strListFree(&rcpt_list);
1064
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
rswindell's avatar
rswindell committed
1065 1066
		return(false); 
	}
1067

1068
	if((instream=fnopen(&file,msgpath,O_RDONLY|O_BINARY))==NULL) {
1069
		strListFree(&rcpt_list);
1070
		smb_close(&smb);
1071
		errormsg(WHERE,ERR_OPEN,msgpath,O_RDONLY|O_BINARY);
rswindell's avatar
rswindell committed
1072 1073
		return(false); 
	}
1074 1075
	off_t length = filelength(file);
	if(length < 1) {
1076
		strListFree(&rcpt_list);
1077
		fclose(instream);
1078
		smb_close(&smb);
1079
		errormsg(WHERE, ERR_LEN, msgpath, length);
rswindell's avatar
rswindell committed
1080 1081
		return(false); 
	}
1082 1083
	char* msgbuf;
	if((msgbuf = (char*)malloc(length + 1)) == NULL) {
1084
		strListFree(&rcpt_list);
1085
		fclose(instream);
1086
		smb_close(&smb);
1087
		errormsg(WHERE, ERR_ALLOC, msgpath, length);
rswindell's avatar
rswindell committed
1088 1089
		return(false); 
	}
1090
	if(fread(msgbuf, sizeof(char), length, instream) != (size_t)length) {
1091
		strListFree(&rcpt_list);
1092 1093
		fclose(instream);
		free(msgbuf);
1094
		smb_close(&smb);
1095
		errormsg(WHERE, ERR_READ, msgpath, length);
rswindell's avatar
rswindell committed
1096 1097
		return(false); 
	}
1098
	msgbuf[length] = 0;
1099 1100 1101 1102 1103
	fclose(instream);

	memset(&msg,0,sizeof(smbmsg_t));
	msg.hdr.version=smb_ver();
	if(mode&WM_FILE)
1104
		msg.hdr.auxattr |= (MSG_FILEATTACH | MSG_KILLFILE);
1105
	msg.hdr.when_written.time=msg.hdr.when_imported.time=time32(NULL);
1106
	msg.hdr.when_written.zone=msg.hdr.when_imported.zone=sys_timezone(&cfg);
1107

1108 1109 1110 1111
	msg.hdr.netattr |= MSG_LOCAL;
	if(cfg.inetmail_misc&NMAIL_KILL)
		msg.hdr.netattr |= MSG_KILLSENT;

1112 1113
	if(rcpt_count > 1)
		smb_hfield_str(&msg, RECIPIENTLIST, to_list);
1114

rswindell's avatar
rswindell committed
1115
	smb_hfield_str(&msg,SENDER,cfg.inetmail_misc&NMAIL_ALIAS ? useron.alias : useron.name);
1116

1117
	SAFEPRINTF(str,"%u",useron.number);
1118
	smb_hfield_str(&msg,SENDEREXT,str);
1119

1120
	/* Security logging */
1121
	msg_client_hfields(&msg,&client);
1122
	smb_hfield_str(&msg,SENDERSERVER, server_host_name());
1123

1124
	smb_hfield_str(&msg,SUBJECT,title);
1125

1126
	editor_info_to_msg(&msg, editor, charset);
1127

1128
	i = savemsg(&cfg, &smb, &msg, &client, server_host_name(), msgbuf, remsg);
1129
	free(msgbuf);
1130

1131
	if(i!=SMB_SUCCESS) {
1132 1133 1134
		smb_close(&smb);
		smb_freemsgmem(&msg);
		strListFree(&rcpt_list);
1135
		errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
1136 1137
		return(false); 
	}
1138

1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 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
	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);

1206 1207 1208 1209
	if(mode&WM_FILE && online==ON_REMOTE)
		autohangup();

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

1212
	if(!(useron.exempt&FLAG('S')))
1213
		subtract_cdt(&cfg,&useron,cfg.inetmail_cost * rcpt_count);
1214

1215 1216
	useron.emails += (ushort)rcpt_count;
	logon_emails += rcpt_count;
1217
	putuserrec(&cfg,useron.number,U_EMAILS,5,ultoa(useron.emails,tmp,10)); 
1218
	useron.etoday += (ushort)rcpt_count;
1219
	putuserrec(&cfg,useron.number,U_ETODAY,5,ultoa(useron.etoday,tmp,10));
1220

1221
	SAFEPRINTF(str, "sent Internet Mail to %s", to_list);
1222 1223 1224 1225
	logline("EN",str);
	return(true);
}

1226
bool sbbs_t::qnetmail(const char *into, const char *subj, long mode, smb_t* resmb, smbmsg_t* remsg)
1227
{
1228
	char	str[256],msgpath[128],fulladdr[128]
1229
			,buf[SDT_BLOCK_LEN],*addr;
1230
	char 	tmp[512];
1231 1232
	char	title[128] = "";
	char	to[128] = "";
1233 1234
	const char*	editor=NULL;
	const char*	charset=NULL;
1235 1236 1237 1238 1239 1240
	ushort	xlat=XLAT_NONE,net=NET_QWK,touser;
	int 	i,j,x,file;
	ulong	length,offset;
	FILE	*instream;
	smbmsg_t msg;

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

1246 1247 1248 1249
	if(into != NULL)
		SAFECOPY(to,into);
	if(subj != NULL)
		SAFECOPY(title,subj);
1250 1251 1252

	if(useron.rest&FLAG('M')) {
		bputs(text[NoNetMailAllowed]);
rswindell's avatar
rswindell committed
1253 1254
		return(false); 
	}
1255 1256 1257

	addr=strrchr(to,'@');
	if(!addr) {
1258
		bprintf(text[InvalidNetMailAddr], to);
1259 1260
		return(false); 
	}
1261 1262 1263 1264
	*addr=0;
	addr++;
	strupr(addr);
	truncsp(addr);
1265
	touser=qwk_route(&cfg,addr,fulladdr,sizeof(fulladdr)-1);
1266
	if(!fulladdr[0]) {
1267
		bprintf(text[InvalidNetMailAddr], addr);
1268 1269
		return(false); 
	}
1270 1271 1272

	truncsp(to);
	if(!stricmp(to