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 },
33 { "filter filterfile", 0 },
37 { "form formfile", 0 },
47 { "whatnowproc program", 0 },
53 { "file file", 4 }, /* interface from msh */
56 { "build", 5 }, /* interface from mhe */
61 static struct swit ccswitches[] = {
74 ** Buffer size for content part of header fields.
75 ** We want this to be large enough so that we don't
76 ** do a lot of extra FLDPLUS calls on m_getfld but
77 ** small enough so that we don't snarf the entire
78 ** message body when we're not going to use any of it.
82 static short ccto = -1;
83 static short cccc = -1;
84 static short ccme = -1;
85 static short querysw = 0;
87 static short groupreply = 0; /* Is this a group reply? */
89 static int mime = 0; /* include original as MIME part */
90 static char *form = NULL; /* form (components) file */
91 static char *filter = NULL; /* message filter file */
95 static char *badaddrs = NULL;
96 static char *dfhost = NULL;
98 static struct mailname mq;
100 static struct format *fmt;
102 static int ncomps = 0; /* # of interesting components */
103 static char **compbuffers = NULL; /* buffers for component text */
104 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
106 static int dat[5]; /* aux. data for format routine */
108 static char *addrcomps[] = {
127 static void docc(char *, int);
128 static int insert(struct mailname *);
129 static void replfilter(FILE *, FILE *, char *);
130 static void replout(FILE *, char *, struct msgs *, int,
135 main(int argc, char **argv)
138 char *cp, *cwd, *maildir, *file = NULL;
139 char *folder = NULL, *msg = NULL;
140 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
141 char **argp, **arguments;
142 struct msgs *mp = NULL;
149 filter = getcpy(etcpath(mhlreply));
152 setlocale(LC_ALL, "");
154 invo_name = mhbasename(argv[0]);
156 /* read user profile/context */
159 arguments = getarguments(invo_name, argc, argv, 1);
162 while ((cp = *argp++)) {
164 switch (smatch(++cp, switches)) {
166 ambigsw(cp, switches);
169 adios(NULL, "-%s unknown", cp);
172 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
173 print_help(buf, switches, 1);
176 print_version(invo_name);
194 if (!(cp = *argp++) || *cp == '-')
195 adios(NULL, "missing argument to %s",
200 if (!(cp = *argp++) || *cp == '-')
201 adios(NULL, "missing argument to %s",
207 if (!(ed = *argp++) || *ed == '-')
208 adios(NULL, "missing argument to %s",
213 if (!(whatnowproc = *argp++) ||
215 adios(NULL, "missing argument to %s",
220 buildsw++; /* fall... */
226 adios(NULL, "only one file at a time!");
227 if (!(cp = *argp++) || *cp == '-')
228 adios(NULL, "missing argument to %s",
230 file = getcpy(expanddir(cp));
233 if (!(form = *argp++) || *form == '-')
234 adios(NULL, "missing argument to %s",
239 if (!(cp = *argp++) || *cp == '-')
240 adios(NULL, "missing argument to %s",
242 filter = getcpy(etcpath(cp));
264 if (*cp == '+' || *cp == '@') {
266 adios(NULL, "only one folder at a time!");
268 folder = getcpy(expandfol(cp));
271 adios(NULL, "only one message at a time!");
286 if (file && (msg || folder))
287 adios(NULL, "can't mix files and folders/msgs");
290 strncpy(drft, buildsw ? toabsdir("reply")
291 : m_draft(seq_beyond), sizeof(drft));
293 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
298 ** We are replying to a file.
300 anot = 0; /* we don't want to annotate a file */
303 ** We are replying to a message.
308 folder = getcurfol();
309 maildir = toabsdir(folder);
311 if (chdir(maildir) == NOTOK)
312 adios(maildir, "unable to change directory to");
314 /* read folder and create message structure */
315 if (!(mp = folder_read(folder)))
316 adios(NULL, "unable to read folder %s", folder);
318 /* check for empty folder */
320 adios(NULL, "no messages in %s", folder);
322 /* parse the message range/sequence/name and set SELECTED */
323 if (!m_convert(mp, msg))
325 seq_setprev(mp); /* set the previous-sequence */
328 adios(NULL, "only one message at a time!");
330 context_replace(curfolder, folder); /* update current folder */
331 seq_setcur(mp, mp->lowsel); /* update current message */
332 seq_save(mp); /* synchronize sequences */
333 context_save(); /* save the context file */
336 msg = file ? file : getcpy(m_name(mp->lowsel));
338 if ((in = fopen(msg, "r")) == NULL)
339 adios(msg, "unable to open");
341 /* find form (components) file */
344 form = etcpath(replgroupcomps);
346 form = etcpath(replcomps);
349 replout(in, drft, mp, mime, form, filter);
354 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
360 docc(char *cp, int ccflag)
362 switch (smatch(cp, ccswitches)) {
364 ambigsw(cp, ccswitches);
367 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
382 ccto = cccc = ccme = ccflag;
391 replout(FILE *inb, char *drft, struct msgs *mp,
392 int mime, char *form, char *filter)
394 register int state, i;
395 register struct comp *cptr;
396 register char *tmpbuf;
397 register char **nxtbuf;
399 register struct comp **savecomp;
400 int char_read = 0, format_len, mask;
401 char name[NAMESZ], *scanl;
405 mask = umask(~m_gmprot());
406 if ((out = fopen(drft, "w")) == NULL)
407 adios(drft, "unable to create");
411 /* get new format string */
412 cp = new_fs(form, NULL);
413 format_len = strlen(cp);
415 /* compile format string */
416 ncomps = fmt_compile(cp, &fmt) + 1;
418 if (!(nxtbuf = compbuffers = (char **)
419 calloc((size_t) ncomps, sizeof(char *))))
420 adios(NULL, "unable to allocate component buffers");
421 if (!(savecomp = used_buf = (struct comp **)
422 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
423 adios(NULL, "unable to allocate component buffer stack");
424 savecomp += ncomps + 1;
425 *--savecomp = NULL; /* point at zero'd end minus 1 */
427 for (i = ncomps; i--; )
428 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
430 nxtbuf = compbuffers; /* point at start */
433 for (ap = addrcomps; *ap; ap++) {
436 cptr->c_type |= CT_ADDR;
440 ** ignore any components killed by command line switches
443 FINDCOMP(cptr, "to");
448 FINDCOMP(cptr, "cc");
452 if ((cp = getenv("USER"))) {
453 FINDCOMP(cptr, "user");
455 cptr->c_text = getcpy(cp);
461 ** pick any interesting stuff out of msg "inb"
463 for (state = FLD;;) {
464 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
469 ** if we're interested in this component, save
470 ** a pointer to the component text, then start
471 ** using our next free buffer as the component
472 ** temp buffer (buffer switching saves an extra
473 ** copy of the component text).
475 if ((cptr = wantcomp[CHASH(name)]))
477 if (!mh_strcasecmp(name, cptr->c_name)) {
478 char_read += msg_count;
479 if (! cptr->c_text) {
480 i = strlen(cptr->c_text = tmpbuf) - 1;
481 if (tmpbuf[i] == '\n')
486 i = strlen(cp = cptr->c_text) - 1;
488 if (cptr->c_type & CT_ADDR) {
490 cp = add(",\n\t", cp);
495 cptr->c_text = add(tmpbuf, cp);
497 while (state == FLDPLUS) {
498 state = m_getfld(state, name, tmpbuf,
500 cptr->c_text = add(tmpbuf, cptr->c_text);
501 char_read += msg_count;
505 } while ((cptr = cptr->c_next));
507 while (state == FLDPLUS)
508 state = m_getfld(state, name, tmpbuf,
519 adios(NULL, "m_getfld() returned %d", state);
524 ** format and output the header lines.
529 ** if there's a "Subject" component, strip any "Re:"s off it
531 FINDCOMP(cptr, "subject")
532 if (cptr && (cp = cptr->c_text)) {
533 register char *sp = cp;
544 if (sp != cptr->c_text) {
546 cptr->c_text = getcpy(sp);
550 i = format_len + char_read + 256;
551 scanl = mh_xmalloc((size_t) i + 2);
555 dat[3] = OUTPUTLINELEN;
557 fmt_scan(fmt, scanl, i, dat);
560 fputs("\nrepl: bad addresses:\n", out);
561 fputs( badaddrs, out);
564 /* Check if we should filter the message */
568 adios(drft, "error writing");
570 replfilter(inb, out, filter);
575 adios(drft, "error writing");
579 /* add an attachment header */
582 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
583 "-comp '%s' -text '+%s %s'",
585 attach_hdr, mp->foldpath, m_name(mp->lowsel));
586 if (system(buffer) != 0) {
587 advise(NULL, "unable to add attachment header");
591 /* return dynamically allocated buffers */
593 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
595 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
597 free(*nxtbuf++); /* free unused nxtbufs */
598 free((char *) compbuffers);
599 free((char *) used_buf);
602 static char *buf; /* our current working buffer */
603 static char *bufend; /* end of working buffer */
604 static char *last_dst; /* buf ptr at end of last call */
605 static unsigned int bufsiz=0; /* current size of buf */
607 #define BUFINCR 512 /* how much to expand buf when if fills */
609 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
612 ** check if there's enough room in buf for str.
613 ** add more mem if needed
615 #define CHECKMEM(str) \
616 if ((len = strlen(str)) >= bufend - dst) {\
618 int n = last_dst - buf;\
619 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
620 buf = mh_xrealloc(buf, bufsiz);\
623 bufend = buf + bufsiz;\
628 ** fmt_scan will call this routine if the user includes the function
629 ** "(formataddr {component})" in a format string. "orig" is the
630 ** original contents of the string register. "str" is the address
631 ** string to be formatted and concatenated onto orig. This routine
632 ** returns a pointer to the concatenated address string.
634 ** We try to not do a lot of malloc/copy/free's (which is why we
635 ** don't call "getcpy") but still place no upper limit on the
636 ** length of the result string.
638 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
642 formataddr(char *orig, char *str)
645 char baddr[BUFSIZ], error[BUFSIZ];
646 register int isgroup;
650 register struct mailname *mp = NULL;
652 /* if we don't have a buffer yet, get one */
654 buf = mh_xmalloc(BUFINCR);
655 last_dst = buf; /* XXX */
656 bufsiz = BUFINCR - 6; /* leave some slop */
657 bufend = buf + bufsiz;
660 ** If "orig" points to our buffer we can just pick up where we
661 ** left off. Otherwise we have to copy orig into our buffer.
665 else if (!orig || !*orig) {
669 dst = last_dst; /* XXX */
674 /* concatenate all the new addresses onto 'buf' */
675 for (isgroup = 0; (cp = getname(str)); ) {
676 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
677 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
679 badaddrs = add(baddr, badaddrs);
682 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
687 /* if we get here we're going to add an address */
693 CHECKMEM(mp->m_gname);
713 insert(struct mailname *np)
716 register struct mailname *mp;
718 if (np->m_mbox == NULL)
721 for (mp = &mq; mp->m_next; mp = mp->m_next) {
722 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
723 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
726 if (!ccme && ismymbox(np))
730 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
732 if (!gans(buffer, anoyes))
743 ** This function expects that argument out has been fflushed by the caller.
746 replfilter(FILE *in, FILE *out, char *filter)
754 if (access(filter, R_OK) == NOTOK)
755 adios(filter, "unable to read");
758 lseek(fileno(in), (off_t) 0, SEEK_SET);
760 switch (pid = fork()) {
762 adios("fork", "unable to");
765 dup2(fileno(in), fileno(stdin));
766 dup2(fileno(out), fileno(stdout));
767 for (n=3; n<OPEN_MAX; n++) {
771 execlp("mhl", "mhl", "-form", filter, NULL);
772 errstr = strerror(errno);
773 write(2, "unable to exec mhl: ", 20);
774 write(2, errstr, strlen(errstr));
779 if (pidXwait(pid, "mhl"))
781 fseek(out, 0L, SEEK_END);