1 /* repl.c -- reply to a message
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.
9 #include "sbr/m_name.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/read_switch_multiword.h"
12 #include "sbr/concat.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_setcur.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/showfile.h"
17 #include "sbr/smatch.h"
18 #include "sbr/refile.h"
19 #include "sbr/m_draft.h"
20 #include "sbr/m_convert.h"
21 #include "sbr/getfolder.h"
22 #include "sbr/folder_read.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_find.h"
26 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/error.h"
34 #include "sbr/m_maildir.h"
37 #define REPL_SWITCHES \
38 X("group", 0, GROUPSW) \
39 X("nogroup", 0, NGROUPSW) \
40 X("annotate", 0, ANNOSW) \
41 X("noannotate", 0, NANNOSW) \
42 X("cc all|to|cc|me", 0, CCSW) \
43 X("nocc all|to|cc|me", 0, NCCSW) \
44 X("draftfolder +folder", 0, DFOLDSW) \
45 X("draftmessage msg", 0, DMSGSW) \
46 X("nodraftfolder", 0, NDFLDSW) \
47 X("editor editor", 0, EDITRSW) \
48 X("noedit", 0, NEDITSW) \
49 X("convertargs type argstring", 0, CONVERTARGSW) \
50 X("fcc folder", 0, FCCSW) \
51 X("filter filterfile", 0, FILTSW) \
52 X("form formfile", 0, FORMSW) \
53 X("format", 5, FRMTSW) \
54 X("noformat", 7, NFRMTSW) \
55 X("inplace", 0, INPLSW) \
56 X("noinplace", 0, NINPLSW) \
57 X("mime", 0, MIMESW) \
58 X("nomime", 0, NMIMESW) \
59 X("query", 0, QURYSW) \
60 X("noquery", 0, NQURYSW) \
61 X("whatnowproc program", 0, WHATSW) \
62 X("nowhatnowproc", 0, NWHATSW) \
63 X("width columns", 0, WIDTHSW) \
64 X("version", 0, VERSIONSW) \
65 X("help", 0, HELPSW) \
66 X("file file", 4, FILESW) \
67 X("build", 5, BILDSW) /* interface from mhe */ \
68 X("atfile", 0, ATFILESW) \
69 X("noatfile", 0, NOATFILESW) \
70 X("fmtproc program", 0, FMTPROCSW) \
71 X("nofmtproc", 0, NFMTPROCSW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(REPL
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(REPL
, switches
);
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(CC
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
95 #define DISPO_SWITCHES \
97 X("replace", 0, YESW) \
98 X("list", 0, LISTDSW) \
99 X("refile +folder", 0, REFILSW) \
102 #define X(sw, minchars, id) id,
103 DEFINE_SWITCH_ENUM(DISPO
);
106 #define X(sw, minchars, id) { sw, minchars, id },
107 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
110 static struct swit aqrl
[] = {
112 { "replace", 0, YESW
},
113 { "list", 0, LISTDSW
},
114 { "refile +folder", 0, REFILSW
},
118 static short outputlinelen
= OUTPUTLINELEN
;
119 static bool groupreply
; /* Is this a group reply? */
121 static bool mime
; /* include original as MIME part */
122 static char *form
= NULL
; /* form (components) file */
123 static char *filter
= NULL
; /* message filter file */
124 static char *fcc
= NULL
; /* folders to add to Fcc: header */
130 static void docc (char *, int);
131 static void add_convert_header (const char *, char *, char *, char *);
135 main (int argc
, char **argv
)
144 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
145 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
146 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
147 char **argp
, **arguments
;
148 svector_t convert_types
= svector_create (10);
149 svector_t convert_args
= svector_create (10);
151 struct msgs
*mp
= NULL
;
154 bool buildsw
= false;
156 if (nmh_init(argv
[0], true, true)) { return 1; }
158 arguments
= getarguments (invo_name
, argc
, argv
, 1);
161 while ((cp
= *argp
++)) {
163 switch (smatch (++cp
, switches
)) {
165 ambigsw (cp
, switches
);
168 die("-%s unknown", cp
);
171 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
173 print_help (buf
, switches
, 1);
176 print_version(invo_name
);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 die("missing argument to %s", argp
[-2]);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 die("missing argument to %s", argp
[-2]);
205 if (!(ed
= *argp
++) || *ed
== '-')
206 die("missing argument to %s", argp
[-2]);
217 if (!(type
= *argp
++)) {
218 die("missing type argument to %s", argp
[-2]);
220 if (!(cp
= *argp
++)) {
221 die("missing argstring argument to %s",
225 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
226 if (! strcmp (svector_at (convert_types
, i
), type
)) {
227 /* Already saw this type, so just update
229 svector_strs (convert_args
)[i
] = cp
;
234 if (i
== svector_size (convert_types
)) {
235 svector_push_back (convert_types
, type
);
236 svector_push_back (convert_args
, cp
);
242 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
243 die("missing argument to %s", argp
[-2]);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
258 cp
= dp
= path (cp
+ 1, TSUBCWF
);
260 fcc
= add (", ", fcc
);
267 die("only one file at a time!");
268 if (!(cp
= *argp
++) || *cp
== '-')
269 die("missing argument to %s", argp
[-2]);
270 file
= path (cp
, TFILE
);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
275 filter
= mh_xstrdup(etcpath(cp
));
279 if (!(form
= *argp
++) || *form
== '-')
280 die("missing argument to %s", argp
[-2]);
284 filter
= mh_xstrdup(etcpath(mhlreply
));
314 if (!(cp
= *argp
++) || *cp
== '-')
315 die("missing argument to %s", argp
[-2]);
316 if ((outputlinelen
= atoi (cp
)) < 10)
317 die("impossible width %d", outputlinelen
);
322 die("only one draft folder at a time!");
323 if (!(cp
= *argp
++) || *cp
== '-')
324 die("missing argument to %s", argp
[-2]);
325 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
326 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
330 die("only one draft message at a time!");
331 if (!(dmsg
= *argp
++) || *dmsg
== '-')
332 die("missing argument to %s", argp
[-2]);
347 if (!(formatproc
= *argp
++) || *formatproc
== '-')
348 die("missing argument to %s", argp
[-2]);
356 if (*cp
== '+' || *cp
== '@') {
358 die("only one folder at a time!");
359 folder
= pluspath (cp
);
362 die("only one message at a time!");
374 cwd
= mh_xstrdup(pwd ());
376 if (!context_find ("path"))
377 free (path ("./", TFOLDER
));
378 if (file
&& (msg
|| folder
))
379 die("can't mix files and folders/msgs");
383 strncpy (drft
, buildsw
? m_maildir ("reply")
384 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
386 /* Check if a draft exists */
387 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
388 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
389 for (i
= LISTDSW
; i
!= YESW
;) {
390 if (!(argp
= read_switch_multiword ("\nDisposition? ",
391 isdf
? aqrnl
: aqrl
)))
393 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
402 showfile (++argp
, drft
);
405 if (refile (++argp
, drft
) == 0)
417 * We are replying to a file.
419 anot
= false; /* we don't want to annotate a file */
422 * We are replying to a message.
427 folder
= getfolder (1);
428 maildir
= m_maildir (folder
);
430 if (chdir (maildir
) == NOTOK
)
431 adios (maildir
, "unable to change directory to");
433 /* read folder and create message structure */
434 if (!(mp
= folder_read (folder
, 1)))
435 die("unable to read folder %s", folder
);
437 /* check for empty folder */
439 die("no messages in %s", folder
);
441 /* parse the message range/sequence/name and set SELECTED */
442 if (!m_convert (mp
, msg
))
444 seq_setprev (mp
); /* set the previous-sequence */
447 die("only one message at a time!");
449 context_replace (pfolder
, folder
); /* update current folder */
450 seq_setcur (mp
, mp
->lowsel
); /* update current message */
451 seq_save (mp
); /* synchronize sequences */
452 context_save (); /* save the context file */
455 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
457 if ((in
= fopen (msg
, "r")) == NULL
)
458 adios (msg
, "unable to open");
460 /* find form (components) file */
463 form
= etcpath (replgroupcomps
);
465 form
= etcpath (replcomps
);
468 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
473 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
475 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
476 add_convert_header (svector_at (convert_types
, n
),
477 svector_at (convert_args
, n
),
487 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
488 inplace
, cwd
, atfile
);
490 svector_free (convert_args
);
491 svector_free (convert_types
);
498 docc (char *cp
, int ccflag
)
500 switch (smatch (cp
, ccswitches
)) {
502 ambigsw (cp
, ccswitches
);
505 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
520 ccto
= cccc
= ccme
= ccflag
;
526 * Add pseudoheaders that will pass the convert arguments to
527 * mhbuild. They have the form:
528 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
529 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
530 * The ARGS pseudoheader is optional, but we always add it when
531 * -convertargs is used.
534 add_convert_header (const char *convert_type
, char *convert_arg
,
535 char *filename
, char *drft
)
539 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
540 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
543 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
544 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);