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/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
17 #include "sbr/m_maildir.h"
20 #define REPL_SWITCHES \
21 X("group", 0, GROUPSW) \
22 X("nogroup", 0, NGROUPSW) \
23 X("annotate", 0, ANNOSW) \
24 X("noannotate", 0, NANNOSW) \
25 X("cc all|to|cc|me", 0, CCSW) \
26 X("nocc all|to|cc|me", 0, NCCSW) \
27 X("draftfolder +folder", 0, DFOLDSW) \
28 X("draftmessage msg", 0, DMSGSW) \
29 X("nodraftfolder", 0, NDFLDSW) \
30 X("editor editor", 0, EDITRSW) \
31 X("noedit", 0, NEDITSW) \
32 X("convertargs type argstring", 0, CONVERTARGSW) \
33 X("fcc folder", 0, FCCSW) \
34 X("filter filterfile", 0, FILTSW) \
35 X("form formfile", 0, FORMSW) \
36 X("format", 5, FRMTSW) \
37 X("noformat", 7, NFRMTSW) \
38 X("inplace", 0, INPLSW) \
39 X("noinplace", 0, NINPLSW) \
40 X("mime", 0, MIMESW) \
41 X("nomime", 0, NMIMESW) \
42 X("query", 0, QURYSW) \
43 X("noquery", 0, NQURYSW) \
44 X("whatnowproc program", 0, WHATSW) \
45 X("nowhatnowproc", 0, NWHATSW) \
46 X("width columns", 0, WIDTHSW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
49 X("file file", 4, FILESW) \
50 X("build", 5, BILDSW) /* interface from mhe */ \
51 X("atfile", 0, ATFILESW) \
52 X("noatfile", 0, NOATFILESW) \
53 X("fmtproc program", 0, FMTPROCSW) \
54 X("nofmtproc", 0, NFMTPROCSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(REPL
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(REPL
, switches
);
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(CC
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
78 #define DISPO_SWITCHES \
80 X("replace", 0, YESW) \
81 X("list", 0, LISTDSW) \
82 X("refile +folder", 0, REFILSW) \
85 #define X(sw, minchars, id) id,
86 DEFINE_SWITCH_ENUM(DISPO
);
89 #define X(sw, minchars, id) { sw, minchars, id },
90 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
93 static struct swit aqrl
[] = {
95 { "replace", 0, YESW
},
96 { "list", 0, LISTDSW
},
97 { "refile +folder", 0, REFILSW
},
101 static short outputlinelen
= OUTPUTLINELEN
;
102 static bool groupreply
; /* Is this a group reply? */
104 static bool mime
; /* include original as MIME part */
105 static char *form
= NULL
; /* form (components) file */
106 static char *filter
= NULL
; /* message filter file */
107 static char *fcc
= NULL
; /* folders to add to Fcc: header */
113 static void docc (char *, int);
114 static void add_convert_header (const char *, char *, char *, char *);
118 main (int argc
, char **argv
)
127 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
128 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
129 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
130 char **argp
, **arguments
;
131 svector_t convert_types
= svector_create (10);
132 svector_t convert_args
= svector_create (10);
134 struct msgs
*mp
= NULL
;
137 bool buildsw
= false;
139 if (nmh_init(argv
[0], true, true)) { return 1; }
141 arguments
= getarguments (invo_name
, argc
, argv
, 1);
144 while ((cp
= *argp
++)) {
146 switch (smatch (++cp
, switches
)) {
148 ambigsw (cp
, switches
);
151 die("-%s unknown", cp
);
154 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
156 print_help (buf
, switches
, 1);
159 print_version(invo_name
);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
182 if (!(cp
= *argp
++) || *cp
== '-')
183 die("missing argument to %s", argp
[-2]);
188 if (!(ed
= *argp
++) || *ed
== '-')
189 die("missing argument to %s", argp
[-2]);
200 if (!(type
= *argp
++)) {
201 die("missing type argument to %s", argp
[-2]);
203 if (!(cp
= *argp
++)) {
204 die("missing argstring argument to %s",
208 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
209 if (! strcmp (svector_at (convert_types
, i
), type
)) {
210 /* Already saw this type, so just update
212 svector_strs (convert_args
)[i
] = cp
;
217 if (i
== svector_size (convert_types
)) {
218 svector_push_back (convert_types
, type
);
219 svector_push_back (convert_args
, cp
);
225 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
226 die("missing argument to %s", argp
[-2]);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 die("missing argument to %s", argp
[-2]);
241 cp
= dp
= path (cp
+ 1, TSUBCWF
);
243 fcc
= add (", ", fcc
);
250 die("only one file at a time!");
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
253 file
= path (cp
, TFILE
);
256 if (!(cp
= *argp
++) || *cp
== '-')
257 die("missing argument to %s", argp
[-2]);
258 filter
= mh_xstrdup(etcpath(cp
));
262 if (!(form
= *argp
++) || *form
== '-')
263 die("missing argument to %s", argp
[-2]);
267 filter
= mh_xstrdup(etcpath(mhlreply
));
297 if (!(cp
= *argp
++) || *cp
== '-')
298 die("missing argument to %s", argp
[-2]);
299 if ((outputlinelen
= atoi (cp
)) < 10)
300 die("impossible width %d", outputlinelen
);
305 die("only one draft folder at a time!");
306 if (!(cp
= *argp
++) || *cp
== '-')
307 die("missing argument to %s", argp
[-2]);
308 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
309 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
313 die("only one draft message at a time!");
314 if (!(dmsg
= *argp
++) || *dmsg
== '-')
315 die("missing argument to %s", argp
[-2]);
330 if (!(formatproc
= *argp
++) || *formatproc
== '-')
331 die("missing argument to %s", argp
[-2]);
339 if (*cp
== '+' || *cp
== '@') {
341 die("only one folder at a time!");
342 folder
= pluspath (cp
);
345 die("only one message at a time!");
357 cwd
= mh_xstrdup(pwd ());
359 if (!context_find ("path"))
360 free (path ("./", TFOLDER
));
361 if (file
&& (msg
|| folder
))
362 die("can't mix files and folders/msgs");
366 strncpy (drft
, buildsw
? m_maildir ("reply")
367 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
369 /* Check if a draft exists */
370 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
371 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
372 for (i
= LISTDSW
; i
!= YESW
;) {
373 if (!(argp
= read_switch_multiword ("\nDisposition? ",
374 isdf
? aqrnl
: aqrl
)))
376 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
385 showfile (++argp
, drft
);
388 if (refile (++argp
, drft
) == 0)
400 * We are replying to a file.
402 anot
= false; /* we don't want to annotate a file */
405 * We are replying to a message.
410 folder
= getfolder (1);
411 maildir
= m_maildir (folder
);
413 if (chdir (maildir
) == NOTOK
)
414 adios (maildir
, "unable to change directory to");
416 /* read folder and create message structure */
417 if (!(mp
= folder_read (folder
, 1)))
418 die("unable to read folder %s", folder
);
420 /* check for empty folder */
422 die("no messages in %s", folder
);
424 /* parse the message range/sequence/name and set SELECTED */
425 if (!m_convert (mp
, msg
))
427 seq_setprev (mp
); /* set the previous-sequence */
430 die("only one message at a time!");
432 context_replace (pfolder
, folder
); /* update current folder */
433 seq_setcur (mp
, mp
->lowsel
); /* update current message */
434 seq_save (mp
); /* synchronize sequences */
435 context_save (); /* save the context file */
438 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
440 if ((in
= fopen (msg
, "r")) == NULL
)
441 adios (msg
, "unable to open");
443 /* find form (components) file */
446 form
= etcpath (replgroupcomps
);
448 form
= etcpath (replcomps
);
451 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
456 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
458 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
459 add_convert_header (svector_at (convert_types
, n
),
460 svector_at (convert_args
, n
),
470 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
471 inplace
, cwd
, atfile
);
473 svector_free (convert_args
);
474 svector_free (convert_types
);
481 docc (char *cp
, int ccflag
)
483 switch (smatch (cp
, ccswitches
)) {
485 ambigsw (cp
, ccswitches
);
488 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
503 ccto
= cccc
= ccme
= ccflag
;
509 * Add pseudoheaders that will pass the convert arguments to
510 * mhbuild. They have the form:
511 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
512 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
513 * The ARGS pseudoheader is optional, but we always add it when
514 * -convertargs is used.
517 add_convert_header (const char *convert_type
, char *convert_arg
,
518 char *filename
, char *drft
)
522 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
523 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
526 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
527 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);