readmail.cpp 23.1 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
78
79
80
81
	if(which==MAIL_SENT)
		act=NODE_RSML;
	else if(which==MAIL_ALL)
		act=NODE_SYSP;
	else
		act=NODE_RMAL;
	action=act;

82
83
84
85
	if(cfg.readmail_mod[0] && !readmail_inside) {
		char cmdline[256];

		readmail_inside = true;
rswindell's avatar
rswindell committed
86
		safe_snprintf(cmdline, sizeof(cmdline), "%s %d %u %lu", cfg.readmail_mod, which, usernumber, lm_mode);
87
88
89
90
91
		exec_bin(cmdline, &main_csi);
		readmail_inside = false;
		return;
	}

92
93
94
95
96
	if(which==MAIL_SENT && useron.rest&FLAG('K')) {
		bputs(text[R_ReadSentMail]);
		return;
	}

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

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

	last=smb.status.last_msg;

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

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

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

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

199
200
201
202
203
		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;
204
205

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

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

217
		if(smb.status.last_msg!=last || lm_mode != last_mode) { 	/* New messages */
218
			last=smb.status.last_msg;
deuce's avatar
deuce committed
219
			free(mail);
220
			order = (lm_mode&LM_REVERSE) ? "newest" : "oldest";
221
222
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);   /* So re-load */
			if(!smb.msgs)
223
				break;
224
225
226
227
228
229
230
231
232
			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);
			}
233
			last_mode = lm_mode;
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
			last_mode = lm_mode;
247
248
			if(smb.curmsg>(smb.msgs-1))
				smb.curmsg=(smb.msgs-1);
249
			mismatches++;
250
251
			continue; 
		}
252
253
254
255
256
257
258
		smb_unlockmsghdr(&smb,&msg);
		msg.idx.attr=msg.hdr.attr;

		mismatches=0;

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

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

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

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

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

				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 */
rswindell's avatar
rswindell committed
356
					SAFEPRINTF(str2,text[Regarding], msghdr_field(&msg, msg.subj));
357
				else						/* Reply to other */
rswindell's avatar
rswindell committed
358
					SAFEPRINTF3(str2,text[RegardingByOn], msghdr_field(&msg, msg.subj), msghdr_field(&msg, msg.from, tmp)
359
						,timestr(msg.hdr.when_written.time));
360
361
362

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

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

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

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

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

804
long sbbs_t::searchmail(mail_t *mail, long start, long msgs, int which, const char *search, const char* order)
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
{
	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)
829
					bprintf(text[MailSentLstHdr], order);
830
				else if(which==MAIL_ALL)
831
					bprintf(text[MailOnSystemLstHdr], order);
832
				else
833
					bprintf(text[MailWaitingLstHdr], order);
834
835
			}
			if(which==MAIL_ALL)
rswindell's avatar
rswindell committed
836
837
				bprintf(P_TRUNCATE|(msg.hdr.auxattr&MSG_HFIELDS_UTF8)
					,msghdr_text(&msg, MailOnSystemLstFmt)
838
839
840
841
					,l+1,msg.from,msg.to
					,mail_listing_flag(&msg)
					,msg.subj);
			else
rswindell's avatar
rswindell committed
842
843
				bprintf(P_TRUNCATE|(msg.hdr.auxattr&MSG_HFIELDS_UTF8)
					,msghdr_text(&msg, MailWaitingLstFmt),l+1
844
845
846
847
848
849
850
851
852
853
854
855
856
					,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);
}