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