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 */
20 static struct swit switches[] = {
30 { "cc all|to|cc|me", 0 },
34 { "editor editor", 0 },
36 { "filter filterfile", 0 },
40 { "form formfile", 0 },
50 { "whatnowproc program", 0 },
56 { "file file", 4 }, /* interface from msh */
58 { "build", 5 }, /* interface from mhe */
62 static struct swit ccswitches[] = {
75 ** Buffer size for content part of header fields.
76 ** We want this to be large enough so that we don't
77 ** do a lot of extra FLDPLUS calls on m_getfld but
78 ** small enough so that we don't snarf the entire
79 ** message body when we're not going to use any of it.
83 static short ccto = -1;
84 static short cccc = -1;
85 static short ccme = -1;
86 static short querysw = 0;
88 static short groupreply = 0; /* Is this a group reply? */
90 static int mime = 0; /* include original as MIME part */
91 static char *form = NULL; /* form (components) file */
92 static char *filter = NULL; /* message filter file */
96 static char *badaddrs = NULL;
97 static char *dfhost = NULL;
99 static struct mailname mq;
101 static struct format *fmt;
103 static int ncomps = 0; /* # of interesting components */
104 static char **compbuffers = NULL; /* buffers for component text */
105 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
107 static int dat[5]; /* aux. data for format routine */
109 static char *addrcomps[] = {
128 static void docc(char *, int);
129 static int insert(struct mailname *);
130 static void replfilter(FILE *, FILE *, char *);
131 static void replout(FILE *, char *, struct msgs *, int,
136 main(int argc, char **argv)
139 char *cp, *cwd, *maildir, *file = NULL;
140 char *folder = NULL, *msg = NULL;
141 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
142 char **argp, **arguments;
143 struct msgs *mp = NULL;
147 filter = getcpy(etcpath(mhlreply));
149 setlocale(LC_ALL, "");
150 invo_name = mhbasename(argv[0]);
152 /* read user profile/context */
155 arguments = getarguments(invo_name, argc, argv, 1);
158 while ((cp = *argp++)) {
160 switch (smatch(++cp, switches)) {
162 ambigsw(cp, switches);
165 adios(NULL, "-%s unknown", cp);
168 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
169 print_help(buf, switches, 1);
172 print_version(invo_name);
190 if (!(cp = *argp++) || *cp == '-')
191 adios(NULL, "missing argument to %s",
196 if (!(cp = *argp++) || *cp == '-')
197 adios(NULL, "missing argument to %s",
203 if (!(ed = *argp++) || *ed == '-')
204 adios(NULL, "missing argument to %s",
209 if (!(whatnowproc = *argp++) ||
211 adios(NULL, "missing argument to %s",
221 adios(NULL, "only one file at a time!");
222 if (!(cp = *argp++) || *cp == '-')
223 adios(NULL, "missing argument to %s",
225 file = getcpy(expanddir(cp));
228 if (!(form = *argp++) || *form == '-')
229 adios(NULL, "missing argument to %s",
234 if (!(cp = *argp++) || *cp == '-')
235 adios(NULL, "missing argument to %s",
237 filter = getcpy(etcpath(cp));
259 if (*cp == '+' || *cp == '@') {
261 adios(NULL, "only one folder at a time!");
263 folder = getcpy(expandfol(cp));
266 adios(NULL, "only one message at a time!");
281 if (file && (msg || folder))
282 adios(NULL, "can't mix files and folders/msgs");
284 strncpy(drft, buildsw ? toabsdir("reply") : m_draft(seq_beyond),
287 ** FIXME: (concerning MHE support (buildsw) only)
288 ** There's no check if the draft already exists. mmh has removed
289 ** this case by having the draft folder. I won't add code only to
290 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
295 ** We are replying to a file.
297 anot = 0; /* we don't want to annotate a file */
300 ** We are replying to a message.
305 folder = getcurfol();
306 maildir = toabsdir(folder);
308 if (chdir(maildir) == NOTOK)
309 adios(maildir, "unable to change directory to");
311 /* read folder and create message structure */
312 if (!(mp = folder_read(folder)))
313 adios(NULL, "unable to read folder %s", folder);
315 /* check for empty folder */
317 adios(NULL, "no messages in %s", folder);
319 /* parse the message range/sequence/name and set SELECTED */
320 if (!m_convert(mp, msg))
321 /* sysexits.h EX_USAGE */
323 seq_setprev(mp); /* set the previous-sequence */
326 adios(NULL, "only one message at a time!");
328 context_replace(curfolder, folder); /* update current folder */
329 seq_setcur(mp, mp->lowsel); /* update current message */
330 seq_save(mp); /* synchronize sequences */
331 context_save(); /* save the context file */
334 msg = file ? file : getcpy(m_name(mp->lowsel));
336 if ((in = fopen(msg, "r")) == NULL)
337 adios(msg, "unable to open");
339 /* find form (components) file */
342 form = etcpath(replgroupcomps);
344 form = etcpath(replcomps);
347 replout(in, drft, mp, mime, form, filter);
352 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
353 /* sysexits.h EX_SOFTWARE */
358 docc(char *cp, int ccflag)
360 switch (smatch(cp, ccswitches)) {
362 ambigsw(cp, ccswitches);
365 adios(NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
380 ccto = cccc = ccme = ccflag;
389 replout(FILE *inb, char *drft, struct msgs *mp,
390 int mime, char *form, char *filter)
392 register int state, i;
393 register struct comp *cptr;
394 register char *tmpbuf;
395 register char **nxtbuf;
397 register struct comp **savecomp;
398 int char_read = 0, format_len, mask;
399 char name[NAMESZ], *scanl;
403 mask = umask(~m_gmprot());
404 if ((out = fopen(drft, "w")) == NULL)
405 adios(drft, "unable to create");
409 /* get new format string */
410 cp = new_fs(form, NULL);
411 format_len = strlen(cp);
413 /* compile format string */
414 ncomps = fmt_compile(cp, &fmt) + 1;
416 if (!(nxtbuf = compbuffers = (char **)
417 calloc((size_t) ncomps, sizeof(char *))))
418 adios(NULL, "unable to allocate component buffers");
419 if (!(savecomp = used_buf = (struct comp **)
420 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
421 adios(NULL, "unable to allocate component buffer stack");
422 savecomp += ncomps + 1;
423 *--savecomp = NULL; /* point at zero'd end minus 1 */
425 for (i = ncomps; i--; )
426 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
428 nxtbuf = compbuffers; /* point at start */
431 for (ap = addrcomps; *ap; ap++) {
434 cptr->c_type |= CT_ADDR;
438 ** ignore any components killed by command line switches
441 FINDCOMP(cptr, "to");
446 FINDCOMP(cptr, "cc");
450 if ((cp = getenv("USER"))) {
451 FINDCOMP(cptr, "user");
453 cptr->c_text = getcpy(cp);
459 ** pick any interesting stuff out of msg "inb"
461 for (state = FLD;;) {
462 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
467 ** if we're interested in this component, save
468 ** a pointer to the component text, then start
469 ** using our next free buffer as the component
470 ** temp buffer (buffer switching saves an extra
471 ** copy of the component text).
473 if ((cptr = wantcomp[CHASH(name)]))
475 if (!mh_strcasecmp(name, cptr->c_name)) {
476 char_read += msg_count;
477 if (! cptr->c_text) {
478 i = strlen(cptr->c_text = tmpbuf) - 1;
479 if (tmpbuf[i] == '\n')
484 i = strlen(cp = cptr->c_text) - 1;
486 if (cptr->c_type & CT_ADDR) {
488 cp = add(",\n\t", cp);
493 cptr->c_text = add(tmpbuf, cp);
495 while (state == FLDPLUS) {
496 state = m_getfld(state, name, tmpbuf,
498 cptr->c_text = add(tmpbuf, cptr->c_text);
499 char_read += msg_count;
503 } while ((cptr = cptr->c_next));
505 while (state == FLDPLUS)
506 state = m_getfld(state, name, tmpbuf,
517 adios(NULL, "m_getfld() returned %d", state);
522 ** format and output the header lines.
527 ** if there's a "Subject" component, strip any "Re:"s off it
529 FINDCOMP(cptr, "subject")
530 if (cptr && (cp = cptr->c_text)) {
531 register char *sp = cp;
542 if (sp != cptr->c_text) {
544 cptr->c_text = getcpy(sp);
548 i = format_len + char_read + 256;
549 scanl = mh_xmalloc((size_t) i + 2);
553 dat[3] = OUTPUTLINELEN;
555 fmt_scan(fmt, scanl, i, dat);
558 fputs("\nrepl: bad addresses:\n", out);
559 fputs( badaddrs, out);
562 /* Check if we should filter the message */
566 adios(drft, "error writing");
568 replfilter(inb, out, filter);
573 adios(drft, "error writing");
577 /* add an attachment header */
580 snprintf(buffer, sizeof buffer, "+%s %s",
581 mp->foldpath, m_name(mp->lowsel));
582 if (execprogl("anno", "anno", "-append", "-nodate",
583 drft, "-comp", attach_hdr, "-text", buffer,
584 (char *)NULL) != 0) {
585 advise(NULL, "unable to add attachment header");
589 /* return dynamically allocated buffers */
591 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
593 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
595 free(*nxtbuf++); /* free unused nxtbufs */
596 free((char *) compbuffers);
597 free((char *) used_buf);
600 static char *buf; /* our current working buffer */
601 static char *bufend; /* end of working buffer */
602 static char *last_dst; /* buf ptr at end of last call */
603 static unsigned int bufsiz=0; /* current size of buf */
605 #define BUFINCR 512 /* how much to expand buf when if fills */
607 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
610 ** check if there's enough room in buf for str.
611 ** add more mem if needed
613 #define CHECKMEM(str) \
614 if ((len = strlen(str)) >= bufend - dst) {\
616 int n = last_dst - buf;\
617 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
618 buf = mh_xrealloc(buf, bufsiz);\
621 bufend = buf + bufsiz;\
626 ** fmt_scan will call this routine if the user includes the function
627 ** "(formataddr {component})" in a format string. "orig" is the
628 ** original contents of the string register. "str" is the address
629 ** string to be formatted and concatenated onto orig. This routine
630 ** returns a pointer to the concatenated address string.
632 ** We try to not do a lot of malloc/copy/free's (which is why we
633 ** don't call "getcpy") but still place no upper limit on the
634 ** length of the result string.
636 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
640 formataddr(char *orig, char *str)
643 char baddr[BUFSIZ], error[BUFSIZ];
644 register int isgroup;
648 register struct mailname *mp = NULL;
650 /* if we don't have a buffer yet, get one */
652 buf = mh_xmalloc(BUFINCR);
653 last_dst = buf; /* XXX */
654 bufsiz = BUFINCR - 6; /* leave some slop */
655 bufend = buf + bufsiz;
658 ** If "orig" points to our buffer we can just pick up where we
659 ** left off. Otherwise we have to copy orig into our buffer.
663 else if (!orig || !*orig) {
667 dst = last_dst; /* XXX */
672 /* concatenate all the new addresses onto 'buf' */
673 for (isgroup = 0; (cp = getname(str)); ) {
674 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
675 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
677 badaddrs = add(baddr, badaddrs);
680 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
685 /* if we get here we're going to add an address */
691 CHECKMEM(mp->m_gname);
711 insert(struct mailname *np)
714 register struct mailname *mp;
716 if (np->m_mbox == NULL)
719 for (mp = &mq; mp->m_next; mp = mp->m_next) {
720 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
721 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
724 if (!ccme && ismymbox(np))
728 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
730 if (!gans(buffer, anoyes))
741 ** This function expects that argument out has been fflushed by the caller.
744 replfilter(FILE *in, FILE *out, char *filter)
752 if (access(filter, R_OK) == NOTOK)
753 adios(filter, "unable to read");
756 lseek(fileno(in), (off_t) 0, SEEK_SET);
758 switch (pid = fork()) {
760 adios("fork", "unable to");
763 dup2(fileno(in), fileno(stdin));
764 dup2(fileno(out), fileno(stdout));
765 for (n=3; n<OPEN_MAX; n++) {
769 execlp("mhl", "mhl", "-form", filter, NULL);
770 errstr = strerror(errno);
771 write(2, "unable to exec mhl: ", 20);
772 write(2, errstr, strlen(errstr));
777 if (pidXwait(pid, "mhl"))
778 /* sysexits.h EX_SOFTWARE */
780 fseek(out, 0L, SEEK_END);