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