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 */
17 static struct swit switches[] = {
27 { "cc all|to|cc|me", 0 },
31 { "editor editor", 0 },
35 { "filter filterfile", 0 },
37 { "form formfile", 0 },
51 { "whatnowproc program", 0 },
53 { "nowhatnowproc", 0 },
59 { "file file", 4 }, /* interface from msh */
63 { "build", 5 }, /* interface from mhe */
69 static struct swit ccswitches[] = {
82 ** Buffer size for content part of header fields.
83 ** We want this to be large enough so that we don't
84 ** do a lot of extra FLDPLUS calls on m_getfld but
85 ** small enough so that we don't snarf the entire
86 ** message body when we're not going to use any of it.
90 static short ccto = -1;
91 static short cccc = -1;
92 static short ccme = -1;
93 static short querysw = 0;
95 static short groupreply = 0; /* Is this a group reply? */
97 static int mime = 0; /* include original as MIME part */
98 static char *form = NULL; /* form (components) file */
99 static char *filter = NULL; /* message filter file */
103 static char *badaddrs = NULL;
104 static char *dfhost = NULL;
106 static struct mailname mq = { NULL };
108 static struct format *fmt;
110 static int ncomps = 0; /* # of interesting components */
111 static char **compbuffers = NULL; /* buffers for component text */
112 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
114 static int dat[5]; /* aux. data for format routine */
116 static char *addrcomps[] = {
135 static void docc(char *, int);
136 static int insert(struct mailname *);
137 static void replfilter(FILE *, FILE *, char *);
138 static void replout(FILE *, char *, char *, struct msgs *, int,
143 main(int argc, char **argv)
146 int nedit = 0, nwhat = 0;
147 char *cp, *cwd, *maildir, *file = NULL;
148 char *folder = NULL, *msg = NULL;
149 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
150 char **argp, **arguments;
151 struct msgs *mp = NULL;
159 setlocale(LC_ALL, "");
161 invo_name = mhbasename(argv[0]);
163 /* read user profile/context */
166 arguments = getarguments(invo_name, argc, argv, 1);
169 while ((cp = *argp++)) {
171 switch (smatch(++cp, switches)) {
173 ambigsw(cp, switches);
176 adios(NULL, "-%s unknown", cp);
179 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
180 print_help(buf, switches, 1);
183 print_version(invo_name);
201 if (!(cp = *argp++) || *cp == '-')
202 adios(NULL, "missing argument to %s",
207 if (!(cp = *argp++) || *cp == '-')
208 adios(NULL, "missing argument to %s",
214 if (!(ed = *argp++) || *ed == '-')
215 adios(NULL, "missing argument to %s",
224 if (!(whatnowproc = *argp++) ||
226 adios(NULL, "missing argument to %s",
232 buildsw++; /* fall... */
240 adios(NULL, "only one file at a time!");
241 if (!(cp = *argp++) || *cp == '-')
242 adios(NULL, "missing argument to %s",
244 file = getcpy(expanddir(cp));
247 if (!(cp = *argp++) || *cp == '-')
248 adios(NULL, "missing argument to %s",
250 filter = getcpy(etcpath(cp));
254 if (!(form = *argp++) || *form == '-')
255 adios(NULL, "missing argument to %s",
260 filter = getcpy(etcpath(mhlreply));
284 if (*cp == '+' || *cp == '@') {
286 adios(NULL, "only one folder at a time!");
288 folder = getcpy(expandfol(cp));
291 adios(NULL, "only one message at a time!");
306 if (file && (msg || folder))
307 adios(NULL, "can't mix files and folders/msgs");
310 strncpy(drft, buildsw ? toabsdir("reply")
311 : m_draft(seq_beyond), sizeof(drft));
313 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
318 ** We are replying to a file.
320 anot = 0; /* we don't want to annotate a file */
323 ** We are replying to a message.
328 folder = getcurfol();
329 maildir = toabsdir(folder);
331 if (chdir(maildir) == NOTOK)
332 adios(maildir, "unable to change directory to");
334 /* read folder and create message structure */
335 if (!(mp = folder_read(folder)))
336 adios(NULL, "unable to read folder %s", folder);
338 /* check for empty folder */
340 adios(NULL, "no messages in %s", folder);
342 /* parse the message range/sequence/name and set SELECTED */
343 if (!m_convert(mp, msg))
345 seq_setprev(mp); /* set the previous-sequence */
348 adios(NULL, "only one message at a time!");
350 context_replace(curfolder, folder); /* update current folder */
351 seq_setcur(mp, mp->lowsel); /* update current message */
352 seq_save(mp); /* synchronize sequences */
353 context_save(); /* save the context file */
356 msg = file ? file : getcpy(m_name(mp->lowsel));
358 if ((in = fopen(msg, "r")) == NULL)
359 adios(msg, "unable to open");
361 /* find form (components) file */
364 form = etcpath(replgroupcomps);
366 form = etcpath(replcomps);
369 replout(in, msg, drft, mp, mime, form, filter);
374 what_now(ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
381 docc(char *cp, int ccflag)
383 switch (smatch(cp, ccswitches)) {
385 ambigsw(cp, ccswitches);
388 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
403 ccto = cccc = ccme = ccflag;
412 replout(FILE *inb, char *msg, char *drft, struct msgs *mp,
413 int mime, char *form, char *filter)
415 register int state, i;
416 register struct comp *cptr;
417 register char *tmpbuf;
418 register char **nxtbuf;
420 register struct comp **savecomp;
421 int char_read = 0, format_len, mask;
422 char name[NAMESZ], *scanl;
426 mask = umask(~m_gmprot());
427 if ((out = fopen(drft, "w")) == NULL)
428 adios(drft, "unable to create");
432 /* get new format string */
433 cp = new_fs(form, NULL, NULL);
434 format_len = strlen(cp);
436 /* compile format string */
437 ncomps = fmt_compile(cp, &fmt) + 1;
439 if (!(nxtbuf = compbuffers = (char **)
440 calloc((size_t) ncomps, sizeof(char *))))
441 adios(NULL, "unable to allocate component buffers");
442 if (!(savecomp = used_buf = (struct comp **)
443 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
444 adios(NULL, "unable to allocate component buffer stack");
445 savecomp += ncomps + 1;
446 *--savecomp = NULL; /* point at zero'd end minus 1 */
448 for (i = ncomps; i--; )
449 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
451 nxtbuf = compbuffers; /* point at start */
454 for (ap = addrcomps; *ap; ap++) {
457 cptr->c_type |= CT_ADDR;
461 ** ignore any components killed by command line switches
464 FINDCOMP(cptr, "to");
469 FINDCOMP(cptr, "cc");
473 if ((cp = getenv("USER"))) {
474 FINDCOMP(cptr, "user");
476 cptr->c_text = getcpy(cp);
482 ** pick any interesting stuff out of msg "inb"
484 for (state = FLD;;) {
485 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
490 ** if we're interested in this component, save
491 ** a pointer to the component text, then start
492 ** using our next free buffer as the component
493 ** temp buffer (buffer switching saves an extra
494 ** copy of the component text).
496 if ((cptr = wantcomp[CHASH(name)]))
498 if (!mh_strcasecmp(name, cptr->c_name)) {
499 char_read += msg_count;
500 if (! cptr->c_text) {
501 i = strlen(cptr->c_text = tmpbuf) - 1;
502 if (tmpbuf[i] == '\n')
507 i = strlen(cp = cptr->c_text) - 1;
509 if (cptr->c_type & CT_ADDR) {
511 cp = add(",\n\t", cp);
516 cptr->c_text = add(tmpbuf, cp);
518 while (state == FLDPLUS) {
519 state = m_getfld(state, name, tmpbuf,
521 cptr->c_text = add(tmpbuf, cptr->c_text);
522 char_read += msg_count;
526 } while ((cptr = cptr->c_next));
528 while (state == FLDPLUS)
529 state = m_getfld(state, name, tmpbuf,
540 adios(NULL, "m_getfld() returned %d", state);
545 ** format and output the header lines.
550 ** if there's a "Subject" component, strip any "Re:"s off it
552 FINDCOMP(cptr, "subject")
553 if (cptr && (cp = cptr->c_text)) {
554 register char *sp = cp;
565 if (sp != cptr->c_text) {
567 cptr->c_text = getcpy(sp);
571 i = format_len + char_read + 256;
572 scanl = mh_xmalloc((size_t) i + 2);
576 dat[3] = OUTPUTLINELEN;
578 fmt_scan(fmt, scanl, i, dat);
581 fputs("\nrepl: bad addresses:\n", out);
582 fputs( badaddrs, out);
585 /* Check if we should filter the message */
589 adios(drft, "error writing");
591 replfilter(inb, out, filter);
596 adios(drft, "error writing");
599 /* if we want mime, then add an attachment header */
600 if (!filter && mime && mp) {
603 snprintf(buffer, sizeof buffer, "+%s %s",
604 mp->foldpath, m_name(mp->lowsel));
605 annotate(drft, attach_hdr, buffer, 0, -2, 1);
608 /* return dynamically allocated buffers */
610 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
612 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
614 free(*nxtbuf++); /* free unused nxtbufs */
615 free((char *) compbuffers);
616 free((char *) used_buf);
619 static char *buf; /* our current working buffer */
620 static char *bufend; /* end of working buffer */
621 static char *last_dst; /* buf ptr at end of last call */
622 static unsigned int bufsiz=0; /* current size of buf */
624 #define BUFINCR 512 /* how much to expand buf when if fills */
626 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
629 ** check if there's enough room in buf for str.
630 ** add more mem if needed
632 #define CHECKMEM(str) \
633 if ((len = strlen(str)) >= bufend - dst) {\
635 int n = last_dst - buf;\
636 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
637 buf = mh_xrealloc(buf, bufsiz);\
640 bufend = buf + bufsiz;\
645 ** fmt_scan will call this routine if the user includes the function
646 ** "(formataddr {component})" in a format string. "orig" is the
647 ** original contents of the string register. "str" is the address
648 ** string to be formatted and concatenated onto orig. This routine
649 ** returns a pointer to the concatenated address string.
651 ** We try to not do a lot of malloc/copy/free's (which is why we
652 ** don't call "getcpy") but still place no upper limit on the
653 ** length of the result string.
655 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
659 formataddr(char *orig, char *str)
662 char baddr[BUFSIZ], error[BUFSIZ];
663 register int isgroup;
667 register struct mailname *mp = NULL;
669 /* if we don't have a buffer yet, get one */
671 buf = mh_xmalloc(BUFINCR);
672 last_dst = buf; /* XXX */
673 bufsiz = BUFINCR - 6; /* leave some slop */
674 bufend = buf + bufsiz;
677 ** If "orig" points to our buffer we can just pick up where we
678 ** left off. Otherwise we have to copy orig into our buffer.
682 else if (!orig || !*orig) {
686 dst = last_dst; /* XXX */
691 /* concatenate all the new addresses onto 'buf' */
692 for (isgroup = 0; (cp = getname(str)); ) {
693 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
694 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
696 badaddrs = add(baddr, badaddrs);
699 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
704 /* if we get here we're going to add an address */
710 CHECKMEM(mp->m_gname);
730 insert(struct mailname *np)
733 register struct mailname *mp;
735 if (np->m_mbox == NULL)
738 for (mp = &mq; mp->m_next; mp = mp->m_next) {
739 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
740 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
743 if (!ccme && ismymbox(np))
747 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
749 if (!gans(buffer, anoyes))
766 ** This function expects that argument out has been fflushed by the caller.
770 replfilter(FILE *in, FILE *out, char *filter)
779 if (access(filter, R_OK) == NOTOK)
780 adios(filter, "unable to read");
782 mhl = mhbasename(mhlproc);
785 lseek(fileno(in), (off_t) 0, SEEK_SET);
787 switch (pid = fork()) {
789 adios("fork", "unable to");
792 dup2(fileno(in), fileno(stdin));
793 dup2(fileno(out), fileno(stdout));
796 execlp(mhlproc, mhl, "-form", filter, "-noclear", NULL);
797 errstr = strerror(errno);
798 write(2, "unable to exec ", 15);
799 write(2, mhlproc, strlen(mhlproc));
801 write(2, errstr, strlen(errstr));
806 if (pidXwait(pid, mhl))
808 fseek(out, 0L, SEEK_END);