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 */
78 { "fmtproc program", 0 },
85 static struct swit ccswitches[] = {
97 static struct swit aqrnl[] = {
105 { "refile +folder", 0 },
111 static struct swit aqrl[] = {
115 { "refile +folder", 0 },
119 short ccto = -1; /* global for replsbr */
124 short outputlinelen = OUTPUTLINELEN;
125 short groupreply = 0; /* Is this a group reply? */
127 int mime = 0; /* include original as MIME part */
128 char *form = NULL; /* form (components) file */
129 char *filter = NULL; /* message filter file */
130 char *fcc = NULL; /* folders to add to Fcc: header */
136 void docc (char *, int);
140 main (int argc, char **argv)
143 int anot = 0, inplace = 1;
144 int nedit = 0, nwhat = 0;
147 char *cp, *cwd, *dp, *maildir, *file = NULL;
148 char *folder = NULL, *msg = NULL, *dfolder = NULL;
149 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
150 char **argp, **arguments;
151 struct msgs *mp = NULL;
158 setlocale(LC_ALL, "");
160 invo_name = r1bindex (argv[0], '/');
162 /* read user profile/context */
165 arguments = getarguments (invo_name, argc, argv, 1);
168 while ((cp = *argp++)) {
170 switch (smatch (++cp, switches)) {
172 ambigsw (cp, switches);
175 adios (NULL, "-%s unknown", cp);
178 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
180 print_help (buf, switches, 1);
183 print_version(invo_name);
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
206 if (!(cp = *argp++) || *cp == '-')
207 adios (NULL, "missing argument to %s", argp[-2]);
212 if (!(ed = *argp++) || *ed == '-')
213 adios (NULL, "missing argument to %s", argp[-2]);
221 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
222 adios (NULL, "missing argument to %s", argp[-2]);
226 buildsw++; /* fall... */
232 if (!(cp = *argp++) || *cp == '-')
233 adios (NULL, "missing argument to %s", argp[-2]);
236 cp = dp = path (cp + 1, TSUBCWF);
238 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 = getcpy (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 = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
373 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
382 showfile (++argp, drft);
385 if (refile (++argp, drft) == 0)
389 advise (NULL, "say what?");
397 * We are replying to a file.
399 anot = 0; /* we don't want to annotate a file */
402 * We are replying to a message.
407 folder = getfolder (1);
408 maildir = m_maildir (folder);
410 if (chdir (maildir) == NOTOK)
411 adios (maildir, "unable to change directory to");
413 /* read folder and create message structure */
414 if (!(mp = folder_read (folder)))
415 adios (NULL, "unable to read folder %s", folder);
417 /* check for empty folder */
419 adios (NULL, "no messages in %s", folder);
421 /* parse the message range/sequence/name and set SELECTED */
422 if (!m_convert (mp, msg))
424 seq_setprev (mp); /* set the previous-sequence */
427 adios (NULL, "only one message at a time!");
429 context_replace (pfolder, folder); /* update current folder */
430 seq_setcur (mp, mp->lowsel); /* update current message */
431 seq_save (mp); /* synchronize sequences */
432 context_save (); /* save the context file */
435 msg = file ? file : getcpy (m_name (mp->lowsel));
437 if ((in = fopen (msg, "r")) == NULL)
438 adios (msg, "unable to open");
440 /* find form (components) file */
443 form = etcpath (replgroupcomps);
445 form = etcpath (replcomps);
448 replout (in, msg, drft, mp, outputlinelen, mime, form, filter,
454 what_now (ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
455 inplace, cwd, atfile);
461 docc (char *cp, int ccflag)
463 switch (smatch (cp, ccswitches)) {
465 ambigsw (cp, ccswitches);
468 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
483 ccto = cccc = ccme = ccflag;