readmail.cpp 21.7 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 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
 *																			*
 * 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"

40
41
42
43
44
45
46
47
48
49
50
51
52
53
static char mail_listing_flag(smbmsg_t* msg)
{
	if(msg->hdr.attr&MSG_DELETE)				return '-';
	if(msg->hdr.attr&MSG_REPLIED)				return 'R';
	if(msg->hdr.attr&MSG_READ)					return ' ';
	if(msg->hdr.attr&MSG_PERMANENT)				return 'p';
	if(msg->hdr.attr&MSG_LOCKED)				return 'L';
	if(msg->hdr.attr&MSG_KILLREAD)				return 'K';
	if(msg->hdr.attr&MSG_NOREPLY)				return '#';
	if(msg->from_net.type || msg->to_net.type)	return 'N';
	if(msg->hdr.attr&MSG_ANONYMOUS)				return 'A';
	return '*';
}

54
55
56
57
58
59
/****************************************************************************/
/* Reads mail waiting for usernumber.                                       */
/****************************************************************************/
void sbbs_t::readmail(uint usernumber, int which)
{
	char	str[256],str2[256],str3[256],done=0,domsg=1
60
			,*p,*tp,*sp,ch;
61
	char	path[MAX_PATH+1];
62
	char 	tmp[512];
63
64
	int		i;
	uint32_t u,v;
rswindell's avatar
rswindell committed
65
	int		error;
66
	int		mismatches=0,act;
67
	uint	unum;
68
69
70
71
72
73
74
    long    length,l,lm_mode;
	ulong	last;
	bool	replied;
	file_t	fd;
	mail_t	*mail;
	smbmsg_t msg;

75
76
77
78
79
80
81
82
83
84
	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;
	}

85
86
87
88
89
	if(which==MAIL_SENT && useron.rest&FLAG('K')) {
		bputs(text[R_ReadSentMail]);
		return;
	}

90
91
92
93
94
95
	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);
96
97
		return; 
	}
98
	sprintf(smb.file,"%smail",cfg.data_dir);
99
	smb.retry_time=cfg.smb_retry_time;
100
	smb.subnum=INVALID_SUB;
101
102
103
	if((i=smb_open(&smb))!=0) {
		smb_stack(&smb,SMB_STACK_POP);
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
104
105
		return; 
	}
106
107
108
109
110

	if(cfg.sys_misc&SM_SYSVDELM && (SYSOP || cfg.sys_misc&SM_USRVDELM))
		lm_mode=LM_INCDEL;
	else
		lm_mode=0;
111
112
	mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
	if(!smb.msgs) {
113
114
115
116
117
118
119
120
		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);
121
122
		return; 
	}
123
124
125
126
127
128
129
130
131
132

	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;
133
	if(smb.msgs>1 && which!=MAIL_ALL) {
134
135
136
137
138
		if(which==MAIL_SENT)
			bputs(text[MailSentLstHdr]);
		else
			bputs(text[MailWaitingLstHdr]);

139
		for(smb.curmsg=0;smb.curmsg<smb.msgs && !msgabort();smb.curmsg++) {
140
141
142
			if(msg.total_hfields)
				smb_freemsgmem(&msg);
			msg.total_hfields=0;
143
			msg.idx.offset=mail[smb.curmsg].offset;
144
			if(loadmsg(&msg,mail[smb.curmsg].number) < 1)
145
146
				continue;
			smb_unlockmsghdr(&smb,&msg);
147
			bprintf(text[MailWaitingLstFmt],smb.curmsg+1
148
149
150
				,which==MAIL_SENT ? msg.to
				: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP ? text[Anonymous]
				: msg.from
151
				,mail_listing_flag(&msg)
152
153
				,msg.subj);
			smb_freemsgmem(&msg);
154
155
			msg.total_hfields=0; 
		}
156
157

		ASYNC;
158
159
160
161
		if(sys_status&SS_ABORT) {
			domsg=0;
			smb.curmsg=0;
		} else {
162
			bprintf(text[StartWithN],1L);
163
164
165
166
			l=getnum(smb.msgs);
			if(l>0)
				smb.curmsg=l-1;
			else if(l==-1) {
deuce's avatar
deuce committed
167
				free(mail);
168
169
				smb_close(&smb);
				smb_stack(&smb,SMB_STACK_POP);
170
				return;
171
			}
172
173
			else
				smb.curmsg=l;
174
175
176
		}
		sys_status&=~SS_ABORT; 
	}
177
	else {
178
		smb.curmsg=0;
179
		if(which==MAIL_ALL)
180
181
			domsg=0; 
	}
182
183
184
185
186
187
188
189
190
191
	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);
	}
192
	if(useron.misc&RIP) {
193
194
		strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
			"allmail" : "sentmail");
195
196
		menu(str); 
	}
197
	current_msg=&msg;	/* For MSG_* @-codes and bbs.msg_* property values */
198
199
200
201
202
203
204
	while(online && !done) {
		action=act;

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

205
206
207
208
209
		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;
210
211

		if((i=smb_locksmbhdr(&smb))!=0) {
212
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);
213
214
			break; 
		}
215
216
217

		if((i=smb_getstatus(&smb))!=0) {
			smb_unlocksmbhdr(&smb);
218
			errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
219
220
			break; 
		}
221
222
223
224
		smb_unlocksmbhdr(&smb);

		if(smb.status.last_msg!=last) { 	/* New messages */
			last=smb.status.last_msg;
deuce's avatar
deuce committed
225
			free(mail);
226
227
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);   /* So re-load */
			if(!smb.msgs)
228
				break;
229
230
			for(smb.curmsg=0;smb.curmsg<smb.msgs;smb.curmsg++)
				if(mail[smb.curmsg].number==msg.idx.number)
231
					break;
232
233
			if(smb.curmsg>=smb.msgs)
				smb.curmsg=(smb.msgs-1);
234
235
			continue; 
		}
236

237
		if(loadmsg(&msg,mail[smb.curmsg].number) < 0) {	/* Message header gone */
238
			if(mismatches>5) {	/* We can't do this too many times in a row */
239
				errormsg(WHERE,ERR_CHK,"message number",mail[smb.curmsg].number);
240
241
				break; 
			}
deuce's avatar
deuce committed
242
			free(mail);
243
244
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
			if(!smb.msgs)
245
				break;
246
247
			if(smb.curmsg>(smb.msgs-1))
				smb.curmsg=(smb.msgs-1);
248
			mismatches++;
249
250
			continue; 
		}
251
252
253
254
255
256
257
258
259
260
261
262
263
		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);
264
				SAFECOPY(str, msg.subj);					/* filenames (multiple?) in title */
265
266
				tp=str;
				while(online) {
267
					p=strchr(tp,' ');
268
269
270
271
272
					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);
273
					SAFEPRINTF3(path,"%sfile/%04u.in/%s"  /* path is path/fname */
274
						,cfg.data_dir,msg.idx.to,tp);
275
					length=(long)flength(path);
276
					if(length<1)
277
						bprintf(text[FileDoesNotExist], tp);
278
279
280
281
282
283
					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));
284
						if(length>0L && text[DownloadAttachedFileQ][0] && yesno(str3)) {
rswindell's avatar
rswindell committed
285
							{	/* Remote User */
286
								xfer_prot_menu(XFER_DOWNLOAD);
287
288
289
290
								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
291
										&& chk_ar(cfg.prot[i]->ar,&useron,&client)) {
292
										sprintf(tmp,"%c",cfg.prot[i]->mnemonic);
293
294
										strcat(str3,tmp); 
									}
295
296
297
								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
298
										&& chk_ar(cfg.prot[i]->ar,&useron,&client))
299
300
										break;
								if(i<cfg.total_prots) {
301
									error=protocol(cfg.prot[i],XFER_DOWNLOAD,path,nulstr,false);
302
303
									if(checkprotresult(cfg.prot[i],error,&fd)) {
										if(which==MAIL_YOUR)
304
											remove(path);
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
										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;
327
					while(*tp==' ') tp++; 
328
				}
329
				sprintf(str,"%sfile/%04u.in",cfg.data_dir,usernumber);
330
331
				rmdir(str); 
			}
332
			if(which==MAIL_YOUR && !(msg.hdr.attr&MSG_READ)) {
333
				mail[smb.curmsg].attr|=MSG_READ;
334
335
336
337
338
339
				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 */
340
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
341
					if(loadmsg(&msg,msg.idx.number) >= 0) {
342
						msg.hdr.attr|=MSG_READ;
343
344
						if(msg.hdr.attr&MSG_KILLREAD)
							msg.hdr.attr|=MSG_DELETE;
345
346
						msg.idx.attr=msg.hdr.attr;
						if((i=smb_putmsg(&smb,&msg))!=0)
347
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
348
349
350
351
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb); 
				}
352
353
				if(!msg.total_hfields) {				/* unsuccessful reload */
					domsg=0;
354
355
					continue; 
				}
356
			}
357
		}
358
359
360
		else domsg=1;

		if(useron.misc&WIP) {
361
362
			strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL ?
				"allmail" : "sentmail");
363
364
			menu(str); 
		}
365
366
367

		ASYNC;
		if(which==MAIL_SENT)
368
			bprintf(text[ReadingSentMail],smb.curmsg+1,smb.msgs);
369
		else if(which==MAIL_ALL)
370
			bprintf(text[ReadingAllMail],smb.curmsg+1,smb.msgs);
371
		else
372
			bprintf(text[ReadingMail],smb.curmsg+1,smb.msgs);
373
		sprintf(str,"ADKFLNQRT?<>[]{}-+");
374
		if(SYSOP)
375
			strcat(str,"CUSPH");
376
377
		if(which!=MAIL_YOUR)
			strcat(str,"E");
378
		l=getkeys(str,smb.msgs);
379
380
381
		if(l&0x80000000L) {
			if(l==-1)	/* ctrl-c */
				break;
382
			smb.curmsg=(l&~0x80000000L)-1;
383
384
			continue; 
		}
385
386
387
388
389
390
391
392
		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;
393
394
				if((msg.hdr.attr&(MSG_NOREPLY|MSG_ANONYMOUS)) && !SYSOP) {
					bputs(text[CantReplyToMsg]);
395
396
					break; 
				}
397

398
				quotemsg(&msg,/* include tails: */TRUE);
399

400
				if(msg.from_net.addr==NULL)
401
					SAFECOPY(str,msg.from);
402
				else if(msg.from_net.type==NET_FIDO) 	/* FidoNet type */
403
					SAFEPRINTF2(str,"%s@%s",msg.from
404
						,smb_faddrtoa((faddr_t *)msg.from_net.addr,tmp));
405
				else if(msg.from_net.type==NET_INTERNET || strchr((char*)msg.from_net.addr,'@')!=NULL) {
406
407
408
409
410
411
412
413
					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);
414
415
416
417
418
419
420
421
422
423
424

				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
425
						,timestr(msg.hdr.when_written.time));
426
427
428
429

				p=strrchr(str,'@');
				if(p) { 							/* name @addr */
					replied=netmail(str,msg.subj,WM_QUOTE);
430
431
					sprintf(str2,text[DeleteMailQ],msg.from); 
				}
432
433
434
435
436
437
438
				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);
439
440
					else
						replied=false;
441
442
					sprintf(str2,text[DeleteMailQ],msg.from); 
				}
443
444
445
446
447
448

				if(replied==true && !(msg.hdr.attr&MSG_REPLIED)) {
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
					msg.idx.offset=0;
449
					if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
450
						if(loadmsg(&msg,msg.idx.number) >= 0) {
451
452
453
							msg.hdr.attr|=MSG_REPLIED;
							msg.idx.attr=msg.hdr.attr;
							if((i=smb_putmsg(&smb,&msg))!=0)
454
								errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
455
456
457
458
							smb_unlockmsghdr(&smb,&msg); 
						}
						smb_unlocksmbhdr(&smb);
					}
459
460
				}

461
				if(msg.hdr.attr&MSG_DELETE || noyes(str2)) {
462
					if(smb.curmsg<smb.msgs-1) smb.curmsg++;
463
464
465
466
467
					else done=1;
					break;	}
				/* Case 'D': must follow! */
			case 'D':   /* Delete last piece (toggle) */
				if(msg.hdr.attr&MSG_PERMANENT) {
468
					bputs(text[CantDeleteMsg]);
469
					domsg=0;
470
471
					break; 
				}
472
473
474
475
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
476
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
477
					if(loadmsg(&msg,msg.idx.number) >= 0) {
478
479
480
481
						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)
482
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
483
484
485
486
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
487
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
488
489
				else done=1;
				break;
490
491
492
493
494
495
			case 'K':	/* Kill All Mail */
				SAFEPRINTF(str,text[DeleteMailQ],"everyone");
				if(!noyes(str))
					delallmail(usernumber, MAIL_YOUR, /* permanent: */false);
				domsg=false;
				break;
496
497
498
			case 'F':  /* Forward last piece */
				domsg=0;
				bputs(text[ForwardMailTo]);
499
				if(!getstr(str,LEN_ALIAS,cfg.uq&UQ_NOUPRLWR ? K_NONE:K_UPRLWR))
500
501
502
503
504
					break;
				i=finduser(str);
				if(!i)
					break;
				domsg=1;
505
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
506
507
508
509
510
511
512
513
514
515
516
517
				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;
518
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
519
					if(loadmsg(&msg,msg.idx.number) >= 0) {
520
521
522
523
						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)
524
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
525
526
527
528
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
529

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

				if(which==MAIL_SENT)
					bputs(text[MailSentLstHdr]);
				else if(which==MAIL_ALL)
					bputs(text[MailOnSystemLstHdr]);
				else
					bputs(text[MailWaitingLstHdr]);
719
				for(;u<v;u++) {
720
721
722
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
723
					msg.idx.offset=mail[u].offset;
724
					if(loadmsg(&msg,mail[u].number) < 0)
725
726
727
728
						continue;
					smb_unlockmsghdr(&smb,&msg);
					if(which==MAIL_ALL)
						bprintf(text[MailOnSystemLstFmt]
729
							,u+1,msg.from,msg.to
730
							,mail_listing_flag(&msg)
731
732
							,msg.subj);
					else
733
						bprintf(text[MailWaitingLstFmt],u+1
734
735
736
							,which==MAIL_SENT ? msg.to
							: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP
							? text[Anonymous] : msg.from
737
							,mail_listing_flag(&msg)
738
739
							,msg.subj);
					smb_freemsgmem(&msg);
740
741
					msg.total_hfields=0; 
				}
742
				smb.curmsg=(u-1);
743
744
745
746
				break;
			case 'U':   /* user edit */
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);
747
748
749
				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)) {
750
751
752
					bputs(text[UnknownUser]);
					domsg=false;
				} else
753
					useredit(unum);
754
755
				break;
			case 'P':   /* Purge author and all mail to/from */
756
				if(noyes(text[UeditDeleteQ]))
757
758
759
					break;
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);
760
761
762
763
764
765
766
				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++;
				}
767
768
				break;
			case '?':
769
770
				strcpy(str,which==MAIL_YOUR ? "mailread" : which==MAIL_ALL
						? "allmail" : "sentmail");
771
772
				menu(str);
				if(SYSOP && which==MAIL_SENT)
773
					menu("syssmail");
774
				else if(SYSOP && which==MAIL_YOUR)
775
					menu("sysmailr");   /* Sysop Mail Read */
776
777
				domsg=0;
				break;
778
779
780
				
		} 
	}
781
782
783
784

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

785
	if(smb.msgs)
deuce's avatar
deuce committed
786
		free(mail);
787
788
789
790
791
792
793

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

	if(cfg.sys_misc&SM_DELEMAIL) {
		if((i=smb_locksmbhdr(&smb))!=0) 			/* Lock the base, so nobody */
794
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);	/* messes with the index */
795
		else
796
797
			delmail(usernumber,which); 
	}
798
799
800

	smb_close(&smb);
	smb_stack(&smb,SMB_STACK_POP);
801
	current_msg=NULL;
802
803
}