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 },
39 { "form formfile", 0 },
49 { "whatnowproc program", 0 },
51 { "nowhatnowproc", 0 },
57 { "file file", 4 }, /* interface from msh */
61 { "build", 5 }, /* interface from mhe */
67 static struct swit ccswitches[] = {
80 ** Buffer size for content part of header fields.
81 ** We want this to be large enough so that we don't
82 ** do a lot of extra FLDPLUS calls on m_getfld but
83 ** small enough so that we don't snarf the entire
84 ** message body when we're not going to use any of it.
88 static short ccto = -1;
89 static short cccc = -1;
90 static short ccme = -1;
91 static short querysw = 0;
93 static short groupreply = 0; /* Is this a group reply? */
95 static int mime = 0; /* include original as MIME part */
96 static char *form = NULL; /* form (components) file */
97 static char *filter = NULL; /* message filter file */
101 static char *badaddrs = NULL;
102 static char *dfhost = NULL;
104 static struct mailname mq = { NULL };
106 static struct format *fmt;
108 static int ncomps = 0; /* # of interesting components */
109 static char **compbuffers = NULL; /* buffers for component text */
110 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
112 static int dat[5]; /* aux. data for format routine */
114 static char *addrcomps[] = {
133 static void docc(char *, int);
134 static int insert(struct mailname *);
135 static void replfilter(FILE *, FILE *, char *);
136 static void replout(FILE *, char *, char *, struct msgs *, int,
141 main(int argc, char **argv)
144 int nedit = 0, nwhat = 0;
145 char *cp, *cwd, *maildir, *file = NULL;
146 char *folder = NULL, *msg = NULL;
147 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
148 char **argp, **arguments;
149 struct msgs *mp = NULL;
156 filter = getcpy(etcpath(mhlreply));
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 (!(form = *argp++) || *form == '-')
248 adios(NULL, "missing argument to %s",
253 if (!(cp = *argp++) || *cp == '-')
254 adios(NULL, "missing argument to %s",
256 filter = getcpy(etcpath(cp));
278 if (*cp == '+' || *cp == '@') {
280 adios(NULL, "only one folder at a time!");
282 folder = getcpy(expandfol(cp));
285 adios(NULL, "only one message at a time!");
300 if (file && (msg || folder))
301 adios(NULL, "can't mix files and folders/msgs");
304 strncpy(drft, buildsw ? toabsdir("reply")
305 : m_draft(seq_beyond), sizeof(drft));
307 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
312 ** We are replying to a file.
314 anot = 0; /* we don't want to annotate a file */
317 ** We are replying to a message.
322 folder = getcurfol();
323 maildir = toabsdir(folder);
325 if (chdir(maildir) == NOTOK)
326 adios(maildir, "unable to change directory to");
328 /* read folder and create message structure */
329 if (!(mp = folder_read(folder)))
330 adios(NULL, "unable to read folder %s", folder);
332 /* check for empty folder */
334 adios(NULL, "no messages in %s", folder);
336 /* parse the message range/sequence/name and set SELECTED */
337 if (!m_convert(mp, msg))
339 seq_setprev(mp); /* set the previous-sequence */
342 adios(NULL, "only one message at a time!");
344 context_replace(curfolder, folder); /* update current folder */
345 seq_setcur(mp, mp->lowsel); /* update current message */
346 seq_save(mp); /* synchronize sequences */
347 context_save(); /* save the context file */
350 msg = file ? file : getcpy(m_name(mp->lowsel));
352 if ((in = fopen(msg, "r")) == NULL)
353 adios(msg, "unable to open");
355 /* find form (components) file */
358 form = etcpath(replgroupcomps);
360 form = etcpath(replcomps);
363 replout(in, msg, drft, mp, mime, form, filter);
368 what_now(ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
375 docc(char *cp, int ccflag)
377 switch (smatch(cp, ccswitches)) {
379 ambigsw(cp, ccswitches);
382 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
397 ccto = cccc = ccme = ccflag;
406 replout(FILE *inb, char *msg, char *drft, struct msgs *mp,
407 int mime, char *form, char *filter)
409 register int state, i;
410 register struct comp *cptr;
411 register char *tmpbuf;
412 register char **nxtbuf;
414 register struct comp **savecomp;
415 int char_read = 0, format_len, mask;
416 char name[NAMESZ], *scanl;
420 mask = umask(~m_gmprot());
421 if ((out = fopen(drft, "w")) == NULL)
422 adios(drft, "unable to create");
426 /* get new format string */
427 cp = new_fs(form, NULL);
428 format_len = strlen(cp);
430 /* compile format string */
431 ncomps = fmt_compile(cp, &fmt) + 1;
433 if (!(nxtbuf = compbuffers = (char **)
434 calloc((size_t) ncomps, sizeof(char *))))
435 adios(NULL, "unable to allocate component buffers");
436 if (!(savecomp = used_buf = (struct comp **)
437 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
438 adios(NULL, "unable to allocate component buffer stack");
439 savecomp += ncomps + 1;
440 *--savecomp = NULL; /* point at zero'd end minus 1 */
442 for (i = ncomps; i--; )
443 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
445 nxtbuf = compbuffers; /* point at start */
448 for (ap = addrcomps; *ap; ap++) {
451 cptr->c_type |= CT_ADDR;
455 ** ignore any components killed by command line switches
458 FINDCOMP(cptr, "to");
463 FINDCOMP(cptr, "cc");
467 if ((cp = getenv("USER"))) {
468 FINDCOMP(cptr, "user");
470 cptr->c_text = getcpy(cp);
476 ** pick any interesting stuff out of msg "inb"
478 for (state = FLD;;) {
479 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
484 ** if we're interested in this component, save
485 ** a pointer to the component text, then start
486 ** using our next free buffer as the component
487 ** temp buffer (buffer switching saves an extra
488 ** copy of the component text).
490 if ((cptr = wantcomp[CHASH(name)]))
492 if (!mh_strcasecmp(name, cptr->c_name)) {
493 char_read += msg_count;
494 if (! cptr->c_text) {
495 i = strlen(cptr->c_text = tmpbuf) - 1;
496 if (tmpbuf[i] == '\n')
501 i = strlen(cp = cptr->c_text) - 1;
503 if (cptr->c_type & CT_ADDR) {
505 cp = add(",\n\t", cp);
510 cptr->c_text = add(tmpbuf, cp);
512 while (state == FLDPLUS) {
513 state = m_getfld(state, name, tmpbuf,
515 cptr->c_text = add(tmpbuf, cptr->c_text);
516 char_read += msg_count;
520 } while ((cptr = cptr->c_next));
522 while (state == FLDPLUS)
523 state = m_getfld(state, name, tmpbuf,
534 adios(NULL, "m_getfld() returned %d", state);
539 ** format and output the header lines.
544 ** if there's a "Subject" component, strip any "Re:"s off it
546 FINDCOMP(cptr, "subject")
547 if (cptr && (cp = cptr->c_text)) {
548 register char *sp = cp;
559 if (sp != cptr->c_text) {
561 cptr->c_text = getcpy(sp);
565 i = format_len + char_read + 256;
566 scanl = mh_xmalloc((size_t) i + 2);
570 dat[3] = OUTPUTLINELEN;
572 fmt_scan(fmt, scanl, i, dat);
575 fputs("\nrepl: bad addresses:\n", out);
576 fputs( badaddrs, out);
579 /* Check if we should filter the message */
583 adios(drft, "error writing");
585 replfilter(inb, out, filter);
590 adios(drft, "error writing");
594 /* add an attachment header */
597 snprintf(buffer, sizeof buffer, "+%s %s",
598 mp->foldpath, m_name(mp->lowsel));
599 annotate(drft, attach_hdr, buffer, 0, -2, 1, 0);
602 /* return dynamically allocated buffers */
604 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
606 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
608 free(*nxtbuf++); /* free unused nxtbufs */
609 free((char *) compbuffers);
610 free((char *) used_buf);
613 static char *buf; /* our current working buffer */
614 static char *bufend; /* end of working buffer */
615 static char *last_dst; /* buf ptr at end of last call */
616 static unsigned int bufsiz=0; /* current size of buf */
618 #define BUFINCR 512 /* how much to expand buf when if fills */
620 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
623 ** check if there's enough room in buf for str.
624 ** add more mem if needed
626 #define CHECKMEM(str) \
627 if ((len = strlen(str)) >= bufend - dst) {\
629 int n = last_dst - buf;\
630 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
631 buf = mh_xrealloc(buf, bufsiz);\
634 bufend = buf + bufsiz;\
639 ** fmt_scan will call this routine if the user includes the function
640 ** "(formataddr {component})" in a format string. "orig" is the
641 ** original contents of the string register. "str" is the address
642 ** string to be formatted and concatenated onto orig. This routine
643 ** returns a pointer to the concatenated address string.
645 ** We try to not do a lot of malloc/copy/free's (which is why we
646 ** don't call "getcpy") but still place no upper limit on the
647 ** length of the result string.
649 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
653 formataddr(char *orig, char *str)
656 char baddr[BUFSIZ], error[BUFSIZ];
657 register int isgroup;
661 register struct mailname *mp = NULL;
663 /* if we don't have a buffer yet, get one */
665 buf = mh_xmalloc(BUFINCR);
666 last_dst = buf; /* XXX */
667 bufsiz = BUFINCR - 6; /* leave some slop */
668 bufend = buf + bufsiz;
671 ** If "orig" points to our buffer we can just pick up where we
672 ** left off. Otherwise we have to copy orig into our buffer.
676 else if (!orig || !*orig) {
680 dst = last_dst; /* XXX */
685 /* concatenate all the new addresses onto 'buf' */
686 for (isgroup = 0; (cp = getname(str)); ) {
687 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
688 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
690 badaddrs = add(baddr, badaddrs);
693 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
698 /* if we get here we're going to add an address */
704 CHECKMEM(mp->m_gname);
724 insert(struct mailname *np)
727 register struct mailname *mp;
729 if (np->m_mbox == NULL)
732 for (mp = &mq; mp->m_next; mp = mp->m_next) {
733 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
734 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
737 if (!ccme && ismymbox(np))
741 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
743 if (!gans(buffer, anoyes))
760 ** This function expects that argument out has been fflushed by the caller.
763 replfilter(FILE *in, FILE *out, char *filter)
771 if (access(filter, R_OK) == NOTOK)
772 adios(filter, "unable to read");
775 lseek(fileno(in), (off_t) 0, SEEK_SET);
777 switch (pid = fork()) {
779 adios("fork", "unable to");
782 dup2(fileno(in), fileno(stdin));
783 dup2(fileno(out), fileno(stdout));
786 execlp("mhl", "mhl", "-form", filter, NULL);
787 errstr = strerror(errno);
788 write(2, "unable to exec mhl: ", 20);
789 write(2, errstr, strlen(errstr));
794 if (pidXwait(pid, "mhl"))
796 fseek(out, 0L, SEEK_END);