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
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);
145
			bprintf(text[MailWaitingLstFmt],smb.curmsg+1
146
147
148
				,which==MAIL_SENT ? msg.to
				: (msg.hdr.attr&MSG_ANONYMOUS) && !SYSOP ? text[Anonymous]
				: msg.from
149
				,mail_listing_flag(&msg)
150
151
				,msg.subj);
			smb_freemsgmem(&msg);
152
153
			msg.total_hfields=0; 
		}
154
155

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

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

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

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

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

216
		if(smb.status.last_msg!=last || lm_mode != last_mode) { 	/* New messages */
217
			last=smb.status.last_msg;
deuce's avatar
deuce committed
218
			free(mail);
219
			order = (lm_mode&LM_REVERSE) ? "newest" : "oldest";
220
221
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);   /* So re-load */
			if(!smb.msgs)
222
				break;
223
224
225
226
227
228
229
230
231
			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);
			}
232
			last_mode = lm_mode;
233
234
			continue; 
		}
235

236
		if(loadmsg(&msg,mail[smb.curmsg].number) < 0) {	/* Message header gone */
237
			if(mismatches>5) {	/* We can't do this too many times in a row */
238
				errormsg(WHERE,ERR_CHK,"message number",mail[smb.curmsg].number);
239
240
				break; 
			}
deuce's avatar
deuce committed
241
			free(mail);
242
243
			mail=loadmail(&smb,&smb.msgs,usernumber,which,lm_mode);
			if(!smb.msgs)
244
				break;
245
			last_mode = lm_mode;
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
		smb_unlockmsghdr(&smb,&msg);
		msg.idx.attr=msg.hdr.attr;

		mismatches=0;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

799
long sbbs_t::searchmail(mail_t *mail, long start, long msgs, int which, const char *search, const char* order)
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
{
	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)
824
					bprintf(text[MailSentLstHdr], order);
825
				else if(which==MAIL_ALL)
826
					bprintf(text[MailOnSystemLstHdr], order);
827
				else
828
					bprintf(text[MailWaitingLstHdr], order);
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
			}
			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);
}