mh_whatnow.c
12.9 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <mh.h>
typedef int (*handler_fp) __PMT((struct mh_whatnow_env *wh,
int argc, char **argv,
int *status));
/* ********************* Auxiliary functions *********************** */
/* Auxiliary function for option comparisons */
static char
strnulcmp (const char *str, const char *pattern)
{
return strncmp (str, pattern, strlen (str));
}
/* Dispatcher functions */
struct action_tab {
char *name;
handler_fp fp;
};
static handler_fp
func (struct action_tab *p, const char *name)
{
int len;
if (!name)
return func (p, "help");
len = strlen (name);
for (; p->name; p++)
{
int min = strlen (p->name);
if (min > len)
min = len;
if (strncmp (p->name, name, min) == 0)
return p->fp;
}
mh_error (_("%s is unknown. Hit <CR> for help"), name);
return NULL;
}
struct helpdata {
char *name;
char *descr;
};
/* Functions for printing help information */
#define OPT_DOC_COL 29 /* column in which option text starts */
#define RMARGIN 79 /* right margin used for wrapping */
static int
print_short (const char *str)
{
int n;
char *s;
for (n = 0; *str; str++, n++)
{
switch (*str)
{
case '+':
putchar ('+');
s = _("FOLDER");
n += printf ("%s", s);
break;
case '<':
switch (str[1])
{
case '>':
s = _("SWITCHES");
n += printf ("%s", s) - 1;
str++;
break;
case 'e':
s = _("EDITOR");
n += printf ("%s", s) - 1;
str++;
break;
default:
putchar (*str);
}
break;
default:
putchar (*str);
}
}
return n;
}
static void
print_descr (int n, char *s)
{
do
{
char *p;
char *space = NULL;
for (; n < OPT_DOC_COL; n++)
putchar (' ');
for (p = s; *p && p < s + (RMARGIN - OPT_DOC_COL); p++)
if (isspace (*p))
space = p;
if (!space || p < s + (RMARGIN - OPT_DOC_COL))
{
printf ("%s", s);
s += strlen (s);
}
else
{
for (; s < space; s++)
putchar (*s);
for (; *s && isspace (*s); s++)
;
}
putchar ('\n');
n = 1;
}
while (*s);
}
static int
_help (struct helpdata *helpdata, char *argname)
{
struct helpdata *p;
printf ("%s\n", _("Options are:"));
if (argname == NULL || argname[0] == '?')
{
/* Short version */
for (p = helpdata; p->name; p++)
{
printf (" ");
print_short (p->name);
putchar ('\n');
}
}
else
{
for (p = helpdata; p->name; p++)
{
int n;
n = printf (" ");
n += print_short (p->name);
print_descr (n+1, _(p->descr));
}
}
return 0;
}
/* Display the contents of the given file on the terminal */
static void
display_file (const char *name)
{
char *pager = mh_global_profile_get ("moreproc", getenv ("PAGER"));
if (pager)
mh_spawnp (pager, name);
else
{
stream_t stream;
int rc;
size_t off = 0;
size_t n;
char buffer[512];
rc = file_stream_create (&stream, name, MU_STREAM_READ);
if (rc)
{
mh_error ("file_stream_create: %s", mu_strerror (rc));
return;
}
rc = stream_open (stream);
if (rc)
{
mh_error ("stream_open: %s", mu_strerror (rc));
return;
}
while (stream_read (stream, buffer, sizeof buffer - 1, off, &n) == 0
&& n != 0)
{
buffer[n] = '\0';
printf ("%s", buffer);
off += n;
}
stream_destroy (&stream, NULL);
}
}
static int
invoke (char *compname, char *defval, int argc, char **argv,
char *extra0, char *extra1, int *status)
{
int i, rc;
char **xargv;
char *progname;
progname = mh_global_profile_get (compname, defval);
if (!progname)
return -1;
xargv = calloc (argc+3, sizeof (*xargv));
if (!xargv)
{
mh_err_memory (0);
return -1;
}
xargv[0] = progname;
for (i = 1; i < argc; i++)
xargv[i] = argv[i];
if (extra0)
xargv[i++] = extra0;
if (extra1)
xargv[i++] = extra1;
xargv[i++] = NULL;
rc = mu_spawnvp (xargv[0], (const char **) xargv, status);
free (xargv);
return rc;
}
/* ************************ Shell Function ************************* */
static int
_whatnow (struct mh_whatnow_env *wh, struct action_tab *tab)
{
int rc, status = 0;
do
{
char *line = NULL;
size_t size = 0;
int argc;
char **argv;
handler_fp fun;
printf ("%s ", wh->prompt);
getline (&line, &size, stdin);
if (!line)
continue;
rc = argcv_get (line, "", "#", &argc, &argv);
free (line);
if (rc)
{
argcv_free (argc, argv);
break;
}
fun = func (tab, argv[0]);
if (fun)
rc = fun (wh, argc, argv, &status);
else
rc = 0;
argcv_free (argc, argv);
}
while (rc == 0);
return status;
}
/* ************************** Actions ****************************** */
/* Display action */
static int
display (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
if (!wh->msg)
mh_error (_("no alternate message to display"));
else
display_file (wh->msg);
return 0;
}
/* Edit action */
static int
edit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
char *name;
int rc;
asprintf (&name, "%s-next", wh->editor);
invoke (name, wh->editor, argc, argv, wh->file, NULL, &rc);
free (name);
return 0;
}
/* List action */
static int
list (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
if (!wh->file)
mh_error (_("no draft file to display"));
else
display_file (wh->file);
return 0;
}
/* Push action */
static int
push (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
return 0;
}
/* Quit action */
static int
quit (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
*status = 0;
if (wh->draftfile)
{
if (argc == 2 && strnulcmp (argv[1], "-delete") == 0)
unlink (wh->draftfile);
else
{
printf (_("draft left on \"%s\".\n"), wh->draftfile);
rename (wh->file, wh->draftfile);
}
}
return 1;
}
/* Refile action */
static int
refile (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
int rc;
invoke ("fileproc", "refile", argc, argv, "-file", wh->file, &rc);
return 0;
}
/* Send action */
static int
send (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
return 0;
}
/* Print an email in more readable form: localpart + "at" + domain */
static void
print_readable (char *email)
{
for (; *email && *email != '@'; email++)
putchar (*email);
if (!*email)
return;
printf (_(" at %s\n"), email+1);
}
/* Whom action */
static int
whom (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
if (!wh->file)
mh_error (_("no draft file to display"));
else
{
mh_context_t *ctx;
ctx = mh_context_create (wh->file, 1);
if (mh_context_read (ctx))
{
mh_error(_("malformed message"));
}
else
{
char *to = mh_context_get_value (ctx, MU_HEADER_TO, NULL);
char *cc = mh_context_get_value (ctx, MU_HEADER_CC, NULL);
char *bcc = mh_context_get_value (ctx, MU_HEADER_BCC, NULL);
char *s = to ? to : (cc ? cc : bcc);
if (!s)
{
mh_error(_("No recipients"));
}
else
{
address_t addr;
size_t i, count;
address_create (&addr, s);
if (cc)
{
address_t a;
address_create (&a, cc);
address_concatenate (addr, &a);
}
if (bcc)
{
address_t a;
address_create (&a, bcc);
address_concatenate (addr, &a);
}
printf (" %s\n", _("-- Network Recipients --"));
address_get_count (addr, &count);
for (i = 1; i <= count; i++)
{
char *buf;
int rc;
rc = address_aget_email (addr, i, &buf);
if (rc)
{
mh_error("address_aget_email: %s", mu_strerror (rc));
continue;
}
printf (" ");
print_readable (buf);
free (buf);
}
}
/* Cleanup everything. Note comment to mh_context_get_value! */
free (bcc);
free (cc);
free (to);
}
free (ctx);
}
return 0;
}
/* Help table for whatnow */
static struct helpdata whatnow_helptab[] = {
{ "display [<>]",
N_("List the message being distributed/replied-to on the terminal.") },
{ "edit [<e <>]",
N_("Edit the message. If EDITOR is omitted use the one that was used on"
" the preceeding round unless the profile entry \"LASTEDITOR-next\""
" names an alternate editor.") },
{ "list [<>]",
N_("List the draft on the terminal.") },
{ "push [<>]",
N_("Send the message in the background.") },
{ "quit [-delete]",
N_("Terminate the session. Preserve the draft, unless -delete flag is given.") },
{ "refile [<>] +",
N_("Refile the draft into the given FOLDER.") },
{ "send [-watch] [<>]",
N_("Send the message. The -watch flag causes the delivery process to be "
"monitored. SWITCHES are passed to send program verbatim.") },
{ "whom [-check] [<>]",
N_("List the addresses and verify that they are acceptable to the "
"transport service.") },
{ NULL },
};
/* Help action for whatnow shell */
static int
whatnow_help (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
return _help (whatnow_helptab, argv[0]);
}
/* Actions specific for the ``disposition'' shell */
/* Help table for ``disposition'' shell */
static struct helpdata disposition_helptab[] = {
{ "quit", N_("Terminate the session. Preserve the draft.") },
{ "replace", N_("Replace the draft with the newly created one") },
{ "use", N_("Use this draft") },
{ "list", N_("List the draft on the terminal.") },
{ "refile [<>] +", N_("Refile the draft into the given FOLDER.") },
{ NULL },
};
/* Help action */
static int
disp_help (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
return _help (disposition_helptab, argv[0]);
}
/* Use action */
static int
use (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
*status = DISP_USE;
return 1;
}
/* Replace action */
static int
replace (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
*status = DISP_REPLACE;
return 1;
}
/* *********************** Interfaces ****************************** */
/* Whatnow shell */
static struct action_tab whatnow_tab[] = {
{ "help", whatnow_help },
{ "?", whatnow_help },
{ "display", display },
{ "edit", edit },
{ "list", list },
{ "push", push },
{ "quit", quit },
{ "refile", refile },
{ "send", send },
{ "whom", whom },
{ NULL }
};
int
mh_whatnow (struct mh_whatnow_env *wh, int initial_edit)
{
if (!wh->editor)
wh->editor = mh_global_profile_get ("Editor", "prompter");
if (initial_edit)
mh_spawnp (wh->editor, wh->file);
if (!wh->prompt)
wh->prompt = _("What now?");
return _whatnow (wh, whatnow_tab);
}
/* Disposition shell */
static struct action_tab disp_tab[] = {
{ "help", disp_help },
{ "?", disp_help },
{ "quit", quit },
{ "replace", replace },
{ "use", use },
{ "list", list },
{ "refile", refile },
{ NULL }
};
int
mh_disposition (const char *filename)
{
struct mh_whatnow_env wh;
memset (&wh, 0, sizeof (wh));
wh.file = filename;
wh.prompt = _("Disposition?");
return _whatnow (&wh, disp_tab);
}
/* Use draft shell */
/* Help table for ``use draft'' shell */
static struct helpdata usedraft_helptab[] = {
{ "no", N_("Don't use the draft.") },
{ "yes", N_("Use the draft.") },
{ "list", N_("List the draft on the terminal.") },
{ NULL },
};
static int
usedraft_help (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
return _help (usedraft_helptab, argv[0]);
}
static int
yes (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
*status = 1;
return 1;
}
static int
no (struct mh_whatnow_env *wh, int argc, char **argv, int *status)
{
*status = 0;
return 1;
}
static struct action_tab usedraft_tab[] = {
{ "help", usedraft_help },
{ "?", usedraft_help },
{ "yes", yes },
{ "no", no },
{ "list", list },
{ NULL }
};
int
mh_usedraft (const char *filename)
{
struct mh_whatnow_env wh;
int rc;
memset (&wh, 0, sizeof (wh));
wh.file = filename;
asprintf (&wh.prompt, _("Use \"%s\"?"), filename);
rc = _whatnow (&wh, usedraft_tab);
free (wh.prompt);
return rc;
}