forked from virtadpt/eBBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_mail.c
426 lines (393 loc) · 10.2 KB
/
c_mail.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Eagles Bulletin Board System
Copyright (C) 1995, Ray Rocker, [email protected]
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "client.h"
#include <stdlib.h>
#include <sys/stat.h>
OpenMailbox( int *openflags, int newonly, int *resp )
{
int code;
OPENINFO openinfo;
code = bbs_open_mailbox(&openinfo);
if (resp) *resp = code;
if (code != S_OK) return -1;
if (openflags) *openflags = (int)openinfo.flags;
return (newonly ? (int)openinfo.newmsgs : (int)openinfo.totalmsgs);
}
int CloseMailbox()
{
int code;
code = bbs_close_board();
return (code == S_OK ? 0 : -1);
}
/*
If mail to ANY recipients fails, return error with retcode holding the mask.
Abort mail, return -2.
Zero length file, return -3.
Otherwise return 0.
*/
/* NAMELIST recips; list of intended recipients */
/* char *subject; subject string */
/* char *textfile; file to send: if NULL invoke editor */
/* int doedit; edit even if textfile != NULL */
/* LONG *retcode; bit mask of failed destinations */
DoMailSend( NAMELIST recips, char *subject, char *textfile, int doedit, LONG *retcode )
{
int rc = 0;
struct stat stbuf;
if (textfile == NULL || doedit) {
if (textfile == NULL) textfile = c_tempfile;
if (Edit(textfile)) {
rc = -2;
}
}
if (rc == 0) {
if (stat(textfile, &stbuf) != 0 || stbuf.st_size == 0) {
rc = -3;
}
else rc = bbs_mail(NULL, NULL, recips, subject, textfile, retcode);
}
if (textfile == c_tempfile) unlink(c_tempfile);
return rc;
}
show_names( int indx, char *userid, LONG *mask )
{
int x, y;
if ((*mask) & (LONG)(1<<indx)) {
getyx(&y, &x);
if (strlen(userid)+3 > (t_columns-x)) prints("\n");
else if (x > 0) prints(", ");
prints("%s", userid);
}
return S_OK;
}
GenericMailSend(int group)
{
int rc;
ADDR nbuf;
NAMELIST recips = NULL;
TITLE subject;
int max, count = 0;
char *prompt;
LONG failmask = ~0;
ACCOUNT acct;
bbs_set_mode(M_MAIL);
if (group) {
max = BBS_MAX_MAILRECIPS;
prompt = "Enter recipient (RETURN when done): ";
}
else {
max = 1;
prompt = "Enter recipient: ";
}
move(2,0);
clrtobot();
for (count = 0; count < max; count++) {
move(2,0);
namecomplete(bbs_acctnames, NULL, prompt, nbuf, sizeof nbuf);
if (nbuf[0] == '\0') break;
if (strchr(nbuf, ':') != NULL) {
if (!HasPerm(C_EXTERNMAIL)) {
move(3,0);
bbperror(S_NOSUCHUSER, NULL);
continue;
}
}
else {
if ((rc = bbs_query(nbuf, &acct)) != S_OK) {
move(3,0);
bbperror(rc, NULL);
continue;
}
strcpy(nbuf, acct.userid);
}
if (!is_in_namelist(recips, nbuf))
add_namelist(&recips, nbuf, NULL);
if (group) {
move(3,0);
prints("\nSending to:\n");
apply_namelist(recips, show_names, &failmask);
}
}
if (recips != NULL) {
getdata(3,0,"Subject: ", subject, sizeof subject, DOECHO, 0);
rc = DoMailSend(recips, subject, (char *)NULL, 0, &failmask);
clear();
move(0,0);
switch (rc) {
case -3:
prints("Zero length file NOT sent.\n");
break;
case -2:
prints("Mail NOT sent.\n");
break;
case 0:
prints("Mail sent!\n");
break;
default:
if (group) {
bbperror(rc, "Mail failed to the following");
apply_namelist(recips, show_names, &failmask);
}
else bbperror(rc, "Mail failed");
break;
}
}
pressreturn();
bbs_set_mode(M_UNDEFINED);
return FULLUPDATE;
}
GroupSend()
{
return (GenericMailSend(1));
}
MailSend()
{
return (GenericMailSend(0));
}
/*ARGSUSED*/
MailDelete( HEADER *hptr, int currmsg, int numrecs, int openflags )
{
int code, rc = FULLUPDATE;
char ans[4];
clear();
prints("Delete message from %s\n", hptr->owner);
prints("entitled '%s'\n", hptr->title);
getdata(2,0,"Are you sure (Y/N)? [N]: ",ans,sizeof(ans),DOECHO, 0);
move(4,0);
if (*ans != 'Y' && *ans != 'y') {
prints("Message not deleted.\n");
}
else {
if ((code = bbs_delete_message(hptr->fileid)) == S_OK) {
prints("Message deleted.\n");
rc |= FETCHNEW;
}
else {
bbperror(code, "Deletion failed");
}
}
pressreturn();
return rc;
}
/*ARGSUSED*/
MailDelRange( HEADER *hptr, int currmsg, int numinbox, int openflags )
{
int code, rc = FULLUPDATE;
char ans[5];
SHORT n1 = 0, n2 = 0, deleted;
clear();
if (numinbox <= 0) {
prints("No messages to delete!\n");
pressreturn();
return rc;
}
prints("Delete Range\n");
prints("[NOTE: Unread messages will NOT be deleted]\n");
do {
if (getdata(2,0,"Enter number of first message to delete: ",
ans,sizeof(ans), DOECHO, 1) == -1) return rc;
n1 = atoi(ans);
if (n1 <= 0 || n1 > numinbox) {
bell();
move(3,0);
prints("Not valid! Must be in range 1-%d.\n", numinbox);
}
} while (n1 <= 0 || n1 > numinbox);
do {
if (getdata(3,0,"Enter number of last message to delete: ",
ans,sizeof(ans), DOECHO, 1) == -1) return rc;
n2 = atoi(ans);
if (n2 < n1 || n2 > numinbox) {
bell();
move(4,0);
prints("Not valid! Must be in range %d-%d.\n", n1, numinbox);
}
} while (n2 < n1 || n2 > numinbox);
move(4,0);
clrtobot();
getdata(4,0,"Are you sure (Y/N)? [N]: ",ans,sizeof(ans),DOECHO,0);
move(6,0);
if (*ans != 'Y' && *ans != 'y') {
prints("No messages deleted.\n");
}
else {
if ((code = bbs_delete_range(n1, n2, &deleted)) == S_OK) {
prints("%d messages deleted.\n", deleted);
rc |= FETCHNEW;
}
else bbperror(code, "Delete range failed\n");
}
pressreturn();
return rc;
}
/*ARGSUSED*/
Forward( HEADER *hptr, int currmsg, int numrecs, int openflags )
{
int code, rc = FULLUPDATE;
ACCOUNT acct;
char ans[4];
clear();
if ((code = bbs_owninfo(&acct)) != S_OK) {
bbperror(code, "Can't fetch address");
pressreturn();
return rc;
}
prints("Mailing '%s' to:\n", hptr->title);
prints("%s\n", acct.email);
getdata(2,0,"Are you sure (Y/N)? [Y]: ",ans,sizeof(ans),DOECHO,0);
move(4,0);
if (*ans == 'N' || *ans == 'n') {
prints("Message not forwarded.\n");
}
else {
if ((code = bbs_forward_message(hptr->fileid)) == S_OK) {
prints("Mail sent!\n");
}
else bbperror(code, "Forward failed");
}
pressreturn();
return rc;
}
int GenericMailReply( HEADER *hptr, int group, char *msgsrc )
{
NAMELIST recips = NULL;
TITLE subject;
char ans[4];
int rc, retcode = FULLUPDATE;
LONG failmask;
PATH msgfile;
clear();
if (msgsrc == NULL) {
if ((rc = bbs_read_message(hptr->fileid, msgfile)) != S_OK) {
bbperror(rc, "Can't read message");
return retcode;
}
}
else strcpy(msgfile, msgsrc);
add_namelist(&recips, hptr->owner, NULL);
if (group) parse_to_list(&recips, msgfile, myinfo.userid);
if (strncasecmp(hptr->title, "Re:", 3)) {
strcpy(subject, "Re: ");
strncat(subject, hptr->title, TITLELEN-5);
}
else strncpy(subject, hptr->title, TITLELEN-1);
getdata(0,0,"Include message text in reply (Y/N)? [N]: ",
ans, sizeof(ans), DOECHO, 0);
if (*ans == 'Y' || *ans == 'y') {
AnnotateMessageBody(c_tempfile, msgfile);
}
rc = DoMailSend(recips, subject, c_tempfile, 1, &failmask);
clear();
move(0,0);
switch (rc) {
case -3:
prints("Zero length reply not sent.\n");
break;
case -2:
prints("Reply NOT sent.\n");
break;
case 0:
prints("Reply sent!\n");
if (is_in_namelist(recips, myinfo.userid)) BITSET(retcode, FETCHNEW);
break;
default:
if (group) {
bbperror(rc, "Reply not delivered to following");
apply_namelist(recips, show_names, &failmask);
}
else bbperror(rc, "Reply could not be delivered.\n");
break;
}
unlink(c_tempfile);
pressreturn();
return retcode;
}
/*ARGSUSED*/
MailReply( HEADER *hptr, int currmsg, int numrecs, int openflags )
{
return (GenericMailReply(hptr, 0, NULL));
}
/*ARGSUSED*/
GroupReply( HEADER *hptr, int currmsg, int numrecs, int openflags )
{
return (GenericMailReply(hptr, 1, NULL));
}
/*ARGSUSED*/
MailDisplay( HEADER *hptr, int currmsg, int numrecs, int openflags )
{
char ans[9], promptstr[80];
PATH msgfile;
int code, rc = FULLUPDATE;
int done = 0, replied = 0;
#ifdef REMOTE_CLIENT
int saved = 0;
#else
int forwarded = 0;
#endif
if ((code = bbs_read_message(hptr->fileid, msgfile)) != S_OK) {
bbperror(code, "Error retrieving file");
pressreturn();
return rc;
}
BITCLR(hptr->flags, FILE_UNREAD);
clear();
More(msgfile, 0);
while (!done) {
promptstr[0] = '\0';
if (!replied) strcat(promptstr, "Reply, Group reply, ");
#ifdef REMOTE_CLIENT
if (!saved) strcat(promptstr, "Save, ");
#else
if (!forwarded) strcat(promptstr, "Forward, ");
#endif
strcat(promptstr, "Delete, or Continue? [C]: ");
getdata(t_lines-1, 0, promptstr, ans, sizeof ans, DOECHO, 0);
switch (*ans) {
case 'r': case 'R':
case 'g': case 'G':
if (replied) done = 1;
else {
rc |= GenericMailReply(hptr, toupper(*ans)=='G'?1:0, msgfile);
replied = 1;
}
break;
#ifdef REMOTE_CLIENT
case 's': case 'S':
if (saved) done = 1;
else {
rc |= SaveFile(msgfile);
saved = 1;
}
break;
#else
case 'f': case 'F':
if (forwarded) done = 1;
else {
rc |= Forward(hptr, currmsg, numrecs, openflags);
forwarded = 1;
}
break;
#endif
case 'd': case 'D':
rc |= MailDelete(hptr, currmsg, numrecs, openflags);
/* fall through */
default:
done = 1;
break;
}
}
return rc;
}