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/concat.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/showfile.h"
14 #include "sbr/smatch.h"
15 #include "sbr/refile.h"
16 #include "sbr/m_draft.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/getfolder.h"
19 #include "sbr/folder_read.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/error.h"
31 #include "sbr/m_maildir.h"
34 #define REPL_SWITCHES \
35 X("group", 0, GROUPSW) \
36 X("nogroup", 0, NGROUPSW) \
37 X("annotate", 0, ANNOSW) \
38 X("noannotate", 0, NANNOSW) \
39 X("cc all|to|cc|me", 0, CCSW) \
40 X("nocc all|to|cc|me", 0, NCCSW) \
41 X("draftfolder +folder", 0, DFOLDSW) \
42 X("draftmessage msg", 0, DMSGSW) \
43 X("nodraftfolder", 0, NDFLDSW) \
44 X("editor editor", 0, EDITRSW) \
45 X("noedit", 0, NEDITSW) \
46 X("convertargs type argstring", 0, CONVERTARGSW) \
47 X("fcc folder", 0, FCCSW) \
48 X("filter filterfile", 0, FILTSW) \
49 X("form formfile", 0, FORMSW) \
50 X("format", 5, FRMTSW) \
51 X("noformat", 7, NFRMTSW) \
52 X("inplace", 0, INPLSW) \
53 X("noinplace", 0, NINPLSW) \
54 X("mime", 0, MIMESW) \
55 X("nomime", 0, NMIMESW) \
56 X("query", 0, QURYSW) \
57 X("noquery", 0, NQURYSW) \
58 X("whatnowproc program", 0, WHATSW) \
59 X("nowhatnowproc", 0, NWHATSW) \
60 X("width columns", 0, WIDTHSW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("file file", 4, FILESW) \
64 X("build", 5, BILDSW) /* interface from mhe */ \
65 X("atfile", 0, ATFILESW) \
66 X("noatfile", 0, NOATFILESW) \
67 X("fmtproc program", 0, FMTPROCSW) \
68 X("nofmtproc", 0, NFMTPROCSW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(REPL
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(REPL
, switches
);
84 #define X(sw, minchars, id) id,
85 DEFINE_SWITCH_ENUM(CC
);
88 #define X(sw, minchars, id) { sw, minchars, id },
89 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
92 #define DISPO_SWITCHES \
94 X("replace", 0, YESW) \
95 X("list", 0, LISTDSW) \
96 X("refile +folder", 0, REFILSW) \
99 #define X(sw, minchars, id) id,
100 DEFINE_SWITCH_ENUM(DISPO
);
103 #define X(sw, minchars, id) { sw, minchars, id },
104 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
107 static struct swit aqrl
[] = {
109 { "replace", 0, YESW
},
110 { "list", 0, LISTDSW
},
111 { "refile +folder", 0, REFILSW
},
115 static short outputlinelen
= OUTPUTLINELEN
;
116 static bool groupreply
; /* Is this a group reply? */
118 static bool mime
; /* include original as MIME part */
119 static char *form
= NULL
; /* form (components) file */
120 static char *filter
= NULL
; /* message filter file */
121 static char *fcc
= NULL
; /* folders to add to Fcc: header */
127 static void docc (char *, int);
128 static void add_convert_header (const char *, char *, char *, char *);
132 main (int argc
, char **argv
)
141 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
142 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
143 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
144 char **argp
, **arguments
;
145 svector_t convert_types
= svector_create (10);
146 svector_t convert_args
= svector_create (10);
148 struct msgs
*mp
= NULL
;
151 bool buildsw
= false;
153 if (nmh_init(argv
[0], true, true)) { return 1; }
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
158 while ((cp
= *argp
++)) {
160 switch (smatch (++cp
, switches
)) {
162 ambigsw (cp
, switches
);
165 die("-%s unknown", cp
);
168 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
170 print_help (buf
, switches
, 1);
173 print_version(invo_name
);
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
202 if (!(ed
= *argp
++) || *ed
== '-')
203 die("missing argument to %s", argp
[-2]);
214 if (!(type
= *argp
++)) {
215 die("missing type argument to %s", argp
[-2]);
217 if (!(cp
= *argp
++)) {
218 die("missing argstring argument to %s",
222 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
223 if (! strcmp (svector_at (convert_types
, i
), type
)) {
224 /* Already saw this type, so just update
226 svector_strs (convert_args
)[i
] = cp
;
231 if (i
== svector_size (convert_types
)) {
232 svector_push_back (convert_types
, type
);
233 svector_push_back (convert_args
, cp
);
239 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
240 die("missing argument to %s", argp
[-2]);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
255 cp
= dp
= path (cp
+ 1, TSUBCWF
);
257 fcc
= add (", ", fcc
);
264 die("only one file at a time!");
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
267 file
= path (cp
, TFILE
);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
272 filter
= mh_xstrdup(etcpath(cp
));
276 if (!(form
= *argp
++) || *form
== '-')
277 die("missing argument to %s", argp
[-2]);
281 filter
= mh_xstrdup(etcpath(mhlreply
));
311 if (!(cp
= *argp
++) || *cp
== '-')
312 die("missing argument to %s", argp
[-2]);
313 if ((outputlinelen
= atoi (cp
)) < 10)
314 die("impossible width %d", outputlinelen
);
319 die("only one draft folder at a time!");
320 if (!(cp
= *argp
++) || *cp
== '-')
321 die("missing argument to %s", argp
[-2]);
322 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
323 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
327 die("only one draft message at a time!");
328 if (!(dmsg
= *argp
++) || *dmsg
== '-')
329 die("missing argument to %s", argp
[-2]);
344 if (!(formatproc
= *argp
++) || *formatproc
== '-')
345 die("missing argument to %s", argp
[-2]);
353 if (*cp
== '+' || *cp
== '@') {
355 die("only one folder at a time!");
356 folder
= pluspath (cp
);
359 die("only one message at a time!");
371 cwd
= mh_xstrdup(pwd ());
373 if (!context_find ("path"))
374 free (path ("./", TFOLDER
));
375 if (file
&& (msg
|| folder
))
376 die("can't mix files and folders/msgs");
380 strncpy (drft
, buildsw
? m_maildir ("reply")
381 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
383 /* Check if a draft exists */
384 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
385 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
386 for (i
= LISTDSW
; i
!= YESW
;) {
387 if (!(argp
= read_switch_multiword ("\nDisposition? ",
388 isdf
? aqrnl
: aqrl
)))
390 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
399 showfile (++argp
, drft
);
402 if (refile (++argp
, drft
) == 0)
414 * We are replying to a file.
416 anot
= false; /* we don't want to annotate a file */
419 * We are replying to a message.
424 folder
= getfolder (1);
425 maildir
= m_maildir (folder
);
427 if (chdir (maildir
) == NOTOK
)
428 adios (maildir
, "unable to change directory to");
430 /* read folder and create message structure */
431 if (!(mp
= folder_read (folder
, 1)))
432 die("unable to read folder %s", folder
);
434 /* check for empty folder */
436 die("no messages in %s", folder
);
438 /* parse the message range/sequence/name and set SELECTED */
439 if (!m_convert (mp
, msg
))
441 seq_setprev (mp
); /* set the previous-sequence */
444 die("only one message at a time!");
446 context_replace (pfolder
, folder
); /* update current folder */
447 seq_setcur (mp
, mp
->lowsel
); /* update current message */
448 seq_save (mp
); /* synchronize sequences */
449 context_save (); /* save the context file */
452 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
454 if ((in
= fopen (msg
, "r")) == NULL
)
455 adios (msg
, "unable to open");
457 /* find form (components) file */
460 form
= etcpath (replgroupcomps
);
462 form
= etcpath (replcomps
);
465 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
470 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
472 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
473 add_convert_header (svector_at (convert_types
, n
),
474 svector_at (convert_args
, n
),
484 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
485 inplace
, cwd
, atfile
);
487 svector_free (convert_args
);
488 svector_free (convert_types
);
495 docc (char *cp
, int ccflag
)
497 switch (smatch (cp
, ccswitches
)) {
499 ambigsw (cp
, ccswitches
);
502 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
517 ccto
= cccc
= ccme
= ccflag
;
523 * Add pseudoheaders that will pass the convert arguments to
524 * mhbuild. They have the form:
525 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
526 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
527 * The ARGS pseudoheader is optional, but we always add it when
528 * -convertargs is used.
531 add_convert_header (const char *convert_type
, char *convert_arg
,
532 char *filename
, char *drft
)
536 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
537 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
540 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
541 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);