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