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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
16 #include "sbr/m_maildir.h"
19 #define REPL_SWITCHES \
20 X("group", 0, GROUPSW) \
21 X("nogroup", 0, NGROUPSW) \
22 X("annotate", 0, ANNOSW) \
23 X("noannotate", 0, NANNOSW) \
24 X("cc all|to|cc|me", 0, CCSW) \
25 X("nocc all|to|cc|me", 0, NCCSW) \
26 X("draftfolder +folder", 0, DFOLDSW) \
27 X("draftmessage msg", 0, DMSGSW) \
28 X("nodraftfolder", 0, NDFLDSW) \
29 X("editor editor", 0, EDITRSW) \
30 X("noedit", 0, NEDITSW) \
31 X("convertargs type argstring", 0, CONVERTARGSW) \
32 X("fcc folder", 0, FCCSW) \
33 X("filter filterfile", 0, FILTSW) \
34 X("form formfile", 0, FORMSW) \
35 X("format", 5, FRMTSW) \
36 X("noformat", 7, NFRMTSW) \
37 X("inplace", 0, INPLSW) \
38 X("noinplace", 0, NINPLSW) \
39 X("mime", 0, MIMESW) \
40 X("nomime", 0, NMIMESW) \
41 X("query", 0, QURYSW) \
42 X("noquery", 0, NQURYSW) \
43 X("whatnowproc program", 0, WHATSW) \
44 X("nowhatnowproc", 0, NWHATSW) \
45 X("width columns", 0, WIDTHSW) \
46 X("version", 0, VERSIONSW) \
47 X("help", 0, HELPSW) \
48 X("file file", 4, FILESW) \
49 X("build", 5, BILDSW) /* interface from mhe */ \
50 X("atfile", 0, ATFILESW) \
51 X("noatfile", 0, NOATFILESW) \
52 X("fmtproc program", 0, FMTPROCSW) \
53 X("nofmtproc", 0, NFMTPROCSW) \
55 #define X(sw, minchars, id) id,
56 DEFINE_SWITCH_ENUM(REPL
);
59 #define X(sw, minchars, id) { sw, minchars, id },
60 DEFINE_SWITCH_ARRAY(REPL
, switches
);
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(CC
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
77 #define DISPO_SWITCHES \
79 X("replace", 0, YESW) \
80 X("list", 0, LISTDSW) \
81 X("refile +folder", 0, REFILSW) \
84 #define X(sw, minchars, id) id,
85 DEFINE_SWITCH_ENUM(DISPO
);
88 #define X(sw, minchars, id) { sw, minchars, id },
89 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
92 static struct swit aqrl
[] = {
94 { "replace", 0, YESW
},
95 { "list", 0, LISTDSW
},
96 { "refile +folder", 0, REFILSW
},
100 static short outputlinelen
= OUTPUTLINELEN
;
101 static bool groupreply
; /* Is this a group reply? */
103 static bool mime
; /* 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 */
112 static void docc (char *, int);
113 static void add_convert_header (const char *, char *, char *, char *);
117 main (int argc
, char **argv
)
126 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
127 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
128 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
129 char **argp
, **arguments
;
130 svector_t convert_types
= svector_create (10);
131 svector_t convert_args
= svector_create (10);
133 struct msgs
*mp
= NULL
;
136 bool buildsw
= false;
138 if (nmh_init(argv
[0], true, true)) { return 1; }
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 while ((cp
= *argp
++)) {
145 switch (smatch (++cp
, switches
)) {
147 ambigsw (cp
, switches
);
150 die("-%s unknown", cp
);
153 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
155 print_help (buf
, switches
, 1);
158 print_version(invo_name
);
176 if (!(cp
= *argp
++) || *cp
== '-')
177 die("missing argument to %s", argp
[-2]);
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
187 if (!(ed
= *argp
++) || *ed
== '-')
188 die("missing argument to %s", argp
[-2]);
199 if (!(type
= *argp
++)) {
200 die("missing type argument to %s", argp
[-2]);
202 if (!(cp
= *argp
++)) {
203 die("missing argstring argument to %s",
207 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
208 if (! strcmp (svector_at (convert_types
, i
), type
)) {
209 /* Already saw this type, so just update
211 svector_strs (convert_args
)[i
] = cp
;
216 if (i
== svector_size (convert_types
)) {
217 svector_push_back (convert_types
, type
);
218 svector_push_back (convert_args
, cp
);
224 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
225 die("missing argument to %s", argp
[-2]);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 die("missing argument to %s", argp
[-2]);
240 cp
= dp
= path (cp
+ 1, TSUBCWF
);
242 fcc
= add (", ", fcc
);
249 die("only one file at a time!");
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
252 file
= path (cp
, TFILE
);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
257 filter
= mh_xstrdup(etcpath(cp
));
261 if (!(form
= *argp
++) || *form
== '-')
262 die("missing argument to %s", argp
[-2]);
266 filter
= mh_xstrdup(etcpath(mhlreply
));
296 if (!(cp
= *argp
++) || *cp
== '-')
297 die("missing argument to %s", argp
[-2]);
298 if ((outputlinelen
= atoi (cp
)) < 10)
299 die("impossible width %d", outputlinelen
);
304 die("only one draft folder at a time!");
305 if (!(cp
= *argp
++) || *cp
== '-')
306 die("missing argument to %s", argp
[-2]);
307 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
308 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
312 die("only one draft message at a time!");
313 if (!(dmsg
= *argp
++) || *dmsg
== '-')
314 die("missing argument to %s", argp
[-2]);
329 if (!(formatproc
= *argp
++) || *formatproc
== '-')
330 die("missing argument to %s", argp
[-2]);
338 if (*cp
== '+' || *cp
== '@') {
340 die("only one folder at a time!");
341 folder
= pluspath (cp
);
344 die("only one message at a time!");
356 cwd
= mh_xstrdup(pwd ());
358 if (!context_find ("path"))
359 free (path ("./", TFOLDER
));
360 if (file
&& (msg
|| folder
))
361 die("can't mix files and folders/msgs");
365 strncpy (drft
, buildsw
? m_maildir ("reply")
366 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
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
)))
375 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
384 showfile (++argp
, drft
);
387 if (refile (++argp
, drft
) == 0)
399 * We are replying to a file.
401 anot
= false; /* we don't want to annotate a file */
404 * We are replying to a message.
409 folder
= getfolder (1);
410 maildir
= m_maildir (folder
);
412 if (chdir (maildir
) == NOTOK
)
413 adios (maildir
, "unable to change directory to");
415 /* read folder and create message structure */
416 if (!(mp
= folder_read (folder
, 1)))
417 die("unable to read folder %s", folder
);
419 /* check for empty folder */
421 die("no messages in %s", folder
);
423 /* parse the message range/sequence/name and set SELECTED */
424 if (!m_convert (mp
, msg
))
426 seq_setprev (mp
); /* set the previous-sequence */
429 die("only one message at a time!");
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 */
437 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
439 if ((in
= fopen (msg
, "r")) == NULL
)
440 adios (msg
, "unable to open");
442 /* find form (components) file */
445 form
= etcpath (replgroupcomps
);
447 form
= etcpath (replcomps
);
450 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
455 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
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
),
469 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
470 inplace
, cwd
, atfile
);
472 svector_free (convert_args
);
473 svector_free (convert_types
);
480 docc (char *cp
, int ccflag
)
482 switch (smatch (cp
, ccswitches
)) {
484 ambigsw (cp
, ccswitches
);
487 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
502 ccto
= cccc
= ccme
= ccflag
;
508 * Add pseudoheaders that will pass the convert arguments to
509 * mhbuild. They have the form:
510 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
511 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
512 * The ARGS pseudoheader is optional, but we always add it when
513 * -convertargs is used.
516 add_convert_header (const char *convert_type
, char *convert_arg
,
517 char *filename
, char *drft
)
521 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
522 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
525 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
526 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);