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 static struct swit switches[] = {
24 { "cc all|to|cc|me", 0 },
28 { "draftfolder +folder", 0 },
30 { "draftmessage msg", 0 },
32 { "nodraftfolder", 0 },
34 { "editor editor", 0 },
40 { "filter filterfile", 0 },
42 { "form formfile", 0 },
60 { "whatnowproc program", 0 },
62 { "nowhatnowproc", 0 },
64 { "width columns", 0 },
70 { "file file", 4 }, /* interface from msh */
72 { "build", 5 }, /* interface from mhe */
81 static struct swit ccswitches[] = {
93 static struct swit aqrnl[] = {
101 { "refile +folder", 0 },
107 static struct swit aqrl[] = {
111 { "refile +folder", 0 },
115 short ccto = -1; /* global for replsbr */
120 short outputlinelen = OUTPUTLINELEN;
121 short groupreply = 0; /* Is this a group reply? */
123 int mime = 0; /* include original as MIME part */
124 char *form = NULL; /* form (components) file */
125 char *filter = NULL; /* message filter file */
126 char *fcc = NULL; /* folders to add to Fcc: header */
132 void docc (char *, int);
136 main (int argc, char **argv)
139 int anot = 0, inplace = 1;
140 int nedit = 0, nwhat = 0;
142 char *cp, *cwd, *dp, *maildir, *file = NULL;
143 char *folder = NULL, *msg = NULL, *dfolder = NULL;
144 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
145 char **argp, **arguments;
146 struct msgs *mp = NULL;
153 setlocale(LC_ALL, "");
155 invo_name = r1bindex (argv[0], '/');
157 /* read user profile/context */
160 arguments = getarguments (invo_name, argc, argv, 1);
163 while ((cp = *argp++)) {
165 switch (smatch (++cp, switches)) {
167 ambigsw (cp, switches);
170 adios (NULL, "-%s unknown", cp);
173 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
175 print_help (buf, switches, 1);
178 print_version(invo_name);
196 if (!(cp = *argp++) || *cp == '-')
197 adios (NULL, "missing argument to %s", argp[-2]);
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
207 if (!(ed = *argp++) || *ed == '-')
208 adios (NULL, "missing argument to %s", argp[-2]);
216 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
217 adios (NULL, "missing argument to %s", argp[-2]);
221 buildsw++; /* fall... */
227 if (!(cp = *argp++) || *cp == '-')
228 adios (NULL, "missing argument to %s", argp[-2]);
231 cp = dp = path (cp + 1, TSUBCWF);
233 fcc = add (", ", fcc);
241 adios (NULL, "only one file at a time!");
242 if (!(cp = *argp++) || *cp == '-')
243 adios (NULL, "missing argument to %s", argp[-2]);
244 file = path (cp, TFILE);
247 if (!(cp = *argp++) || *cp == '-')
248 adios (NULL, "missing argument to %s", argp[-2]);
249 filter = getcpy (etcpath (cp));
253 if (!(form = *argp++) || *form == '-')
254 adios (NULL, "missing argument to %s", argp[-2]);
258 filter = getcpy (etcpath (mhlreply));
288 if (!(cp = *argp++) || *cp == '-')
289 adios (NULL, "missing argument to %s", argp[-2]);
290 if ((outputlinelen = atoi (cp)) < 10)
291 adios (NULL, "impossible width %d", outputlinelen);
296 adios (NULL, "only one draft folder at a time!");
297 if (!(cp = *argp++) || *cp == '-')
298 adios (NULL, "missing argument to %s", argp[-2]);
299 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
300 *cp != '@' ? TFOLDER : TSUBCWF);
304 adios (NULL, "only one draft message at a time!");
305 if (!(dmsg = *argp++) || *dmsg == '-')
306 adios (NULL, "missing argument to %s", argp[-2]);
321 if (*cp == '+' || *cp == '@') {
323 adios (NULL, "only one folder at a time!");
325 folder = pluspath (cp);
328 adios (NULL, "only one message at a time!");
341 cwd = getcpy (pwd ());
343 if (!context_find ("path"))
344 free (path ("./", TFOLDER));
345 if (file && (msg || folder))
346 adios (NULL, "can't mix files and folders/msgs");
350 strncpy (drft, buildsw ? m_maildir ("reply")
351 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
353 /* Check if a draft exists */
354 if (!buildsw && stat (drft, &st) != NOTOK) {
355 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
356 for (i = LISTDSW; i != YESW;) {
357 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
359 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
368 showfile (++argp, drft);
371 if (refile (++argp, drft) == 0)
375 advise (NULL, "say what?");
383 * We are replying to a file.
385 anot = 0; /* we don't want to annotate a file */
388 * We are replying to a message.
393 folder = getfolder (1);
394 maildir = m_maildir (folder);
396 if (chdir (maildir) == NOTOK)
397 adios (maildir, "unable to change directory to");
399 /* read folder and create message structure */
400 if (!(mp = folder_read (folder)))
401 adios (NULL, "unable to read folder %s", folder);
403 /* check for empty folder */
405 adios (NULL, "no messages in %s", folder);
407 /* parse the message range/sequence/name and set SELECTED */
408 if (!m_convert (mp, msg))
410 seq_setprev (mp); /* set the previous-sequence */
413 adios (NULL, "only one message at a time!");
415 context_replace (pfolder, folder); /* update current folder */
416 seq_setcur (mp, mp->lowsel); /* update current message */
417 seq_save (mp); /* synchronize sequences */
418 context_save (); /* save the context file */
421 msg = file ? file : getcpy (m_name (mp->lowsel));
423 if ((in = fopen (msg, "r")) == NULL)
424 adios (msg, "unable to open");
426 /* find form (components) file */
429 form = etcpath (replgroupcomps);
431 form = etcpath (replcomps);
434 replout (in, msg, drft, mp, outputlinelen, mime, form, filter, fcc);
439 what_now (ed, nedit, NOUSE, drft, atfile ? msg : NULL, 0, mp,
440 anot ? "Replied" : NULL, inplace, cwd);
446 docc (char *cp, int ccflag)
448 switch (smatch (cp, ccswitches)) {
450 ambigsw (cp, ccswitches);
453 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
468 ccto = cccc = ccme = ccflag;