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.
11 #include "../sbr/m_maildir.h"
13 #define REPL_SWITCHES \
14 X("group", 0, GROUPSW) \
15 X("nogroup", 0, NGROUPSW) \
16 X("annotate", 0, ANNOSW) \
17 X("noannotate", 0, NANNOSW) \
18 X("cc all|to|cc|me", 0, CCSW) \
19 X("nocc all|to|cc|me", 0, NCCSW) \
20 X("draftfolder +folder", 0, DFOLDSW) \
21 X("draftmessage msg", 0, DMSGSW) \
22 X("nodraftfolder", 0, NDFLDSW) \
23 X("editor editor", 0, EDITRSW) \
24 X("noedit", 0, NEDITSW) \
25 X("convertargs type argstring", 0, CONVERTARGSW) \
26 X("fcc folder", 0, FCCSW) \
27 X("filter filterfile", 0, FILTSW) \
28 X("form formfile", 0, FORMSW) \
29 X("format", 5, FRMTSW) \
30 X("noformat", 7, NFRMTSW) \
31 X("inplace", 0, INPLSW) \
32 X("noinplace", 0, NINPLSW) \
33 X("mime", 0, MIMESW) \
34 X("nomime", 0, NMIMESW) \
35 X("query", 0, QURYSW) \
36 X("noquery", 0, NQURYSW) \
37 X("whatnowproc program", 0, WHATSW) \
38 X("nowhatnowproc", 0, NWHATSW) \
39 X("width columns", 0, WIDTHSW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
42 X("file file", 4, FILESW) \
43 X("build", 5, BILDSW) /* interface from mhe */ \
44 X("atfile", 0, ATFILESW) \
45 X("noatfile", 0, NOATFILESW) \
46 X("fmtproc program", 0, FMTPROCSW) \
47 X("nofmtproc", 0, NFMTPROCSW) \
49 #define X(sw, minchars, id) id,
50 DEFINE_SWITCH_ENUM(REPL
);
53 #define X(sw, minchars, id) { sw, minchars, id },
54 DEFINE_SWITCH_ARRAY(REPL
, switches
);
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(CC
);
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
71 #define DISPO_SWITCHES \
73 X("replace", 0, YESW) \
74 X("list", 0, LISTDSW) \
75 X("refile +folder", 0, REFILSW) \
78 #define X(sw, minchars, id) id,
79 DEFINE_SWITCH_ENUM(DISPO
);
82 #define X(sw, minchars, id) { sw, minchars, id },
83 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
86 static struct swit aqrl
[] = {
88 { "replace", 0, YESW
},
89 { "list", 0, LISTDSW
},
90 { "refile +folder", 0, REFILSW
},
94 short ccto
= -1; /* global for replsbr */
99 static short outputlinelen
= OUTPUTLINELEN
;
100 static short groupreply
= 0; /* Is this a group reply? */
102 static int mime
= 0; /* include original as MIME part */
103 static char *form
= NULL
; /* form (components) file */
104 static char *filter
= NULL
; /* message filter file */
105 static char *fcc
= NULL
; /* folders to add to Fcc: header */
111 static void docc (char *, int);
112 static void add_convert_header (const char *, char *, char *, char *);
116 main (int argc
, char **argv
)
119 int anot
= 0, inplace
= 1;
120 int nedit
= 0, nwhat
= 0;
123 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
124 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
125 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
126 char **argp
, **arguments
;
127 svector_t convert_types
= svector_create (10);
128 svector_t convert_args
= svector_create (10);
130 struct msgs
*mp
= NULL
;
136 if (nmh_init(argv
[0], 1)) { return 1; }
138 arguments
= getarguments (invo_name
, argc
, argv
, 1);
141 while ((cp
= *argp
++)) {
143 switch (smatch (++cp
, switches
)) {
145 ambigsw (cp
, switches
);
148 adios (NULL
, "-%s unknown", cp
);
151 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
153 print_help (buf
, switches
, 1);
156 print_version(invo_name
);
174 if (!(cp
= *argp
++) || *cp
== '-')
175 adios (NULL
, "missing argument to %s", argp
[-2]);
179 if (!(cp
= *argp
++) || *cp
== '-')
180 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (!(ed
= *argp
++) || *ed
== '-')
186 adios (NULL
, "missing argument to %s", argp
[-2]);
197 if (!(type
= *argp
++)) {
198 adios (NULL
, "missing type argument to %s", argp
[-2]);
200 if (!(cp
= *argp
++)) {
201 adios (NULL
, "missing argstring argument to %s",
205 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
206 if (! strcmp (svector_at (convert_types
, i
), type
)) {
207 /* Already saw this type, so just update
209 svector_strs (convert_args
)[i
] = cp
;
214 if (i
== svector_size (convert_types
)) {
215 svector_push_back (convert_types
, type
);
216 svector_push_back (convert_args
, cp
);
222 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
223 adios (NULL
, "missing argument to %s", argp
[-2]);
234 if (!(cp
= *argp
++) || *cp
== '-')
235 adios (NULL
, "missing argument to %s", argp
[-2]);
238 cp
= dp
= path (cp
+ 1, TSUBCWF
);
240 fcc
= add (", ", fcc
);
247 adios (NULL
, "only one file at a time!");
248 if (!(cp
= *argp
++) || *cp
== '-')
249 adios (NULL
, "missing argument to %s", argp
[-2]);
250 file
= path (cp
, TFILE
);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 adios (NULL
, "missing argument to %s", argp
[-2]);
255 filter
= getcpy (etcpath (cp
));
259 if (!(form
= *argp
++) || *form
== '-')
260 adios (NULL
, "missing argument to %s", argp
[-2]);
264 filter
= getcpy (etcpath (mhlreply
));
294 if (!(cp
= *argp
++) || *cp
== '-')
295 adios (NULL
, "missing argument to %s", argp
[-2]);
296 if ((outputlinelen
= atoi (cp
)) < 10)
297 adios (NULL
, "impossible width %d", outputlinelen
);
302 adios (NULL
, "only one draft folder at a time!");
303 if (!(cp
= *argp
++) || *cp
== '-')
304 adios (NULL
, "missing argument to %s", argp
[-2]);
305 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
306 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
310 adios (NULL
, "only one draft message at a time!");
311 if (!(dmsg
= *argp
++) || *dmsg
== '-')
312 adios (NULL
, "missing argument to %s", argp
[-2]);
327 if (!(formatproc
= *argp
++) || *formatproc
== '-')
328 adios (NULL
, "missing argument to %s", argp
[-2]);
336 if (*cp
== '+' || *cp
== '@') {
338 adios (NULL
, "only one folder at a time!");
340 folder
= pluspath (cp
);
343 adios (NULL
, "only one message at a time!");
356 cwd
= mh_xstrdup(pwd ());
358 if (!context_find ("path"))
359 free (path ("./", TFOLDER
));
360 if (file
&& (msg
|| folder
))
361 adios (NULL
, "can't mix files and folders/msgs");
365 strncpy (drft
, buildsw
? m_maildir ("reply")
366 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
368 /* Check if a draft exists */
369 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
370 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
371 for (i
= LISTDSW
; i
!= YESW
;) {
372 if (!(argp
= read_switch_multiword ("\nDisposition? ",
373 isdf
? aqrnl
: aqrl
)))
375 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
384 showfile (++argp
, drft
);
387 if (refile (++argp
, drft
) == 0)
399 * We are replying to a file.
401 anot
= 0; /* we don't want to annotate a file */
404 * We are replying to a message.
409 folder
= getfolder (1);
410 maildir
= m_maildir (folder
);
412 if (chdir (maildir
) == NOTOK
)
413 adios (maildir
, "unable to change directory to");
415 /* read folder and create message structure */
416 if (!(mp
= folder_read (folder
, 1)))
417 adios (NULL
, "unable to read folder %s", folder
);
419 /* check for empty folder */
421 adios (NULL
, "no messages in %s", folder
);
423 /* parse the message range/sequence/name and set SELECTED */
424 if (!m_convert (mp
, msg
))
426 seq_setprev (mp
); /* set the previous-sequence */
429 adios (NULL
, "only one message at a time!");
431 context_replace (pfolder
, folder
); /* update current folder */
432 seq_setcur (mp
, mp
->lowsel
); /* update current message */
433 seq_save (mp
); /* synchronize sequences */
434 context_save (); /* save the context file */
437 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
439 if ((in
= fopen (msg
, "r")) == NULL
)
440 adios (msg
, "unable to open");
442 /* find form (components) file */
445 form
= etcpath (replgroupcomps
);
447 form
= etcpath (replcomps
);
450 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
455 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
457 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
458 add_convert_header (svector_at (convert_types
, n
),
459 svector_at (convert_args
, n
),
469 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
470 inplace
, cwd
, atfile
);
472 svector_free (convert_args
);
473 svector_free (convert_types
);
480 docc (char *cp
, int ccflag
)
482 switch (smatch (cp
, ccswitches
)) {
484 ambigsw (cp
, ccswitches
);
487 adios (NULL
, "-%scc %s unknown", ccflag
? "" : "no", cp
);
502 ccto
= cccc
= ccme
= ccflag
;
508 * Add pseudoheaders that will pass the convert arguments to
509 * mhbuild. They have the form:
510 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
511 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
512 * The ARGS pseudoheader is optional, but we always add it when
513 * -convertargs is used.
516 add_convert_header (const char *convert_type
, char *convert_arg
,
517 char *filename
, char *drft
) {
520 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
521 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
524 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
525 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);