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 #define REPL_SWITCHES \
13 X("group", 0, GROUPSW) \
14 X("nogroup", 0, NGROUPSW) \
15 X("annotate", 0, ANNOSW) \
16 X("noannotate", 0, NANNOSW) \
17 X("cc all|to|cc|me", 0, CCSW) \
18 X("nocc all|to|cc|me", 0, NCCSW) \
19 X("draftfolder +folder", 0, DFOLDSW) \
20 X("draftmessage msg", 0, DMSGSW) \
21 X("nodraftfolder", 0, NDFLDSW) \
22 X("editor editor", 0, EDITRSW) \
23 X("noedit", 0, NEDITSW) \
24 X("convertargs type argstring", 0, CONVERTARGSW) \
25 X("fcc folder", 0, FCCSW) \
26 X("filter filterfile", 0, FILTSW) \
27 X("form formfile", 0, FORMSW) \
28 X("format", 5, FRMTSW) \
29 X("noformat", 7, NFRMTSW) \
30 X("inplace", 0, INPLSW) \
31 X("noinplace", 0, NINPLSW) \
32 X("mime", 0, MIMESW) \
33 X("nomime", 0, NMIMESW) \
34 X("query", 0, QURYSW) \
35 X("noquery", 0, NQURYSW) \
36 X("whatnowproc program", 0, WHATSW) \
37 X("nowhatnowproc", 0, NWHATSW) \
38 X("width columns", 0, WIDTHSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41 X("file file", 4, FILESW) /* interface from msh */ \
42 X("build", 5, BILDSW) /* interface from mhe */ \
43 X("atfile", 0, ATFILESW) \
44 X("noatfile", 0, NOATFILESW) \
45 X("fmtproc program", 0, FMTPROCSW) \
46 X("nofmtproc", 0, NFMTPROCSW) \
48 #define X(sw, minchars, id) id,
49 DEFINE_SWITCH_ENUM(REPL
);
52 #define X(sw, minchars, id) { sw, minchars, id },
53 DEFINE_SWITCH_ARRAY(REPL
, switches
);
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(CC
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
70 #define DISPO_SWITCHES \
72 X("replace", 0, YESW) \
73 X("list", 0, LISTDSW) \
74 X("refile +folder", 0, REFILSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(DISPO
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
85 static struct swit aqrl
[] = {
87 { "replace", 0, YESW
},
88 { "list", 0, LISTDSW
},
89 { "refile +folder", 0, REFILSW
},
93 short ccto
= -1; /* global for replsbr */
98 static short outputlinelen
= OUTPUTLINELEN
;
99 static short groupreply
= 0; /* Is this a group reply? */
101 static int mime
= 0; /* include original as MIME part */
102 static char *form
= NULL
; /* form (components) file */
103 static char *filter
= NULL
; /* message filter file */
104 static char *fcc
= NULL
; /* folders to add to Fcc: header */
110 static void docc (char *, int);
111 static void add_convert_header (const char *, char *, char *, char *);
115 main (int argc
, char **argv
)
118 int anot
= 0, inplace
= 1;
119 int nedit
= 0, nwhat
= 0;
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
;
135 if (nmh_init(argv
[0], 1)) { return 1; }
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 adios (NULL
, "-%s unknown", cp
);
150 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
173 if (!(cp
= *argp
++) || *cp
== '-')
174 adios (NULL
, "missing argument to %s", argp
[-2]);
178 if (!(cp
= *argp
++) || *cp
== '-')
179 adios (NULL
, "missing argument to %s", argp
[-2]);
184 if (!(ed
= *argp
++) || *ed
== '-')
185 adios (NULL
, "missing argument to %s", argp
[-2]);
196 if (!(type
= *argp
++)) {
197 adios (NULL
, "missing type argument to %s", argp
[-2]);
199 if (!(cp
= *argp
++)) {
200 adios (NULL
, "missing argstring argument to %s",
204 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
205 if (! strcmp (svector_at (convert_types
, i
), type
)) {
206 /* Already saw this type, so just update
208 svector_strs (convert_args
)[i
] = cp
;
213 if (i
== svector_size (convert_types
)) {
214 svector_push_back (convert_types
, type
);
215 svector_push_back (convert_args
, cp
);
221 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
222 adios (NULL
, "missing argument to %s", argp
[-2]);
233 if (!(cp
= *argp
++) || *cp
== '-')
234 adios (NULL
, "missing argument to %s", argp
[-2]);
237 cp
= dp
= path (cp
+ 1, TSUBCWF
);
239 fcc
= add (", ", fcc
);
246 adios (NULL
, "only one file at a time!");
247 if (!(cp
= *argp
++) || *cp
== '-')
248 adios (NULL
, "missing argument to %s", argp
[-2]);
249 file
= path (cp
, TFILE
);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 adios (NULL
, "missing argument to %s", argp
[-2]);
254 filter
= getcpy (etcpath (cp
));
258 if (!(form
= *argp
++) || *form
== '-')
259 adios (NULL
, "missing argument to %s", argp
[-2]);
263 filter
= getcpy (etcpath (mhlreply
));
293 if (!(cp
= *argp
++) || *cp
== '-')
294 adios (NULL
, "missing argument to %s", argp
[-2]);
295 if ((outputlinelen
= atoi (cp
)) < 10)
296 adios (NULL
, "impossible width %d", outputlinelen
);
301 adios (NULL
, "only one draft folder at a time!");
302 if (!(cp
= *argp
++) || *cp
== '-')
303 adios (NULL
, "missing argument to %s", argp
[-2]);
304 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
305 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
309 adios (NULL
, "only one draft message at a time!");
310 if (!(dmsg
= *argp
++) || *dmsg
== '-')
311 adios (NULL
, "missing argument to %s", argp
[-2]);
326 if (!(formatproc
= *argp
++) || *formatproc
== '-')
327 adios (NULL
, "missing argument to %s", argp
[-2]);
335 if (*cp
== '+' || *cp
== '@') {
337 adios (NULL
, "only one folder at a time!");
339 folder
= pluspath (cp
);
342 adios (NULL
, "only one message at a time!");
355 cwd
= mh_xstrdup(pwd ());
357 if (!context_find ("path"))
358 free (path ("./", TFOLDER
));
359 if (file
&& (msg
|| folder
))
360 adios (NULL
, "can't mix files and folders/msgs");
364 strncpy (drft
, buildsw
? m_maildir ("reply")
365 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
367 /* Check if a draft exists */
368 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
369 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
370 for (i
= LISTDSW
; i
!= YESW
;) {
371 if (!(argp
= read_switch_multiword ("\nDisposition? ",
372 isdf
? aqrnl
: aqrl
)))
374 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
383 showfile (++argp
, drft
);
386 if (refile (++argp
, drft
) == 0)
398 * We are replying to a file.
400 anot
= 0; /* we don't want to annotate a file */
403 * We are replying to a message.
408 folder
= getfolder (1);
409 maildir
= m_maildir (folder
);
411 if (chdir (maildir
) == NOTOK
)
412 adios (maildir
, "unable to change directory to");
414 /* read folder and create message structure */
415 if (!(mp
= folder_read (folder
, 1)))
416 adios (NULL
, "unable to read folder %s", folder
);
418 /* check for empty folder */
420 adios (NULL
, "no messages in %s", folder
);
422 /* parse the message range/sequence/name and set SELECTED */
423 if (!m_convert (mp
, msg
))
425 seq_setprev (mp
); /* set the previous-sequence */
428 adios (NULL
, "only one message at a time!");
430 context_replace (pfolder
, folder
); /* update current folder */
431 seq_setcur (mp
, mp
->lowsel
); /* update current message */
432 seq_save (mp
); /* synchronize sequences */
433 context_save (); /* save the context file */
436 msg
= file
? file
: mh_xstrdup(m_name (mp
->lowsel
));
438 if ((in
= fopen (msg
, "r")) == NULL
)
439 adios (msg
, "unable to open");
441 /* find form (components) file */
444 form
= etcpath (replgroupcomps
);
446 form
= etcpath (replcomps
);
449 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
454 char *filename
= file
? file
: concat (mp
->foldpath
, "/", msg
, NULL
);
456 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
457 add_convert_header (svector_at (convert_types
, n
),
458 svector_at (convert_args
, n
),
468 what_now (ed
, nedit
, NOUSE
, drft
, msg
, 0, mp
, anot
? "Replied" : NULL
,
469 inplace
, cwd
, atfile
);
471 svector_free (convert_args
);
472 svector_free (convert_types
);
479 docc (char *cp
, int ccflag
)
481 switch (smatch (cp
, ccswitches
)) {
483 ambigsw (cp
, ccswitches
);
486 adios (NULL
, "-%scc %s unknown", ccflag
? "" : "no", cp
);
501 ccto
= cccc
= ccme
= ccflag
;
507 * Add pseudoheaders that will pass the convert arguments to
508 * mhbuild. They have the form:
509 * MHBUILD_FILE_PSEUDOHEADER-text/calendar: /home/user/Mail/inbox/7
510 * MHBUILD_ARGS_PSEUDOHEADER-text/calendar: reply -accept
511 * The ARGS pseudoheader is optional, but we always add it when
512 * -convertargs is used.
515 add_convert_header (const char *convert_type
, char *convert_arg
,
516 char *filename
, char *drft
) {
519 field_name
= concat (MHBUILD_FILE_PSEUDOHEADER
, convert_type
, NULL
);
520 annotate (drft
, field_name
, filename
, 1, 0, -2, 1);
523 field_name
= concat (MHBUILD_ARGS_PSEUDOHEADER
, convert_type
, NULL
);
524 annotate (drft
, field_name
, convert_arg
, 1, 0, -2, 1);