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