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))
316 adios(EX_USAGE, NULL, "only one message at a time!");
318 context_replace(curfolder, folder); /* update current folder */
319 seq_setcur(mp, mp->lowsel); /* update current message */
320 seq_save(mp); /* synchronize sequences */
321 context_save(); /* save the context file */
324 msg = file ? file : mh_xstrdup(m_name(mp->lowsel));
326 if ((in = fopen(msg, "r")) == NULL)
327 adios(EX_IOERR, msg, "unable to open");
329 /* find form (components) file */
332 form = etcpath(replgroupcomps);
334 form = etcpath(replcomps);
337 replout(in, drft, mp, mime, form, filter);
342 what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
347 docc(char *cp, int ccflag)
349 switch (smatch(cp, ccswitches)) {
351 ambigsw(cp, ccswitches);
354 adios(EX_USAGE, NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
369 ccto = cccc = ccme = ccflag;
378 replout(FILE *inb, char *drft, struct msgs *mp,
379 int mime, char *form, char *filter)
382 struct field f = {{0}};
386 int char_read = 0, format_len, mask;
391 mask = umask(~m_gmprot());
392 if ((out = fopen(drft, "w")) == NULL)
393 adios(EX_CANTCREAT, drft, "unable to create");
397 /* get new format string */
398 cp = new_fs(form, NULL);
399 format_len = strlen(cp);
401 /* compile format string */
402 ncomps = fmt_compile(cp, &fmt) + 1;
404 for (ap = addrcomps; *ap; ap++) {
407 cptr->c_type |= CT_ADDR;
411 ** ignore any components killed by command line switches
414 FINDCOMP(cptr, "to");
419 FINDCOMP(cptr, "cc");
423 if ((cp = getenv("USER"))) {
424 FINDCOMP(cptr, "user");
426 cptr->c_text = mh_xstrdup(cp);
432 ** pick any interesting stuff out of msg "inb"
434 for (state = FLD2;;) {
435 state = m_getfld2(state, &f, inb);
439 ** if we're interested in this component, save
440 ** a pointer to the component text, then start
441 ** using our next free buffer as the component
442 ** temp buffer (buffer switching saves an extra
443 ** copy of the component text).
445 if ((cptr = wantcomp[CHASH(f.name)])) {
447 if (mh_strcasecmp(f.name, cptr->c_name)!=0) {
450 char_read += strlen(f.value);
452 cptr->c_text = mh_xstrdup(f.value);
453 i = strlen(cptr->c_text) - 1;
454 if (cptr->c_text[i] == '\n') {
455 cptr->c_text[i] = '\0';
461 if (cptr->c_type & CT_ADDR) {
463 cp = add(",\n\t", cp);
468 cptr->c_text = add(f.value, cp);
471 } while ((cptr = cptr->c_next));
483 adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
488 ** format and output the header lines.
493 ** if there's a "Subject" component, strip any "Re:"s off it
495 FINDCOMP(cptr, "subject")
496 if (cptr && (cp = cptr->c_text)) {
508 if (sp != cptr->c_text) {
510 cptr->c_text = mh_xstrdup(sp);
514 i = format_len + char_read + 256;
515 scanl = mh_xcalloc(i + 2, sizeof(char));
519 dat[3] = OUTPUTLINELEN;
521 fmt_scan(fmt, scanl, i, dat);
524 fputs("\nrepl: bad addresses:\n", out);
525 fputs( badaddrs, out);
528 /* Check if we should filter the message */
532 adios(EX_IOERR, drft, "error writing");
534 replfilter(inb, out, filter);
539 adios(EX_IOERR, drft, "error writing");
543 /* add an attachment header */
546 snprintf(buffer, sizeof buffer, "+%s %s",
547 mp->foldpath, m_name(mp->lowsel));
548 if (execprogl("anno", "anno", "-append", "-nodate",
549 drft, "-comp", attach_hdr, "-text", buffer,
550 (char *)NULL) != 0) {
551 advise(NULL, "unable to add attachment header");
555 /* return dynamically allocated buffers */
559 static char *buf; /* our current working buffer */
560 static char *bufend; /* end of working buffer */
561 static char *last_dst; /* buf ptr at end of last call */
562 static unsigned int bufsiz=0; /* current size of buf */
564 #define BUFINCR 512 /* how much to expand buf when if fills */
566 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
569 ** check if there's enough room in buf for str.
570 ** add more mem if needed
572 #define CHECKMEM(str) \
573 if ((len = strlen(str)) >= bufend - dst) {\
575 int n = last_dst - buf;\
576 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
577 buf = mh_xrealloc(buf, bufsiz);\
580 bufend = buf + bufsiz;\
585 ** fmt_scan will call this routine if the user includes the function
586 ** "(formataddr {component})" in a format string. "orig" is the
587 ** original contents of the string register. "str" is the address
588 ** string to be formatted and concatenated onto orig. This routine
589 ** returns a pointer to the concatenated address string.
591 ** We try to not do a lot of malloc/copy/free's (which is why we
592 ** don't call "mh_xstrdup") but still place no upper limit on the
593 ** length of the result string.
595 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
599 formataddr(char *orig, char *str)
602 char baddr[BUFSIZ], error[BUFSIZ];
607 struct mailname *mp = NULL;
609 /* if we don't have a buffer yet, get one */
611 buf = mh_xcalloc(BUFINCR, sizeof(char));
612 last_dst = buf; /* XXX */
613 bufsiz = BUFINCR - 6; /* leave some slop */
614 bufend = buf + bufsiz;
617 ** If "orig" points to our buffer we can just pick up where we
618 ** left off. Otherwise we have to copy orig into our buffer.
622 else if (!orig || !*orig) {
626 dst = last_dst; /* XXX */
631 /* concatenate all the new addresses onto 'buf' */
632 for (isgroup = 0; (cp = getname(str)); ) {
633 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
634 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
636 badaddrs = add(baddr, badaddrs);
639 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
644 /* if we get here we're going to add an address */
650 CHECKMEM(mp->m_gname);
670 insert(struct mailname *np)
675 if (np->m_mbox == NULL)
678 for (mp = &mq; mp->m_next; mp = mp->m_next) {
679 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
680 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
683 if (!ccme && ismymbox(np))
687 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
689 if (!gans(buffer, anoyes))
700 ** This function expects that argument out has been fflushed by the caller.
703 replfilter(FILE *in, FILE *out, char *filter)
705 int pid, pid_show, n;
711 if (access(filter, R_OK) == NOTOK)
712 adios(EX_IOERR, filter, "unable to read");
715 lseek(fileno(in), (off_t) 0, SEEK_SET);
717 switch (pid = fork()) {
719 adios(EX_OSERR, "fork", "unable to");
722 if (pipe(mailpipe) == -1) {
723 adios(EX_OSERR, "pipe", "can't create pipe");
726 switch (pid_show = fork()) {
728 adios(EX_OSERR, "fork", "unable to");
730 dup2(fileno(in), STDIN_FILENO);
731 dup2(mailpipe[1], STDOUT_FILENO);
736 for (n=3; n<OPEN_MAX; n++) {
739 execlp("show", "show", "-file", "-", NULL);
740 adios(EX_OSERR, "exec", "unable to");
743 dup2(mailpipe[0], STDIN_FILENO);
744 dup2(fileno(out), STDOUT_FILENO);
749 for (n=3; n<OPEN_MAX; n++) {
752 execlp("mhl", "mhl", "-form", filter, NULL);
753 adios(EX_OSERR, "exec", "unable to");
758 if (pidXwait(pid, "mhl")) {
761 fseek(out, 0L, SEEK_END);