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))
319 seq_setprev(mp); /* set the previous-sequence */
322 adios(NULL, "only one message at a time!");
324 context_replace(curfolder, folder); /* update current folder */
325 seq_setcur(mp, mp->lowsel); /* update current message */
326 seq_save(mp); /* synchronize sequences */
327 context_save(); /* save the context file */
330 msg = file ? file : getcpy(m_name(mp->lowsel));
332 if ((in = fopen(msg, "r")) == NULL)
333 adios(msg, "unable to open");
335 /* find form (components) file */
338 form = etcpath(replgroupcomps);
340 form = etcpath(replcomps);
343 replout(in, drft, mp, mime, form, filter);
348 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
354 docc(char *cp, int ccflag)
356 switch (smatch(cp, ccswitches)) {
358 ambigsw(cp, ccswitches);
361 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
376 ccto = cccc = ccme = ccflag;
385 replout(FILE *inb, char *drft, struct msgs *mp,
386 int mime, char *form, char *filter)
388 register int state, i;
389 register struct comp *cptr;
390 register char *tmpbuf;
391 register char **nxtbuf;
393 register struct comp **savecomp;
394 int char_read = 0, format_len, mask;
395 char name[NAMESZ], *scanl;
399 mask = umask(~m_gmprot());
400 if ((out = fopen(drft, "w")) == NULL)
401 adios(drft, "unable to create");
405 /* get new format string */
406 cp = new_fs(form, NULL);
407 format_len = strlen(cp);
409 /* compile format string */
410 ncomps = fmt_compile(cp, &fmt) + 1;
412 if (!(nxtbuf = compbuffers = (char **)
413 calloc((size_t) ncomps, sizeof(char *))))
414 adios(NULL, "unable to allocate component buffers");
415 if (!(savecomp = used_buf = (struct comp **)
416 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
417 adios(NULL, "unable to allocate component buffer stack");
418 savecomp += ncomps + 1;
419 *--savecomp = NULL; /* point at zero'd end minus 1 */
421 for (i = ncomps; i--; )
422 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
424 nxtbuf = compbuffers; /* point at start */
427 for (ap = addrcomps; *ap; ap++) {
430 cptr->c_type |= CT_ADDR;
434 ** ignore any components killed by command line switches
437 FINDCOMP(cptr, "to");
442 FINDCOMP(cptr, "cc");
446 if ((cp = getenv("USER"))) {
447 FINDCOMP(cptr, "user");
449 cptr->c_text = getcpy(cp);
455 ** pick any interesting stuff out of msg "inb"
457 for (state = FLD;;) {
458 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
463 ** if we're interested in this component, save
464 ** a pointer to the component text, then start
465 ** using our next free buffer as the component
466 ** temp buffer (buffer switching saves an extra
467 ** copy of the component text).
469 if ((cptr = wantcomp[CHASH(name)]))
471 if (!mh_strcasecmp(name, cptr->c_name)) {
472 char_read += msg_count;
473 if (! cptr->c_text) {
474 i = strlen(cptr->c_text = tmpbuf) - 1;
475 if (tmpbuf[i] == '\n')
480 i = strlen(cp = cptr->c_text) - 1;
482 if (cptr->c_type & CT_ADDR) {
484 cp = add(",\n\t", cp);
489 cptr->c_text = add(tmpbuf, cp);
491 while (state == FLDPLUS) {
492 state = m_getfld(state, name, tmpbuf,
494 cptr->c_text = add(tmpbuf, cptr->c_text);
495 char_read += msg_count;
499 } while ((cptr = cptr->c_next));
501 while (state == FLDPLUS)
502 state = m_getfld(state, name, tmpbuf,
513 adios(NULL, "m_getfld() returned %d", state);
518 ** format and output the header lines.
523 ** if there's a "Subject" component, strip any "Re:"s off it
525 FINDCOMP(cptr, "subject")
526 if (cptr && (cp = cptr->c_text)) {
527 register char *sp = cp;
538 if (sp != cptr->c_text) {
540 cptr->c_text = getcpy(sp);
544 i = format_len + char_read + 256;
545 scanl = mh_xmalloc((size_t) i + 2);
549 dat[3] = OUTPUTLINELEN;
551 fmt_scan(fmt, scanl, i, dat);
554 fputs("\nrepl: bad addresses:\n", out);
555 fputs( badaddrs, out);
558 /* Check if we should filter the message */
562 adios(drft, "error writing");
564 replfilter(inb, out, filter);
569 adios(drft, "error writing");
573 /* add an attachment header */
576 snprintf(buffer, sizeof buffer, "anno -append -nodate '%s' "
577 "-comp '%s' -text '+%s %s'",
579 attach_hdr, mp->foldpath, m_name(mp->lowsel));
580 if (system(buffer) != 0) {
581 advise(NULL, "unable to add attachment header");
585 /* return dynamically allocated buffers */
587 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
589 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
591 free(*nxtbuf++); /* free unused nxtbufs */
592 free((char *) compbuffers);
593 free((char *) used_buf);
596 static char *buf; /* our current working buffer */
597 static char *bufend; /* end of working buffer */
598 static char *last_dst; /* buf ptr at end of last call */
599 static unsigned int bufsiz=0; /* current size of buf */
601 #define BUFINCR 512 /* how much to expand buf when if fills */
603 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
606 ** check if there's enough room in buf for str.
607 ** add more mem if needed
609 #define CHECKMEM(str) \
610 if ((len = strlen(str)) >= bufend - dst) {\
612 int n = last_dst - buf;\
613 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
614 buf = mh_xrealloc(buf, bufsiz);\
617 bufend = buf + bufsiz;\
622 ** fmt_scan will call this routine if the user includes the function
623 ** "(formataddr {component})" in a format string. "orig" is the
624 ** original contents of the string register. "str" is the address
625 ** string to be formatted and concatenated onto orig. This routine
626 ** returns a pointer to the concatenated address string.
628 ** We try to not do a lot of malloc/copy/free's (which is why we
629 ** don't call "getcpy") but still place no upper limit on the
630 ** length of the result string.
632 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
636 formataddr(char *orig, char *str)
639 char baddr[BUFSIZ], error[BUFSIZ];
640 register int isgroup;
644 register struct mailname *mp = NULL;
646 /* if we don't have a buffer yet, get one */
648 buf = mh_xmalloc(BUFINCR);
649 last_dst = buf; /* XXX */
650 bufsiz = BUFINCR - 6; /* leave some slop */
651 bufend = buf + bufsiz;
654 ** If "orig" points to our buffer we can just pick up where we
655 ** left off. Otherwise we have to copy orig into our buffer.
659 else if (!orig || !*orig) {
663 dst = last_dst; /* XXX */
668 /* concatenate all the new addresses onto 'buf' */
669 for (isgroup = 0; (cp = getname(str)); ) {
670 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
671 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
673 badaddrs = add(baddr, badaddrs);
676 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
681 /* if we get here we're going to add an address */
687 CHECKMEM(mp->m_gname);
707 insert(struct mailname *np)
710 register struct mailname *mp;
712 if (np->m_mbox == NULL)
715 for (mp = &mq; mp->m_next; mp = mp->m_next) {
716 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
717 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
720 if (!ccme && ismymbox(np))
724 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
726 if (!gans(buffer, anoyes))
737 ** This function expects that argument out has been fflushed by the caller.
740 replfilter(FILE *in, FILE *out, char *filter)
748 if (access(filter, R_OK) == NOTOK)
749 adios(filter, "unable to read");
752 lseek(fileno(in), (off_t) 0, SEEK_SET);
754 switch (pid = fork()) {
756 adios("fork", "unable to");
759 dup2(fileno(in), fileno(stdin));
760 dup2(fileno(out), fileno(stdout));
761 for (n=3; n<OPEN_MAX; n++) {
765 execlp("mhl", "mhl", "-form", filter, NULL);
766 errstr = strerror(errno);
767 write(2, "unable to exec mhl: ", 20);
768 write(2, errstr, strlen(errstr));
773 if (pidXwait(pid, "mhl"))
775 fseek(out, 0L, SEEK_END);