2 * repl.c -- reply to a message
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 static struct swit switches[] = {
23 { "cc all|to|cc|me", 0 },
27 { "draftfolder +folder", 0 },
29 { "draftmessage msg", 0 },
31 { "nodraftfolder", 0 },
33 { "editor editor", 0 },
39 { "filter filterfile", 0 },
41 { "form formfile", 0 },
59 { "whatnowproc program", 0 },
61 { "nowhatnowproc", 0 },
63 { "width columns", 0 },
69 { "file file", 4 }, /* interface from msh */
73 { "build", 5 }, /* interface from mhe */
79 static struct swit ccswitches[] = {
91 static struct swit aqrnl[] = {
99 { "refile +folder", 0 },
105 static struct swit aqrl[] = {
109 { "refile +folder", 0 },
113 short ccto = -1; /* global for replsbr */
118 short outputlinelen = OUTPUTLINELEN;
119 short groupreply = 0; /* Is this a group reply? */
121 int mime = 0; /* include original as MIME part */
122 char *form = NULL; /* form (components) file */
123 char *filter = NULL; /* message filter file */
124 char *fcc = NULL; /* folders to add to Fcc: header */
130 void docc (char *, int);
134 main (int argc, char **argv)
137 int anot = 0, inplace = 1;
138 int nedit = 0, nwhat = 0;
139 char *cp, *cwd, *dp, *maildir, *file = NULL;
140 char *folder = NULL, *msg = NULL, *dfolder = NULL;
141 char *dmsg = NULL, *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
142 char **argp, **arguments;
143 struct msgs *mp = NULL;
152 setlocale(LC_ALL, "");
154 invo_name = r1bindex (argv[0], '/');
156 /* read user profile/context */
159 arguments = getarguments (invo_name, argc, argv, 1);
162 while ((cp = *argp++)) {
164 switch (smatch (++cp, switches)) {
166 ambigsw (cp, switches);
169 adios (NULL, "-%s unknown", cp);
172 snprintf (buf, sizeof(buf), "%s: [+folder] [msg] [switches]",
174 print_help (buf, switches, 1);
177 print_version(invo_name);
195 if (!(cp = *argp++) || *cp == '-')
196 adios (NULL, "missing argument to %s", argp[-2]);
200 if (!(cp = *argp++) || *cp == '-')
201 adios (NULL, "missing argument to %s", argp[-2]);
206 if (!(ed = *argp++) || *ed == '-')
207 adios (NULL, "missing argument to %s", argp[-2]);
215 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
216 adios (NULL, "missing argument to %s", argp[-2]);
221 buildsw++; /* fall... */
228 if (!(cp = *argp++) || *cp == '-')
229 adios (NULL, "missing argument to %s", argp[-2]);
232 cp = dp = path (cp + 1, TSUBCWF);
234 fcc = add (", ", fcc);
242 adios (NULL, "only one file at a time!");
243 if (!(cp = *argp++) || *cp == '-')
244 adios (NULL, "missing argument to %s", argp[-2]);
245 file = path (cp, TFILE);
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
250 filter = getcpy (etcpath (cp));
254 if (!(form = *argp++) || *form == '-')
255 adios (NULL, "missing argument to %s", argp[-2]);
259 filter = getcpy (etcpath (mhlreply));
289 if (!(cp = *argp++) || *cp == '-')
290 adios (NULL, "missing argument to %s", argp[-2]);
291 if ((outputlinelen = atoi (cp)) < 10)
292 adios (NULL, "impossible width %d", outputlinelen);
297 adios (NULL, "only one draft folder at a time!");
298 if (!(cp = *argp++) || *cp == '-')
299 adios (NULL, "missing argument to %s", argp[-2]);
300 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
301 *cp != '@' ? TFOLDER : TSUBCWF);
305 adios (NULL, "only one draft message at a time!");
306 if (!(dmsg = *argp++) || *dmsg == '-')
307 adios (NULL, "missing argument to %s", argp[-2]);
315 if (*cp == '+' || *cp == '@') {
317 adios (NULL, "only one folder at a time!");
319 folder = pluspath (cp);
322 adios (NULL, "only one message at a time!");
335 cwd = getcpy (pwd ());
337 if (!context_find ("path"))
338 free (path ("./", TFOLDER));
339 if (file && (msg || folder))
340 adios (NULL, "can't mix files and folders/msgs");
345 strncpy (drft, buildsw ? m_maildir ("reply")
346 : m_draft (dfolder, NULL, NOUSE, &isdf), sizeof(drft));
348 /* Check if a draft exists */
349 if (!buildsw && stat (drft, &st) != NOTOK) {
351 strncpy (drft, m_draft (dfolder, dmsg, NOUSE, &isdf), sizeof(drft));
353 /* Check if a draft exists */
354 if (stat (drft, &st) != NOTOK) {
356 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
357 for (i = LISTDSW; i != YESW;) {
358 if (!(argp = getans ("\nDisposition? ", isdf ? aqrnl : aqrl)))
360 switch (i = smatch (*argp, isdf ? aqrnl : aqrl)) {
369 showfile (++argp, drft);
372 if (refile (++argp, drft) == 0)
376 advise (NULL, "say what?");
384 * We are replying to a file.
386 anot = 0; /* we don't want to annotate a file */
389 * We are replying to a message.
394 folder = getfolder (1);
395 maildir = m_maildir (folder);
397 if (chdir (maildir) == NOTOK)
398 adios (maildir, "unable to change directory to");
400 /* read folder and create message structure */
401 if (!(mp = folder_read (folder)))
402 adios (NULL, "unable to read folder %s", folder);
404 /* check for empty folder */
406 adios (NULL, "no messages in %s", folder);
408 /* parse the message range/sequence/name and set SELECTED */
409 if (!m_convert (mp, msg))
411 seq_setprev (mp); /* set the previous-sequence */
414 adios (NULL, "only one message at a time!");
416 context_replace (pfolder, folder); /* update current folder */
417 seq_setcur (mp, mp->lowsel); /* update current message */
418 seq_save (mp); /* synchronize sequences */
419 context_save (); /* save the context file */
422 msg = file ? file : getcpy (m_name (mp->lowsel));
424 if ((in = fopen (msg, "r")) == NULL)
425 adios (msg, "unable to open");
427 /* find form (components) file */
430 form = etcpath (replgroupcomps);
432 form = etcpath (replcomps);
435 replout (in, msg, drft, mp, outputlinelen, mime, form, filter, fcc);
440 what_now (ed, nedit, NOUSE, drft, msg, 0, mp,
441 anot ? "Replied" : NULL, inplace, cwd);
447 docc (char *cp, int ccflag)
449 switch (smatch (cp, ccswitches)) {
451 ambigsw (cp, ccswitches);
454 adios (NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
469 ccto = cccc = ccme = ccflag;