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.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <sys/file.h> /* L_SET */
21 static struct swit switches[] = {
31 { "cc all|to|cc|me", 0 },
35 { "editor editor", 0 },
37 { "filter filterfile", 0 },
41 { "form formfile", 0 },
51 { "whatnowproc program", 0 },
57 { "file file", 4 }, /* interface from msh */
59 { "build", 5 }, /* interface from mhe */
63 static struct swit ccswitches[] = {
76 ** Buffer size for content part of header fields.
77 ** We want this to be large enough so that we don't
78 ** do a lot of extra FLDPLUS calls on m_getfld but
79 ** small enough so that we don't snarf the entire
80 ** message body when we're not going to use any of it.
84 static short ccto = -1;
85 static short cccc = -1;
86 static short ccme = -1;
87 static short querysw = 0;
89 static short groupreply = 0; /* Is this a group reply? */
91 static int mime = 0; /* include original as MIME part */
92 static char *form = NULL; /* form (components) file */
93 static char *filter = NULL; /* message filter file */
97 static char *badaddrs = NULL;
98 static char *dfhost = NULL;
100 static struct mailname mq;
102 static struct format *fmt;
104 static int ncomps = 0; /* # of interesting components */
105 static char **compbuffers = NULL; /* buffers for component text */
106 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
108 static int dat[5]; /* aux. data for format routine */
110 static char *addrcomps[] = {
129 static void docc(char *, int);
130 static int insert(struct mailname *);
131 static void replfilter(FILE *, FILE *, char *);
132 static void replout(FILE *, char *, struct msgs *, int,
137 main(int argc, char **argv)
140 char *cp, *cwd, *maildir, *file = NULL;
141 char *folder = NULL, *msg = NULL;
142 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
143 char **argp, **arguments;
144 struct msgs *mp = NULL;
148 setlocale(LC_ALL, "");
149 invo_name = mhbasename(argv[0]);
151 /* read user profile/context */
154 filter = mh_xstrdup(etcpath(mhlreply));
156 arguments = getarguments(invo_name, argc, argv, 1);
159 while ((cp = *argp++)) {
161 switch (smatch(++cp, switches)) {
163 ambigsw(cp, switches);
166 adios(EX_USAGE, NULL, "-%s unknown", cp);
169 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
170 print_help(buf, switches, 1);
171 exit(argc == 2 ? EX_OK : EX_USAGE);
173 print_version(invo_name);
174 exit(argc == 2 ? EX_OK : EX_USAGE);
191 if (!(cp = *argp++) || *cp == '-')
192 adios(EX_USAGE, NULL, "missing argument to %s",
197 if (!(cp = *argp++) || *cp == '-')
198 adios(EX_USAGE, NULL, "missing argument to %s",
204 if (!(ed = *argp++) || *ed == '-')
205 adios(EX_USAGE, NULL, "missing argument to %s",
210 if (!(whatnowproc = *argp++) ||
212 adios(EX_USAGE, NULL, "missing argument to %s",
222 adios(EX_USAGE, NULL, "only one file at a time!");
223 if (!(cp = *argp++) || *cp == '-')
224 adios(EX_USAGE, NULL, "missing argument to %s",
226 file = mh_xstrdup(expanddir(cp));
229 if (!(form = *argp++) || *form == '-')
230 adios(EX_USAGE, NULL, "missing argument to %s",
235 if (!(cp = *argp++) || *cp == '-')
236 adios(EX_USAGE, NULL, "missing argument to %s",
238 filter = mh_xstrdup(etcpath(cp));
260 if (*cp == '+' || *cp == '@') {
262 adios(EX_USAGE, NULL, "only one folder at a time!");
264 folder = mh_xstrdup(expandfol(cp));
267 adios(EX_USAGE, NULL, "only one message at a time!");
280 cwd = mh_xstrdup(pwd());
282 if (file && (msg || folder))
283 adios(EX_USAGE, NULL, "can't mix files and folders/msgs");
285 strncpy(drft, buildsw ? toabsdir("reply") : m_draft(seq_beyond),
288 ** FIXME: (concerning MHE support (buildsw) only)
289 ** There's no check if the draft already exists. mmh has removed
290 ** this case by having the draft folder. I won't add code only to
291 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
296 ** We are replying to a file.
298 anot = 0; /* we don't want to annotate a file */
301 ** We are replying to a message.
306 folder = getcurfol();
307 maildir = toabsdir(folder);
309 if (chdir(maildir) == NOTOK)
310 adios(EX_OSERR, maildir, "unable to change directory to");
312 /* read folder and create message structure */
313 if (!(mp = folder_read(folder)))
314 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
316 /* check for empty folder */
318 adios(EX_DATAERR, NULL, "no messages in %s", folder);
320 /* parse the message range/sequence/name and set SELECTED */
321 if (!m_convert(mp, msg))
323 seq_setprev(mp); /* set the previous-sequence */
326 adios(EX_USAGE, NULL, "only one message at a time!");
328 context_replace(curfolder, folder); /* update current folder */
329 seq_setcur(mp, mp->lowsel); /* update current message */
330 seq_save(mp); /* synchronize sequences */
331 context_save(); /* save the context file */
334 msg = file ? file : mh_xstrdup(m_name(mp->lowsel));
336 if ((in = fopen(msg, "r")) == NULL)
337 adios(EX_IOERR, msg, "unable to open");
339 /* find form (components) file */
342 form = etcpath(replgroupcomps);
344 form = etcpath(replcomps);
347 replout(in, drft, mp, mime, form, filter);
352 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
357 docc(char *cp, int ccflag)
359 switch (smatch(cp, ccswitches)) {
361 ambigsw(cp, ccswitches);
364 adios(EX_USAGE, NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
379 ccto = cccc = ccme = ccflag;
388 replout(FILE *inb, char *drft, struct msgs *mp,
389 int mime, char *form, char *filter)
396 struct comp **savecomp;
397 int char_read = 0, format_len, mask;
398 char name[NAMESZ], *scanl;
402 mask = umask(~m_gmprot());
403 if ((out = fopen(drft, "w")) == NULL)
404 adios(EX_CANTCREAT, drft, "unable to create");
408 /* get new format string */
409 cp = new_fs(form, NULL);
410 format_len = strlen(cp);
412 /* compile format string */
413 ncomps = fmt_compile(cp, &fmt) + 1;
415 nxtbuf = compbuffers = mh_xcalloc(ncomps, sizeof(char *));
416 savecomp = used_buf = mh_xcalloc(ncomps+1, sizeof(struct comp *));
417 savecomp += ncomps + 1;
418 *--savecomp = NULL; /* point at zero'd end minus 1 */
420 for (i = ncomps; i--; )
421 *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
423 nxtbuf = compbuffers; /* point at start */
426 for (ap = addrcomps; *ap; ap++) {
429 cptr->c_type |= CT_ADDR;
433 ** ignore any components killed by command line switches
436 FINDCOMP(cptr, "to");
441 FINDCOMP(cptr, "cc");
445 if ((cp = getenv("USER"))) {
446 FINDCOMP(cptr, "user");
448 cptr->c_text = mh_xstrdup(cp);
454 ** pick any interesting stuff out of msg "inb"
456 for (state = FLD;;) {
457 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
462 ** if we're interested in this component, save
463 ** a pointer to the component text, then start
464 ** using our next free buffer as the component
465 ** temp buffer (buffer switching saves an extra
466 ** copy of the component text).
468 if ((cptr = wantcomp[CHASH(name)]))
470 if (!mh_strcasecmp(name, cptr->c_name)) {
471 char_read += msg_count;
472 if (! cptr->c_text) {
473 i = strlen(cptr->c_text = tmpbuf) - 1;
474 if (tmpbuf[i] == '\n')
479 i = strlen(cp = cptr->c_text) - 1;
481 if (cptr->c_type & CT_ADDR) {
483 cp = add(",\n\t", cp);
488 cptr->c_text = add(tmpbuf, cp);
490 while (state == FLDPLUS) {
491 state = m_getfld(state, name, tmpbuf,
493 cptr->c_text = add(tmpbuf, cptr->c_text);
494 char_read += msg_count;
498 } while ((cptr = cptr->c_next));
500 while (state == FLDPLUS)
501 state = m_getfld(state, name, tmpbuf,
512 adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
517 ** format and output the header lines.
522 ** if there's a "Subject" component, strip any "Re:"s off it
524 FINDCOMP(cptr, "subject")
525 if (cptr && (cp = cptr->c_text)) {
537 if (sp != cptr->c_text) {
539 cptr->c_text = mh_xstrdup(sp);
543 i = format_len + char_read + 256;
544 scanl = mh_xcalloc(i + 2, sizeof(char));
548 dat[3] = OUTPUTLINELEN;
550 fmt_scan(fmt, scanl, i, dat);
553 fputs("\nrepl: bad addresses:\n", out);
554 fputs( badaddrs, out);
557 /* Check if we should filter the message */
561 adios(EX_IOERR, drft, "error writing");
563 replfilter(inb, out, filter);
568 adios(EX_IOERR, drft, "error writing");
572 /* add an attachment header */
575 snprintf(buffer, sizeof buffer, "+%s %s",
576 mp->foldpath, m_name(mp->lowsel));
577 if (execprogl("anno", "anno", "-append", "-nodate",
578 drft, "-comp", attach_hdr, "-text", buffer,
579 (char *)NULL) != 0) {
580 advise(NULL, "unable to add attachment header");
584 /* return dynamically allocated buffers */
586 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
588 mh_free0(&(cptr->c_text)); /* if not nxtbuf, nxtbuf already freed */
590 mh_free0(nxtbuf++); /* free unused nxtbufs */
591 mh_free0(&compbuffers);
595 static char *buf; /* our current working buffer */
596 static char *bufend; /* end of working buffer */
597 static char *last_dst; /* buf ptr at end of last call */
598 static unsigned int bufsiz=0; /* current size of buf */
600 #define BUFINCR 512 /* how much to expand buf when if fills */
602 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
605 ** check if there's enough room in buf for str.
606 ** add more mem if needed
608 #define CHECKMEM(str) \
609 if ((len = strlen(str)) >= bufend - dst) {\
611 int n = last_dst - buf;\
612 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
613 buf = mh_xrealloc(buf, bufsiz);\
616 bufend = buf + bufsiz;\
621 ** fmt_scan will call this routine if the user includes the function
622 ** "(formataddr {component})" in a format string. "orig" is the
623 ** original contents of the string register. "str" is the address
624 ** string to be formatted and concatenated onto orig. This routine
625 ** returns a pointer to the concatenated address string.
627 ** We try to not do a lot of malloc/copy/free's (which is why we
628 ** don't call "mh_xstrdup") but still place no upper limit on the
629 ** length of the result string.
631 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
635 formataddr(char *orig, char *str)
638 char baddr[BUFSIZ], error[BUFSIZ];
643 struct mailname *mp = NULL;
645 /* if we don't have a buffer yet, get one */
647 buf = mh_xcalloc(BUFINCR, sizeof(char));
648 last_dst = buf; /* XXX */
649 bufsiz = BUFINCR - 6; /* leave some slop */
650 bufend = buf + bufsiz;
653 ** If "orig" points to our buffer we can just pick up where we
654 ** left off. Otherwise we have to copy orig into our buffer.
658 else if (!orig || !*orig) {
662 dst = last_dst; /* XXX */
667 /* concatenate all the new addresses onto 'buf' */
668 for (isgroup = 0; (cp = getname(str)); ) {
669 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
670 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
672 badaddrs = add(baddr, badaddrs);
675 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
680 /* if we get here we're going to add an address */
686 CHECKMEM(mp->m_gname);
706 insert(struct mailname *np)
711 if (np->m_mbox == NULL)
714 for (mp = &mq; mp->m_next; mp = mp->m_next) {
715 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
716 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
719 if (!ccme && ismymbox(np))
723 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
725 if (!gans(buffer, anoyes))
736 ** This function expects that argument out has been fflushed by the caller.
739 replfilter(FILE *in, FILE *out, char *filter)
741 int pid, pid_show, n;
748 if (access(filter, R_OK) == NOTOK)
749 adios(EX_IOERR, filter, "unable to read");
752 lseek(fileno(in), (off_t) 0, SEEK_SET);
754 if (pipe(mailpipe) == -1) {
755 adios(EX_OSERR, "pipe", "can't create pipe");
758 switch (pid_show = fork()) {
760 adios(EX_OSERR, "fork", "unable to");
763 dup2(fileno(in), fileno(stdin));
764 dup2(mailpipe[1], fileno(stdout));
765 for (n=3; n<OPEN_MAX; n++) {
769 execlp("show", "show", "-file", "-", NULL);
771 adios(EX_OSERR, "exec", "unable to");
774 switch (pid = fork()) {
776 adios(EX_OSERR, "fork", "unable to");
779 dup2(mailpipe[0], fileno(stdin));
780 dup2(fileno(out), fileno(stdout));
781 for (n=3; n<OPEN_MAX; n++) {
785 execlp("mhl", "mhl", "-form", filter, NULL);
786 errstr = strerror(errno);
787 write(2, "unable to exec mhl: ", 20);
788 write(2, errstr, strlen(errstr));
793 if (pidXwait(-1, "show | mhl"))
795 fseek(out, 0L, SEEK_END);