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/refile.h"
10 #include "sbr/m_draft.h"
11 #include "sbr/m_convert.h"
12 #include "sbr/getfolder.h"
13 #include "sbr/folder_read.h"
14 #include "sbr/context_save.h"
15 #include "sbr/context_replace.h"
16 #include "sbr/context_find.h"
17 #include "sbr/ambigsw.h"
19 #include "sbr/print_version.h"
20 #include "sbr/print_help.h"
21 #include "sbr/error.h"
25 #include "sbr/m_maildir.h"
28 #define REPL_SWITCHES \
29 X("group", 0, GROUPSW) \
30 X("nogroup", 0, NGROUPSW) \
31 X("annotate", 0, ANNOSW) \
32 X("noannotate", 0, NANNOSW) \
33 X("cc all|to|cc|me", 0, CCSW) \
34 X("nocc all|to|cc|me", 0, NCCSW) \
35 X("draftfolder +folder", 0, DFOLDSW) \
36 X("draftmessage msg", 0, DMSGSW) \
37 X("nodraftfolder", 0, NDFLDSW) \
38 X("editor editor", 0, EDITRSW) \
39 X("noedit", 0, NEDITSW) \
40 X("convertargs type argstring", 0, CONVERTARGSW) \
41 X("fcc folder", 0, FCCSW) \
42 X("filter filterfile", 0, FILTSW) \
43 X("form formfile", 0, FORMSW) \
44 X("format", 5, FRMTSW) \
45 X("noformat", 7, NFRMTSW) \
46 X("inplace", 0, INPLSW) \
47 X("noinplace", 0, NINPLSW) \
48 X("mime", 0, MIMESW) \
49 X("nomime", 0, NMIMESW) \
50 X("query", 0, QURYSW) \
51 X("noquery", 0, NQURYSW) \
52 X("whatnowproc program", 0, WHATSW) \
53 X("nowhatnowproc", 0, NWHATSW) \
54 X("width columns", 0, WIDTHSW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
57 X("file file", 4, FILESW) \
58 X("build", 5, BILDSW) /* interface from mhe */ \
59 X("atfile", 0, ATFILESW) \
60 X("noatfile", 0, NOATFILESW) \
61 X("fmtproc program", 0, FMTPROCSW) \
62 X("nofmtproc", 0, NFMTPROCSW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(REPL
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(REPL
, switches
);
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(CC
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
86 #define DISPO_SWITCHES \
88 X("replace", 0, YESW) \
89 X("list", 0, LISTDSW) \
90 X("refile +folder", 0, REFILSW) \
93 #define X(sw, minchars, id) id,
94 DEFINE_SWITCH_ENUM(DISPO
);
97 #define X(sw, minchars, id) { sw, minchars, id },
98 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
101 static struct swit aqrl
[] = {
103 { "replace", 0, YESW
},
104 { "list", 0, LISTDSW
},
105 { "refile +folder", 0, REFILSW
},
109 static short outputlinelen
= OUTPUTLINELEN
;
110 static bool groupreply
; /* Is this a group reply? */
112 static bool mime
; /* include original as MIME part */
113 static char *form
= NULL
; /* form (components) file */
114 static char *filter
= NULL
; /* message filter file */
115 static char *fcc
= NULL
; /* folders to add to Fcc: header */
121 static void docc (char *, int);
122 static void add_convert_header (const char *, char *, char *, char *);
126 main (int argc
, char **argv
)
135 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
136 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
137 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
138 char **argp
, **arguments
;
139 svector_t convert_types
= svector_create (10);
140 svector_t convert_args
= svector_create (10);
142 struct msgs
*mp
= NULL
;
145 bool buildsw
= false;
147 if (nmh_init(argv
[0], true, true)) { return 1; }
149 arguments
= getarguments (invo_name
, argc
, argv
, 1);
152 while ((cp
= *argp
++)) {
154 switch (smatch (++cp
, switches
)) {
156 ambigsw (cp
, switches
);
159 die("-%s unknown", cp
);
162 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
164 print_help (buf
, switches
, 1);
167 print_version(invo_name
);
185 if (!(cp
= *argp
++) || *cp
== '-')
186 die("missing argument to %s", argp
[-2]);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
196 if (!(ed
= *argp
++) || *ed
== '-')
197 die("missing argument to %s", argp
[-2]);
208 if (!(type
= *argp
++)) {
209 die("missing type argument to %s", argp
[-2]);
211 if (!(cp
= *argp
++)) {
212 die("missing argstring argument to %s",
216 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
217 if (! strcmp (svector_at (convert_types
, i
), type
)) {
218 /* Already saw this type, so just update
220 svector_strs (convert_args
)[i
] = cp
;
225 if (i
== svector_size (convert_types
)) {
226 svector_push_back (convert_types
, type
);
227 svector_push_back (convert_args
, cp
);
233 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
234 die("missing argument to %s", argp
[-2]);
245 if (!(cp
= *argp
++) || *cp
== '-')
246 die("missing argument to %s", argp
[-2]);
249 cp
= dp
= path (cp
+ 1, TSUBCWF
);
251 fcc
= add (", ", fcc
);
258 die("only one file at a time!");
259 if (!(cp
= *argp
++) || *cp
== '-')
260 die("missing argument to %s", argp
[-2]);
261 file
= path (cp
, TFILE
);
264 if (!(cp
= *argp
++) || *cp
== '-')
265 die("missing argument to %s", argp
[-2]);
266 filter
= mh_xstrdup(etcpath(cp
));
270 if (!(form
= *argp
++) || *form
== '-')
271 die("missing argument to %s", argp
[-2]);
275 filter
= mh_xstrdup(etcpath(mhlreply
));
305 if (!(cp
= *argp
++) || *cp
== '-')
306 die("missing argument to %s", argp
[-2]);
307 if ((outputlinelen
= atoi (cp
)) < 10)
308 die("impossible width %d", outputlinelen
);
313 die("only one draft folder at a time!");
314 if (!(cp
= *argp
++) || *cp
== '-')
315 die("missing argument to %s", argp
[-2]);
316 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
317 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
321 die("only one draft message at a time!");
322 if (!(dmsg
= *argp
++) || *dmsg
== '-')
323 die("missing argument to %s", argp
[-2]);
338 if (!(formatproc
= *argp
++) || *formatproc
== '-')
339 die("missing argument to %s", argp
[-2]);
347 if (*cp
== '+' || *cp
== '@') {
349 die("only one folder at a time!");
350 folder
= pluspath (cp
);
353 die("only one message at a time!");
365 cwd
= mh_xstrdup(pwd ());
367 if (!context_find ("path"))
368 free (path ("./", TFOLDER
));
369 if (file
&& (msg
|| folder
))
370 die("can't mix files and folders/msgs");
374 strncpy (drft
, buildsw
? m_maildir ("reply")
375 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
377 /* Check if a draft exists */
378 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
379 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
380 for (i
= LISTDSW
; i
!= YESW
;) {
381 if (!(argp
= read_switch_multiword ("\nDisposition? ",
382 isdf
? aqrnl
: aqrl
)))
384 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
393 showfile (++argp
, drft
);
396 if (refile (++argp
, drft
) == 0)
408 * We are replying to a file.
410 anot
= false; /* we don't want to annotate a file */
413 * We are replying to a message.
418 folder
= getfolder (1);
419 maildir
= m_maildir (folder
);
421 if (chdir (maildir
) == NOTOK
)
422 adios (maildir
, "unable to change directory to");
424 /* read folder and create message structure */
425 if (!(mp
= folder_read (folder
, 1)))
426 die("unable to read folder %s", folder
);
428 /* check for empty folder */
430 die("no messages in %s", folder
);
432 /* parse the message range/sequence/name and set SELECTED */
433 if (!m_convert (mp
, msg
))
435 seq_setprev (mp
); /* set the previous-sequence */
438 die("only one message at a time!");
440 context_replace (pfolder
, folder
); /* update current folder */
441 seq_setcur (mp
, mp
->lowsel
); /* update current message */
442 seq_save (mp
); /* synchronize sequences */
443 context_save (); /* save the context file */
446 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
448 if ((in
= fopen (msg
, "r")) == NULL
)
449 adios (msg
, "unable to open");
451 /* find form (components) file */
454 form
= etcpath (replgroupcomps
);
456 form
= etcpath (replcomps
);
459 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
464 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
466 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
467 add_convert_header (svector_at (convert_types
, n
),
468 svector_at (convert_args
, n
),
478 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
479 inplace
, cwd
, atfile
);
481 svector_free (convert_args
);
482 svector_free (convert_types
);
489 docc (char *cp
, int ccflag
)
491 switch (smatch (cp
, ccswitches
)) {
493 ambigsw (cp
, ccswitches
);
496 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
511 ccto
= cccc
= ccme
= ccflag
;
517 * Add pseudoheaders that will pass the convert arguments to
518 * mhbuild. They have the form:
519 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
520 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
521 * The ARGS pseudoheader is optional, but we always add it when
522 * -convertargs is used.
525 add_convert_header (const char *convert_type
, char *convert_arg
,
526 char *filename
, char *drft
)
530 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
531 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
534 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
535 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);