forw.c
11.8 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
GNU Mailutils 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, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
/* MH forw command */
#include <mh.h>
const char *program_version = "forw (" PACKAGE_STRING ")";
static char doc[] = N_("GNU MH forw")"\v"
N_("Options marked with `*' are not yet implemented.\n\
Use -help to obtain the list of traditional MH options.");
static char args_doc[] = "[msgs]";
/* GNU options */
static struct argp_option options[] = {
{"annotate", ARG_ANNOTATE, N_("BOOL"), OPTION_ARG_OPTIONAL,
N_("Add Forwarded: header to each forwarded message")},
{"build", ARG_BUILD, 0, 0,
N_("Build the draft and quit immediately")},
{"draftfolder", ARG_DRAFTFOLDER, N_("FOLDER"), 0,
N_("Specify the folder for message drafts")},
{"nodraftfolder", ARG_NODRAFTFOLDER, 0, 0,
N_("Undo the effect of the last --draftfolder option")},
{"draftmessage" , ARG_DRAFTMESSAGE, N_("MSG"), 0,
N_("Invoke the draftmessage facility")},
{"folder", ARG_FOLDER, N_("FOLDER"), 0,
N_("Specify folder to operate upon")},
{"editor", ARG_EDITOR, N_("PROG"), 0,
N_("Set the editor program to use")},
{"noedit", ARG_NOEDIT, 0, 0,
N_("Suppress the initial edit")},
{"format", ARG_FORMAT, N_("BOOL"), 0,
N_("Format messages")},
{"noformat", ARG_NOFORMAT, NULL, 0,
N_("Undo the effect of the last --format option") },
{"form", ARG_FORM, N_("FILE"), 0,
N_("Read format from given file")},
{"filter", ARG_FILTER, N_("FILE"), 0,
N_("Use filter FILE to preprocess the body of the message") },
{"nofilter", ARG_NOFILTER, NULL, 0,
N_("Undo the effect of the last --filter option") },
{"inplace", ARG_INPLACE, N_("BOOL"), OPTION_ARG_OPTIONAL,
N_("* Annotate the message in place")},
{"noinplace", ARG_NOINPLACE, 0, OPTION_HIDDEN, "" },
{"mime", ARG_MIME, N_("BOOL"), OPTION_ARG_OPTIONAL,
N_("Use MIME encapsulation") },
{"nomime", ARG_NOMIME, NULL, OPTION_HIDDEN, "" },
{"width", ARG_WIDTH, N_("NUMBER"), 0, N_("Set output width")},
{"whatnowproc", ARG_WHATNOWPROC, N_("PROG"), 0,
N_("* Set the replacement for whatnow program")},
{"nowhatnowproc", ARG_NOWHATNOWPROC, NULL, 0,
N_("* Ignore whatnowproc variable. Use standard `whatnow' shell instead")},
{"use", ARG_USE, N_("BOOL"), OPTION_ARG_OPTIONAL,
N_("Use draft file preserved after the last session") },
{"nouse", ARG_NOUSE, N_("BOOL"), OPTION_HIDDEN, "" },
{"license", ARG_LICENSE, 0, 0,
N_("Display software license"), -1},
{NULL},
};
/* Traditional MH options */
struct mh_option mh_option[] = {
{"annotate", 1, MH_OPT_BOOL },
{"build", 1, },
{"form", 4, MH_OPT_ARG, "formatfile"},
{"format", 5, MH_OPT_ARG, "string"},
{"draftfolder", 6, MH_OPT_ARG, "folder"},
{"nodraftfolder", 3 },
{"draftmessage", 6, },
{"editor", 1, MH_OPT_ARG, "program"},
{"noedit", 3, },
{"filter", 2, MH_OPT_ARG, "program"},
{"inplace", 1, MH_OPT_BOOL },
{"whatnowproc", 2, MH_OPT_ARG, "program"},
{"nowhatnowproc", 3 },
{"mime", 2, MH_OPT_BOOL, NULL},
{NULL}
};
enum encap_type {
encap_clear,
encap_mhl,
encap_mime
};
static char *formfile;
struct mh_whatnow_env wh_env = { 0 };
static int initial_edit = 1;
static char *mhl_filter = NULL; /* --filter flag */
static int build_only = 0; /* --build flag */
static int annotate = 0; /* --annotate flag */
static enum encap_type encap = encap_clear; /* controlled by --format, --form
and --mime flags */
static int use_draft = 0; /* --use flag */
static int width = 80; /* --width flag */
static char *draftmessage = "new";
static mh_msgset_t msgset;
static mu_mailbox_t mbox;
static int
opt_handler (int key, char *arg, void *unused, struct argp_state *state)
{
switch (key)
{
case ARGP_KEY_INIT:
wh_env.draftfolder = mh_global_profile_get ("Draft-Folder",
mu_folder_directory ());
break;
case ARG_ANNOTATE:
annotate = is_true (arg);
break;
case ARG_BUILD:
build_only = 1;
break;
case ARG_DRAFTFOLDER:
wh_env.draftfolder = arg;
break;
case ARG_NODRAFTFOLDER:
wh_env.draftfolder = NULL;
break;
case ARG_DRAFTMESSAGE:
draftmessage = arg;
break;
case ARG_USE:
use_draft = is_true (arg);
break;
case ARG_NOUSE:
use_draft = 0;
break;
case ARG_WIDTH:
width = strtoul (arg, NULL, 0);
if (!width)
{
argp_error (state, _("Invalid width"));
exit (1);
}
break;
case ARG_EDITOR:
wh_env.editor = arg;
break;
case ARG_FOLDER:
mh_set_current_folder (arg);
break;
case ARG_FORM:
formfile = arg;
break;
case ARG_FORMAT:
if (is_true (arg))
{
encap = encap_mhl;
break;
}
/*FALLTHRU*/
case ARG_NOFORMAT:
if (encap == encap_mhl)
encap = encap_clear;
break;
case ARG_FILTER:
encap = encap_mhl;
break;
case ARG_MIME:
if (is_true (arg))
{
encap = encap_mime;
break;
}
/*FALLTHRU*/
case ARG_NOMIME:
if (encap == encap_mime)
encap = encap_clear;
break;
case ARG_INPLACE:
case ARG_WHATNOWPROC:
case ARG_NOWHATNOWPROC:
argp_error (state, _("Option is not yet implemented"));
exit (1);
case ARG_LICENSE:
mh_license (argp_program_version);
break;
default:
return 1;
}
return 0;
}
struct format_data {
int num;
mu_stream_t stream;
mu_list_t format;
};
/* State machine according to RFC 934:
S1 :: CRLF {CRLF} S1
| "-" {"- -"} S2
| c {c} S2
S2 :: CRLF {CRLF} S1
| c {c} S2
*/
enum rfc934_state { S1, S2 };
static int
msg_copy (mu_message_t msg, mu_stream_t ostream)
{
mu_stream_t istream;
int rc;
size_t n;
char buf[512];
enum rfc934_state state = S1;
rc = mu_message_get_stream (msg, &istream);
if (rc)
return rc;
mu_stream_seek (istream, 0, SEEK_SET);
while (rc == 0
&& mu_stream_sequential_read (istream, buf, sizeof buf, &n) == 0
&& n > 0)
{
size_t start, i;
for (i = start = 0; i < n; i++)
switch (state)
{
case S1:
if (buf[i] == '-')
{
rc = mu_stream_sequential_write (ostream, buf + start,
i - start + 1);
if (rc)
return rc;
rc = mu_stream_sequential_write (ostream, " -", 2);
if (rc)
return rc;
start = i + 1;
state = S2;
}
else if (buf[i] != '\n')
state = S2;
break;
case S2:
if (buf[i] == '\n')
state = S1;
}
if (i > start)
rc = mu_stream_sequential_write (ostream, buf + start, i - start);
}
return rc;
}
void
format_message (mu_mailbox_t mbox, mu_message_t msg, size_t num, void *data)
{
struct format_data *fp = data;
char *s;
int rc;
if (annotate)
mu_list_append (wh_env.anno_list, msg);
if (fp->num)
{
asprintf (&s, "\n------- Message %d\n", fp->num++);
rc = mu_stream_sequential_write (fp->stream, s, strlen (s));
free (s);
}
if (fp->format)
rc = mhl_format_run (fp->format, width, 0, 0, msg, fp->stream);
else
rc = msg_copy (msg, fp->stream);
}
void
finish_draft ()
{
int rc;
mu_stream_t stream;
mu_list_t format = NULL;
struct format_data fd;
char *str;
if (!mhl_filter)
{
char *s = mh_expand_name (MHLIBDIR, "mhl.forward", 0);
if (access (s, R_OK) == 0)
mhl_filter = "mhl.forward";
free (s);
}
if (mhl_filter)
{
char *s = mh_expand_name (MHLIBDIR, mhl_filter, 0);
format = mhl_format_compile (s);
if (!format)
exit (1);
free (s);
}
if ((rc = mu_file_stream_create (&stream,
wh_env.file,
MU_STREAM_WRITE|MU_STREAM_CREAT)) != 0
|| (rc = mu_stream_open (stream)))
{
mu_error (_("Cannot open output file `%s': %s"),
wh_env.file, mu_strerror (rc));
exit (1);
}
mu_stream_seek (stream, 0, SEEK_END);
if (annotate)
{
wh_env.anno_field = "Forwarded";
mu_list_create (&wh_env.anno_list);
}
if (encap == encap_mime)
{
mu_url_t url;
const char *mbox_path;
const char *p;
size_t i;
mu_mailbox_get_url (mbox, &url);
mbox_path = mu_url_to_string (url);
if (memcmp (mbox_path, "mh:", 3) == 0)
mbox_path += 3;
asprintf (&str, "#forw [] +%s", mbox_path);
rc = mu_stream_sequential_write (stream, str, strlen (str));
free (str);
for (i = 0; rc == 0 && i < msgset.count; i++)
{
mu_message_t msg;
size_t num;
mu_mailbox_get_message (mbox, msgset.list[i], &msg);
if (annotate)
mu_list_append (wh_env.anno_list, msg);
mh_message_number (msg, &num);
p = mu_umaxtostr (0, num);
rc = mu_stream_sequential_write (stream, p, strlen (p));
}
}
else
{
str = "\n------- ";
rc = mu_stream_sequential_write (stream, str, strlen (str));
if (msgset.count == 1)
{
fd.num = 0;
str = _("Forwarded message\n");
}
else
{
fd.num = 1;
str = _("Forwarded messages\n");
}
rc = mu_stream_sequential_write (stream, str, strlen (str));
fd.stream = stream;
fd.format = format;
rc = mh_iterate (mbox, &msgset, format_message, &fd);
str = "\n------- ";
rc = mu_stream_sequential_write (stream, str, strlen (str));
if (msgset.count == 1)
str = _("End of Forwarded message");
else
str = _("End of Forwarded messages");
rc = mu_stream_sequential_write (stream, str, strlen (str));
}
rc = mu_stream_sequential_write (stream, "\n\n", 2);
mu_stream_close (stream);
mu_stream_destroy (&stream, mu_stream_get_owner (stream));
}
int
main (int argc, char **argv)
{
int index, rc;
/* Native Language Support */
mu_init_nls ();
mu_argp_init (program_version, NULL);
mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
opt_handler, NULL, &index);
argc -= index;
argv += index;
mbox = mh_open_folder (mh_current_folder (), 0);
mh_msgset_parse (mbox, &msgset, argc, argv, "cur");
if (build_only || !wh_env.draftfolder)
wh_env.file = mh_expand_name (NULL, "draft", 0);
else
{
if (mh_draft_message (NULL, draftmessage, &wh_env.file))
return 1;
}
wh_env.draftfile = wh_env.file;
switch (build_only ?
DISP_REPLACE : check_draft_disposition (&wh_env, use_draft))
{
case DISP_QUIT:
exit (0);
case DISP_USE:
break;
case DISP_REPLACE:
unlink (wh_env.draftfile);
mh_comp_draft (formfile, "forwcomps", wh_env.file);
finish_draft ();
}
/* Exit immediately if --build is given */
if (build_only)
{
if (strcmp (wh_env.file, wh_env.draftfile))
rename (wh_env.file, wh_env.draftfile);
return 0;
}
rc = mh_whatnow (&wh_env, initial_edit);
mu_mailbox_save_attributes (mbox);
mu_mailbox_close (mbox);
mu_mailbox_destroy (&mbox);
return rc;
}