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