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 },
55 { "file file", 4 }, /* interface from msh */
58 { "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)
141 char *cp, *cwd, *maildir, *file = NULL;
142 char *folder = NULL, *msg = NULL;
143 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
144 char **argp, **arguments;
145 struct msgs *mp = NULL;
152 filter = getcpy(etcpath(mhlreply));
155 setlocale(LC_ALL, "");
157 invo_name = mhbasename(argv[0]);
159 /* read user profile/context */
162 arguments = getarguments(invo_name, argc, argv, 1);
165 while ((cp = *argp++)) {
167 switch (smatch(++cp, switches)) {
169 ambigsw(cp, switches);
172 adios(NULL, "-%s unknown", cp);
175 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
176 print_help(buf, switches, 1);
179 print_version(invo_name);
197 if (!(cp = *argp++) || *cp == '-')
198 adios(NULL, "missing argument to %s",
203 if (!(cp = *argp++) || *cp == '-')
204 adios(NULL, "missing argument to %s",
210 if (!(ed = *argp++) || *ed == '-')
211 adios(NULL, "missing argument to %s",
220 if (!(whatnowproc = *argp++) ||
222 adios(NULL, "missing argument to %s",
227 buildsw++; /* fall... */
233 adios(NULL, "only one file at a time!");
234 if (!(cp = *argp++) || *cp == '-')
235 adios(NULL, "missing argument to %s",
237 file = getcpy(expanddir(cp));
240 if (!(form = *argp++) || *form == '-')
241 adios(NULL, "missing argument to %s",
246 if (!(cp = *argp++) || *cp == '-')
247 adios(NULL, "missing argument to %s",
249 filter = getcpy(etcpath(cp));
271 if (*cp == '+' || *cp == '@') {
273 adios(NULL, "only one folder at a time!");
275 folder = getcpy(expandfol(cp));
278 adios(NULL, "only one message at a time!");
293 if (file && (msg || folder))
294 adios(NULL, "can't mix files and folders/msgs");
297 strncpy(drft, buildsw ? toabsdir("reply")
298 : m_draft(seq_beyond), sizeof(drft));
300 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
305 ** We are replying to a file.
307 anot = 0; /* we don't want to annotate a file */
310 ** We are replying to a message.
315 folder = getcurfol();
316 maildir = toabsdir(folder);
318 if (chdir(maildir) == NOTOK)
319 adios(maildir, "unable to change directory to");
321 /* read folder and create message structure */
322 if (!(mp = folder_read(folder)))
323 adios(NULL, "unable to read folder %s", folder);
325 /* check for empty folder */
327 adios(NULL, "no messages in %s", folder);
329 /* parse the message range/sequence/name and set SELECTED */
330 if (!m_convert(mp, msg))
332 seq_setprev(mp); /* set the previous-sequence */
335 adios(NULL, "only one message at a time!");
337 context_replace(curfolder, folder); /* update current folder */
338 seq_setcur(mp, mp->lowsel); /* update current message */
339 seq_save(mp); /* synchronize sequences */
340 context_save(); /* save the context file */
343 msg = file ? file : getcpy(m_name(mp->lowsel));
345 if ((in = fopen(msg, "r")) == NULL)
346 adios(msg, "unable to open");
348 /* find form (components) file */
351 form = etcpath(replgroupcomps);
353 form = etcpath(replcomps);
356 replout(in, drft, mp, mime, form, filter);
361 what_now(ed, nedit, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL,
368 docc(char *cp, int ccflag)
370 switch (smatch(cp, ccswitches)) {
372 ambigsw(cp, ccswitches);
375 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
390 ccto = cccc = ccme = ccflag;
399 replout(FILE *inb, char *drft, struct msgs *mp,
400 int mime, char *form, char *filter)
402 register int state, i;
403 register struct comp *cptr;
404 register char *tmpbuf;
405 register char **nxtbuf;
407 register struct comp **savecomp;
408 int char_read = 0, format_len, mask;
409 char name[NAMESZ], *scanl;
413 mask = umask(~m_gmprot());
414 if ((out = fopen(drft, "w")) == NULL)
415 adios(drft, "unable to create");
419 /* get new format string */
420 cp = new_fs(form, NULL);
421 format_len = strlen(cp);
423 /* compile format string */
424 ncomps = fmt_compile(cp, &fmt) + 1;
426 if (!(nxtbuf = compbuffers = (char **)
427 calloc((size_t) ncomps, sizeof(char *))))
428 adios(NULL, "unable to allocate component buffers");
429 if (!(savecomp = used_buf = (struct comp **)
430 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
431 adios(NULL, "unable to allocate component buffer stack");
432 savecomp += ncomps + 1;
433 *--savecomp = NULL; /* point at zero'd end minus 1 */
435 for (i = ncomps; i--; )
436 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
438 nxtbuf = compbuffers; /* point at start */
441 for (ap = addrcomps; *ap; ap++) {
444 cptr->c_type |= CT_ADDR;
448 ** ignore any components killed by command line switches
451 FINDCOMP(cptr, "to");
456 FINDCOMP(cptr, "cc");
460 if ((cp = getenv("USER"))) {
461 FINDCOMP(cptr, "user");
463 cptr->c_text = getcpy(cp);
469 ** pick any interesting stuff out of msg "inb"
471 for (state = FLD;;) {
472 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
477 ** if we're interested in this component, save
478 ** a pointer to the component text, then start
479 ** using our next free buffer as the component
480 ** temp buffer (buffer switching saves an extra
481 ** copy of the component text).
483 if ((cptr = wantcomp[CHASH(name)]))
485 if (!mh_strcasecmp(name, cptr->c_name)) {
486 char_read += msg_count;
487 if (! cptr->c_text) {
488 i = strlen(cptr->c_text = tmpbuf) - 1;
489 if (tmpbuf[i] == '\n')
494 i = strlen(cp = cptr->c_text) - 1;
496 if (cptr->c_type & CT_ADDR) {
498 cp = add(",\n\t", cp);
503 cptr->c_text = add(tmpbuf, cp);
505 while (state == FLDPLUS) {
506 state = m_getfld(state, name, tmpbuf,
508 cptr->c_text = add(tmpbuf, cptr->c_text);
509 char_read += msg_count;
513 } while ((cptr = cptr->c_next));
515 while (state == FLDPLUS)
516 state = m_getfld(state, name, tmpbuf,
527 adios(NULL, "m_getfld() returned %d", state);
532 ** format and output the header lines.
537 ** if there's a "Subject" component, strip any "Re:"s off it
539 FINDCOMP(cptr, "subject")
540 if (cptr && (cp = cptr->c_text)) {
541 register char *sp = cp;
552 if (sp != cptr->c_text) {
554 cptr->c_text = getcpy(sp);
558 i = format_len + char_read + 256;
559 scanl = mh_xmalloc((size_t) i + 2);
563 dat[3] = OUTPUTLINELEN;
565 fmt_scan(fmt, scanl, i, dat);
568 fputs("\nrepl: bad addresses:\n", out);
569 fputs( badaddrs, out);
572 /* Check if we should filter the message */
576 adios(drft, "error writing");
578 replfilter(inb, out, filter);
583 adios(drft, "error writing");
587 /* add an attachment header */
590 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
591 "-comp '%s' -text '+%s %s'",
593 attach_hdr, mp->foldpath, m_name(mp->lowsel));
594 if (system(buffer) != 0) {
595 advise(NULL, "unable to add attachment header");
599 /* return dynamically allocated buffers */
601 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
603 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
605 free(*nxtbuf++); /* free unused nxtbufs */
606 free((char *) compbuffers);
607 free((char *) used_buf);
610 static char *buf; /* our current working buffer */
611 static char *bufend; /* end of working buffer */
612 static char *last_dst; /* buf ptr at end of last call */
613 static unsigned int bufsiz=0; /* current size of buf */
615 #define BUFINCR 512 /* how much to expand buf when if fills */
617 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
620 ** check if there's enough room in buf for str.
621 ** add more mem if needed
623 #define CHECKMEM(str) \
624 if ((len = strlen(str)) >= bufend - dst) {\
626 int n = last_dst - buf;\
627 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
628 buf = mh_xrealloc(buf, bufsiz);\
631 bufend = buf + bufsiz;\
636 ** fmt_scan will call this routine if the user includes the function
637 ** "(formataddr {component})" in a format string. "orig" is the
638 ** original contents of the string register. "str" is the address
639 ** string to be formatted and concatenated onto orig. This routine
640 ** returns a pointer to the concatenated address string.
642 ** We try to not do a lot of malloc/copy/free's (which is why we
643 ** don't call "getcpy") but still place no upper limit on the
644 ** length of the result string.
646 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
650 formataddr(char *orig, char *str)
653 char baddr[BUFSIZ], error[BUFSIZ];
654 register int isgroup;
658 register struct mailname *mp = NULL;
660 /* if we don't have a buffer yet, get one */
662 buf = mh_xmalloc(BUFINCR);
663 last_dst = buf; /* XXX */
664 bufsiz = BUFINCR - 6; /* leave some slop */
665 bufend = buf + bufsiz;
668 ** If "orig" points to our buffer we can just pick up where we
669 ** left off. Otherwise we have to copy orig into our buffer.
673 else if (!orig || !*orig) {
677 dst = last_dst; /* XXX */
682 /* concatenate all the new addresses onto 'buf' */
683 for (isgroup = 0; (cp = getname(str)); ) {
684 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
685 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
687 badaddrs = add(baddr, badaddrs);
690 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
695 /* if we get here we're going to add an address */
701 CHECKMEM(mp->m_gname);
721 insert(struct mailname *np)
724 register struct mailname *mp;
726 if (np->m_mbox == NULL)
729 for (mp = &mq; mp->m_next; mp = mp->m_next) {
730 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
731 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
734 if (!ccme && ismymbox(np))
738 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
740 if (!gans(buffer, anoyes))
751 ** This function expects that argument out has been fflushed by the caller.
754 replfilter(FILE *in, FILE *out, char *filter)
762 if (access(filter, R_OK) == NOTOK)
763 adios(filter, "unable to read");
766 lseek(fileno(in), (off_t) 0, SEEK_SET);
768 switch (pid = fork()) {
770 adios("fork", "unable to");
773 dup2(fileno(in), fileno(stdin));
774 dup2(fileno(out), fileno(stdout));
775 for (n=3; n<OPEN_MAX; n++) {
779 execlp("mhl", "mhl", "-form", filter, NULL);
780 errstr = strerror(errno);
781 write(2, "unable to exec mhl: ", 20);
782 write(2, errstr, strlen(errstr));
787 if (pidXwait(pid, "mhl"))
789 fseek(out, 0L, SEEK_END);