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.
12 #include "sbr/m_maildir.h"
15 #define REPL_SWITCHES \
16 X("group", 0, GROUPSW) \
17 X("nogroup", 0, NGROUPSW) \
18 X("annotate", 0, ANNOSW) \
19 X("noannotate", 0, NANNOSW) \
20 X("cc all|to|cc|me", 0, CCSW) \
21 X("nocc all|to|cc|me", 0, NCCSW) \
22 X("draftfolder +folder", 0, DFOLDSW) \
23 X("draftmessage msg", 0, DMSGSW) \
24 X("nodraftfolder", 0, NDFLDSW) \
25 X("editor editor", 0, EDITRSW) \
26 X("noedit", 0, NEDITSW) \
27 X("convertargs type argstring", 0, CONVERTARGSW) \
28 X("fcc folder", 0, FCCSW) \
29 X("filter filterfile", 0, FILTSW) \
30 X("form formfile", 0, FORMSW) \
31 X("format", 5, FRMTSW) \
32 X("noformat", 7, NFRMTSW) \
33 X("inplace", 0, INPLSW) \
34 X("noinplace", 0, NINPLSW) \
35 X("mime", 0, MIMESW) \
36 X("nomime", 0, NMIMESW) \
37 X("query", 0, QURYSW) \
38 X("noquery", 0, NQURYSW) \
39 X("whatnowproc program", 0, WHATSW) \
40 X("nowhatnowproc", 0, NWHATSW) \
41 X("width columns", 0, WIDTHSW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44 X("file file", 4, FILESW) \
45 X("build", 5, BILDSW) /* interface from mhe */ \
46 X("atfile", 0, ATFILESW) \
47 X("noatfile", 0, NOATFILESW) \
48 X("fmtproc program", 0, FMTPROCSW) \
49 X("nofmtproc", 0, NFMTPROCSW) \
51 #define X(sw, minchars, id) id,
52 DEFINE_SWITCH_ENUM(REPL
);
55 #define X(sw, minchars, id) { sw, minchars, id },
56 DEFINE_SWITCH_ARRAY(REPL
, switches
);
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(CC
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
73 #define DISPO_SWITCHES \
75 X("replace", 0, YESW) \
76 X("list", 0, LISTDSW) \
77 X("refile +folder", 0, REFILSW) \
80 #define X(sw, minchars, id) id,
81 DEFINE_SWITCH_ENUM(DISPO
);
84 #define X(sw, minchars, id) { sw, minchars, id },
85 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
88 static struct swit aqrl
[] = {
90 { "replace", 0, YESW
},
91 { "list", 0, LISTDSW
},
92 { "refile +folder", 0, REFILSW
},
96 static short outputlinelen
= OUTPUTLINELEN
;
97 static bool groupreply
; /* Is this a group reply? */
99 static bool mime
; /* include original as MIME part */
100 static char *form
= NULL
; /* form (components) file */
101 static char *filter
= NULL
; /* message filter file */
102 static char *fcc
= NULL
; /* folders to add to Fcc: header */
108 static void docc (char *, int);
109 static void add_convert_header (const char *, char *, char *, char *);
113 main (int argc
, char **argv
)
122 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
123 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
124 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
125 char **argp
, **arguments
;
126 svector_t convert_types
= svector_create (10);
127 svector_t convert_args
= svector_create (10);
129 struct msgs
*mp
= NULL
;
132 bool buildsw
= false;
134 if (nmh_init(argv
[0], true, true)) { return 1; }
136 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 while ((cp
= *argp
++)) {
141 switch (smatch (++cp
, switches
)) {
143 ambigsw (cp
, switches
);
146 die("-%s unknown", cp
);
149 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
151 print_help (buf
, switches
, 1);
154 print_version(invo_name
);
172 if (!(cp
= *argp
++) || *cp
== '-')
173 die("missing argument to %s", argp
[-2]);
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
183 if (!(ed
= *argp
++) || *ed
== '-')
184 die("missing argument to %s", argp
[-2]);
195 if (!(type
= *argp
++)) {
196 die("missing type argument to %s", argp
[-2]);
198 if (!(cp
= *argp
++)) {
199 die("missing argstring argument to %s",
203 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
204 if (! strcmp (svector_at (convert_types
, i
), type
)) {
205 /* Already saw this type, so just update
207 svector_strs (convert_args
)[i
] = cp
;
212 if (i
== svector_size (convert_types
)) {
213 svector_push_back (convert_types
, type
);
214 svector_push_back (convert_args
, cp
);
220 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
221 die("missing argument to %s", argp
[-2]);
232 if (!(cp
= *argp
++) || *cp
== '-')
233 die("missing argument to %s", argp
[-2]);
236 cp
= dp
= path (cp
+ 1, TSUBCWF
);
238 fcc
= add (", ", fcc
);
245 die("only one file at a time!");
246 if (!(cp
= *argp
++) || *cp
== '-')
247 die("missing argument to %s", argp
[-2]);
248 file
= path (cp
, TFILE
);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 die("missing argument to %s", argp
[-2]);
253 filter
= mh_xstrdup(etcpath(cp
));
257 if (!(form
= *argp
++) || *form
== '-')
258 die("missing argument to %s", argp
[-2]);
262 filter
= mh_xstrdup(etcpath(mhlreply
));
292 if (!(cp
= *argp
++) || *cp
== '-')
293 die("missing argument to %s", argp
[-2]);
294 if ((outputlinelen
= atoi (cp
)) < 10)
295 die("impossible width %d", outputlinelen
);
300 die("only one draft folder at a time!");
301 if (!(cp
= *argp
++) || *cp
== '-')
302 die("missing argument to %s", argp
[-2]);
303 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
304 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
308 die("only one draft message at a time!");
309 if (!(dmsg
= *argp
++) || *dmsg
== '-')
310 die("missing argument to %s", argp
[-2]);
325 if (!(formatproc
= *argp
++) || *formatproc
== '-')
326 die("missing argument to %s", argp
[-2]);
334 if (*cp
== '+' || *cp
== '@') {
336 die("only one folder at a time!");
337 folder
= pluspath (cp
);
340 die("only one message at a time!");
352 cwd
= mh_xstrdup(pwd ());
354 if (!context_find ("path"))
355 free (path ("./", TFOLDER
));
356 if (file
&& (msg
|| folder
))
357 die("can't mix files and folders/msgs");
361 strncpy (drft
, buildsw
? m_maildir ("reply")
362 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
364 /* Check if a draft exists */
365 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
366 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
367 for (i
= LISTDSW
; i
!= YESW
;) {
368 if (!(argp
= read_switch_multiword ("\nDisposition? ",
369 isdf
? aqrnl
: aqrl
)))
371 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
380 showfile (++argp
, drft
);
383 if (refile (++argp
, drft
) == 0)
395 * We are replying to a file.
397 anot
= false; /* we don't want to annotate a file */
400 * We are replying to a message.
405 folder
= getfolder (1);
406 maildir
= m_maildir (folder
);
408 if (chdir (maildir
) == NOTOK
)
409 adios (maildir
, "unable to change directory to");
411 /* read folder and create message structure */
412 if (!(mp
= folder_read (folder
, 1)))
413 die("unable to read folder %s", folder
);
415 /* check for empty folder */
417 die("no messages in %s", folder
);
419 /* parse the message range/sequence/name and set SELECTED */
420 if (!m_convert (mp
, msg
))
422 seq_setprev (mp
); /* set the previous-sequence */
425 die("only one message at a time!");
427 context_replace (pfolder
, folder
); /* update current folder */
428 seq_setcur (mp
, mp
->lowsel
); /* update current message */
429 seq_save (mp
); /* synchronize sequences */
430 context_save (); /* save the context file */
433 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
435 if ((in
= fopen (msg
, "r")) == NULL
)
436 adios (msg
, "unable to open");
438 /* find form (components) file */
441 form
= etcpath (replgroupcomps
);
443 form
= etcpath (replcomps
);
446 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
451 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
453 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
454 add_convert_header (svector_at (convert_types
, n
),
455 svector_at (convert_args
, n
),
465 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
466 inplace
, cwd
, atfile
);
468 svector_free (convert_args
);
469 svector_free (convert_types
);
476 docc (char *cp
, int ccflag
)
478 switch (smatch (cp
, ccswitches
)) {
480 ambigsw (cp
, ccswitches
);
483 die("-%scc %s unknown", ccflag
? "" : "no", cp
);
498 ccto
= cccc
= ccme
= ccflag
;
504 * Add pseudoheaders that will pass the convert arguments to
505 * mhbuild. They have the form:
506 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
507 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
508 * The ARGS pseudoheader is optional, but we always add it when
509 * -convertargs is used.
512 add_convert_header (const char *convert_type
, char *convert_arg
,
513 char *filename
, char *drft
)
517 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
518 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
521 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
522 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);