]> diplodocus.org Git - nmh/blob - uip/repl.c
mhshowsbr.c: Delete single-use global int `nolist'.
[nmh] / uip / repl.c
1 /* repl.c -- reply to a message
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "whatnowproc.h"
10 #include "annosbr.h"
11 #include "sbr/m_name.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/read_switch_multiword.h"
14 #include "sbr/concat.h"
15 #include "sbr/seq_setprev.h"
16 #include "sbr/seq_setcur.h"
17 #include "sbr/seq_save.h"
18 #include "sbr/showfile.h"
19 #include "sbr/smatch.h"
20 #include "sbr/refile.h"
21 #include "sbr/m_draft.h"
22 #include "sbr/m_convert.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/ambigsw.h"
29 #include "sbr/path.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
33 #include "h/mime.h"
34 #include "h/done.h"
35 #include "h/utils.h"
36 #include "sbr/m_maildir.h"
37 #include "replsbr.h"
38
39 #define REPL_SWITCHES \
40 X("group", 0, GROUPSW) \
41 X("nogroup", 0, NGROUPSW) \
42 X("annotate", 0, ANNOSW) \
43 X("noannotate", 0, NANNOSW) \
44 X("cc all|to|cc|me", 0, CCSW) \
45 X("nocc all|to|cc|me", 0, NCCSW) \
46 X("draftfolder +folder", 0, DFOLDSW) \
47 X("draftmessage msg", 0, DMSGSW) \
48 X("nodraftfolder", 0, NDFLDSW) \
49 X("editor editor", 0, EDITRSW) \
50 X("noedit", 0, NEDITSW) \
51 X("convertargs type argstring", 0, CONVERTARGSW) \
52 X("fcc folder", 0, FCCSW) \
53 X("filter filterfile", 0, FILTSW) \
54 X("form formfile", 0, FORMSW) \
55 X("format", 5, FRMTSW) \
56 X("noformat", 7, NFRMTSW) \
57 X("inplace", 0, INPLSW) \
58 X("noinplace", 0, NINPLSW) \
59 X("mime", 0, MIMESW) \
60 X("nomime", 0, NMIMESW) \
61 X("query", 0, QURYSW) \
62 X("noquery", 0, NQURYSW) \
63 X("whatnowproc program", 0, WHATSW) \
64 X("nowhatnowproc", 0, NWHATSW) \
65 X("width columns", 0, WIDTHSW) \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
68 X("file file", 4, FILESW) \
69 X("build", 5, BILDSW) /* interface from mhe */ \
70 X("atfile", 0, ATFILESW) \
71 X("noatfile", 0, NOATFILESW) \
72 X("fmtproc program", 0, FMTPROCSW) \
73 X("nofmtproc", 0, NFMTPROCSW) \
74
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(REPL);
77 #undef X
78
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(REPL, switches);
81 #undef X
82
83 #define CC_SWITCHES \
84 X("to", 0, CTOSW) \
85 X("cc", 0, CCCSW) \
86 X("me", 0, CMESW) \
87 X("all", 0, CALSW) \
88
89 #define X(sw, minchars, id) id,
90 DEFINE_SWITCH_ENUM(CC);
91 #undef X
92
93 #define X(sw, minchars, id) { sw, minchars, id },
94 DEFINE_SWITCH_ARRAY(CC, ccswitches);
95 #undef X
96
97 #define DISPO_SWITCHES \
98 X("quit", 0, NOSW) \
99 X("replace", 0, YESW) \
100 X("list", 0, LISTDSW) \
101 X("refile +folder", 0, REFILSW) \
102 X("new", 0, NEWSW) \
103
104 #define X(sw, minchars, id) id,
105 DEFINE_SWITCH_ENUM(DISPO);
106 #undef X
107
108 #define X(sw, minchars, id) { sw, minchars, id },
109 DEFINE_SWITCH_ARRAY(DISPO, aqrnl);
110 #undef X
111
112 static struct swit aqrl[] = {
113 { "quit", 0, NOSW },
114 { "replace", 0, YESW },
115 { "list", 0, LISTDSW },
116 { "refile +folder", 0, REFILSW },
117 { NULL, 0, 0 }
118 };
119
120 static short outputlinelen = OUTPUTLINELEN;
121 static bool groupreply; /* Is this a group reply? */
122
123 static bool mime; /* include original as MIME part */
124 static char *form = NULL; /* form (components) file */
125 static char *filter = NULL; /* message filter file */
126 static char *fcc = NULL; /* folders to add to Fcc: header */
127
128
129 /*
130 * prototypes
131 */
132 static void docc (char *, int);
133 static void add_convert_header (const char *, char *, char *, char *);
134
135
136 int
137 main (int argc, char **argv)
138 {
139 int i, isdf = 0;
140 bool anot = false;
141 bool inplace = true;
142 bool nedit = false;
143 bool nwhat = false;
144 bool atfile = false;
145 int fmtproc = -1;
146 char *cp, *cwd, *dp, *maildir, *file = NULL;
147 char *folder = NULL, *msg = NULL, *dfolder = NULL;
148 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
149 char **argp, **arguments;
150 svector_t convert_types = svector_create (10);
151 svector_t convert_args = svector_create (10);
152 size_t n;
153 struct msgs *mp = NULL;
154 struct stat st;
155 FILE *in;
156 bool buildsw = false;
157
158 if (nmh_init(argv[0], true, true)) { return 1; }
159
160 arguments = getarguments (invo_name, argc, argv, 1);
161 argp = arguments;
162
163 while ((cp = *argp++)) {
164 if (*cp == '-') {
165 switch (smatch (++cp, switches)) {
166 case AMBIGSW:
167 ambigsw (cp, switches);
168 done (1);
169 case UNKWNSW:
170 die("-%s unknown", cp);
171
172 case HELPSW:
173 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
174 invo_name);
175 print_help (buf, switches, 1);
176 done (0);
177 case VERSIONSW:
178 print_version(invo_name);
179 done (0);
180
181 case GROUPSW:
182 groupreply = true;
183 continue;
184 case NGROUPSW:
185 groupreply = false;
186 continue;
187
188 case ANNOSW:
189 anot = true;
190 continue;
191 case NANNOSW:
192 anot = false;
193 continue;
194
195 case CCSW:
196 if (!(cp = *argp++) || *cp == '-')
197 die("missing argument to %s", argp[-2]);
198 docc (cp, 1);
199 continue;
200 case NCCSW:
201 if (!(cp = *argp++) || *cp == '-')
202 die("missing argument to %s", argp[-2]);
203 docc (cp, 0);
204 continue;
205
206 case EDITRSW:
207 if (!(ed = *argp++) || *ed == '-')
208 die("missing argument to %s", argp[-2]);
209 nedit = false;
210 continue;
211 case NEDITSW:
212 nedit = true;
213 continue;
214
215 case CONVERTARGSW: {
216 char *type;
217 size_t i;
218
219 if (!(type = *argp++)) {
220 die("missing type argument to %s", argp[-2]);
221 }
222 if (!(cp = *argp++)) {
223 die("missing argstring argument to %s",
224 argp[-3]);
225 }
226
227 for (i = 0; i < svector_size (convert_types); ++i) {
228 if (! strcmp (svector_at (convert_types, i), type)) {
229 /* Already saw this type, so just update
230 its args. */
231 svector_strs (convert_args)[i] = cp;
232 break;
233 }
234 }
235
236 if (i == svector_size (convert_types)) {
237 svector_push_back (convert_types, type);
238 svector_push_back (convert_args, cp);
239 }
240 continue;
241 }
242
243 case WHATSW:
244 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
245 die("missing argument to %s", argp[-2]);
246 nwhat = false;
247 continue;
248 case BILDSW:
249 buildsw = true;
250 /* FALLTHRU */
251 case NWHATSW:
252 nwhat = true;
253 continue;
254
255 case FCCSW:
256 if (!(cp = *argp++) || *cp == '-')
257 die("missing argument to %s", argp[-2]);
258 dp = NULL;
259 if (*cp == '@')
260 cp = dp = path (cp + 1, TSUBCWF);
261 if (fcc)
262 fcc = add (", ", fcc);
263 fcc = add (cp, fcc);
264 free(dp);
265 continue;
266
267 case FILESW:
268 if (file)
269 die("only one file at a time!");
270 if (!(cp = *argp++) || *cp == '-')
271 die("missing argument to %s", argp[-2]);
272 file = path (cp, TFILE);
273 continue;
274 case FILTSW:
275 if (!(cp = *argp++) || *cp == '-')
276 die("missing argument to %s", argp[-2]);
277 filter = mh_xstrdup(etcpath(cp));
278 mime = false;
279 continue;
280 case FORMSW:
281 if (!(form = *argp++) || *form == '-')
282 die("missing argument to %s", argp[-2]);
283 continue;
284
285 case FRMTSW:
286 filter = mh_xstrdup(etcpath(mhlreply));
287 mime = false;
288 continue;
289 case NFRMTSW:
290 filter = NULL;
291 continue;
292
293 case INPLSW:
294 inplace = true;
295 continue;
296 case NINPLSW:
297 inplace = false;
298 continue;
299
300 case MIMESW:
301 mime = true;
302 filter = NULL;
303 continue;
304 case NMIMESW:
305 mime = false;
306 continue;
307
308 case QURYSW:
309 querysw++;
310 continue;
311 case NQURYSW:
312 querysw = 0;
313 continue;
314
315 case WIDTHSW:
316 if (!(cp = *argp++) || *cp == '-')
317 die("missing argument to %s", argp[-2]);
318 if ((outputlinelen = atoi (cp)) < 10)
319 die("impossible width %d", outputlinelen);
320 continue;
321
322 case DFOLDSW:
323 if (dfolder)
324 die("only one draft folder at a time!");
325 if (!(cp = *argp++) || *cp == '-')
326 die("missing argument to %s", argp[-2]);
327 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
328 *cp != '@' ? TFOLDER : TSUBCWF);
329 continue;
330 case DMSGSW:
331 if (dmsg)
332 die("only one draft message at a time!");
333 if (!(dmsg = *argp++) || *dmsg == '-')
334 die("missing argument to %s", argp[-2]);
335 continue;
336 case NDFLDSW:
337 dfolder = NULL;
338 isdf = NOTOK;
339 continue;
340
341 case ATFILESW:
342 atfile = true;
343 continue;
344 case NOATFILESW:
345 atfile = false;
346 continue;
347
348 case FMTPROCSW:
349 if (!(formatproc = *argp++) || *formatproc == '-')
350 die("missing argument to %s", argp[-2]);
351 fmtproc = 1;
352 continue;
353 case NFMTPROCSW:
354 fmtproc = 0;
355 continue;
356 }
357 }
358 if (*cp == '+' || *cp == '@') {
359 if (folder)
360 die("only one folder at a time!");
361 folder = pluspath (cp);
362 } else {
363 if (msg)
364 die("only one message at a time!");
365 msg = cp;
366 }
367 }
368
369 if (ccto == -1)
370 ccto = groupreply;
371 if (cccc == -1)
372 cccc = groupreply;
373 if (ccme == -1)
374 ccme = groupreply;
375
376 cwd = mh_xstrdup(pwd ());
377
378 if (!context_find ("path"))
379 free (path ("./", TFOLDER));
380 if (file && (msg || folder))
381 die("can't mix files and folders/msgs");
382
383 try_it_again:
384
385 strncpy (drft, buildsw ? m_maildir ("reply")
386 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
387
388 /* Check if a draft exists */
389 if (!buildsw && stat (drft, &st) != NOTOK) {
390 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
391 for (i = LISTDSW; i != YESW;) {
392 if (!(argp = read_switch_multiword ("\nDisposition? ",
393 isdf ? aqrnl : aqrl)))
394 done (1);
395 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
396 case NOSW:
397 done (0);
398 case NEWSW:
399 dmsg = NULL;
400 goto try_it_again;
401 case YESW:
402 break;
403 case LISTDSW:
404 showfile (++argp, drft);
405 break;
406 case REFILSW:
407 if (refile (++argp, drft) == 0)
408 i = YESW;
409 break;
410 default:
411 inform("say what?");
412 break;
413 }
414 }
415 }
416
417 if (file) {
418 /*
419 * We are replying to a file.
420 */
421 anot = false; /* we don't want to annotate a file */
422 } else {
423 /*
424 * We are replying to a message.
425 */
426 if (!msg)
427 msg = "cur";
428 if (!folder)
429 folder = getfolder (1);
430 maildir = m_maildir (folder);
431
432 if (chdir (maildir) == NOTOK)
433 adios (maildir, "unable to change directory to");
434
435 /* read folder and create message structure */
436 if (!(mp = folder_read (folder, 1)))
437 die("unable to read folder %s", folder);
438
439 /* check for empty folder */
440 if (mp->nummsg == 0)
441 die("no messages in %s", folder);
442
443 /* parse the message range/sequence/name and set SELECTED */
444 if (!m_convert (mp, msg))
445 done (1);
446 seq_setprev (mp); /* set the previous-sequence */
447
448 if (mp->numsel > 1)
449 die("only one message at a time!");
450
451 context_replace (pfolder, folder); /* update current folder */
452 seq_setcur (mp, mp->lowsel); /* update current message */
453 seq_save (mp); /* synchronize sequences */
454 context_save (); /* save the context file */
455 }
456
457 msg = file ? file : mh_xstrdup(m_name (mp->lowsel));
458
459 if ((in = fopen (msg, "r")) == NULL)
460 adios (msg, "unable to open");
461
462 /* find form (components) file */
463 if (!form) {
464 if (groupreply)
465 form = etcpath (replgroupcomps);
466 else
467 form = etcpath (replcomps);
468 }
469
470 replout (in, msg, drft, mp, outputlinelen, mime, form, filter,
471 fcc, fmtproc);
472 fclose (in);
473
474 {
475 char *filename = file ? file : concat (mp->foldpath, "/", msg, NULL);
476
477 for (n = 0; n < svector_size (convert_types); ++n) {
478 add_convert_header (svector_at (convert_types, n),
479 svector_at (convert_args, n),
480 filename, drft);
481 }
482 if (! file) {
483 free (filename);
484 }
485 }
486
487 if (nwhat)
488 done (0);
489 what_now (ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
490 inplace, cwd, atfile);
491
492 svector_free (convert_args);
493 svector_free (convert_types);
494
495 done (1);
496 return 1;
497 }
498
499 static void
500 docc (char *cp, int ccflag)
501 {
502 switch (smatch (cp, ccswitches)) {
503 case AMBIGSW:
504 ambigsw (cp, ccswitches);
505 done (1);
506 case UNKWNSW:
507 die("-%scc %s unknown", ccflag ? "" : "no", cp);
508
509 case CTOSW:
510 ccto = ccflag;
511 break;
512
513 case CCCSW:
514 cccc = ccflag;
515 break;
516
517 case CMESW:
518 ccme = ccflag;
519 break;
520
521 case CALSW:
522 ccto = cccc = ccme = ccflag;
523 break;
524 }
525 }
526
527 /*
528 * Add pseudoheaders that will pass the convert arguments to
529 * mhbuild. They have the form:
530 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
531 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
532 * The ARGS pseudoheader is optional, but we always add it when
533 * -convertargs is used.
534 */
535 static void
536 add_convert_header (const char *convert_type, char *convert_arg,
537 char *filename, char *drft)
538 {
539 char *field_name;
540
541 field_name = concat (MHBUILD_FILE_PSEUDOHEADER, convert_type, NULL);
542 annotate (drft, field_name, filename, 1, 0, -2, 1);
543 free (field_name);
544
545 field_name = concat (MHBUILD_ARGS_PSEUDOHEADER, convert_type, NULL);
546 annotate (drft, field_name, convert_arg, 1, 0, -2, 1);
547 free (field_name);
548 }