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/context_replace.h"
10 #include "sbr/context_find.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
19 #include "sbr/m_maildir.h"
22 #define REPL_SWITCHES \
23 X("group", 0, GROUPSW) \
24 X("nogroup", 0, NGROUPSW) \
25 X("annotate", 0, ANNOSW) \
26 X("noannotate", 0, NANNOSW) \
27 X("cc all|to|cc|me", 0, CCSW) \
28 X("nocc all|to|cc|me", 0, NCCSW) \
29 X("draftfolder +folder", 0, DFOLDSW) \
30 X("draftmessage msg", 0, DMSGSW) \
31 X("nodraftfolder", 0, NDFLDSW) \
32 X("editor editor", 0, EDITRSW) \
33 X("noedit", 0, NEDITSW) \
34 X("convertargs type argstring", 0, CONVERTARGSW) \
35 X("fcc folder", 0, FCCSW) \
36 X("filter filterfile", 0, FILTSW) \
37 X("form formfile", 0, FORMSW) \
38 X("format", 5, FRMTSW) \
39 X("noformat", 7, NFRMTSW) \
40 X("inplace", 0, INPLSW) \
41 X("noinplace", 0, NINPLSW) \
42 X("mime", 0, MIMESW) \
43 X("nomime", 0, NMIMESW) \
44 X("query", 0, QURYSW) \
45 X("noquery", 0, NQURYSW) \
46 X("whatnowproc program", 0, WHATSW) \
47 X("nowhatnowproc", 0, NWHATSW) \
48 X("width columns", 0, WIDTHSW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
51 X("file file", 4, FILESW) \
52 X("build", 5, BILDSW) /* interface from mhe */ \
53 X("atfile", 0, ATFILESW) \
54 X("noatfile", 0, NOATFILESW) \
55 X("fmtproc program", 0, FMTPROCSW) \
56 X("nofmtproc", 0, NFMTPROCSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(REPL
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(REPL
, switches
);
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(CC
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
80 #define DISPO_SWITCHES \
82 X("replace", 0, YESW) \
83 X("list", 0, LISTDSW) \
84 X("refile +folder", 0, REFILSW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(DISPO
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
95 static struct swit aqrl
[] = {
97 { "replace", 0, YESW
},
98 { "list", 0, LISTDSW
},
99 { "refile +folder", 0, REFILSW
},
103 static short outputlinelen
= OUTPUTLINELEN
;
104 static bool groupreply
; /* Is this a group reply? */
106 static bool mime
; /* include original as MIME part */
107 static char *form
= NULL
; /* form (components) file */
108 static char *filter
= NULL
; /* message filter file */
109 static char *fcc
= NULL
; /* folders to add to Fcc: header */
115 static void docc (char *, int);
116 static void add_convert_header (const char *, char *, char *, char *);
120 main (int argc
, char **argv
)
129 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
130 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
131 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
132 char **argp
, **arguments
;
133 svector_t convert_types
= svector_create (10);
134 svector_t convert_args
= svector_create (10);
136 struct msgs
*mp
= NULL
;
139 bool buildsw
= false;
141 if (nmh_init(argv
[0], true, true)) { return 1; }
143 arguments
= getarguments (invo_name
, argc
, argv
, 1);
146 while ((cp
= *argp
++)) {
148 switch (smatch (++cp
, switches
)) {
150 ambigsw (cp
, switches
);
153 die("-%s unknown", cp
);
156 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
158 print_help (buf
, switches
, 1);
161 print_version(invo_name
);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 die("missing argument to %s", argp
[-2]);
184 if (!(cp
= *argp
++) || *cp
== '-')
185 die("missing argument to %s", argp
[-2]);
190 if (!(ed
= *argp
++) || *ed
== '-')
191 die("missing argument to %s", argp
[-2]);
202 if (!(type
= *argp
++)) {
203 die("missing type argument to %s", argp
[-2]);
205 if (!(cp
= *argp
++)) {
206 die("missing argstring argument to %s",
210 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
211 if (! strcmp (svector_at (convert_types
, i
), type
)) {
212 /* Already saw this type, so just update
214 svector_strs (convert_args
)[i
] = cp
;
219 if (i
== svector_size (convert_types
)) {
220 svector_push_back (convert_types
, type
);
221 svector_push_back (convert_args
, cp
);
227 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
228 die("missing argument to %s", argp
[-2]);
239 if (!(cp
= *argp
++) || *cp
== '-')
240 die("missing argument to %s", argp
[-2]);
243 cp
= dp
= path (cp
+ 1, TSUBCWF
);
245 fcc
= add (", ", fcc
);
252 die("only one file at a time!");
253 if (!(cp
= *argp
++) || *cp
== '-')
254 die("missing argument to %s", argp
[-2]);
255 file
= path (cp
, TFILE
);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 die("missing argument to %s", argp
[-2]);
260 filter
= mh_xstrdup(etcpath(cp
));
264 if (!(form
= *argp
++) || *form
== '-')
265 die("missing argument to %s", argp
[-2]);
269 filter
= mh_xstrdup(etcpath(mhlreply
));
299 if (!(cp
= *argp
++) || *cp
== '-')
300 die("missing argument to %s", argp
[-2]);
301 if ((outputlinelen
= atoi (cp
)) < 10)
302 die("impossible width %d", outputlinelen
);
307 die("only one draft folder at a time!");
308 if (!(cp
= *argp
++) || *cp
== '-')
309 die("missing argument to %s", argp
[-2]);
310 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
311 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
315 die("only one draft message at a time!");
316 if (!(dmsg
= *argp
++) || *dmsg
== '-')
317 die("missing argument to %s", argp
[-2]);
332 if (!(formatproc
= *argp
++) || *formatproc
== '-')
333 die("missing argument to %s", argp
[-2]);
341 if (*cp
== '+' || *cp
== '@') {
343 die("only one folder at a time!");
344 folder
= pluspath (cp
);
347 die("only one message at a time!");
359 cwd
= mh_xstrdup(pwd ());
361 if (!context_find ("path"))
362 free (path ("./", TFOLDER
));
363 if (file
&& (msg
|| folder
))
364 die("can't mix files and folders/msgs");
368 strncpy (drft
, buildsw
? m_maildir ("reply")
369 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
371 /* Check if a draft exists */
372 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
373 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
374 for (i
= LISTDSW
; i
!= YESW
;) {
375 if (!(argp
= read_switch_multiword ("\nDisposition? ",
376 isdf
? aqrnl
: aqrl
)))
378 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
387 showfile (++argp
, drft
);
390 if (refile (++argp
, drft
) == 0)
402 * We are replying to a file.
404 anot
= false; /* we don't want to annotate a file */
407 * We are replying to a message.
412 folder
= getfolder (1);
413 maildir
= m_maildir (folder
);
415 if (chdir (maildir
) == NOTOK
)
416 adios (maildir
, "unable to change directory to");
418 /* read folder and create message structure */
419 if (!(mp
= folder_read (folder
, 1)))
420 die("unable to read folder %s", folder
);
422 /* check for empty folder */
424 die("no messages in %s", folder
);
426 /* parse the message range/sequence/name and set SELECTED */
427 if (!m_convert (mp
, msg
))
429 seq_setprev (mp
); /* set the previous-sequence */
432 die("only one message at a time!");
434 context_replace (pfolder
, folder
); /* update current folder */
435 seq_setcur (mp
, mp
->lowsel
); /* update current message */
436 seq_save (mp
); /* synchronize sequences */
437 context_save (); /* save the context file */
440 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
442 if ((in
= fopen (msg
, "r")) == NULL
)
443 adios (msg
, "unable to open");
445 /* find form (components) file */
448 form
= etcpath (replgroupcomps
);
450 form
= etcpath (replcomps
);
453 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
458 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
460 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
461 add_convert_header (svector_at (convert_types
, n
),
462 svector_at (convert_args
, n
),
472 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
473 inplace
, cwd
, atfile
);
475 svector_free (convert_args
);
476 svector_free (convert_types
);
483 docc (char *cp
, int ccflag
)
485 switch (smatch (cp
, ccswitches
)) {
487 ambigsw (cp
, ccswitches
);
490 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
505 ccto
= cccc
= ccme
= ccflag
;
511 * Add pseudoheaders that will pass the convert arguments to
512 * mhbuild. They have the form:
513 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
514 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
515 * The ARGS pseudoheader is optional, but we always add it when
516 * -convertargs is used.
519 add_convert_header (const char *convert_type
, char *convert_arg
,
520 char *filename
, char *drft
)
524 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
525 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
528 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
529 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);