readmail.cpp 21.2 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 2013 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
	if(which==MAIL_SENT && useron.rest&FLAG('K')) {
		bputs(text[R_ReadSentMail]);
		return;
	}

65 66 67 68 69 70
	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);
71 72
		return; 
	}
73
	sprintf(smb.file,"%smail",cfg.data_dir);
74
	smb.retry_time=cfg.smb_retry_time;
75
	smb.subnum=INVALID_SUB;
76 77 78
	if((i=smb_open(&smb))!=0) {
		smb_stack(&smb,SMB_STACK_POP);
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
79 80
		return; 
	}
81 82 83 84 85

	if(cfg.sys_misc&SM_SYSVDELM && (SYSOP || cfg.sys_misc&SM_USRVDELM))
		lm_mode=LM_INCDEL;
	else
		lm_mode=0;
86 87
	mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
	if(!smb.msgs) {
88 89 90 91 92 93 94 95
		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);
96 97
		return; 
	}
98 99 100 101 102 103 104 105 106 107

	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;
108
	if(smb.msgs>1 && which!=MAIL_ALL) {
109 110 111 112 113
		if(which==MAIL_SENT)
			bputs(text[MailSentLstHdr]);
		else
			bputs(text[MailWaitingLstHdr]);

114
		for(smb.curmsg=0;smb.curmsg<smb.msgs && !msgabort();smb.curmsg++) {
115 116 117
			if(msg.total_hfields)
				smb_freemsgmem(&msg);
			msg.total_hfields=0;
118 119
			msg.idx.offset=mail[smb.curmsg].offset;
			if(!loadmsg(&msg,mail[smb.curmsg].number))
120 121
				continue;
			smb_unlockmsghdr(&smb,&msg);
122
			bprintf(text[MailWaitingLstFmt],smb.curmsg+1
123 124 125 126 127 128 129 130
				,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);
131 132
			msg.total_hfields=0; 
		}
133 134 135 136

		ASYNC;
		if(!(sys_status&SS_ABORT)) {
			bprintf(text[StartWithN],1L);
137 138 139 140
			l=getnum(smb.msgs);
			if(l>0)
				smb.curmsg=l-1;
			else if(l==-1) {
deuce's avatar
deuce committed
141
				free(mail);
142 143
				smb_close(&smb);
				smb_stack(&smb,SMB_STACK_POP);
144
				return;
145
			}
146 147
			else
				smb.curmsg=l;
148 149 150
		}
		sys_status&=~SS_ABORT; 
	}
151
	else {
152
		smb.curmsg=0;
153
		if(which==MAIL_ALL)
154 155
			domsg=0; 
	}
156 157 158 159 160 161 162 163 164 165
	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);
	}
166
	if(useron.misc&RIP) {
167 168
		strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
			"allmail" : "sentmail");
169 170
		menu(str); 
	}
171
	current_msg=&msg;	/* For MSG_* @-codes and bbs.msg_* property values */
172 173 174 175 176 177 178
	while(online && !done) {
		action=act;

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

179 180 181 182 183
		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;
184 185

		if((i=smb_locksmbhdr(&smb))!=0) {
186
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);
187 188
			break; 
		}
189 190 191

		if((i=smb_getstatus(&smb))!=0) {
			smb_unlocksmbhdr(&smb);
192
			errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
193 194
			break; 
		}
195 196 197 198
		smb_unlocksmbhdr(&smb);

		if(smb.status.last_msg!=last) { 	/* New messages */
			last=smb.status.last_msg;
deuce's avatar
deuce committed
199
			free(mail);
200 201
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);   /* So re-load */
			if(!smb.msgs)
202
				break;
203 204
			for(smb.curmsg=0;smb.curmsg<smb.msgs;smb.curmsg++)
				if(mail[smb.curmsg].number==msg.idx.number)
205
					break;
206 207
			if(smb.curmsg>=smb.msgs)
				smb.curmsg=(smb.msgs-1);
208 209
			continue; 
		}
210

211
		if(!loadmsg(&msg,mail[smb.curmsg].number)) {	/* Message header gone */
212
			if(mismatches>5) {	/* We can't do this too many times in a row */
213
				errormsg(WHERE,ERR_CHK,"message number",mail[smb.curmsg].number);
214 215
				break; 
			}
deuce's avatar
deuce committed
216
			free(mail);
217 218
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
			if(!smb.msgs)
219
				break;
220 221
			if(smb.curmsg>(smb.msgs-1))
				smb.curmsg=(smb.msgs-1);
222
			mismatches++;
223 224
			continue; 
		}
225 226 227 228 229 230 231 232 233 234 235 236 237 238
		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 */
239
//				strupr(str);
240 241
				tp=str;
				while(online) {
242
					p=strchr(tp,' ');
243 244 245 246 247
					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);
248
					sprintf(str2,"%sfile/%04u.in/%s"  /* str2 is path/fname */
249
						,cfg.data_dir,msg.idx.to,tp);
250
					length=(long)flength(str2);
251 252 253 254 255 256 257 258
					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));
259
						if(length>0L && text[DownloadAttachedFileQ][0] && yesno(str3)) {
rswindell's avatar
rswindell committed
260
							{	/* Remote User */
261
								xfer_prot_menu(XFER_DOWNLOAD);
262 263 264 265
								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
266
										&& chk_ar(cfg.prot[i]->ar,&useron,&client)) {
267
										sprintf(tmp,"%c",cfg.prot[i]->mnemonic);
268 269
										strcat(str3,tmp); 
									}
270 271 272
								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
273
										&& chk_ar(cfg.prot[i]->ar,&useron,&client))
274 275
										break;
								if(i<cfg.total_prots) {
276
									error=protocol(cfg.prot[i],XFER_DOWNLOAD,str2,nulstr,false);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
									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;
302
					while(*tp==' ') tp++; 
303
				}
304
				sprintf(str,"%sfile/%04u.in",cfg.data_dir,usernumber);
305 306
				rmdir(str); 
			}
307
			if(which==MAIL_YOUR && !(msg.hdr.attr&MSG_READ)) {
308
				mail[smb.curmsg].attr|=MSG_READ;
309 310 311 312 313 314
				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 */
315
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
316 317
					if(loadmsg(&msg,msg.idx.number)) {
						msg.hdr.attr|=MSG_READ;
318 319
						if(msg.hdr.attr&MSG_KILLREAD)
							msg.hdr.attr|=MSG_DELETE;
320 321
						msg.idx.attr=msg.hdr.attr;
						if((i=smb_putmsg(&smb,&msg))!=0)
322
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
323 324 325 326
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb); 
				}
327 328
				if(!msg.total_hfields) {				/* unsuccessful reload */
					domsg=0;
329 330
					continue; 
				}
331
			}
332
		}
333 334 335
		else domsg=1;

		if(useron.misc&WIP) {
336 337
			strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
				"allmail" : "sentmail");
338 339
			menu(str); 
		}
340 341 342

		ASYNC;
		if(which==MAIL_SENT)
343
			bprintf(text[ReadingSentMail],smb.curmsg+1,smb.msgs);
344
		else if(which==MAIL_ALL)
345
			bprintf(text[ReadingAllMail],smb.curmsg+1,smb.msgs);
346
		else
347
			bprintf(text[ReadingMail],smb.curmsg+1,smb.msgs);
348
		sprintf(str,"ADKFLNQRT?<>[]{}-+");
349
		if(SYSOP)
350
			strcat(str,"CUSPH");
351 352
		if(which!=MAIL_YOUR)
			strcat(str,"E");
353
		l=getkeys(str,smb.msgs);
354 355 356
		if(l&0x80000000L) {
			if(l==-1)	/* ctrl-c */
				break;
357
			smb.curmsg=(l&~0x80000000L)-1;
358 359
			continue; 
		}
360 361 362 363 364 365 366 367
		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;
368 369
				if((msg.hdr.attr&(MSG_NOREPLY|MSG_ANONYMOUS)) && !SYSOP) {
					bputs(text[CantReplyToMsg]);
370 371
					break; 
				}
372

373
				quotemsg(&msg,/* include tails: */TRUE);
374

375
				if(msg.from_net.addr==NULL)
376
					SAFECOPY(str,msg.from);
377
				else if(msg.from_net.type==NET_FIDO) 	/* FidoNet type */
378
					SAFEPRINTF2(str,"%s@%s",msg.from
379
						,smb_faddrtoa((faddr_t *)msg.from_net.addr,tmp));
380
				else if(msg.from_net.type==NET_INTERNET || strchr((char*)msg.from_net.addr,'@')!=NULL) {
381 382 383 384 385 386 387 388
					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);
389 390 391 392 393 394 395 396 397 398 399

				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
400
						,timestr(msg.hdr.when_written.time));
401 402 403 404

				p=strrchr(str,'@');
				if(p) { 							/* name @addr */
					replied=netmail(str,msg.subj,WM_QUOTE);
405 406
					sprintf(str2,text[DeleteMailQ],msg.from); 
				}
407 408 409 410 411 412 413
				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);
414 415
					else
						replied=false;
416 417
					sprintf(str2,text[DeleteMailQ],msg.from); 
				}
418 419 420 421 422 423

				if(replied==true && !(msg.hdr.attr&MSG_REPLIED)) {
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
					msg.idx.offset=0;
424 425 426 427 428
					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)
429
								errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
430 431 432 433
							smb_unlockmsghdr(&smb,&msg); 
						}
						smb_unlocksmbhdr(&smb);
					}
434 435
				}

436
				if(msg.hdr.attr&MSG_DELETE || noyes(str2)) {
437
					if(smb.curmsg<smb.msgs-1) smb.curmsg++;
438 439 440 441 442
					else done=1;
					break;	}
				/* Case 'D': must follow! */
			case 'D':   /* Delete last piece (toggle) */
				if(msg.hdr.attr&MSG_PERMANENT) {
443
					bputs(text[CantDeleteMsg]);
444
					domsg=0;
445 446
					break; 
				}
447 448 449 450
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
451 452 453 454 455 456
				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)
457
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
458 459 460 461
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
462
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
463 464
				else done=1;
				break;
465 466 467 468 469 470
			case 'K':	/* Kill All Mail */
				SAFEPRINTF(str,text[DeleteMailQ],"everyone");
				if(!noyes(str))
					delallmail(usernumber, MAIL_YOUR, /* permanent: */false);
				domsg=false;
				break;
471 472 473
			case 'F':  /* Forward last piece */
				domsg=0;
				bputs(text[ForwardMailTo]);
474
				if(!getstr(str,LEN_ALIAS,cfg.uq&UQ_NOUPRLWR ? K_NONE:K_UPRLWR))
475 476 477 478 479
					break;
				i=finduser(str);
				if(!i)
					break;
				domsg=1;
480
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
481 482 483 484 485 486 487 488 489 490 491 492
				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;
493 494 495 496 497 498
				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)
499
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
500 501 502 503
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
504

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

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

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

756
	if(smb.msgs)
deuce's avatar
deuce committed
757
		free(mail);
758 759 760 761 762 763 764

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

	if(cfg.sys_misc&SM_DELEMAIL) {
		if((i=smb_locksmbhdr(&smb))!=0) 			/* Lock the base, so nobody */
765
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);	/* messes with the index */
766
		else
767 768
			delmail(usernumber,which); 
	}
769 770 771

	smb_close(&smb);
	smb_stack(&smb,SMB_STACK_POP);
772
	current_msg=NULL;
773 774
}