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