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/seq_setprev.h"
10 #include "sbr/seq_setcur.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/showfile.h"
13 #include "sbr/smatch.h"
14 #include "sbr/refile.h"
15 #include "sbr/m_draft.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/error.h"
30 #include "sbr/m_maildir.h"
33 #define REPL_SWITCHES \
34 X("group", 0, GROUPSW) \
35 X("nogroup", 0, NGROUPSW) \
36 X("annotate", 0, ANNOSW) \
37 X("noannotate", 0, NANNOSW) \
38 X("cc all|to|cc|me", 0, CCSW) \
39 X("nocc all|to|cc|me", 0, NCCSW) \
40 X("draftfolder +folder", 0, DFOLDSW) \
41 X("draftmessage msg", 0, DMSGSW) \
42 X("nodraftfolder", 0, NDFLDSW) \
43 X("editor editor", 0, EDITRSW) \
44 X("noedit", 0, NEDITSW) \
45 X("convertargs type argstring", 0, CONVERTARGSW) \
46 X("fcc folder", 0, FCCSW) \
47 X("filter filterfile", 0, FILTSW) \
48 X("form formfile", 0, FORMSW) \
49 X("format", 5, FRMTSW) \
50 X("noformat", 7, NFRMTSW) \
51 X("inplace", 0, INPLSW) \
52 X("noinplace", 0, NINPLSW) \
53 X("mime", 0, MIMESW) \
54 X("nomime", 0, NMIMESW) \
55 X("query", 0, QURYSW) \
56 X("noquery", 0, NQURYSW) \
57 X("whatnowproc program", 0, WHATSW) \
58 X("nowhatnowproc", 0, NWHATSW) \
59 X("width columns", 0, WIDTHSW) \
60 X("version", 0, VERSIONSW) \
61 X("help", 0, HELPSW) \
62 X("file file", 4, FILESW) \
63 X("build", 5, BILDSW) /* interface from mhe */ \
64 X("atfile", 0, ATFILESW) \
65 X("noatfile", 0, NOATFILESW) \
66 X("fmtproc program", 0, FMTPROCSW) \
67 X("nofmtproc", 0, NFMTPROCSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(REPL
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(REPL
, switches
);
83 #define X(sw, minchars, id) id,
84 DEFINE_SWITCH_ENUM(CC
);
87 #define X(sw, minchars, id) { sw, minchars, id },
88 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
91 #define DISPO_SWITCHES \
93 X("replace", 0, YESW) \
94 X("list", 0, LISTDSW) \
95 X("refile +folder", 0, REFILSW) \
98 #define X(sw, minchars, id) id,
99 DEFINE_SWITCH_ENUM(DISPO
);
102 #define X(sw, minchars, id) { sw, minchars, id },
103 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
106 static struct swit aqrl
[] = {
108 { "replace", 0, YESW
},
109 { "list", 0, LISTDSW
},
110 { "refile +folder", 0, REFILSW
},
114 static short outputlinelen
= OUTPUTLINELEN
;
115 static bool groupreply
; /* Is this a group reply? */
117 static bool mime
; /* include original as MIME part */
118 static char *form
= NULL
; /* form (components) file */
119 static char *filter
= NULL
; /* message filter file */
120 static char *fcc
= NULL
; /* folders to add to Fcc: header */
126 static void docc (char *, int);
127 static void add_convert_header (const char *, char *, char *, char *);
131 main (int argc
, char **argv
)
140 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
141 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
142 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
143 char **argp
, **arguments
;
144 svector_t convert_types
= svector_create (10);
145 svector_t convert_args
= svector_create (10);
147 struct msgs
*mp
= NULL
;
150 bool buildsw
= false;
152 if (nmh_init(argv
[0], true, true)) { return 1; }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
157 while ((cp
= *argp
++)) {
159 switch (smatch (++cp
, switches
)) {
161 ambigsw (cp
, switches
);
164 die("-%s unknown", cp
);
167 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
169 print_help (buf
, switches
, 1);
172 print_version(invo_name
);
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
201 if (!(ed
= *argp
++) || *ed
== '-')
202 die("missing argument to %s", argp
[-2]);
213 if (!(type
= *argp
++)) {
214 die("missing type argument to %s", argp
[-2]);
216 if (!(cp
= *argp
++)) {
217 die("missing argstring argument to %s",
221 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
222 if (! strcmp (svector_at (convert_types
, i
), type
)) {
223 /* Already saw this type, so just update
225 svector_strs (convert_args
)[i
] = cp
;
230 if (i
== svector_size (convert_types
)) {
231 svector_push_back (convert_types
, type
);
232 svector_push_back (convert_args
, cp
);
238 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
239 die("missing argument to %s", argp
[-2]);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
254 cp
= dp
= path (cp
+ 1, TSUBCWF
);
256 fcc
= add (", ", fcc
);
263 die("only one file at a time!");
264 if (!(cp
= *argp
++) || *cp
== '-')
265 die("missing argument to %s", argp
[-2]);
266 file
= path (cp
, TFILE
);
269 if (!(cp
= *argp
++) || *cp
== '-')
270 die("missing argument to %s", argp
[-2]);
271 filter
= mh_xstrdup(etcpath(cp
));
275 if (!(form
= *argp
++) || *form
== '-')
276 die("missing argument to %s", argp
[-2]);
280 filter
= mh_xstrdup(etcpath(mhlreply
));
310 if (!(cp
= *argp
++) || *cp
== '-')
311 die("missing argument to %s", argp
[-2]);
312 if ((outputlinelen
= atoi (cp
)) < 10)
313 die("impossible width %d", outputlinelen
);
318 die("only one draft folder at a time!");
319 if (!(cp
= *argp
++) || *cp
== '-')
320 die("missing argument to %s", argp
[-2]);
321 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
322 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
326 die("only one draft message at a time!");
327 if (!(dmsg
= *argp
++) || *dmsg
== '-')
328 die("missing argument to %s", argp
[-2]);
343 if (!(formatproc
= *argp
++) || *formatproc
== '-')
344 die("missing argument to %s", argp
[-2]);
352 if (*cp
== '+' || *cp
== '@') {
354 die("only one folder at a time!");
355 folder
= pluspath (cp
);
358 die("only one message at a time!");
370 cwd
= mh_xstrdup(pwd ());
372 if (!context_find ("path"))
373 free (path ("./", TFOLDER
));
374 if (file
&& (msg
|| folder
))
375 die("can't mix files and folders/msgs");
379 strncpy (drft
, buildsw
? m_maildir ("reply")
380 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
382 /* Check if a draft exists */
383 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
384 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
385 for (i
= LISTDSW
; i
!= YESW
;) {
386 if (!(argp
= read_switch_multiword ("\nDisposition? ",
387 isdf
? aqrnl
: aqrl
)))
389 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
398 showfile (++argp
, drft
);
401 if (refile (++argp
, drft
) == 0)
413 * We are replying to a file.
415 anot
= false; /* we don't want to annotate a file */
418 * We are replying to a message.
423 folder
= getfolder (1);
424 maildir
= m_maildir (folder
);
426 if (chdir (maildir
) == NOTOK
)
427 adios (maildir
, "unable to change directory to");
429 /* read folder and create message structure */
430 if (!(mp
= folder_read (folder
, 1)))
431 die("unable to read folder %s", folder
);
433 /* check for empty folder */
435 die("no messages in %s", folder
);
437 /* parse the message range/sequence/name and set SELECTED */
438 if (!m_convert (mp
, msg
))
440 seq_setprev (mp
); /* set the previous-sequence */
443 die("only one message at a time!");
445 context_replace (pfolder
, folder
); /* update current folder */
446 seq_setcur (mp
, mp
->lowsel
); /* update current message */
447 seq_save (mp
); /* synchronize sequences */
448 context_save (); /* save the context file */
451 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
453 if ((in
= fopen (msg
, "r")) == NULL
)
454 adios (msg
, "unable to open");
456 /* find form (components) file */
459 form
= etcpath (replgroupcomps
);
461 form
= etcpath (replcomps
);
464 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
469 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
471 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
472 add_convert_header (svector_at (convert_types
, n
),
473 svector_at (convert_args
, n
),
483 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
484 inplace
, cwd
, atfile
);
486 svector_free (convert_args
);
487 svector_free (convert_types
);
494 docc (char *cp
, int ccflag
)
496 switch (smatch (cp
, ccswitches
)) {
498 ambigsw (cp
, ccswitches
);
501 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
516 ccto
= cccc
= ccme
= ccflag
;
522 * Add pseudoheaders that will pass the convert arguments to
523 * mhbuild. They have the form:
524 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
525 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
526 * The ARGS pseudoheader is optional, but we always add it when
527 * -convertargs is used.
530 add_convert_header (const char *convert_type
, char *convert_arg
,
531 char *filename
, char *drft
)
535 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
536 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
539 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
540 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);