readmail.cpp 20.9 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 2011 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
	int		i,j;
rswindell's avatar
rswindell committed
49
	int		error;
50
51
52
53
54
55
56
57
	int		mismatches=0,act;
    long    length,l,lm_mode;
	ulong	last;
	bool	replied;
	file_t	fd;
	mail_t	*mail;
	smbmsg_t msg;

58
59
60
61
62
	if(which==MAIL_SENT && useron.rest&FLAG('K')) {
		bputs(text[R_ReadSentMail]);
		return;
	}

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

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

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

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

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

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

174
175
176
177
178
		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;
179
180

		if((i=smb_locksmbhdr(&smb))!=0) {
181
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);
182
183
			break; 
		}
184
185
186

		if((i=smb_getstatus(&smb))!=0) {
			smb_unlocksmbhdr(&smb);
187
			errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
188
189
			break; 
		}
190
191
192
193
		smb_unlocksmbhdr(&smb);

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

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

		if(useron.misc&WIP) {
331
332
			strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
				"allmail" : "sentmail");
333
334
			menu(str); 
		}
335
336
337

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

368
				quotemsg(&msg,/* include tails: */TRUE);
369

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

				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
395
						,timestr(msg.hdr.when_written.time));
396
397
398
399

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

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

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

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

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

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

739
	if(smb.msgs)
deuce's avatar
deuce committed
740
		free(mail);
741
742
743
744
745
746
747

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

	if(cfg.sys_misc&SM_DELEMAIL) {
		if((i=smb_locksmbhdr(&smb))!=0) 			/* Lock the base, so nobody */
748
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);	/* messes with the index */
749
		else
750
751
			delmail(usernumber,which); 
	}
752
753
754
755
756

	smb_close(&smb);
	smb_stack(&smb,SMB_STACK_POP);
}