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 */
55 { "build", 5 }, /* interface from mhe */
59 static struct swit ccswitches[] = {
72 ** Buffer size for content part of header fields.
73 ** We want this to be large enough so that we don't
74 ** do a lot of extra FLDPLUS calls on m_getfld but
75 ** small enough so that we don't snarf the entire
76 ** message body when we're not going to use any of it.
80 static short ccto = -1;
81 static short cccc = -1;
82 static short ccme = -1;
83 static short querysw = 0;
85 static short groupreply = 0; /* Is this a group reply? */
87 static int mime = 0; /* include original as MIME part */
88 static char *form = NULL; /* form (components) file */
89 static char *filter = NULL; /* message filter file */
93 static char *badaddrs = NULL;
94 static char *dfhost = NULL;
96 static struct mailname mq;
98 static struct format *fmt;
100 static int ncomps = 0; /* # of interesting components */
101 static char **compbuffers = NULL; /* buffers for component text */
102 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
104 static int dat[5]; /* aux. data for format routine */
106 static char *addrcomps[] = {
125 static void docc(char *, int);
126 static int insert(struct mailname *);
127 static void replfilter(FILE *, FILE *, char *);
128 static void replout(FILE *, char *, struct msgs *, int,
133 main(int argc, char **argv)
136 char *cp, *cwd, *maildir, *file = NULL;
137 char *folder = NULL, *msg = NULL;
138 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
139 char **argp, **arguments;
140 struct msgs *mp = NULL;
144 filter = getcpy(etcpath(mhlreply));
146 setlocale(LC_ALL, "");
147 invo_name = mhbasename(argv[0]);
149 /* read user profile/context */
152 arguments = getarguments(invo_name, argc, argv, 1);
155 while ((cp = *argp++)) {
157 switch (smatch(++cp, switches)) {
159 ambigsw(cp, switches);
162 adios(NULL, "-%s unknown", cp);
165 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
166 print_help(buf, switches, 1);
169 print_version(invo_name);
187 if (!(cp = *argp++) || *cp == '-')
188 adios(NULL, "missing argument to %s",
193 if (!(cp = *argp++) || *cp == '-')
194 adios(NULL, "missing argument to %s",
200 if (!(ed = *argp++) || *ed == '-')
201 adios(NULL, "missing argument to %s",
206 if (!(whatnowproc = *argp++) ||
208 adios(NULL, "missing argument to %s",
218 adios(NULL, "only one file at a time!");
219 if (!(cp = *argp++) || *cp == '-')
220 adios(NULL, "missing argument to %s",
222 file = getcpy(expanddir(cp));
225 if (!(form = *argp++) || *form == '-')
226 adios(NULL, "missing argument to %s",
231 if (!(cp = *argp++) || *cp == '-')
232 adios(NULL, "missing argument to %s",
234 filter = getcpy(etcpath(cp));
256 if (*cp == '+' || *cp == '@') {
258 adios(NULL, "only one folder at a time!");
260 folder = getcpy(expandfol(cp));
263 adios(NULL, "only one message at a time!");
278 if (file && (msg || folder))
279 adios(NULL, "can't mix files and folders/msgs");
281 strncpy(drft, buildsw ? toabsdir("reply") : m_draft(seq_beyond),
284 ** FIXME: (concerning MHE support (buildsw) only)
285 ** There's no check if the draft already exists. mmh has removed
286 ** this case by having the draft folder. I won't add code only to
287 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
292 ** We are replying to a file.
294 anot = 0; /* we don't want to annotate a file */
297 ** We are replying to a message.
302 folder = getcurfol();
303 maildir = toabsdir(folder);
305 if (chdir(maildir) == NOTOK)
306 adios(maildir, "unable to change directory to");
308 /* read folder and create message structure */
309 if (!(mp = folder_read(folder)))
310 adios(NULL, "unable to read folder %s", folder);
312 /* check for empty folder */
314 adios(NULL, "no messages in %s", folder);
316 /* parse the message range/sequence/name and set SELECTED */
317 if (!m_convert(mp, msg))
318 /* sysexits.h EX_USAGE */
320 seq_setprev(mp); /* set the previous-sequence */
323 adios(NULL, "only one message at a time!");
325 context_replace(curfolder, folder); /* update current folder */
326 seq_setcur(mp, mp->lowsel); /* update current message */
327 seq_save(mp); /* synchronize sequences */
328 context_save(); /* save the context file */
331 msg = file ? file : getcpy(m_name(mp->lowsel));
333 if ((in = fopen(msg, "r")) == NULL)
334 adios(msg, "unable to open");
336 /* find form (components) file */
339 form = etcpath(replgroupcomps);
341 form = etcpath(replcomps);
344 replout(in, drft, mp, mime, form, filter);
349 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
350 /* sysexits.h EX_SOFTWARE */
355 docc(char *cp, int ccflag)
357 switch (smatch(cp, ccswitches)) {
359 ambigsw(cp, ccswitches);
362 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
377 ccto = cccc = ccme = ccflag;
386 replout(FILE *inb, char *drft, struct msgs *mp,
387 int mime, char *form, char *filter)
389 register int state, i;
390 register struct comp *cptr;
391 register char *tmpbuf;
392 register char **nxtbuf;
394 register struct comp **savecomp;
395 int char_read = 0, format_len, mask;
396 char name[NAMESZ], *scanl;
400 mask = umask(~m_gmprot());
401 if ((out = fopen(drft, "w")) == NULL)
402 adios(drft, "unable to create");
406 /* get new format string */
407 cp = new_fs(form, NULL);
408 format_len = strlen(cp);
410 /* compile format string */
411 ncomps = fmt_compile(cp, &fmt) + 1;
413 if (!(nxtbuf = compbuffers = (char **)
414 calloc((size_t) ncomps, sizeof(char *))))
415 adios(NULL, "unable to allocate component buffers");
416 if (!(savecomp = used_buf = (struct comp **)
417 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
418 adios(NULL, "unable to allocate component buffer stack");
419 savecomp += ncomps + 1;
420 *--savecomp = NULL; /* point at zero'd end minus 1 */
422 for (i = ncomps; i--; )
423 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
425 nxtbuf = compbuffers; /* point at start */
428 for (ap = addrcomps; *ap; ap++) {
431 cptr->c_type |= CT_ADDR;
435 ** ignore any components killed by command line switches
438 FINDCOMP(cptr, "to");
443 FINDCOMP(cptr, "cc");
447 if ((cp = getenv("USER"))) {
448 FINDCOMP(cptr, "user");
450 cptr->c_text = getcpy(cp);
456 ** pick any interesting stuff out of msg "inb"
458 for (state = FLD;;) {
459 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
464 ** if we're interested in this component, save
465 ** a pointer to the component text, then start
466 ** using our next free buffer as the component
467 ** temp buffer (buffer switching saves an extra
468 ** copy of the component text).
470 if ((cptr = wantcomp[CHASH(name)]))
472 if (!mh_strcasecmp(name, cptr->c_name)) {
473 char_read += msg_count;
474 if (! cptr->c_text) {
475 i = strlen(cptr->c_text = tmpbuf) - 1;
476 if (tmpbuf[i] == '\n')
481 i = strlen(cp = cptr->c_text) - 1;
483 if (cptr->c_type & CT_ADDR) {
485 cp = add(",\n\t", cp);
490 cptr->c_text = add(tmpbuf, cp);
492 while (state == FLDPLUS) {
493 state = m_getfld(state, name, tmpbuf,
495 cptr->c_text = add(tmpbuf, cptr->c_text);
496 char_read += msg_count;
500 } while ((cptr = cptr->c_next));
502 while (state == FLDPLUS)
503 state = m_getfld(state, name, tmpbuf,
514 adios(NULL, "m_getfld() returned %d", state);
519 ** format and output the header lines.
524 ** if there's a "Subject" component, strip any "Re:"s off it
526 FINDCOMP(cptr, "subject")
527 if (cptr && (cp = cptr->c_text)) {
528 register char *sp = cp;
539 if (sp != cptr->c_text) {
541 cptr->c_text = getcpy(sp);
545 i = format_len + char_read + 256;
546 scanl = mh_xmalloc((size_t) i + 2);
550 dat[3] = OUTPUTLINELEN;
552 fmt_scan(fmt, scanl, i, dat);
555 fputs("\nrepl: bad addresses:\n", out);
556 fputs( badaddrs, out);
559 /* Check if we should filter the message */
563 adios(drft, "error writing");
565 replfilter(inb, out, filter);
570 adios(drft, "error writing");
574 /* add an attachment header */
577 snprintf(buffer, sizeof buffer, "+%s %s",
578 mp->foldpath, m_name(mp->lowsel));
579 if (execprogl("anno", "anno", "-append", "-nodate",
580 drft, "-comp", attach_hdr, "-text", buffer,
581 (char *)NULL) != 0) {
582 advise(NULL, "unable to add attachment header");
586 /* return dynamically allocated buffers */
588 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
590 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
592 free(*nxtbuf++); /* free unused nxtbufs */
593 free((char *) compbuffers);
594 free((char *) used_buf);
597 static char *buf; /* our current working buffer */
598 static char *bufend; /* end of working buffer */
599 static char *last_dst; /* buf ptr at end of last call */
600 static unsigned int bufsiz=0; /* current size of buf */
602 #define BUFINCR 512 /* how much to expand buf when if fills */
604 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
607 ** check if there's enough room in buf for str.
608 ** add more mem if needed
610 #define CHECKMEM(str) \
611 if ((len = strlen(str)) >= bufend - dst) {\
613 int n = last_dst - buf;\
614 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
615 buf = mh_xrealloc(buf, bufsiz);\
618 bufend = buf + bufsiz;\
623 ** fmt_scan will call this routine if the user includes the function
624 ** "(formataddr {component})" in a format string. "orig" is the
625 ** original contents of the string register. "str" is the address
626 ** string to be formatted and concatenated onto orig. This routine
627 ** returns a pointer to the concatenated address string.
629 ** We try to not do a lot of malloc/copy/free's (which is why we
630 ** don't call "getcpy") but still place no upper limit on the
631 ** length of the result string.
633 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
637 formataddr(char *orig, char *str)
640 char baddr[BUFSIZ], error[BUFSIZ];
641 register int isgroup;
645 register struct mailname *mp = NULL;
647 /* if we don't have a buffer yet, get one */
649 buf = mh_xmalloc(BUFINCR);
650 last_dst = buf; /* XXX */
651 bufsiz = BUFINCR - 6; /* leave some slop */
652 bufend = buf + bufsiz;
655 ** If "orig" points to our buffer we can just pick up where we
656 ** left off. Otherwise we have to copy orig into our buffer.
660 else if (!orig || !*orig) {
664 dst = last_dst; /* XXX */
669 /* concatenate all the new addresses onto 'buf' */
670 for (isgroup = 0; (cp = getname(str)); ) {
671 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
672 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
674 badaddrs = add(baddr, badaddrs);
677 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
682 /* if we get here we're going to add an address */
688 CHECKMEM(mp->m_gname);
708 insert(struct mailname *np)
711 register struct mailname *mp;
713 if (np->m_mbox == NULL)
716 for (mp = &mq; mp->m_next; mp = mp->m_next) {
717 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
718 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
721 if (!ccme && ismymbox(np))
725 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
727 if (!gans(buffer, anoyes))
738 ** This function expects that argument out has been fflushed by the caller.
741 replfilter(FILE *in, FILE *out, char *filter)
749 if (access(filter, R_OK) == NOTOK)
750 adios(filter, "unable to read");
753 lseek(fileno(in), (off_t) 0, SEEK_SET);
755 switch (pid = fork()) {
757 adios("fork", "unable to");
760 dup2(fileno(in), fileno(stdin));
761 dup2(fileno(out), fileno(stdout));
762 for (n=3; n<OPEN_MAX; n++) {
766 execlp("mhl", "mhl", "-form", filter, NULL);
767 errstr = strerror(errno);
768 write(2, "unable to exec mhl: ", 20);
769 write(2, errstr, strlen(errstr));
774 if (pidXwait(pid, "mhl"))
775 /* sysexits.h EX_SOFTWARE */
777 fseek(out, 0L, SEEK_END);