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/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/ambigsw.h"
15 #include "sbr/print_version.h"
16 #include "sbr/print_help.h"
17 #include "sbr/error.h"
21 #include "sbr/m_maildir.h"
24 #define REPL_SWITCHES \
25 X("group", 0, GROUPSW) \
26 X("nogroup", 0, NGROUPSW) \
27 X("annotate", 0, ANNOSW) \
28 X("noannotate", 0, NANNOSW) \
29 X("cc all|to|cc|me", 0, CCSW) \
30 X("nocc all|to|cc|me", 0, NCCSW) \
31 X("draftfolder +folder", 0, DFOLDSW) \
32 X("draftmessage msg", 0, DMSGSW) \
33 X("nodraftfolder", 0, NDFLDSW) \
34 X("editor editor", 0, EDITRSW) \
35 X("noedit", 0, NEDITSW) \
36 X("convertargs type argstring", 0, CONVERTARGSW) \
37 X("fcc folder", 0, FCCSW) \
38 X("filter filterfile", 0, FILTSW) \
39 X("form formfile", 0, FORMSW) \
40 X("format", 5, FRMTSW) \
41 X("noformat", 7, NFRMTSW) \
42 X("inplace", 0, INPLSW) \
43 X("noinplace", 0, NINPLSW) \
44 X("mime", 0, MIMESW) \
45 X("nomime", 0, NMIMESW) \
46 X("query", 0, QURYSW) \
47 X("noquery", 0, NQURYSW) \
48 X("whatnowproc program", 0, WHATSW) \
49 X("nowhatnowproc", 0, NWHATSW) \
50 X("width columns", 0, WIDTHSW) \
51 X("version", 0, VERSIONSW) \
52 X("help", 0, HELPSW) \
53 X("file file", 4, FILESW) \
54 X("build", 5, BILDSW) /* interface from mhe */ \
55 X("atfile", 0, ATFILESW) \
56 X("noatfile", 0, NOATFILESW) \
57 X("fmtproc program", 0, FMTPROCSW) \
58 X("nofmtproc", 0, NFMTPROCSW) \
60 #define X(sw, minchars, id) id,
61 DEFINE_SWITCH_ENUM(REPL
);
64 #define X(sw, minchars, id) { sw, minchars, id },
65 DEFINE_SWITCH_ARRAY(REPL
, switches
);
74 #define X(sw, minchars, id) id,
75 DEFINE_SWITCH_ENUM(CC
);
78 #define X(sw, minchars, id) { sw, minchars, id },
79 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
82 #define DISPO_SWITCHES \
84 X("replace", 0, YESW) \
85 X("list", 0, LISTDSW) \
86 X("refile +folder", 0, REFILSW) \
89 #define X(sw, minchars, id) id,
90 DEFINE_SWITCH_ENUM(DISPO
);
93 #define X(sw, minchars, id) { sw, minchars, id },
94 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
97 static struct swit aqrl
[] = {
99 { "replace", 0, YESW
},
100 { "list", 0, LISTDSW
},
101 { "refile +folder", 0, REFILSW
},
105 static short outputlinelen
= OUTPUTLINELEN
;
106 static bool groupreply
; /* Is this a group reply? */
108 static bool mime
; /* include original as MIME part */
109 static char *form
= NULL
; /* form (components) file */
110 static char *filter
= NULL
; /* message filter file */
111 static char *fcc
= NULL
; /* folders to add to Fcc: header */
117 static void docc (char *, int);
118 static void add_convert_header (const char *, char *, char *, char *);
122 main (int argc
, char **argv
)
131 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
132 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
133 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
134 char **argp
, **arguments
;
135 svector_t convert_types
= svector_create (10);
136 svector_t convert_args
= svector_create (10);
138 struct msgs
*mp
= NULL
;
141 bool buildsw
= false;
143 if (nmh_init(argv
[0], true, true)) { return 1; }
145 arguments
= getarguments (invo_name
, argc
, argv
, 1);
148 while ((cp
= *argp
++)) {
150 switch (smatch (++cp
, switches
)) {
152 ambigsw (cp
, switches
);
155 die("-%s unknown", cp
);
158 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
160 print_help (buf
, switches
, 1);
163 print_version(invo_name
);
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
186 if (!(cp
= *argp
++) || *cp
== '-')
187 die("missing argument to %s", argp
[-2]);
192 if (!(ed
= *argp
++) || *ed
== '-')
193 die("missing argument to %s", argp
[-2]);
204 if (!(type
= *argp
++)) {
205 die("missing type argument to %s", argp
[-2]);
207 if (!(cp
= *argp
++)) {
208 die("missing argstring argument to %s",
212 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
213 if (! strcmp (svector_at (convert_types
, i
), type
)) {
214 /* Already saw this type, so just update
216 svector_strs (convert_args
)[i
] = cp
;
221 if (i
== svector_size (convert_types
)) {
222 svector_push_back (convert_types
, type
);
223 svector_push_back (convert_args
, cp
);
229 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
230 die("missing argument to %s", argp
[-2]);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 die("missing argument to %s", argp
[-2]);
245 cp
= dp
= path (cp
+ 1, TSUBCWF
);
247 fcc
= add (", ", fcc
);
254 die("only one file at a time!");
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
257 file
= path (cp
, TFILE
);
260 if (!(cp
= *argp
++) || *cp
== '-')
261 die("missing argument to %s", argp
[-2]);
262 filter
= mh_xstrdup(etcpath(cp
));
266 if (!(form
= *argp
++) || *form
== '-')
267 die("missing argument to %s", argp
[-2]);
271 filter
= mh_xstrdup(etcpath(mhlreply
));
301 if (!(cp
= *argp
++) || *cp
== '-')
302 die("missing argument to %s", argp
[-2]);
303 if ((outputlinelen
= atoi (cp
)) < 10)
304 die("impossible width %d", outputlinelen
);
309 die("only one draft folder at a time!");
310 if (!(cp
= *argp
++) || *cp
== '-')
311 die("missing argument to %s", argp
[-2]);
312 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
313 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
317 die("only one draft message at a time!");
318 if (!(dmsg
= *argp
++) || *dmsg
== '-')
319 die("missing argument to %s", argp
[-2]);
334 if (!(formatproc
= *argp
++) || *formatproc
== '-')
335 die("missing argument to %s", argp
[-2]);
343 if (*cp
== '+' || *cp
== '@') {
345 die("only one folder at a time!");
346 folder
= pluspath (cp
);
349 die("only one message at a time!");
361 cwd
= mh_xstrdup(pwd ());
363 if (!context_find ("path"))
364 free (path ("./", TFOLDER
));
365 if (file
&& (msg
|| folder
))
366 die("can't mix files and folders/msgs");
370 strncpy (drft
, buildsw
? m_maildir ("reply")
371 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
373 /* Check if a draft exists */
374 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
375 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
376 for (i
= LISTDSW
; i
!= YESW
;) {
377 if (!(argp
= read_switch_multiword ("\nDisposition? ",
378 isdf
? aqrnl
: aqrl
)))
380 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
389 showfile (++argp
, drft
);
392 if (refile (++argp
, drft
) == 0)
404 * We are replying to a file.
406 anot
= false; /* we don't want to annotate a file */
409 * We are replying to a message.
414 folder
= getfolder (1);
415 maildir
= m_maildir (folder
);
417 if (chdir (maildir
) == NOTOK
)
418 adios (maildir
, "unable to change directory to");
420 /* read folder and create message structure */
421 if (!(mp
= folder_read (folder
, 1)))
422 die("unable to read folder %s", folder
);
424 /* check for empty folder */
426 die("no messages in %s", folder
);
428 /* parse the message range/sequence/name and set SELECTED */
429 if (!m_convert (mp
, msg
))
431 seq_setprev (mp
); /* set the previous-sequence */
434 die("only one message at a time!");
436 context_replace (pfolder
, folder
); /* update current folder */
437 seq_setcur (mp
, mp
->lowsel
); /* update current message */
438 seq_save (mp
); /* synchronize sequences */
439 context_save (); /* save the context file */
442 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
444 if ((in
= fopen (msg
, "r")) == NULL
)
445 adios (msg
, "unable to open");
447 /* find form (components) file */
450 form
= etcpath (replgroupcomps
);
452 form
= etcpath (replcomps
);
455 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
460 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
462 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
463 add_convert_header (svector_at (convert_types
, n
),
464 svector_at (convert_args
, n
),
474 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
475 inplace
, cwd
, atfile
);
477 svector_free (convert_args
);
478 svector_free (convert_types
);
485 docc (char *cp
, int ccflag
)
487 switch (smatch (cp
, ccswitches
)) {
489 ambigsw (cp
, ccswitches
);
492 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
507 ccto
= cccc
= ccme
= ccflag
;
513 * Add pseudoheaders that will pass the convert arguments to
514 * mhbuild. They have the form:
515 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
516 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
517 * The ARGS pseudoheader is optional, but we always add it when
518 * -convertargs is used.
521 add_convert_header (const char *convert_type
, char *convert_arg
,
522 char *filename
, char *drft
)
526 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
527 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
530 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
531 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);