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