readmail.cpp 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* readmail.cpp */

/* Synchronet private mail reading function */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2015 Rob Swindell - http://www.synchro.net/copyright.html		*
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

/****************************************************************************/
/* Reads mail waiting for usernumber.                                       */
/****************************************************************************/
void sbbs_t::readmail(uint usernumber, int which)
{
	char	str[256],str2[256],str3[256],done=0,domsg=1
46
			,*p,*tp,*sp,ch;
47
	char 	tmp[512];
48 49
	int		i;
	uint32_t u,v;
rswindell's avatar
rswindell committed
50
	int		error;
51
	int		mismatches=0,act;
52
	uint	unum;
53 54 55 56 57 58 59
    long    length,l,lm_mode;
	ulong	last;
	bool	replied;
	file_t	fd;
	mail_t	*mail;
	smbmsg_t msg;

60 61 62 63 64 65 66 67 68 69
	if(cfg.readmail_mod[0] && !readmail_inside) {
		char cmdline[256];

		readmail_inside = true;
		safe_snprintf(cmdline, sizeof(cmdline), "%s %d %u", cfg.readmail_mod, which, usernumber);
		exec_bin(cmdline, &main_csi);
		readmail_inside = false;
		return;
	}

70 71 72 73 74
	if(which==MAIL_SENT && useron.rest&FLAG('K')) {
		bputs(text[R_ReadSentMail]);
		return;
	}

75 76 77 78 79 80
	msg.total_hfields=0;			/* init to NULL, cause not allocated yet */

	fd.dir=cfg.total_dirs+1;			/* temp dir for file attachments */

	if((i=smb_stack(&smb,SMB_STACK_PUSH))!=0) {
		errormsg(WHERE,ERR_OPEN,"MAIL",i);
81 82
		return; 
	}
83
	sprintf(smb.file,"%smail",cfg.data_dir);
84
	smb.retry_time=cfg.smb_retry_time;
85
	smb.subnum=INVALID_SUB;
86 87 88
	if((i=smb_open(&smb))!=0) {
		smb_stack(&smb,SMB_STACK_POP);
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
89 90
		return; 
	}
91 92 93 94 95

	if(cfg.sys_misc&SM_SYSVDELM && (SYSOP || cfg.sys_misc&SM_USRVDELM))
		lm_mode=LM_INCDEL;
	else
		lm_mode=0;
96 97
	mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
	if(!smb.msgs) {
98 99 100 101 102 103 104 105
		if(which==MAIL_SENT)
			bputs(text[NoMailSent]);
		else if(which==MAIL_ALL)
			bputs(text[NoMailOnSystem]);
		else
			bputs(text[NoMailWaiting]);
		smb_close(&smb);
		smb_stack(&smb,SMB_STACK_POP);
106 107
		return; 
	}
108 109 110 111 112 113 114 115 116 117

	last=smb.status.last_msg;

	if(which==MAIL_SENT)
		act=NODE_RSML;
	else if(which==MAIL_ALL)
		act=NODE_SYSP;
	else
		act=NODE_RMAL;
	action=act;
118
	if(smb.msgs>1 && which!=MAIL_ALL) {
119 120 121 122 123
		if(which==MAIL_SENT)
			bputs(text[MailSentLstHdr]);
		else
			bputs(text[MailWaitingLstHdr]);

124
		for(smb.curmsg=0;smb.curmsg<smb.msgs && !msgabort();smb.curmsg++) {
125 126 127
			if(msg.total_hfields)
				smb_freemsgmem(&msg);
			msg.total_hfields=0;
128 129
			msg.idx.offset=mail[smb.curmsg].offset;
			if(!loadmsg(&msg,mail[smb.curmsg].number))
130 131
				continue;
			smb_unlockmsghdr(&smb,&msg);
132
			bprintf(text[MailWaitingLstFmt],smb.curmsg+1
133 134 135 136 137 138 139 140
				,which==MAIL_SENT ? msg.to
				: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP ? text[Anonymous]
				: msg.from
				,msg.hdr.attr&MSG_DELETE ? '-' : msg.hdr.attr&MSG_REPLIED ? 'R'
					: msg.hdr.attr&MSG_READ ? ' '
					: msg.from_net.type || msg.to_net.type ? 'N':'*'
				,msg.subj);
			smb_freemsgmem(&msg);
141 142
			msg.total_hfields=0; 
		}
143 144 145 146

		ASYNC;
		if(!(sys_status&SS_ABORT)) {
			bprintf(text[StartWithN],1L);
147 148 149 150
			l=getnum(smb.msgs);
			if(l>0)
				smb.curmsg=l-1;
			else if(l==-1) {
deuce's avatar
deuce committed
151
				free(mail);
152 153
				smb_close(&smb);
				smb_stack(&smb,SMB_STACK_POP);
154
				return;
155
			}
156 157
			else
				smb.curmsg=l;
158 159 160
		}
		sys_status&=~SS_ABORT; 
	}
161
	else {
162
		smb.curmsg=0;
163
		if(which==MAIL_ALL)
164 165
			domsg=0; 
	}
166 167 168 169 170 171 172 173 174 175
	if(which==MAIL_SENT) {
		sprintf(str,"%s read sent mail",useron.alias);
		logline("E",str);
	} else if(which==MAIL_ALL) {
		sprintf(str,"%s read all mail",useron.alias);
		logline("S+",str);
	} else {
		sprintf(str,"%s read mail",useron.alias);
		logline("E",str);
	}
176
	if(useron.misc&RIP) {
177 178
		strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
			"allmail" : "sentmail");
179 180
		menu(str); 
	}
181
	current_msg=&msg;	/* For MSG_* @-codes and bbs.msg_* property values */
182 183 184 185 186 187 188
	while(online && !done) {
		action=act;

		if(msg.total_hfields)
			smb_freemsgmem(&msg);
		msg.total_hfields=0;

189 190 191 192 193
		msg.idx.offset=mail[smb.curmsg].offset;
		msg.idx.number=mail[smb.curmsg].number;
		msg.idx.to=mail[smb.curmsg].to;
		msg.idx.from=mail[smb.curmsg].from;
		msg.idx.subj=mail[smb.curmsg].subj;
194 195

		if((i=smb_locksmbhdr(&smb))!=0) {
196
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);
197 198
			break; 
		}
199 200 201

		if((i=smb_getstatus(&smb))!=0) {
			smb_unlocksmbhdr(&smb);
202
			errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
203 204
			break; 
		}
205 206 207 208
		smb_unlocksmbhdr(&smb);

		if(smb.status.last_msg!=last) { 	/* New messages */
			last=smb.status.last_msg;
deuce's avatar
deuce committed
209
			free(mail);
210 211
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);   /* So re-load */
			if(!smb.msgs)
212
				break;
213 214
			for(smb.curmsg=0;smb.curmsg<smb.msgs;smb.curmsg++)
				if(mail[smb.curmsg].number==msg.idx.number)
215
					break;
216 217
			if(smb.curmsg>=smb.msgs)
				smb.curmsg=(smb.msgs-1);
218 219
			continue; 
		}
220

221
		if(!loadmsg(&msg,mail[smb.curmsg].number)) {	/* Message header gone */
222
			if(mismatches>5) {	/* We can't do this too many times in a row */
223
				errormsg(WHERE,ERR_CHK,"message number",mail[smb.curmsg].number);
224 225
				break; 
			}
deuce's avatar
deuce committed
226
			free(mail);
227 228
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
			if(!smb.msgs)
229
				break;
230 231
			if(smb.curmsg>(smb.msgs-1))
				smb.curmsg=(smb.msgs-1);
232
			mismatches++;
233 234
			continue; 
		}
235 236 237 238 239 240 241 242 243 244 245 246 247 248
		smb_unlockmsghdr(&smb,&msg);
		msg.idx.attr=msg.hdr.attr;

		mismatches=0;

		if(domsg && !(sys_status&SS_ABORT)) {

			show_msg(&msg
				,msg.from_ext && msg.idx.from==1 && !msg.from_net.type
					? 0:P_NOATCODES);

			if(msg.hdr.auxattr&MSG_FILEATTACH) {  /* Attached file */
				smb_getmsgidx(&smb,&msg);
				strcpy(str,msg.subj);					/* filenames in title */
249
//				strupr(str);
250 251
				tp=str;
				while(online) {
252
					p=strchr(tp,' ');
253 254 255 256 257
					if(p) *p=0;
					sp=strrchr(tp,'/');              /* sp is slash pointer */
					if(!sp) sp=strrchr(tp,'\\');
					if(sp) tp=sp+1;
					padfname(tp,fd.name);
258
					sprintf(str2,"%sfile/%04u.in/%s"  /* str2 is path/fname */
259
						,cfg.data_dir,msg.idx.to,tp);
260
					length=(long)flength(str2);
261 262 263 264 265 266 267 268
					if(length<1)
						bputs(text[FileNotFound]);
					else if(!(useron.exempt&FLAG('T')) && cur_cps && !SYSOP
						&& length/(long)cur_cps>(time_t)timeleft)
						bputs(text[NotEnoughTimeToDl]);
					else {
						sprintf(str3,text[DownloadAttachedFileQ]
							,tp,ultoac(length,tmp));
269
						if(length>0L && text[DownloadAttachedFileQ][0] && yesno(str3)) {
rswindell's avatar
rswindell committed
270
							{	/* Remote User */
271
								xfer_prot_menu(XFER_DOWNLOAD);
272 273 274 275
								mnemonics(text[ProtocolOrQuit]);
								strcpy(str3,"Q");
								for(i=0;i<cfg.total_prots;i++)
									if(cfg.prot[i]->dlcmd[0]
rswindell's avatar
rswindell committed
276
										&& chk_ar(cfg.prot[i]->ar,&useron,&client)) {
277
										sprintf(tmp,"%c",cfg.prot[i]->mnemonic);
278 279
										strcat(str3,tmp); 
									}
280 281 282
								ch=(char)getkeys(str3,0);
								for(i=0;i<cfg.total_prots;i++)
									if(cfg.prot[i]->dlcmd[0] && ch==cfg.prot[i]->mnemonic
rswindell's avatar
rswindell committed
283
										&& chk_ar(cfg.prot[i]->ar,&useron,&client))
284 285
										break;
								if(i<cfg.total_prots) {
286
									error=protocol(cfg.prot[i],XFER_DOWNLOAD,str2,nulstr,false);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
									if(checkprotresult(cfg.prot[i],error,&fd)) {
										if(which==MAIL_YOUR)
											remove(str2);
										logon_dlb+=length;	/* Update stats */
										logon_dls++;
										useron.dls=(ushort)adjustuserrec(&cfg,useron.number
											,U_DLS,5,1);
										useron.dlb=adjustuserrec(&cfg,useron.number
											,U_DLB,10,length);
										bprintf(text[FileNBytesSent]
											,fd.name,ultoac(length,tmp));
										sprintf(str3
											,"%s downloaded attached file: %s"
											,useron.alias
											,fd.name);
										logline("D-",str3); 
									}
									autohangup(); 
								} 
							} 
						} 
					}
					if(!p)
						break;
					tp=p+1;
312
					while(*tp==' ') tp++; 
313
				}
314
				sprintf(str,"%sfile/%04u.in",cfg.data_dir,usernumber);
315 316
				rmdir(str); 
			}
317
			if(which==MAIL_YOUR && !(msg.hdr.attr&MSG_READ)) {
318
				mail[smb.curmsg].attr|=MSG_READ;
319 320 321 322 323 324
				if(thisnode.status==NODE_INUSE)
					telluser(&msg);
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;						/* Search by number */
325
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
326 327
					if(loadmsg(&msg,msg.idx.number)) {
						msg.hdr.attr|=MSG_READ;
328 329
						if(msg.hdr.attr&MSG_KILLREAD)
							msg.hdr.attr|=MSG_DELETE;
330 331
						msg.idx.attr=msg.hdr.attr;
						if((i=smb_putmsg(&smb,&msg))!=0)
332
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
333 334 335 336
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb); 
				}
337 338
				if(!msg.total_hfields) {				/* unsuccessful reload */
					domsg=0;
339 340
					continue; 
				}
341
			}
342
		}
343 344 345
		else domsg=1;

		if(useron.misc&WIP) {
346 347
			strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
				"allmail" : "sentmail");
348 349
			menu(str); 
		}
350 351 352

		ASYNC;
		if(which==MAIL_SENT)
353
			bprintf(text[ReadingSentMail],smb.curmsg+1,smb.msgs);
354
		else if(which==MAIL_ALL)
355
			bprintf(text[ReadingAllMail],smb.curmsg+1,smb.msgs);
356
		else
357
			bprintf(text[ReadingMail],smb.curmsg+1,smb.msgs);
358
		sprintf(str,"ADKFLNQRT?<>[]{}-+");
359
		if(SYSOP)
360
			strcat(str,"CUSPH");
361 362
		if(which!=MAIL_YOUR)
			strcat(str,"E");
363
		l=getkeys(str,smb.msgs);
364 365 366
		if(l&0x80000000L) {
			if(l==-1)	/* ctrl-c */
				break;
367
			smb.curmsg=(l&~0x80000000L)-1;
368 369
			continue; 
		}
370 371 372 373 374 375 376 377
		switch(l) {
			case 'A':   /* Auto-reply to last piece */
			case 'R':
				if(l==(cfg.sys_misc&SM_RA_EMU ? 'A' : 'R'))  /* re-read last message */
					break;

				if(which==MAIL_SENT)
					break;
378 379
				if((msg.hdr.attr&(MSG_NOREPLY|MSG_ANONYMOUS)) && !SYSOP) {
					bputs(text[CantReplyToMsg]);
380 381
					break; 
				}
382

383
				quotemsg(&msg,/* include tails: */TRUE);
384

385
				if(msg.from_net.addr==NULL)
386
					SAFECOPY(str,msg.from);
387
				else if(msg.from_net.type==NET_FIDO) 	/* FidoNet type */
388
					SAFEPRINTF2(str,"%s@%s",msg.from
389
						,smb_faddrtoa((faddr_t *)msg.from_net.addr,tmp));
390
				else if(msg.from_net.type==NET_INTERNET || strchr((char*)msg.from_net.addr,'@')!=NULL) {
391 392 393 394 395 396 397 398
					if(msg.replyto_net.type==NET_INTERNET)
						SAFECOPY(str,(char *)msg.replyto_net.addr);
					else
						SAFECOPY(str,(char *)msg.from_net.addr);
				} else
					SAFEPRINTF2(str,"%s@%s",msg.from,(char*)msg.from_net.addr);

				SAFECOPY(str2,str);
399 400 401 402 403 404 405 406 407 408 409

				bputs(text[Email]);
				if(!getstr(str,64,K_EDIT|K_AUTODEL))
					break;
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);

				if(!stricmp(str2,str))		/* Reply to sender */
					sprintf(str2,text[Regarding],msg.subj);
				else						/* Reply to other */
					sprintf(str2,text[RegardingByOn],msg.subj,msg.from
410
						,timestr(msg.hdr.when_written.time));
411 412 413 414

				p=strrchr(str,'@');
				if(p) { 							/* name @addr */
					replied=netmail(str,msg.subj,WM_QUOTE);
415 416
					sprintf(str2,text[DeleteMailQ],msg.from); 
				}
417 418 419 420 421 422 423
				else {
					if(!msg.from_net.type && !stricmp(str,msg.from))
						replied=email(msg.idx.from,str2,msg.subj,WM_EMAIL|WM_QUOTE);
					else if(!stricmp(str,"SYSOP"))
						replied=email(1,str2,msg.subj,WM_EMAIL|WM_QUOTE);
					else if((i=finduser(str))!=0)
						replied=email(i,str2,msg.subj,WM_EMAIL|WM_QUOTE);
424 425
					else
						replied=false;
426 427
					sprintf(str2,text[DeleteMailQ],msg.from); 
				}
428 429 430 431 432 433

				if(replied==true && !(msg.hdr.attr&MSG_REPLIED)) {
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
					msg.idx.offset=0;
434 435 436 437 438
					if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
						if(loadmsg(&msg,msg.idx.number)) {
							msg.hdr.attr|=MSG_REPLIED;
							msg.idx.attr=msg.hdr.attr;
							if((i=smb_putmsg(&smb,&msg))!=0)
439
								errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
440 441 442 443
							smb_unlockmsghdr(&smb,&msg); 
						}
						smb_unlocksmbhdr(&smb);
					}
444 445
				}

446
				if(msg.hdr.attr&MSG_DELETE || noyes(str2)) {
447
					if(smb.curmsg<smb.msgs-1) smb.curmsg++;
448 449 450 451 452
					else done=1;
					break;	}
				/* Case 'D': must follow! */
			case 'D':   /* Delete last piece (toggle) */
				if(msg.hdr.attr&MSG_PERMANENT) {
453
					bputs(text[CantDeleteMsg]);
454
					domsg=0;
455 456
					break; 
				}
457 458 459 460
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
461 462 463 464 465 466
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
					if(loadmsg(&msg,msg.idx.number)) {
						msg.hdr.attr^=MSG_DELETE;
						msg.idx.attr=msg.hdr.attr;
		//				  mail[smb.curmsg].attr=msg.hdr.attr;
						if((i=smb_putmsg(&smb,&msg))!=0)
467
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
468 469 470 471
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
472
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
473 474
				else done=1;
				break;
475 476 477 478 479 480
			case 'K':	/* Kill All Mail */
				SAFEPRINTF(str,text[DeleteMailQ],"everyone");
				if(!noyes(str))
					delallmail(usernumber, MAIL_YOUR, /* permanent: */false);
				domsg=false;
				break;
481 482 483
			case 'F':  /* Forward last piece */
				domsg=0;
				bputs(text[ForwardMailTo]);
484
				if(!getstr(str,LEN_ALIAS,cfg.uq&UQ_NOUPRLWR ? K_NONE:K_UPRLWR))
485 486 487 488 489
					break;
				i=finduser(str);
				if(!i)
					break;
				domsg=1;
490
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
491 492 493 494 495 496 497 498 499 500 501 502
				else done=1;
				smb_getmsgidx(&smb,&msg);
				forwardmail(&msg,i);
				if(msg.hdr.attr&MSG_PERMANENT)
					break;
				sprintf(str2,text[DeleteMailQ],msg.from);
				if(!yesno(str2))
					break;
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
503 504 505 506 507 508
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
					if(loadmsg(&msg,msg.idx.number)) {
						msg.hdr.attr|=MSG_DELETE;
						msg.idx.attr=msg.hdr.attr;
		//				  mail[smb.curmsg].attr=msg.hdr.attr;
						if((i=smb_putmsg(&smb,&msg))!=0)
509
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
510 511 512 513
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
514

515 516
				break;
			case 'H':
517
				domsg=0;
518
				msghdr(&msg);
519 520 521
				break;
			case 'L':     /* List mail */
				domsg=0;
522 523
				bprintf(text[StartWithN],(long)smb.curmsg+1);
				if((i=getnum(smb.msgs))>0)
524 525 526 527
					i--;
				else if(i==-1)
					break;
				else
528
					i=smb.curmsg;
529 530 531 532 533 534
				if(which==MAIL_SENT)
					bputs(text[MailSentLstHdr]);
				else if(which==MAIL_ALL)
					bputs(text[MailOnSystemLstHdr]);
				else
					bputs(text[MailWaitingLstHdr]);
deuce's avatar
deuce committed
535
				for(u=i;u<smb.msgs && !msgabort();u++) {
536 537 538
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
539 540
					msg.idx.offset=mail[u].offset;
					if(!loadmsg(&msg,mail[u].number))
541 542 543 544
						continue;
					smb_unlockmsghdr(&smb,&msg);
					if(which==MAIL_ALL)
						bprintf(text[MailOnSystemLstFmt]
545
							,u+1,msg.from,msg.to
546
							,msg.hdr.attr&MSG_DELETE ? '-' : msg.hdr.attr&MSG_REPLIED ? 'R'
547
								: msg.hdr.attr&MSG_READ ? ' '
548 549 550
								: msg.from_net.type || msg.to_net.type ? 'N':'*'
							,msg.subj);
					else
551
						bprintf(text[MailWaitingLstFmt],u+1
552 553 554 555
							,which==MAIL_SENT ? msg.to
							: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP
							? text[Anonymous] : msg.from
							,msg.hdr.attr&MSG_DELETE ? '-' : msg.hdr.attr&MSG_REPLIED ? 'R'
556
								: msg.hdr.attr&MSG_READ ? ' '
557 558 559
								: msg.from_net.type || msg.to_net.type ? 'N':'*'
							,msg.subj);
					smb_freemsgmem(&msg);
560 561
					msg.total_hfields=0; 
				}
562 563 564 565 566 567 568 569 570 571 572 573
				break;
			case 'Q':
				done=1;
				break;
			case 'C':   /* Change attributes of last piece */
				i=chmsgattr(msg.hdr.attr);
				if(msg.hdr.attr==i)
					break;
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
574 575 576 577
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
					if(loadmsg(&msg,msg.idx.number)) {
						msg.hdr.attr=msg.idx.attr=(ushort)i;
						if((i=smb_putmsg(&smb,&msg))!=0)
578
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
579 580 581 582
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
583 584
				break;
			case '>':
585 586
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(mail[u].subj==msg.idx.subj)
587
						break;
588 589
				if(u<smb.msgs)
					smb.curmsg=u;
590 591 592 593
				else
					domsg=0;
				break;
			case '<':   /* Search Title backward */
594
				for(i=smb.curmsg-1;i>-1;i--)
595 596 597
					if(mail[i].subj==msg.idx.subj)
						break;
				if(i>-1)
598
					smb.curmsg=i;
599 600 601 602 603
				else
					domsg=0;
				break;
			case '}':   /* Search Author forward */
				strcpy(str,msg.from);
604 605
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(mail[u].from==msg.idx.from)
606
						break;
607 608
				if(u<smb.msgs)
					smb.curmsg=u;
609 610 611 612
				else
					domsg=0;
				break;
			case 'N':   /* Got to next un-read message */
613 614
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(!(mail[u].attr&MSG_READ))
615
						break;
616 617
				if(u<smb.msgs)
					smb.curmsg=u;
618 619 620 621 622
				else
					domsg=0;
				break;
			case '{':   /* Search Author backward */
				strcpy(str,msg.from);
623 624 625 626 627 628 629 630 631 632 633 634
				if(smb.curmsg > 0) {
					for(u=smb.curmsg-1;;u--) {
						if(mail[u].from==msg.idx.from) {
							smb.curmsg=u;
							break;
						}
						if(u==0) {
							domsg=0;
							break;
						}
					}
				}
635 636 637
				break;
			case ']':   /* Search To User forward */
				strcpy(str,msg.to);
638 639
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(mail[u].to==msg.idx.to)
640
						break;
641 642
				if(u<smb.msgs)
					smb.curmsg=u;
643 644 645 646 647
				else
					domsg=0;
				break;
			case '[':   /* Search To User backward */
				strcpy(str,msg.to);
648 649 650 651 652 653 654 655 656 657 658 659
				if(smb.curmsg > 0) {
					for(u=smb.curmsg-1;;u--) {
						if(mail[u].to==msg.idx.to) {
							smb.curmsg=u;
							break;
						}
						if(u==0) {
							domsg=0;
							break;
						}
					}
				}
660 661 662
				break;
			case 0:
			case '+':
663
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
664 665 666
				else done=1;
				break;
			case '-':
667
				if(smb.curmsg>0) smb.curmsg--;
668 669 670 671 672 673 674 675
				break;
			case 'S':
				domsg=0;
	/*
				if(!yesno(text[SaveMsgToFile]))
					break;
	*/
				bputs(text[FileToWriteTo]);
676
				if(getstr(str,40,K_LINE))
677 678 679 680 681 682 683
					msgtotxt(&msg,str,1,1);
				break;
			case 'E':
				editmsg(&msg,INVALID_SUB);
				break;
			case 'T':
				domsg=0;
684 685 686 687 688
				u=smb.curmsg;
				if(u) u++;
				v=u+10;
				if(v>smb.msgs)
					v=smb.msgs;
689 690 691 692 693 694 695

				if(which==MAIL_SENT)
					bputs(text[MailSentLstHdr]);
				else if(which==MAIL_ALL)
					bputs(text[MailOnSystemLstHdr]);
				else
					bputs(text[MailWaitingLstHdr]);
696
				for(;u<v;u++) {
697 698 699
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
700 701
					msg.idx.offset=mail[u].offset;
					if(!loadmsg(&msg,mail[u].number))
702 703 704 705
						continue;
					smb_unlockmsghdr(&smb,&msg);
					if(which==MAIL_ALL)
						bprintf(text[MailOnSystemLstFmt]
706
							,u+1,msg.from,msg.to
707
							,msg.hdr.attr&MSG_DELETE ? '-' : msg.hdr.attr&MSG_REPLIED ? 'R'
708
								: msg.hdr.attr&MSG_READ ? ' ' 
709 710 711
								: msg.from_net.type || msg.to_net.type ? 'N':'*'
							,msg.subj);
					else
712
						bprintf(text[MailWaitingLstFmt],u+1
713 714 715 716
							,which==MAIL_SENT ? msg.to
							: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP
							? text[Anonymous] : msg.from
							,msg.hdr.attr&MSG_DELETE ? '-' : msg.hdr.attr&MSG_REPLIED ? 'R'
717
								: msg.hdr.attr&MSG_READ ? ' '
718 719 720
								: msg.from_net.type || msg.to_net.type ? 'N':'*'
							,msg.subj);
					smb_freemsgmem(&msg);
721 722
					msg.total_hfields=0; 
				}
723
				smb.curmsg=(u-1);
724 725 726 727
				break;
			case 'U':   /* user edit */
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);
728 729 730
				if((unum=(which==MAIL_SENT ? msg.idx.to : msg.idx.from)) == 0)
					unum=(which==MAIL_SENT ? msg.idx.from : msg.idx.to);
				if(unum == 0 || unum > lastuser(&cfg)) {
731 732 733
					bputs(text[UnknownUser]);
					domsg=false;
				} else
734
					useredit(unum);
735 736
				break;
			case 'P':   /* Purge author and all mail to/from */
737
				if(noyes(text[UeditDeleteQ]))
738 739 740
					break;
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);
741 742 743 744 745 746 747
				if((which==MAIL_SENT ? msg.idx.to : msg.idx.from) == 0) {
					bputs(text[UnknownUser]);
					domsg=false;
				} else {
					purgeuser(msg.idx.from);
					if(smb.curmsg<smb.msgs-1) smb.curmsg++;
				}
748 749
				break;
			case '?':
750 751
				strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL
						? "allmail" : "sentmail");
752 753
				menu(str);
				if(SYSOP && which==MAIL_SENT)
754
					menu("syssmail");
755
				else if(SYSOP && which==MAIL_YOUR)
756
					menu("sysmailr");   /* Sysop Mail Read */
757 758
				domsg=0;
				break;
759 760 761
				
		} 
	}
762 763 764 765

	if(msg.total_hfields)
		smb_freemsgmem(&msg);

766
	if(smb.msgs)
deuce's avatar
deuce committed
767
		free(mail);
768 769 770 771 772 773 774

	/***************************************/
	/* Delete messages marked for deletion */
	/***************************************/

	if(cfg.sys_misc&SM_DELEMAIL) {
		if((i=smb_locksmbhdr(&smb))!=0) 			/* Lock the base, so nobody */
775
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);	/* messes with the index */
776
		else
777 778
			delmail(usernumber,which); 
	}
779 780 781

	smb_close(&smb);
	smb_stack(&smb,SMB_STACK_POP);
782
	current_msg=NULL;
783 784
}