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 */
21 static struct swit switches[] = {
31 { "cc all|to|cc|me", 0 },
35 { "editor editor", 0 },
37 { "filter filterfile", 0 },
41 { "form formfile", 0 },
51 { "whatnowproc program", 0 },
57 { "file file", 4 }, /* interface from msh */
59 { "build", 5 }, /* interface from mhe */
63 char *version=VERSION;
65 static struct swit ccswitches[] = {
77 static short ccto = -1;
78 static short cccc = -1;
79 static short ccme = -1;
80 static short querysw = 0;
82 static short groupreply = 0; /* Is this a group reply? */
84 static int mime = 0; /* include original as MIME part */
85 static char *form = NULL; /* form (components) file */
86 static char *filter = NULL; /* message filter file */
90 static char *badaddrs = NULL;
91 static char *dfhost = NULL;
93 static struct mailname mq;
95 static struct format *fmt;
97 static int ncomps = 0; /* # of interesting components */
99 static int dat[5]; /* aux. data for format routine */
101 static char *addrcomps[] = {
120 static void docc(char *, int);
121 static int insert(struct mailname *);
122 static void replfilter(FILE *, FILE *, char *);
123 static void replout(FILE *, char *, struct msgs *, int,
128 main(int argc, char **argv)
131 char *cp, *cwd, *maildir, *file = NULL;
132 char *folder = NULL, *msg = NULL;
133 char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
134 char **argp, **arguments;
135 struct msgs *mp = NULL;
139 setlocale(LC_ALL, "");
140 invo_name = mhbasename(argv[0]);
142 /* read user profile/context */
145 filter = mh_xstrdup(etcpath(mhlreply));
147 arguments = getarguments(invo_name, argc, argv, 1);
150 while ((cp = *argp++)) {
152 switch (smatch(++cp, switches)) {
154 ambigsw(cp, switches);
157 adios(EX_USAGE, NULL, "-%s unknown", cp);
160 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
161 print_help(buf, switches, 1);
162 exit(argc == 2 ? EX_OK : EX_USAGE);
164 print_version(invo_name);
165 exit(argc == 2 ? EX_OK : EX_USAGE);
182 if (!(cp = *argp++) || *cp == '-')
183 adios(EX_USAGE, NULL, "missing argument to %s",
188 if (!(cp = *argp++) || *cp == '-')
189 adios(EX_USAGE, NULL, "missing argument to %s",
195 if (!(ed = *argp++) || *ed == '-')
196 adios(EX_USAGE, NULL, "missing argument to %s",
201 if (!(whatnowproc = *argp++) ||
203 adios(EX_USAGE, NULL, "missing argument to %s",
213 adios(EX_USAGE, NULL, "only one file at a time!");
214 if (!(cp = *argp++) || *cp == '-')
215 adios(EX_USAGE, NULL, "missing argument to %s",
217 file = mh_xstrdup(expanddir(cp));
220 if (!(form = *argp++) || *form == '-')
221 adios(EX_USAGE, NULL, "missing argument to %s",
226 if (!(cp = *argp++) || *cp == '-')
227 adios(EX_USAGE, NULL, "missing argument to %s",
229 filter = mh_xstrdup(etcpath(cp));
251 if (*cp == '+' || *cp == '@') {
253 adios(EX_USAGE, NULL, "only one folder at a time!");
255 folder = mh_xstrdup(expandfol(cp));
258 adios(EX_USAGE, NULL, "only one message at a time!");
271 cwd = mh_xstrdup(pwd());
273 if (file && (msg || folder))
274 adios(EX_USAGE, NULL, "can't mix files and folders/msgs");
276 strncpy(drft, buildsw ? toabsdir("reply") : m_draft(seq_beyond),
279 ** FIXME: (concerning MHE support (buildsw) only)
280 ** There's no check if the draft already exists. mmh has removed
281 ** this case by having the draft folder. I won't add code only to
282 ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
287 ** We are replying to a file.
289 anot = 0; /* we don't want to annotate a file */
292 ** We are replying to a message.
297 folder = getcurfol();
298 maildir = toabsdir(folder);
300 if (chdir(maildir) == NOTOK)
301 adios(EX_OSERR, maildir, "unable to change directory to");
303 /* read folder and create message structure */
304 if (!(mp = folder_read(folder)))
305 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
307 /* check for empty folder */
309 adios(EX_DATAERR, NULL, "no messages in %s", folder);
311 /* parse the message range/sequence/name and set SELECTED */
312 if (!m_convert(mp, msg))
314 seq_setprev(mp); /* set the previous-sequence */
317 adios(EX_USAGE, NULL, "only one message at a time!");
319 context_replace(curfolder, folder); /* update current folder */
320 seq_setcur(mp, mp->lowsel); /* update current message */
321 seq_save(mp); /* synchronize sequences */
322 context_save(); /* save the context file */
325 msg = file ? file : mh_xstrdup(m_name(mp->lowsel));
327 if ((in = fopen(msg, "r")) == NULL)
328 adios(EX_IOERR, msg, "unable to open");
330 /* find form (components) file */
333 form = etcpath(replgroupcomps);
335 form = etcpath(replcomps);
338 replout(in, drft, mp, mime, form, filter);
343 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
348 docc(char *cp, int ccflag)
350 switch (smatch(cp, ccswitches)) {
352 ambigsw(cp, ccswitches);
355 adios(EX_USAGE, NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
370 ccto = cccc = ccme = ccflag;
379 replout(FILE *inb, char *drft, struct msgs *mp,
380 int mime, char *form, char *filter)
383 struct field f = {{0}};
387 int char_read = 0, format_len, mask;
392 mask = umask(~m_gmprot());
393 if ((out = fopen(drft, "w")) == NULL)
394 adios(EX_CANTCREAT, drft, "unable to create");
398 /* get new format string */
399 cp = new_fs(form, NULL);
400 format_len = strlen(cp);
402 /* compile format string */
403 ncomps = fmt_compile(cp, &fmt) + 1;
405 for (ap = addrcomps; *ap; ap++) {
408 cptr->c_type |= CT_ADDR;
412 ** ignore any components killed by command line switches
415 FINDCOMP(cptr, "to");
420 FINDCOMP(cptr, "cc");
424 if ((cp = getenv("USER"))) {
425 FINDCOMP(cptr, "user");
427 cptr->c_text = mh_xstrdup(cp);
433 ** pick any interesting stuff out of msg "inb"
435 for (state = FLD2;;) {
436 state = m_getfld2(state, &f, inb);
440 ** if we're interested in this component, save
441 ** a pointer to the component text, then start
442 ** using our next free buffer as the component
443 ** temp buffer (buffer switching saves an extra
444 ** copy of the component text).
446 if ((cptr = wantcomp[CHASH(f.name)])) {
448 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
451 char_read += strlen(f.value);
453 cptr->c_text = mh_xstrdup(f.value);
454 i = strlen(cptr->c_text) - 1;
455 if (cptr->c_text[i] == '\n') {
456 cptr->c_text[i] = '\0';
462 if (cptr->c_type & CT_ADDR) {
464 cp = add(",\n\t", cp);
469 cptr->c_text = add(f.value, cp);
472 } while ((cptr = cptr->c_next));
484 adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
489 ** format and output the header lines.
494 ** if there's a "Subject" component, strip any "Re:"s off it
496 FINDCOMP(cptr, "subject")
497 if (cptr && (cp = cptr->c_text)) {
509 if (sp != cptr->c_text) {
511 cptr->c_text = mh_xstrdup(sp);
515 i = format_len + char_read + 256;
516 scanl = mh_xcalloc(i + 2, sizeof(char));
520 dat[3] = OUTPUTLINELEN;
522 fmt_scan(fmt, scanl, i, dat);
525 fputs("\nrepl: bad addresses:\n", out);
526 fputs( badaddrs, out);
529 /* Check if we should filter the message */
533 adios(EX_IOERR, drft, "error writing");
535 replfilter(inb, out, filter);
540 adios(EX_IOERR, drft, "error writing");
544 /* add an attachment header */
547 snprintf(buffer, sizeof buffer, "+%s %s",
548 mp->foldpath, m_name(mp->lowsel));
549 if (execprogl("anno", "anno", "-append", "-nodate",
550 drft, "-comp", attach_hdr, "-text", buffer,
551 (char *)NULL) != 0) {
552 advise(NULL, "unable to add attachment header");
556 /* return dynamically allocated buffers */
560 static char *buf; /* our current working buffer */
561 static char *bufend; /* end of working buffer */
562 static char *last_dst; /* buf ptr at end of last call */
563 static unsigned int bufsiz=0; /* current size of buf */
565 #define BUFINCR 512 /* how much to expand buf when if fills */
567 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
570 ** check if there's enough room in buf for str.
571 ** add more mem if needed
573 #define CHECKMEM(str) \
574 if ((len = strlen(str)) >= bufend - dst) {\
576 int n = last_dst - buf;\
577 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
578 buf = mh_xrealloc(buf, bufsiz);\
581 bufend = buf + bufsiz;\
586 ** fmt_scan will call this routine if the user includes the function
587 ** "(formataddr {component})" in a format string. "orig" is the
588 ** original contents of the string register. "str" is the address
589 ** string to be formatted and concatenated onto orig. This routine
590 ** returns a pointer to the concatenated address string.
592 ** We try to not do a lot of malloc/copy/free's (which is why we
593 ** don't call "mh_xstrdup") but still place no upper limit on the
594 ** length of the result string.
596 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
600 formataddr(char *orig, char *str)
603 char baddr[BUFSIZ], error[BUFSIZ];
608 struct mailname *mp = NULL;
610 /* if we don't have a buffer yet, get one */
612 buf = mh_xcalloc(BUFINCR, sizeof(char));
613 last_dst = buf; /* XXX */
614 bufsiz = BUFINCR - 6; /* leave some slop */
615 bufend = buf + bufsiz;
618 ** If "orig" points to our buffer we can just pick up where we
619 ** left off. Otherwise we have to copy orig into our buffer.
623 else if (!orig || !*orig) {
627 dst = last_dst; /* XXX */
632 /* concatenate all the new addresses onto 'buf' */
633 for (isgroup = 0; (cp = getname(str)); ) {
634 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
635 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
637 badaddrs = add(baddr, badaddrs);
640 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
645 /* if we get here we're going to add an address */
651 CHECKMEM(mp->m_gname);
671 insert(struct mailname *np)
676 if (np->m_mbox == NULL)
679 for (mp = &mq; mp->m_next; mp = mp->m_next) {
680 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
681 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
684 if (!ccme && ismymbox(np))
688 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
690 if (!gans(buffer, anoyes))
701 ** This function expects that argument out has been fflushed by the caller.
704 replfilter(FILE *in, FILE *out, char *filter)
706 int pid, pid_show, n;
712 if (access(filter, R_OK) == NOTOK)
713 adios(EX_IOERR, filter, "unable to read");
716 lseek(fileno(in), (off_t) 0, SEEK_SET);
718 switch (pid = fork()) {
720 adios(EX_OSERR, "fork", "unable to");
723 if (pipe(mailpipe) == -1) {
724 adios(EX_OSERR, "pipe", "can't create pipe");
727 switch (pid_show = fork()) {
729 adios(EX_OSERR, "fork", "unable to");
731 dup2(fileno(in), STDIN_FILENO);
732 dup2(mailpipe[1], STDOUT_FILENO);
737 for (n=3; n<OPEN_MAX; n++) {
740 execlp("show", "show", "-file", "-", NULL);
741 adios(EX_OSERR, "exec", "unable to");
744 dup2(mailpipe[0], STDIN_FILENO);
745 dup2(fileno(out), STDOUT_FILENO);
750 for (n=3; n<OPEN_MAX; n++) {
753 execlp("mhl", "mhl", "-form", filter, NULL);
754 adios(EX_OSERR, "exec", "unable to");
759 if (pidXwait(pid, "mhl")) {
762 fseek(out, 0L, SEEK_END);