3 * repl.c -- reply to a message
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #define REPL_SWITCHES \
15 X("group", 0, GROUPSW) \
16 X("nogroup", 0, NGROUPSW) \
17 X("annotate", 0, ANNOSW) \
18 X("noannotate", 0, NANNOSW) \
19 X("cc all|to|cc|me", 0, CCSW) \
20 X("nocc all|to|cc|me", 0, NCCSW) \
21 X("draftfolder +folder", 0, DFOLDSW) \
22 X("draftmessage msg", 0, DMSGSW) \
23 X("nodraftfolder", 0, NDFLDSW) \
24 X("editor editor", 0, EDITRSW) \
25 X("noedit", 0, NEDITSW) \
26 X("convertargs type argstring", 0, CONVERTARGSW) \
27 X("fcc folder", 0, FCCSW) \
28 X("filter filterfile", 0, FILTSW) \
29 X("form formfile", 0, FORMSW) \
30 X("format", 5, FRMTSW) \
31 X("noformat", 7, NFRMTSW) \
32 X("inplace", 0, INPLSW) \
33 X("noinplace", 0, NINPLSW) \
34 X("mime", 0, MIMESW) \
35 X("nomime", 0, NMIMESW) \
36 X("query", 0, QURYSW) \
37 X("noquery", 0, NQURYSW) \
38 X("whatnowproc program", 0, WHATSW) \
39 X("nowhatnowproc", 0, NWHATSW) \
40 X("width columns", 0, WIDTHSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
43 X("file file", 4, FILESW) /* interface from msh */ \
44 X("build", 5, BILDSW) /* interface from mhe */ \
45 X("atfile", 0, ATFILESW) \
46 X("noatfile", 0, NOATFILESW) \
47 X("fmtproc program", 0, FMTPROCSW) \
48 X("nofmtproc", 0, NFMTPROCSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(REPL
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(REPL
, switches
);
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(CC
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(CC
, ccswitches
);
72 #define DISPO_SWITCHES \
74 X("replace", 0, YESW) \
75 X("list", 0, LISTDSW) \
76 X("refile +folder", 0, REFILSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(DISPO
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(DISPO
, aqrnl
);
87 static struct swit aqrl
[] = {
89 { "replace", 0, YESW
},
90 { "list", 0, LISTDSW
},
91 { "refile +folder", 0, REFILSW
},
95 short ccto
= -1; /* global for replsbr */
100 static short outputlinelen
= OUTPUTLINELEN
;
101 static short groupreply
= 0; /* Is this a group reply? */
103 static int mime
= 0; /* include original as MIME part */
104 static char *form
= NULL
; /* form (components) file */
105 static char *filter
= NULL
; /* message filter file */
106 static char *fcc
= NULL
; /* folders to add to Fcc: header */
112 static void docc (char *, int);
113 static void add_convert_header (const char *, char *, char *, char *);
117 main (int argc
, char **argv
)
120 int anot
= 0, inplace
= 1;
121 int nedit
= 0, nwhat
= 0;
124 char *cp
, *cwd
, *dp
, *maildir
, *file
= NULL
;
125 char *folder
= NULL
, *msg
= NULL
, *dfolder
= NULL
;
126 char *dmsg
= NULL
, *ed
= NULL
, drft
[BUFSIZ
], buf
[BUFSIZ
];
127 char **argp
, **arguments
;
128 svector_t convert_types
= svector_create (10);
129 svector_t convert_args
= svector_create (10);
131 struct msgs
*mp
= NULL
;
137 if (nmh_init(argv
[0], 1)) { return 1; }
139 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 while ((cp
= *argp
++)) {
144 switch (smatch (++cp
, switches
)) {
146 ambigsw (cp
, switches
);
149 adios (NULL
, "-%s unknown", cp
);
152 snprintf (buf
, sizeof(buf
), "%s: [+folder] [msg] [switches]",
154 print_help (buf
, switches
, 1);
157 print_version(invo_name
);
175 if (!(cp
= *argp
++) || *cp
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
180 if (!(cp
= *argp
++) || *cp
== '-')
181 adios (NULL
, "missing argument to %s", argp
[-2]);
186 if (!(ed
= *argp
++) || *ed
== '-')
187 adios (NULL
, "missing argument to %s", argp
[-2]);
198 if (!(type
= *argp
++)) {
199 adios (NULL
, "missing type argument to %s", argp
[-2]);
201 if (!(cp
= *argp
++)) {
202 adios (NULL
, "missing argstring argument to %s",
206 for (i
= 0; i
< svector_size (convert_types
); ++i
) {
207 if (! strcmp (svector_at (convert_types
, i
), type
)) {
208 /* Already saw this type, so just update
210 svector_strs (convert_args
)[i
] = cp
;
215 if (i
== svector_size (convert_types
)) {
216 svector_push_back (convert_types
, type
);
217 svector_push_back (convert_args
, cp
);
223 if (!(whatnowproc
= *argp
++) || *whatnowproc
== '-')
224 adios (NULL
, "missing argument to %s", argp
[-2]);
228 buildsw
++; /* fall... */
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
);
248 adios (NULL
, "only one file at a time!");
249 if (!(cp
= *argp
++) || *cp
== '-')
250 adios (NULL
, "missing argument to %s", argp
[-2]);
251 file
= path (cp
, TFILE
);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 adios (NULL
, "missing argument to %s", argp
[-2]);
256 filter
= getcpy (etcpath (cp
));
260 if (!(form
= *argp
++) || *form
== '-')
261 adios (NULL
, "missing argument to %s", argp
[-2]);
265 filter
= getcpy (etcpath (mhlreply
));
295 if (!(cp
= *argp
++) || *cp
== '-')
296 adios (NULL
, "missing argument to %s", argp
[-2]);
297 if ((outputlinelen
= atoi (cp
)) < 10)
298 adios (NULL
, "impossible width %d", outputlinelen
);
303 adios (NULL
, "only one draft folder at a time!");
304 if (!(cp
= *argp
++) || *cp
== '-')
305 adios (NULL
, "missing argument to %s", argp
[-2]);
306 dfolder
= path (*cp
== '+' || *cp
== '@' ? cp
+ 1 : cp
,
307 *cp
!= '@' ? TFOLDER
: TSUBCWF
);
311 adios (NULL
, "only one draft message at a time!");
312 if (!(dmsg
= *argp
++) || *dmsg
== '-')
313 adios (NULL
, "missing argument to %s", argp
[-2]);
328 if (!(formatproc
= *argp
++) || *formatproc
== '-')
329 adios (NULL
, "missing argument to %s", argp
[-2]);
337 if (*cp
== '+' || *cp
== '@') {
339 adios (NULL
, "only one folder at a time!");
341 folder
= pluspath (cp
);
344 adios (NULL
, "only one message at a time!");
357 cwd
= getcpy (pwd ());
359 if (!context_find ("path"))
360 free (path ("./", TFOLDER
));
361 if (file
&& (msg
|| folder
))
362 adios (NULL
, "can't mix files and folders/msgs");
366 strncpy (drft
, buildsw
? m_maildir ("reply")
367 : m_draft (dfolder
, NULL
, NOUSE
, &isdf
), sizeof(drft
));
369 /* Check if a draft exists */
370 if (!buildsw
&& stat (drft
, &st
) != NOTOK
) {
371 printf ("Draft \"%s\" exists (%ld bytes).", drft
, (long) st
.st_size
);
372 for (i
= LISTDSW
; i
!= YESW
;) {
373 if (!(argp
= read_switch_multiword ("\nDisposition? ",
374 isdf
? aqrnl
: aqrl
)))
376 switch (i
= smatch (*argp
, isdf
? aqrnl
: aqrl
)) {
385 showfile (++argp
, drft
);
388 if (refile (++argp
, drft
) == 0)
392 advise (NULL
, "say what?");
400 * We are replying to a file.
402 anot
= 0; /* we don't want to annotate a file */
405 * We are replying to a message.
410 folder
= getfolder (1);
411 maildir
= m_maildir (folder
);
413 if (chdir (maildir
) == NOTOK
)
414 adios (maildir
, "unable to change directory to");
416 /* read folder and create message structure */
417 if (!(mp
= folder_read (folder
, 1)))
418 adios (NULL
, "unable to read folder %s", folder
);
420 /* check for empty folder */
422 adios (NULL
, "no messages in %s", folder
);
424 /* parse the message range/sequence/name and set SELECTED */
425 if (!m_convert (mp
, msg
))
427 seq_setprev (mp
); /* set the previous-sequence */
430 adios (NULL
, "only one message at a time!");
432 context_replace (pfolder
, folder
); /* update current folder */
433 seq_setcur (mp
, mp
->lowsel
); /* update current message */
434 seq_save (mp
); /* synchronize sequences */
435 context_save (); /* save the context file */
438 msg
= file
? file
: getcpy (m_name (mp
->lowsel
));
440 if ((in
= fopen (msg
, "r")) == NULL
)
441 adios (msg
, "unable to open");
443 /* find form (components) file */
446 form
= etcpath (replgroupcomps
);
448 form
= etcpath (replcomps
);
451 replout (in
, msg
, drft
, mp
, outputlinelen
, mime
, form
, filter
,
456 char *filename
= concat (mp
->foldpath
, "/", msg
, NULL
);
458 for (n
= 0; n
< svector_size (convert_types
); ++n
) {
459 add_convert_header (svector_at (convert_types
, n
),
460 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);