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/m_name.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/read_switch_multiword.h"
13 #include "sbr/concat.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/showfile.h"
18 #include "sbr/smatch.h"
19 #include "sbr/refile.h"
20 #include "sbr/m_draft.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/getfolder.h"
23 #include "sbr/folder_read.h"
24 #include "sbr/context_save.h"
25 #include "sbr/context_replace.h"
26 #include "sbr/context_find.h"
27 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/error.h"
35 #include "sbr/m_maildir.h"
38 #define REPL_SWITCHES \
39 X("group", 0, GROUPSW) \
40 X("nogroup", 0, NGROUPSW) \
41 X("annotate", 0, ANNOSW) \
42 X("noannotate", 0, NANNOSW) \
43 X("cc all|to|cc|me", 0, CCSW) \
44 X("nocc all|to|cc|me", 0, NCCSW) \
45 X("draftfolder +folder", 0, DFOLDSW) \
46 X("draftmessage msg", 0, DMSGSW) \
47 X("nodraftfolder", 0, NDFLDSW) \
48 X("editor editor", 0, EDITRSW) \
49 X("noedit", 0, NEDITSW) \
50 X("convertargs type argstring", 0, CONVERTARGSW) \
51 X("fcc folder", 0, FCCSW) \
52 X("filter filterfile", 0, FILTSW) \
53 X("form formfile", 0, FORMSW) \
54 X("format", 5, FRMTSW) \
55 X("noformat", 7, NFRMTSW) \
56 X("inplace", 0, INPLSW) \
57 X("noinplace", 0, NINPLSW) \
58 X("mime", 0, MIMESW) \
59 X("nomime", 0, NMIMESW) \
60 X("query", 0, QURYSW) \
61 X("noquery", 0, NQURYSW) \
62 X("whatnowproc program", 0, WHATSW) \
63 X("nowhatnowproc", 0, NWHATSW) \
64 X("width columns", 0, WIDTHSW) \
65 X("version", 0, VERSIONSW) \
66 X("help", 0, HELPSW) \
67 X("file file", 4, FILESW) \
68 X("build", 5, BILDSW) /* interface from mhe */ \
69 X("atfile", 0, ATFILESW) \
70 X("noatfile", 0, NOATFILESW) \
71 X("fmtproc program", 0, FMTPROCSW) \
72 X("nofmtproc", 0, NFMTPROCSW) \
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(REPL
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(REPL
, switches
);
88 #define X(sw, minchars, id) id,
89 DEFINE_SWITCH_ENUM(CC
);
92 #define X(sw, minchars, id) { sw, minchars, id },
93 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
96 #define DISPO_SWITCHES \
98 X("replace", 0, YESW) \
99 X("list", 0, LISTDSW) \
100 X("refile +folder", 0, REFILSW) \
103 #define X(sw, minchars, id) id,
104 DEFINE_SWITCH_ENUM(DISPO
);
107 #define X(sw, minchars, id) { sw, minchars, id },
108 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
111 static struct swit aqrl
[] = {
113 { "replace", 0, YESW
},
114 { "list", 0, LISTDSW
},
115 { "refile +folder", 0, REFILSW
},
119 static short outputlinelen
= OUTPUTLINELEN
;
120 static bool groupreply
; /* Is this a group reply? */
122 static bool mime
; /* include original as MIME part */
123 static char *form
= NULL
; /* form (components) file */
124 static char *filter
= NULL
; /* message filter file */
125 static char *fcc
= NULL
; /* folders to add to Fcc: header */
131 static void docc (char *, int);
132 static void add_convert_header (const char *, char *, char *, char *);
136 main (int argc
, char **argv
)
145 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
146 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
147 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
148 char **argp
, **arguments
;
149 svector_t convert_types
= svector_create (10);
150 svector_t convert_args
= svector_create (10);
152 struct msgs
*mp
= NULL
;
155 bool buildsw
= false;
157 if (nmh_init(argv
[0], true, true)) { return 1; }
159 arguments
= getarguments (invo_name
, argc
, argv
, 1);
162 while ((cp
= *argp
++)) {
164 switch (smatch (++cp
, switches
)) {
166 ambigsw (cp
, switches
);
169 die("-%s unknown", cp
);
172 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
174 print_help (buf
, switches
, 1);
177 print_version(invo_name
);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
200 if (!(cp
= *argp
++) || *cp
== '-')
201 die("missing argument to %s", argp
[-2]);
206 if (!(ed
= *argp
++) || *ed
== '-')
207 die("missing argument to %s", argp
[-2]);
218 if (!(type
= *argp
++)) {
219 die("missing type argument to %s", argp
[-2]);
221 if (!(cp
= *argp
++)) {
222 die("missing argstring argument to %s",
226 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
227 if (! strcmp (svector_at (convert_types
, i
), type
)) {
228 /* Already saw this type, so just update
230 svector_strs (convert_args
)[i
] = cp
;
235 if (i
== svector_size (convert_types
)) {
236 svector_push_back (convert_types
, type
);
237 svector_push_back (convert_args
, cp
);
243 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
244 die("missing argument to %s", argp
[-2]);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
259 cp
= dp
= path (cp
+ 1, TSUBCWF
);
261 fcc
= add (", ", fcc
);
268 die("only one file at a time!");
269 if (!(cp
= *argp
++) || *cp
== '-')
270 die("missing argument to %s", argp
[-2]);
271 file
= path (cp
, TFILE
);
274 if (!(cp
= *argp
++) || *cp
== '-')
275 die("missing argument to %s", argp
[-2]);
276 filter
= mh_xstrdup(etcpath(cp
));
280 if (!(form
= *argp
++) || *form
== '-')
281 die("missing argument to %s", argp
[-2]);
285 filter
= mh_xstrdup(etcpath(mhlreply
));
315 if (!(cp
= *argp
++) || *cp
== '-')
316 die("missing argument to %s", argp
[-2]);
317 if ((outputlinelen
= atoi (cp
)) < 10)
318 die("impossible width %d", outputlinelen
);
323 die("only one draft folder at a time!");
324 if (!(cp
= *argp
++) || *cp
== '-')
325 die("missing argument to %s", argp
[-2]);
326 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
327 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
331 die("only one draft message at a time!");
332 if (!(dmsg
= *argp
++) || *dmsg
== '-')
333 die("missing argument to %s", argp
[-2]);
348 if (!(formatproc
= *argp
++) || *formatproc
== '-')
349 die("missing argument to %s", argp
[-2]);
357 if (*cp
== '+' || *cp
== '@') {
359 die("only one folder at a time!");
360 folder
= pluspath (cp
);
363 die("only one message at a time!");
375 cwd
= mh_xstrdup(pwd ());
377 if (!context_find ("path"))
378 free (path ("./", TFOLDER
));
379 if (file
&& (msg
|| folder
))
380 die("can't mix files and folders/msgs");
384 strncpy (drft
, buildsw
? m_maildir ("reply")
385 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
387 /* Check if a draft exists */
388 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
389 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
390 for (i
= LISTDSW
; i
!= YESW
;) {
391 if (!(argp
= read_switch_multiword ("\nDisposition? ",
392 isdf
? aqrnl
: aqrl
)))
394 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
403 showfile (++argp
, drft
);
406 if (refile (++argp
, drft
) == 0)
418 * We are replying to a file.
420 anot
= false; /* we don't want to annotate a file */
423 * We are replying to a message.
428 folder
= getfolder (1);
429 maildir
= m_maildir (folder
);
431 if (chdir (maildir
) == NOTOK
)
432 adios (maildir
, "unable to change directory to");
434 /* read folder and create message structure */
435 if (!(mp
= folder_read (folder
, 1)))
436 die("unable to read folder %s", folder
);
438 /* check for empty folder */
440 die("no messages in %s", folder
);
442 /* parse the message range/sequence/name and set SELECTED */
443 if (!m_convert (mp
, msg
))
445 seq_setprev (mp
); /* set the previous-sequence */
448 die("only one message at a time!");
450 context_replace (pfolder
, folder
); /* update current folder */
451 seq_setcur (mp
, mp
->lowsel
); /* update current message */
452 seq_save (mp
); /* synchronize sequences */
453 context_save (); /* save the context file */
456 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
458 if ((in
= fopen (msg
, "r")) == NULL
)
459 adios (msg
, "unable to open");
461 /* find form (components) file */
464 form
= etcpath (replgroupcomps
);
466 form
= etcpath (replcomps
);
469 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
474 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
476 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
477 add_convert_header (svector_at (convert_types
, n
),
478 svector_at (convert_args
, n
),
488 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
489 inplace
, cwd
, atfile
);
491 svector_free (convert_args
);
492 svector_free (convert_types
);
499 docc (char *cp
, int ccflag
)
501 switch (smatch (cp
, ccswitches
)) {
503 ambigsw (cp
, ccswitches
);
506 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
521 ccto
= cccc
= ccme
= ccflag
;
527 * Add pseudoheaders that will pass the convert arguments to
528 * mhbuild. They have the form:
529 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
530 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
531 * The ARGS pseudoheader is optional, but we always add it when
532 * -convertargs is used.
535 add_convert_header (const char *convert_type
, char *convert_arg
,
536 char *filename
, char *drft
)
540 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
541 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
544 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
545 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);