3 * repl.c -- reply to a message
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
16 static struct swit switches[] = {
26 { "cc all|to|cc|me", 0 },
30 { "draftfolder +folder", 0 },
32 { "draftmessage msg", 0 },
34 { "nodraftfolder", 0 },
36 { "editor editor", 0 },
42 { "filter filterfile", 0 },
44 { "form formfile", 0 },
62 { "whatnowproc program", 0 },
64 { "nowhatnowproc", 0 },
66 { "width columns", 0 },
72 { "file file", 4 }, /* interface from msh */
76 { "build", 5 }, /* interface from mhe */
82 static struct swit ccswitches[] = {
94 static struct swit aqrnl[] = {
102 { "refile +folder", 0 },
108 static struct swit aqrl[] = {
112 { "refile +folder", 0 },
116 short ccto = -1; /* global for replsbr */
121 short outputlinelen = OUTPUTLINELEN;
122 short groupreply = 0; /* Is this a group reply? */
124 int mime = 0; /* include original as MIME part */
125 char *form = NULL; /* form (components) file */
126 char *filter = NULL; /* message filter file */
127 char *fcc = NULL; /* folders to add to Fcc: header */
133 void docc (char *, int);
137 main (int argc, char **argv)
140 int anot = 0, inplace = 1;
141 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;
155 setlocale(LC_ALL, "");
157 invo_name = r1bindex (argv[0], '/');
159 /* read user profile/context */
162 arguments = getarguments (invo_name, argc, argv, 1);
165 while ((cp = *argp++)) {
167 switch (smatch (++cp, switches)) {
169 ambigsw (cp, switches);
172 adios (NULL, "-%s unknown", cp);
175 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
177 print_help (buf, switches, 1);
180 print_version(invo_name);
198 if (!(cp = *argp++) || *cp == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
203 if (!(cp = *argp++) || *cp == '-')
204 adios (NULL, "missing argument to %s", argp[-2]);
209 if (!(ed = *argp++) || *ed == '-')
210 adios (NULL, "missing argument to %s", argp[-2]);
218 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
219 adios (NULL, "missing argument to %s", argp[-2]);
224 buildsw++; /* fall... */
231 if (!(cp = *argp++) || *cp == '-')
232 adios (NULL, "missing argument to %s", argp[-2]);
235 cp = dp = path (cp + 1, TSUBCWF);
237 fcc = add (", ", fcc);
245 adios (NULL, "only one file at a time!");
246 if (!(cp = *argp++) || *cp == '-')
247 adios (NULL, "missing argument to %s", argp[-2]);
248 file = path (cp, TFILE);
251 if (!(cp = *argp++) || *cp == '-')
252 adios (NULL, "missing argument to %s", argp[-2]);
253 filter = getcpy (etcpath (cp));
257 if (!(form = *argp++) || *form == '-')
258 adios (NULL, "missing argument to %s", argp[-2]);
262 filter = getcpy (etcpath (mhlreply));
292 if (!(cp = *argp++) || *cp == '-')
293 adios (NULL, "missing argument to %s", argp[-2]);
294 if ((outputlinelen = atoi (cp)) < 10)
295 adios (NULL, "impossible width %d", outputlinelen);
300 adios (NULL, "only one draft folder at a time!");
301 if (!(cp = *argp++) || *cp == '-')
302 adios (NULL, "missing argument to %s", argp[-2]);
303 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
304 *cp != '@' ? TFOLDER : TSUBCWF);
308 adios (NULL, "only one draft message at a time!");
309 if (!(dmsg = *argp++) || *dmsg == '-')
310 adios (NULL, "missing argument to %s", argp[-2]);
318 if (*cp == '+' || *cp == '@') {
320 adios (NULL, "only one folder at a time!");
322 folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF);
325 adios (NULL, "only one message at a time!");
338 cwd = getcpy (pwd ());
340 if (!context_find ("path"))
341 free (path ("./", TFOLDER));
342 if (file && (msg || folder))
343 adios (NULL, "can't mix files and folders/msgs");
348 strncpy (drft, buildsw ? m_maildir ("reply")
349 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
351 /* Check if a draft exists */
352 if (!buildsw && stat (drft, &st) != NOTOK) {
354 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
356 /* Check if a draft exists */
357 if (stat (drft, &st) != NOTOK) {
359 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
360 for (i = LISTDSW; i != YESW;) {
361 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
363 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
372 showfile (++argp, drft);
375 if (refile (++argp, drft) == 0)
379 advise (NULL, "say what?");
387 * We are replying to a file.
389 anot = 0; /* we don't want to annotate a file */
392 * We are replying to a message.
397 folder = getfolder (1);
398 maildir = m_maildir (folder);
400 if (chdir (maildir) == NOTOK)
401 adios (maildir, "unable to change directory to");
403 /* read folder and create message structure */
404 if (!(mp = folder_read (folder)))
405 adios (NULL, "unable to read folder %s", folder);
407 /* check for empty folder */
409 adios (NULL, "no messages in %s", folder);
411 /* parse the message range/sequence/name and set SELECTED */
412 if (!m_convert (mp, msg))
414 seq_setprev (mp); /* set the previous-sequence */
417 adios (NULL, "only one message at a time!");
419 context_replace (pfolder, folder); /* update current folder */
420 seq_setcur (mp, mp->lowsel); /* update current message */
421 seq_save (mp); /* synchronize sequences */
422 context_save (); /* save the context file */
425 msg = file ? file : getcpy (m_name (mp->lowsel));
427 if ((in = fopen (msg, "r")) == NULL)
428 adios (msg, "unable to open");
430 /* find form (components) file */
433 form = etcpath (replgroupcomps);
435 form = etcpath (replcomps);
438 replout (in, msg, drft, mp, outputlinelen, mime, form, filter, fcc);
443 what_now (ed, nedit, NOUSE, drft, msg, 0, mp,
444 anot ? "Replied" : NULL, inplace, cwd);
449 docc (char *cp, int ccflag)
451 switch (smatch (cp, ccswitches)) {
453 ambigsw (cp, ccswitches);
456 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
471 ccto = cccc = ccme = ccflag;