readmail.cpp 22.5 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
static char mail_listing_flag(smbmsg_t* msg)
{
	if(msg->hdr.attr&MSG_DELETE)				return '-';
43
	if(msg->hdr.attr&MSG_SPAM)					return 'S';
44
45
46
47
48
49
50
51
52
53
54
	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 '*';
}

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

74
75
76
77
	if(cfg.readmail_mod[0] && !readmail_inside) {
		char cmdline[256];

		readmail_inside = true;
rswindell's avatar
rswindell committed
78
		safe_snprintf(cmdline, sizeof(cmdline), "%s %d %u %lu", cfg.readmail_mod, which, usernumber, lm_mode);
79
80
81
82
83
		exec_bin(cmdline, &main_csi);
		readmail_inside = false;
		return;
	}

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

89
90
91
92
	msg.total_hfields=0;			/* init to NULL, cause not allocated yet */

	if((i=smb_stack(&smb,SMB_STACK_PUSH))!=0) {
		errormsg(WHERE,ERR_OPEN,"MAIL",i);
93
94
		return; 
	}
95
	SAFEPRINTF(smb.file,"%smail",cfg.data_dir);
96
	smb.retry_time=cfg.smb_retry_time;
97
	smb.subnum=INVALID_SUB;
98
99
100
	if((i=smb_open(&smb))!=0) {
		smb_stack(&smb,SMB_STACK_POP);
		errormsg(WHERE,ERR_OPEN,smb.file,i,smb.last_error);
101
102
		return; 
	}
103
104

	if(cfg.sys_misc&SM_SYSVDELM && (SYSOP || cfg.sys_misc&SM_USRVDELM))
rswindell's avatar
rswindell committed
105
		lm_mode |= LM_INCDEL;
106
	mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
107
	last_mode = lm_mode;
108
	if(!smb.msgs) {
109
110
111
112
113
		if(which==MAIL_SENT)
			bputs(text[NoMailSent]);
		else if(which==MAIL_ALL)
			bputs(text[NoMailOnSystem]);
		else
rswindell's avatar
rswindell committed
114
			bprintf(text[NoMailWaiting], lm_mode&LM_UNREAD ? "un-read mail" : "mail");
115
116
		smb_close(&smb);
		smb_stack(&smb,SMB_STACK_POP);
117
118
		return; 
	}
119
120
121
122
123
124
125
126
127
128

	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;
129
	const char* order = (lm_mode&LM_REVERSE) ? "newest" : "oldest";
130
	if(smb.msgs>1 && which!=MAIL_ALL) {
131
		if(which==MAIL_SENT)
132
			bprintf(text[MailSentLstHdr], order);
133
		else
134
			bprintf(text[MailWaitingLstHdr], order);
135

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

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

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

197
198
199
200
201
		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;
202
203

		if((i=smb_locksmbhdr(&smb))!=0) {
204
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);
205
206
			break; 
		}
207
208
209

		if((i=smb_getstatus(&smb))!=0) {
			smb_unlocksmbhdr(&smb);
210
			errormsg(WHERE,ERR_READ,smb.file,i,smb.last_error);
211
212
			break; 
		}
213
214
		smb_unlocksmbhdr(&smb);

215
		if(smb.status.last_msg!=last || lm_mode != last_mode) { 	/* New messages */
216
			last=smb.status.last_msg;
deuce's avatar
deuce committed
217
			free(mail);
218
			order = (lm_mode&LM_REVERSE) ? "newest" : "oldest";
219
220
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);   /* So re-load */
			if(!smb.msgs)
221
				break;
222
223
224
225
226
227
228
229
230
			if(lm_mode != last_mode)
				smb.curmsg = 0;
			else {
				for(smb.curmsg=0;smb.curmsg<smb.msgs;smb.curmsg++)
					if(mail[smb.curmsg].number==msg.idx.number)
						break;
				if(smb.curmsg>=smb.msgs)
					smb.curmsg=(smb.msgs-1);
			}
231
			last_mode = lm_mode;
232
233
			continue; 
		}
234

235
		if(loadmsg(&msg,mail[smb.curmsg].number) < 0) {	/* Message header gone */
236
			if(mismatches>5) {	/* We can't do this too many times in a row */
237
				errormsg(WHERE,ERR_CHK,"message number",mail[smb.curmsg].number);
238
239
				break; 
			}
deuce's avatar
deuce committed
240
			free(mail);
241
242
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
			if(!smb.msgs)
243
				break;
244
			last_mode = lm_mode;
245
246
			if(smb.curmsg>(smb.msgs-1))
				smb.curmsg=(smb.msgs-1);
247
			mismatches++;
248
249
			continue; 
		}
250
251
252
253
254
255
256
		smb_unlockmsghdr(&smb,&msg);
		msg.idx.attr=msg.hdr.attr;

		mismatches=0;

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

rswindell's avatar
rswindell committed
257
			show_msg(&smb, &msg
258
259
				,msg.from_ext && msg.idx.from==1 && !msg.from_net.type
					? 0:P_NOATCODES);
260
			download_msg_attachments(&smb, &msg, which == MAIL_YOUR);
261
			if(which==MAIL_YOUR && !(msg.hdr.attr&MSG_READ)) {
262
				mail[smb.curmsg].attr|=MSG_READ;
263
264
265
266
267
268
				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 */
269
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
270
					if(loadmsg(&msg,msg.idx.number) >= 0) {
271
						msg.hdr.attr|=MSG_READ;
272
273
						if(msg.hdr.attr&MSG_KILLREAD)
							msg.hdr.attr|=MSG_DELETE;
274
275
						msg.idx.attr=msg.hdr.attr;
						if((i=smb_putmsg(&smb,&msg))!=0)
276
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
277
278
279
280
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb); 
				}
281
282
				if(!msg.total_hfields) {				/* unsuccessful reload */
					domsg=0;
283
284
					continue; 
				}
285
			}
286
		}
287
288
		else domsg=1;

rswindell's avatar
rswindell committed
289
290
		if(useron.misc&WIP)
			menu(menu_file); 
291
292
293

		ASYNC;
		if(which==MAIL_SENT)
294
			bprintf(text[ReadingSentMail],smb.curmsg+1,smb.msgs);
295
		else if(which==MAIL_ALL)
296
			bprintf(text[ReadingAllMail],smb.curmsg+1,smb.msgs);
297
		else
298
			bprintf(text[ReadingMail],smb.curmsg+1,smb.msgs);
299
		sprintf(str,"ADFLNQRT?<>[]{}()-+/!");
300
		if(SYSOP)
301
			strcat(str,"CUSPH");
302
303
304
305
306
307
		if(which == MAIL_YOUR)
			strcat(str,"K");	// kill all (visible)
		else
			strcat(str,"E");	// edit msg
		if(which != MAIL_SENT)
			strcat(str,"V");	// View SPAM toggle
308
		l=getkeys(str,smb.msgs);
309
310
311
		if(l&0x80000000L) {
			if(l==-1)	/* ctrl-c */
				break;
312
			smb.curmsg=(l&~0x80000000L)-1;
313
314
			continue; 
		}
315
		switch(l) {
316
317
318
319
			case '!':
				lm_mode ^= LM_REVERSE;
				domsg=0;
				break;
320
321
322
323
324
325
326
			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;
327
328
				if((msg.hdr.attr&(MSG_NOREPLY|MSG_ANONYMOUS)) && !SYSOP) {
					bputs(text[CantReplyToMsg]);
329
330
					break; 
				}
331

332
				if(msg.from_net.addr==NULL)
333
					SAFECOPY(str,msg.from);
334
				else if(msg.from_net.type==NET_FIDO) 	/* FidoNet type */
335
					SAFEPRINTF2(str,"%s@%s",msg.from
336
						,smb_faddrtoa((faddr_t *)msg.from_net.addr,tmp));
337
				else if(msg.from_net.type==NET_INTERNET || strchr((char*)msg.from_net.addr,'@')!=NULL) {
338
339
340
341
342
343
344
345
					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);
346
347
348
349
350
351
352
353

				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 */
354
					SAFEPRINTF(str2,text[Regarding],msg.subj);
355
				else						/* Reply to other */
356
					SAFEPRINTF3(str2,text[RegardingByOn],msg.subj,msg.from
357
						,timestr(msg.hdr.when_written.time));
358
359
360

				p=strrchr(str,'@');
				if(p) { 							/* name @addr */
361
					replied=netmail(str,msg.subj,WM_NONE, &smb, &msg);
362
					SAFEPRINTF(str2,text[DeleteMailQ],msg.from); 
363
				}
364
365
				else {
					if(!msg.from_net.type && !stricmp(str,msg.from))
366
						replied=email(msg.idx.from,str2,msg.subj,WM_NONE, &smb, &msg);
367
					else if(!stricmp(str,"SYSOP"))
368
						replied=email(1,str2,msg.subj,WM_NONE, &smb, &msg);
369
					else if((i=finduser(str))!=0)
370
						replied=email(i,str2,msg.subj,WM_NONE, &smb, &msg);
371
372
					else
						replied=false;
373
					SAFEPRINTF(str2,text[DeleteMailQ],msg.from); 
374
				}
375
376
377
378
379
380

				if(replied==true && !(msg.hdr.attr&MSG_REPLIED)) {
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
					msg.idx.offset=0;
381
					if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
382
						if(loadmsg(&msg,msg.idx.number) >= 0) {
383
384
385
							msg.hdr.attr|=MSG_REPLIED;
							msg.idx.attr=msg.hdr.attr;
							if((i=smb_putmsg(&smb,&msg))!=0)
386
								errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
387
388
389
390
							smb_unlockmsghdr(&smb,&msg); 
						}
						smb_unlocksmbhdr(&smb);
					}
391
392
				}

393
				if(msg.hdr.attr&MSG_DELETE || noyes(str2)) {
394
					if(smb.curmsg<smb.msgs-1) smb.curmsg++;
395
396
397
398
399
					else done=1;
					break;	}
				/* Case 'D': must follow! */
			case 'D':   /* Delete last piece (toggle) */
				if(msg.hdr.attr&MSG_PERMANENT) {
400
					bprintf(text[CantDeleteMsg], smb.curmsg + 1);
401
					domsg=0;
402
403
					break; 
				}
404
405
406
407
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
408
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
409
					if(loadmsg(&msg,msg.idx.number) >= 0) {
410
411
412
413
						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)
414
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
415
416
417
418
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
419
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
420
421
				else done=1;
				break;
422
423
424
			case 'K':	/* Kill All Mail */
				SAFEPRINTF(str,text[DeleteMailQ],"everyone");
				if(!noyes(str))
425
					delallmail(usernumber, which, /* permanent: */false, lm_mode);
426
427
				domsg=false;
				break;
428
429
430
			case 'F':  /* Forward last piece */
				domsg=0;
				bputs(text[ForwardMailTo]);
431
				if(!getstr(str,LEN_ALIAS,cfg.uq&UQ_NOUPRLWR ? K_NONE:K_UPRLWR))
432
433
434
435
436
					break;
				i=finduser(str);
				if(!i)
					break;
				domsg=1;
437
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
438
439
440
441
442
				else done=1;
				smb_getmsgidx(&smb,&msg);
				forwardmail(&msg,i);
				if(msg.hdr.attr&MSG_PERMANENT)
					break;
443
				SAFEPRINTF(str2,text[DeleteMailQ],msg.from);
444
445
446
447
448
449
				if(!yesno(str2))
					break;
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
450
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
451
					if(loadmsg(&msg,msg.idx.number) >= 0) {
452
453
454
455
						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)
456
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
457
458
459
460
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
461

462
463
				break;
			case 'H':
464
				domsg=0;
465
				msghdr(&msg);
466
467
468
				break;
			case 'L':     /* List mail */
				domsg=0;
469
470
				bprintf(text[StartWithN],(long)smb.curmsg+1);
				if((i=getnum(smb.msgs))>0)
471
472
473
474
					i--;
				else if(i==-1)
					break;
				else
475
					i=smb.curmsg;
476
				if(which==MAIL_SENT)
477
					bprintf(text[MailSentLstHdr], order);
478
				else if(which==MAIL_ALL)
479
					bprintf(text[MailOnSystemLstHdr], order);
480
				else
481
					bprintf(text[MailWaitingLstHdr], order);
deuce's avatar
deuce committed
482
				for(u=i;u<smb.msgs && !msgabort();u++) {
483
484
485
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
486
					msg.idx.offset=mail[u].offset;
487
					if(loadmsg(&msg,mail[u].number) < 0)
488
489
490
491
						continue;
					smb_unlockmsghdr(&smb,&msg);
					if(which==MAIL_ALL)
						bprintf(text[MailOnSystemLstFmt]
492
							,u+1,msg.from,msg.to
493
							,mail_listing_flag(&msg)
494
495
							,msg.subj);
					else
496
						bprintf(text[MailWaitingLstFmt],u+1
497
498
499
							,which==MAIL_SENT ? msg.to
							: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP
							? text[Anonymous] : msg.from
500
							,mail_listing_flag(&msg)
501
502
							,msg.subj);
					smb_freemsgmem(&msg);
503
504
					msg.total_hfields=0; 
				}
505
506
507
508
509
				break;
			case 'Q':
				done=1;
				break;
			case 'C':   /* Change attributes of last piece */
rswindell's avatar
rswindell committed
510
				i=chmsgattr(msg);
511
512
513
514
515
516
				if(msg.hdr.attr==i)
					break;
				if(msg.total_hfields)
					smb_freemsgmem(&msg);
				msg.total_hfields=0;
				msg.idx.offset=0;
517
				if(smb_locksmbhdr(&smb)==SMB_SUCCESS) {	/* Lock the entire base */
518
					if(loadmsg(&msg,msg.idx.number) >= 0) {
519
520
						msg.hdr.attr=msg.idx.attr=(ushort)i;
						if((i=smb_putmsg(&smb,&msg))!=0)
521
							errormsg(WHERE,ERR_WRITE,smb.file,i,smb.last_error);
522
523
524
525
						smb_unlockmsghdr(&smb,&msg); 
					}
					smb_unlocksmbhdr(&smb);
				}
526
527
				break;
			case '>':
528
529
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(mail[u].subj==msg.idx.subj)
530
						break;
531
532
				if(u<smb.msgs)
					smb.curmsg=u;
533
				else {
534
					domsg=0;
535
536
					bputs(text[NoMessagesFound]);
				}
537
538
				break;
			case '<':   /* Search Title backward */
539
				for(i=smb.curmsg-1;i>-1;i--)
540
541
542
					if(mail[i].subj==msg.idx.subj)
						break;
				if(i>-1)
543
					smb.curmsg=i;
544
				else {
545
					domsg=0;
546
547
					bputs(text[NoMessagesFound]);
				}
548
				break;
549
			case ')':
550
551
			case '}':   /* Search Author forward */
				strcpy(str,msg.from);
552
553
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(mail[u].from==msg.idx.from)
554
						break;
555
556
				if(u<smb.msgs)
					smb.curmsg=u;
557
				else {
558
					domsg=0;
559
560
					bputs(text[NoMessagesFound]);
				}
561
562
				break;
			case 'N':   /* Got to next un-read message */
563
564
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(!(mail[u].attr&MSG_READ))
565
						break;
566
567
				if(u<smb.msgs)
					smb.curmsg=u;
568
				else {
569
					domsg=0;
570
571
					bputs(text[NoMessagesFound]);
				}
572
				break;
573
			case '(':
574
575
			case '{':   /* Search Author backward */
				strcpy(str,msg.from);
576
577
578
579
580
581
582
583
				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;
584
							bputs(text[NoMessagesFound]);
585
586
587
588
							break;
						}
					}
				}
589
590
591
				break;
			case ']':   /* Search To User forward */
				strcpy(str,msg.to);
592
593
				for(u=smb.curmsg+1;u<smb.msgs;u++)
					if(mail[u].to==msg.idx.to)
594
						break;
595
596
				if(u<smb.msgs)
					smb.curmsg=u;
597
				else {
598
					domsg=0;
599
600
					bputs(text[NoMessagesFound]);
				}
601
602
603
				break;
			case '[':   /* Search To User backward */
				strcpy(str,msg.to);
604
605
606
607
608
609
610
611
				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;
612
							bputs(text[NoMessagesFound]);
613
614
615
616
							break;
						}
					}
				}
617
618
619
				break;
			case 0:
			case '+':
620
				if(smb.curmsg<smb.msgs-1) smb.curmsg++;
621
622
623
				else done=1;
				break;
			case '-':
624
				if(smb.curmsg>0) smb.curmsg--;
625
626
627
628
629
630
631
632
				break;
			case 'S':
				domsg=0;
	/*
				if(!yesno(text[SaveMsgToFile]))
					break;
	*/
				bputs(text[FileToWriteTo]);
633
				if(getstr(str,50,K_LINE))
rswindell's avatar
rswindell committed
634
					msgtotxt(&smb, &msg, str, /* header: */true, /* mode: */GETMSGTXT_ALL);
635
636
637
638
639
640
				break;
			case 'E':
				editmsg(&msg,INVALID_SUB);
				break;
			case 'T':
				domsg=0;
641
642
643
644
645
				u=smb.curmsg;
				if(u) u++;
				v=u+10;
				if(v>smb.msgs)
					v=smb.msgs;
646
647

				if(which==MAIL_SENT)
648
					bprintf(text[MailSentLstHdr], order);
649
				else if(which==MAIL_ALL)
650
					bprintf(text[MailOnSystemLstHdr], order);
651
				else
652
					bprintf(text[MailWaitingLstHdr], order);
653
				for(;u<v;u++) {
654
655
656
					if(msg.total_hfields)
						smb_freemsgmem(&msg);
					msg.total_hfields=0;
657
					msg.idx.offset=mail[u].offset;
658
					if(loadmsg(&msg,mail[u].number) < 0)
659
660
661
662
						continue;
					smb_unlockmsghdr(&smb,&msg);
					if(which==MAIL_ALL)
						bprintf(text[MailOnSystemLstFmt]
663
							,u+1,msg.from,msg.to
664
							,mail_listing_flag(&msg)
665
666
							,msg.subj);
					else
667
						bprintf(text[MailWaitingLstFmt],u+1
668
669
670
							,which==MAIL_SENT ? msg.to
							: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP
							? text[Anonymous] : msg.from
671
							,mail_listing_flag(&msg)
672
673
							,msg.subj);
					smb_freemsgmem(&msg);
674
675
					msg.total_hfields=0; 
				}
676
				smb.curmsg=(u-1);
677
				break;
678
			case 'U':   /* user edit */
679
680
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);
681
682
683
684
685
				unum = msg.idx.from;
				if(unum == 0)
					unum = matchuser(&cfg, msg.from, /*sysop_alias: */FALSE);
				if(unum == 0 && which != MAIL_YOUR)
					unum = msg.idx.to;
686
				if(unum == 0 || unum > lastuser(&cfg)) {
687
688
689
					bputs(text[UnknownUser]);
					domsg=false;
				} else
690
					useredit(unum);
691
				break;
692
#if 0
693
694
695
696
			case 'U':	/* View Unread-Only (toggle) */
			{
				domsg = false;
				if(!(lm_mode&LM_UNREAD)) {
697
698
					if(getmail(&cfg, usernumber, /* Sent: */FALSE, /* attr: */0)
						== getmail(&cfg, usernumber, /* Sent: */FALSE, /* attr: */MSG_READ)) {
699
						bprintf(text[NoMailWaiting], "un-read mail");
700
701
702
703
						break;
					}
				}
				lm_mode ^= LM_UNREAD;
704
705
706
				bprintf("%s: %s"
					,text[DisplayUnreadMessagesOnlyQ]
					,(lm_mode&LM_UNREAD) ? text[On] : text[Off]);
707
708
709
				CRLF;
				break;
			}
710
#endif
711
712
713
			case 'V':	/* View SPAM (toggle) */
			{
				domsg = false;
714
				int spam = getmail(&cfg, usernumber, /* Sent: */FALSE, /* attr: */MSG_SPAM);
715
716
717
718
				if(!spam) {
					bprintf(text[NoMailWaiting], "SPAM");
					break;
				}
719
				if(spam >= getmail(&cfg, usernumber, /* Sent: */FALSE, /* attr: */0)) {
720
721
722
					bprintf(text[NoMailWaiting], "HAM");
					break;
				}
723
				bputs(text[SPAMVisibilityIsNow]);
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
				switch(lm_mode&(LM_SPAMONLY | LM_NOSPAM)) {
					case 0:
						lm_mode |= LM_NOSPAM;
						bputs(text[Off]);
						break;
					case LM_NOSPAM:
						lm_mode ^= (LM_SPAMONLY | LM_NOSPAM);
						bputs(text[Only]);
						break;
					case LM_SPAMONLY:
						lm_mode &= ~LM_SPAMONLY;
						bputs(text[On]);
						break;
				}
				CRLF;
				break;
			}
741
			case 'P':   /* Purge author and all mail to/from */
742
				if(noyes(text[UeditDeleteQ]))
743
744
745
					break;
				msg.hdr.number=msg.idx.number;
				smb_getmsgidx(&smb,&msg);
746
747
748
749
750
751
752
				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++;
				}
753
				break;
754
755
756
757
758
759
760
761
			case '/':
				domsg = false;
				int64_t i64;
				if((i64=get_start_msgnum(&smb))<0)
					break;
				bputs(text[SearchStringPrompt]);
				if(!getstr(search_str,40,K_LINE|K_UPPER|K_EDIT|K_AUTODEL))
					break;
762
				searchmail(mail, (long)i64, smb.msgs, which, search_str, order);
763
				break;
764
			case '?':
rswindell's avatar
rswindell committed
765
				menu(menu_file);
766
				if(SYSOP && which==MAIL_SENT)
767
					menu("syssmail");
768
				else if(SYSOP && which==MAIL_YOUR)
769
					menu("sysmailr");   /* Sysop Mail Read */
770
771
				domsg=0;
				break;
772
773
774
				
		} 
	}
775
776
777
778

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

779
	if(smb.msgs)
deuce's avatar
deuce committed
780
		free(mail);
781
782
783
784
785
786
787

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

	if(cfg.sys_misc&SM_DELEMAIL) {
		if((i=smb_locksmbhdr(&smb))!=0) 			/* Lock the base, so nobody */
788
			errormsg(WHERE,ERR_LOCK,smb.file,i,smb.last_error);	/* messes with the index */
789
		else
790
791
			delmail(usernumber,which); 
	}
792
793
794

	smb_close(&smb);
	smb_stack(&smb,SMB_STACK_POP);
795
	current_msg=NULL;
796
797
}

798
long sbbs_t::searchmail(mail_t *mail, long start, long msgs, int which, const char *search, const char* order)
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
{
	char*	buf;
	char	subj[128];
	long	l,found=0;
	smbmsg_t msg;

	msg.total_hfields=0;
	for(l=start;l<msgs && !msgabort();l++) {
		msg.idx.offset=mail[l].offset;
		if(loadmsg(&msg,mail[l].number) < 0)
			continue;
		smb_unlockmsghdr(&smb,&msg);
		buf=smb_getmsgtxt(&smb,&msg,GETMSGTXT_ALL);
		if(!buf) {
			smb_freemsgmem(&msg);
			continue; 
		}
		strupr(buf);
		strip_ctrl(buf, buf);
		SAFECOPY(subj,msg.subj);
		strupr(subj);
		if(strstr(buf,search) || strstr(subj,search)) {
			if(!found) {
				if(which==MAIL_SENT)
823
					bprintf(text[MailSentLstHdr], order);
824
				else if(which==MAIL_ALL)
825
					bprintf(text[MailOnSystemLstHdr], order);
826
				else
827
					bprintf(text[MailWaitingLstHdr], order);
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
			}
			if(which==MAIL_ALL)
				bprintf(text[MailOnSystemLstFmt]
					,l+1,msg.from,msg.to
					,mail_listing_flag(&msg)
					,msg.subj);
			else
				bprintf(text[MailWaitingLstFmt],l+1
					,which==MAIL_SENT ? msg.to
					: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP
					? text[Anonymous] : msg.from
					,mail_listing_flag(&msg)
					,msg.subj);
			found++; 
		}
		free(buf);
		smb_freemsgmem(&msg); 
	}

	return(found);
}