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