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));
253 if (!(form = *argp++) || *form == '-')
254 adios(NULL, "missing argument to %s",
259 filter = getcpy(etcpath(mhlreply));
281 if (*cp == '+' || *cp == '@') {
283 adios(NULL, "only one folder at a time!");
285 folder = getcpy(expandfol(cp));
288 adios(NULL, "only one message at a time!");
303 if (file && (msg || folder))
304 adios(NULL, "can't mix files and folders/msgs");
307 strncpy(drft, buildsw ? toabsdir("reply")
308 : m_draft(seq_beyond), sizeof(drft));
310 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
315 ** We are replying to a file.
317 anot = 0; /* we don't want to annotate a file */
320 ** We are replying to a message.
325 folder = getcurfol();
326 maildir = toabsdir(folder);
328 if (chdir(maildir) == NOTOK)
329 adios(maildir, "unable to change directory to");
331 /* read folder and create message structure */
332 if (!(mp = folder_read(folder)))
333 adios(NULL, "unable to read folder %s", folder);
335 /* check for empty folder */
337 adios(NULL, "no messages in %s", folder);
339 /* parse the message range/sequence/name and set SELECTED */
340 if (!m_convert(mp, msg))
342 seq_setprev(mp); /* set the previous-sequence */
345 adios(NULL, "only one message at a time!");
347 context_replace(curfolder, folder); /* update current folder */
348 seq_setcur(mp, mp->lowsel); /* update current message */
349 seq_save(mp); /* synchronize sequences */
350 context_save(); /* save the context file */
353 msg = file ? file : getcpy(m_name(mp->lowsel));
355 if ((in = fopen(msg, "r")) == NULL)
356 adios(msg, "unable to open");
358 /* find form (components) file */
361 form = etcpath(replgroupcomps);
363 form = etcpath(replcomps);
366 replout(in, msg, drft, mp, mime, form, filter);
371 what_now(ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
378 docc(char *cp, int ccflag)
380 switch (smatch(cp, ccswitches)) {
382 ambigsw(cp, ccswitches);
385 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
400 ccto = cccc = ccme = ccflag;
409 replout(FILE *inb, char *msg, char *drft, struct msgs *mp,
410 int mime, char *form, char *filter)
412 register int state, i;
413 register struct comp *cptr;
414 register char *tmpbuf;
415 register char **nxtbuf;
417 register struct comp **savecomp;
418 int char_read = 0, format_len, mask;
419 char name[NAMESZ], *scanl;
423 mask = umask(~m_gmprot());
424 if ((out = fopen(drft, "w")) == NULL)
425 adios(drft, "unable to create");
429 /* get new format string */
430 cp = new_fs(form, NULL, NULL);
431 format_len = strlen(cp);
433 /* compile format string */
434 ncomps = fmt_compile(cp, &fmt) + 1;
436 if (!(nxtbuf = compbuffers = (char **)
437 calloc((size_t) ncomps, sizeof(char *))))
438 adios(NULL, "unable to allocate component buffers");
439 if (!(savecomp = used_buf = (struct comp **)
440 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
441 adios(NULL, "unable to allocate component buffer stack");
442 savecomp += ncomps + 1;
443 *--savecomp = NULL; /* point at zero'd end minus 1 */
445 for (i = ncomps; i--; )
446 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
448 nxtbuf = compbuffers; /* point at start */
451 for (ap = addrcomps; *ap; ap++) {
454 cptr->c_type |= CT_ADDR;
458 ** ignore any components killed by command line switches
461 FINDCOMP(cptr, "to");
466 FINDCOMP(cptr, "cc");
470 if ((cp = getenv("USER"))) {
471 FINDCOMP(cptr, "user");
473 cptr->c_text = getcpy(cp);
479 ** pick any interesting stuff out of msg "inb"
481 for (state = FLD;;) {
482 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
487 ** if we're interested in this component, save
488 ** a pointer to the component text, then start
489 ** using our next free buffer as the component
490 ** temp buffer (buffer switching saves an extra
491 ** copy of the component text).
493 if ((cptr = wantcomp[CHASH(name)]))
495 if (!mh_strcasecmp(name, cptr->c_name)) {
496 char_read += msg_count;
497 if (! cptr->c_text) {
498 i = strlen(cptr->c_text = tmpbuf) - 1;
499 if (tmpbuf[i] == '\n')
504 i = strlen(cp = cptr->c_text) - 1;
506 if (cptr->c_type & CT_ADDR) {
508 cp = add(",\n\t", cp);
513 cptr->c_text = add(tmpbuf, cp);
515 while (state == FLDPLUS) {
516 state = m_getfld(state, name, tmpbuf,
518 cptr->c_text = add(tmpbuf, cptr->c_text);
519 char_read += msg_count;
523 } while ((cptr = cptr->c_next));
525 while (state == FLDPLUS)
526 state = m_getfld(state, name, tmpbuf,
537 adios(NULL, "m_getfld() returned %d", state);
542 ** format and output the header lines.
547 ** if there's a "Subject" component, strip any "Re:"s off it
549 FINDCOMP(cptr, "subject")
550 if (cptr && (cp = cptr->c_text)) {
551 register char *sp = cp;
562 if (sp != cptr->c_text) {
564 cptr->c_text = getcpy(sp);
568 i = format_len + char_read + 256;
569 scanl = mh_xmalloc((size_t) i + 2);
573 dat[3] = OUTPUTLINELEN;
575 fmt_scan(fmt, scanl, i, dat);
578 fputs("\nrepl: bad addresses:\n", out);
579 fputs( badaddrs, out);
582 /* Check if we should filter the message */
586 adios(drft, "error writing");
588 replfilter(inb, out, filter);
593 adios(drft, "error writing");
597 /* add an attachment header */
600 snprintf(buffer, sizeof buffer, "+%s %s",
601 mp->foldpath, m_name(mp->lowsel));
602 annotate(drft, attach_hdr, buffer, 0, -2, 1);
605 /* return dynamically allocated buffers */
607 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
609 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
611 free(*nxtbuf++); /* free unused nxtbufs */
612 free((char *) compbuffers);
613 free((char *) used_buf);
616 static char *buf; /* our current working buffer */
617 static char *bufend; /* end of working buffer */
618 static char *last_dst; /* buf ptr at end of last call */
619 static unsigned int bufsiz=0; /* current size of buf */
621 #define BUFINCR 512 /* how much to expand buf when if fills */
623 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
626 ** check if there's enough room in buf for str.
627 ** add more mem if needed
629 #define CHECKMEM(str) \
630 if ((len = strlen(str)) >= bufend - dst) {\
632 int n = last_dst - buf;\
633 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
634 buf = mh_xrealloc(buf, bufsiz);\
637 bufend = buf + bufsiz;\
642 ** fmt_scan will call this routine if the user includes the function
643 ** "(formataddr {component})" in a format string. "orig" is the
644 ** original contents of the string register. "str" is the address
645 ** string to be formatted and concatenated onto orig. This routine
646 ** returns a pointer to the concatenated address string.
648 ** We try to not do a lot of malloc/copy/free's (which is why we
649 ** don't call "getcpy") but still place no upper limit on the
650 ** length of the result string.
652 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
656 formataddr(char *orig, char *str)
659 char baddr[BUFSIZ], error[BUFSIZ];
660 register int isgroup;
664 register struct mailname *mp = NULL;
666 /* if we don't have a buffer yet, get one */
668 buf = mh_xmalloc(BUFINCR);
669 last_dst = buf; /* XXX */
670 bufsiz = BUFINCR - 6; /* leave some slop */
671 bufend = buf + bufsiz;
674 ** If "orig" points to our buffer we can just pick up where we
675 ** left off. Otherwise we have to copy orig into our buffer.
679 else if (!orig || !*orig) {
683 dst = last_dst; /* XXX */
688 /* concatenate all the new addresses onto 'buf' */
689 for (isgroup = 0; (cp = getname(str)); ) {
690 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
691 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
693 badaddrs = add(baddr, badaddrs);
696 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
701 /* if we get here we're going to add an address */
707 CHECKMEM(mp->m_gname);
727 insert(struct mailname *np)
730 register struct mailname *mp;
732 if (np->m_mbox == NULL)
735 for (mp = &mq; mp->m_next; mp = mp->m_next) {
736 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
737 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
740 if (!ccme && ismymbox(np))
744 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
746 if (!gans(buffer, anoyes))
763 ** This function expects that argument out has been fflushed by the caller.
767 replfilter(FILE *in, FILE *out, char *filter)
776 if (access(filter, R_OK) == NOTOK)
777 adios(filter, "unable to read");
779 mhl = mhbasename(mhlproc);
782 lseek(fileno(in), (off_t) 0, SEEK_SET);
784 switch (pid = fork()) {
786 adios("fork", "unable to");
789 dup2(fileno(in), fileno(stdin));
790 dup2(fileno(out), fileno(stdout));
793 execlp(mhlproc, mhl, "-form", filter, "-noclear", NULL);
794 errstr = strerror(errno);
795 write(2, "unable to exec ", 15);
796 write(2, mhlproc, strlen(mhlproc));
798 write(2, errstr, strlen(errstr));
803 if (pidXwait(pid, mhl))
805 fseek(out, 0L, SEEK_END);