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