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 "whatnowproc.h"
11 #include "sbr/m_name.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/read_switch_multiword.h"
14 #include "sbr/concat.h"
15 #include "sbr/seq_setprev.h"
16 #include "sbr/seq_setcur.h"
17 #include "sbr/seq_save.h"
18 #include "sbr/showfile.h"
19 #include "sbr/smatch.h"
20 #include "sbr/refile.h"
21 #include "sbr/m_draft.h"
22 #include "sbr/m_convert.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/ambigsw.h"
30 #include "sbr/print_version.h"
31 #include "sbr/print_help.h"
32 #include "sbr/error.h"
36 #include "sbr/m_maildir.h"
39 #define REPL_SWITCHES \
40 X("group", 0, GROUPSW) \
41 X("nogroup", 0, NGROUPSW) \
42 X("annotate", 0, ANNOSW) \
43 X("noannotate", 0, NANNOSW) \
44 X("cc all|to|cc|me", 0, CCSW) \
45 X("nocc all|to|cc|me", 0, NCCSW) \
46 X("draftfolder +folder", 0, DFOLDSW) \
47 X("draftmessage msg", 0, DMSGSW) \
48 X("nodraftfolder", 0, NDFLDSW) \
49 X("editor editor", 0, EDITRSW) \
50 X("noedit", 0, NEDITSW) \
51 X("convertargs type argstring", 0, CONVERTARGSW) \
52 X("fcc folder", 0, FCCSW) \
53 X("filter filterfile", 0, FILTSW) \
54 X("form formfile", 0, FORMSW) \
55 X("format", 5, FRMTSW) \
56 X("noformat", 7, NFRMTSW) \
57 X("inplace", 0, INPLSW) \
58 X("noinplace", 0, NINPLSW) \
59 X("mime", 0, MIMESW) \
60 X("nomime", 0, NMIMESW) \
61 X("query", 0, QURYSW) \
62 X("noquery", 0, NQURYSW) \
63 X("whatnowproc program", 0, WHATSW) \
64 X("nowhatnowproc", 0, NWHATSW) \
65 X("width columns", 0, WIDTHSW) \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
68 X("file file", 4, FILESW) \
69 X("build", 5, BILDSW) /* interface from mhe */ \
70 X("atfile", 0, ATFILESW) \
71 X("noatfile", 0, NOATFILESW) \
72 X("fmtproc program", 0, FMTPROCSW) \
73 X("nofmtproc", 0, NFMTPROCSW) \
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(REPL
);
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(REPL
, switches
);
89 #define X(sw, minchars, id) id,
90 DEFINE_SWITCH_ENUM(CC
);
93 #define X(sw, minchars, id) { sw, minchars, id },
94 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
97 #define DISPO_SWITCHES \
99 X("replace", 0, YESW) \
100 X("list", 0, LISTDSW) \
101 X("refile +folder", 0, REFILSW) \
104 #define X(sw, minchars, id) id,
105 DEFINE_SWITCH_ENUM(DISPO
);
108 #define X(sw, minchars, id) { sw, minchars, id },
109 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
112 static struct swit aqrl
[] = {
114 { "replace", 0, YESW
},
115 { "list", 0, LISTDSW
},
116 { "refile +folder", 0, REFILSW
},
120 static short outputlinelen
= OUTPUTLINELEN
;
121 static bool groupreply
; /* Is this a group reply? */
123 static bool mime
; /* include original as MIME part */
124 static char *form
= NULL
; /* form (components) file */
125 static char *filter
= NULL
; /* message filter file */
126 static char *fcc
= NULL
; /* folders to add to Fcc: header */
132 static void docc (char *, int);
133 static void add_convert_header (const char *, char *, char *, char *);
137 main (int argc
, char **argv
)
146 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
147 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
148 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
149 char **argp
, **arguments
;
150 svector_t convert_types
= svector_create (10);
151 svector_t convert_args
= svector_create (10);
153 struct msgs
*mp
= NULL
;
156 bool buildsw
= false;
158 if (nmh_init(argv
[0], true, true)) { return 1; }
160 arguments
= getarguments (invo_name
, argc
, argv
, 1);
163 while ((cp
= *argp
++)) {
165 switch (smatch (++cp
, switches
)) {
167 ambigsw (cp
, switches
);
170 die("-%s unknown", cp
);
173 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
175 print_help (buf
, switches
, 1);
178 print_version(invo_name
);
196 if (!(cp
= *argp
++) || *cp
== '-')
197 die("missing argument to %s", argp
[-2]);
201 if (!(cp
= *argp
++) || *cp
== '-')
202 die("missing argument to %s", argp
[-2]);
207 if (!(ed
= *argp
++) || *ed
== '-')
208 die("missing argument to %s", argp
[-2]);
219 if (!(type
= *argp
++)) {
220 die("missing type argument to %s", argp
[-2]);
222 if (!(cp
= *argp
++)) {
223 die("missing argstring argument to %s",
227 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
228 if (! strcmp (svector_at (convert_types
, i
), type
)) {
229 /* Already saw this type, so just update
231 svector_strs (convert_args
)[i
] = cp
;
236 if (i
== svector_size (convert_types
)) {
237 svector_push_back (convert_types
, type
);
238 svector_push_back (convert_args
, cp
);
244 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
245 die("missing argument to %s", argp
[-2]);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 die("missing argument to %s", argp
[-2]);
260 cp
= dp
= path (cp
+ 1, TSUBCWF
);
262 fcc
= add (", ", fcc
);
269 die("only one file at a time!");
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
272 file
= path (cp
, TFILE
);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 die("missing argument to %s", argp
[-2]);
277 filter
= mh_xstrdup(etcpath(cp
));
281 if (!(form
= *argp
++) || *form
== '-')
282 die("missing argument to %s", argp
[-2]);
286 filter
= mh_xstrdup(etcpath(mhlreply
));
316 if (!(cp
= *argp
++) || *cp
== '-')
317 die("missing argument to %s", argp
[-2]);
318 if ((outputlinelen
= atoi (cp
)) < 10)
319 die("impossible width %d", outputlinelen
);
324 die("only one draft folder at a time!");
325 if (!(cp
= *argp
++) || *cp
== '-')
326 die("missing argument to %s", argp
[-2]);
327 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
328 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
332 die("only one draft message at a time!");
333 if (!(dmsg
= *argp
++) || *dmsg
== '-')
334 die("missing argument to %s", argp
[-2]);
349 if (!(formatproc
= *argp
++) || *formatproc
== '-')
350 die("missing argument to %s", argp
[-2]);
358 if (*cp
== '+' || *cp
== '@') {
360 die("only one folder at a time!");
361 folder
= pluspath (cp
);
364 die("only one message at a time!");
376 cwd
= mh_xstrdup(pwd ());
378 if (!context_find ("path"))
379 free (path ("./", TFOLDER
));
380 if (file
&& (msg
|| folder
))
381 die("can't mix files and folders/msgs");
385 strncpy (drft
, buildsw
? m_maildir ("reply")
386 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
388 /* Check if a draft exists */
389 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
390 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
391 for (i
= LISTDSW
; i
!= YESW
;) {
392 if (!(argp
= read_switch_multiword ("\nDisposition? ",
393 isdf
? aqrnl
: aqrl
)))
395 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
404 showfile (++argp
, drft
);
407 if (refile (++argp
, drft
) == 0)
419 * We are replying to a file.
421 anot
= false; /* we don't want to annotate a file */
424 * We are replying to a message.
429 folder
= getfolder (1);
430 maildir
= m_maildir (folder
);
432 if (chdir (maildir
) == NOTOK
)
433 adios (maildir
, "unable to change directory to");
435 /* read folder and create message structure */
436 if (!(mp
= folder_read (folder
, 1)))
437 die("unable to read folder %s", folder
);
439 /* check for empty folder */
441 die("no messages in %s", folder
);
443 /* parse the message range/sequence/name and set SELECTED */
444 if (!m_convert (mp
, msg
))
446 seq_setprev (mp
); /* set the previous-sequence */
449 die("only one message at a time!");
451 context_replace (pfolder
, folder
); /* update current folder */
452 seq_setcur (mp
, mp
->lowsel
); /* update current message */
453 seq_save (mp
); /* synchronize sequences */
454 context_save (); /* save the context file */
457 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
459 if ((in
= fopen (msg
, "r")) == NULL
)
460 adios (msg
, "unable to open");
462 /* find form (components) file */
465 form
= etcpath (replgroupcomps
);
467 form
= etcpath (replcomps
);
470 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
475 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
477 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
478 add_convert_header (svector_at (convert_types
, n
),
479 svector_at (convert_args
, n
),
489 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
490 inplace
, cwd
, atfile
);
492 svector_free (convert_args
);
493 svector_free (convert_types
);
500 docc (char *cp
, int ccflag
)
502 switch (smatch (cp
, ccswitches
)) {
504 ambigsw (cp
, ccswitches
);
507 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
522 ccto
= cccc
= ccme
= ccflag
;
528 * Add pseudoheaders that will pass the convert arguments to
529 * mhbuild. They have the form:
530 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
531 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
532 * The ARGS pseudoheader is optional, but we always add it when
533 * -convertargs is used.
536 add_convert_header (const char *convert_type
, char *convert_arg
,
537 char *filename
, char *drft
)
541 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
542 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
545 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
546 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);