2 ** replsbr.c -- routines to help repl along...
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.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
13 #include <sys/file.h> /* L_SET */
16 extern short ccto; /* from repl.c */
23 static char *badaddrs = NULL;
24 static char *dfhost = NULL;
26 static struct mailname mq = { NULL };
29 ** Buffer size for content part of header fields.
30 ** We want this to be large enough so that we don't
31 ** do a lot of extra FLDPLUS calls on m_getfld but
32 ** small enough so that we don't snarf the entire
33 ** message body when we're not going to use any of it.
37 static struct format *fmt;
39 static int ncomps = 0; /* # of interesting components */
40 static char **compbuffers = NULL; /* buffers for component text */
41 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
43 static int dat[5]; /* aux. data for format routine */
45 static char *addrcomps[] = {
64 static int insert(struct mailname *);
65 static void replfilter(FILE *, FILE *, char *);
69 replout(FILE *inb, char *msg, char *drft, struct msgs *mp,
70 int mime, char *form, char *filter)
72 register int state, i;
73 register struct comp *cptr;
74 register char *tmpbuf;
75 register char **nxtbuf;
77 register struct comp **savecomp;
78 int char_read = 0, format_len, mask;
79 char name[NAMESZ], *scanl;
83 mask = umask(~m_gmprot());
84 if ((out = fopen(drft, "w")) == NULL)
85 adios(drft, "unable to create");
89 /* get new format string */
90 cp = new_fs(form, NULL, NULL);
91 format_len = strlen(cp);
93 /* compile format string */
94 ncomps = fmt_compile(cp, &fmt) + 1;
96 if (!(nxtbuf = compbuffers = (char **)
97 calloc((size_t) ncomps, sizeof(char *))))
98 adios(NULL, "unable to allocate component buffers");
99 if (!(savecomp = used_buf = (struct comp **)
100 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
101 adios(NULL, "unable to allocate component buffer stack");
102 savecomp += ncomps + 1;
103 *--savecomp = NULL; /* point at zero'd end minus 1 */
105 for (i = ncomps; i--; )
106 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
108 nxtbuf = compbuffers; /* point at start */
111 for (ap = addrcomps; *ap; ap++) {
114 cptr->c_type |= CT_ADDR;
118 ** ignore any components killed by command line switches
121 FINDCOMP(cptr, "to");
126 FINDCOMP(cptr, "cc");
130 if ((cp = getenv("USER"))) {
131 FINDCOMP(cptr, "user");
133 cptr->c_text = getcpy(cp);
139 ** pick any interesting stuff out of msg "inb"
141 for (state = FLD;;) {
142 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
147 ** if we're interested in this component, save
148 ** a pointer to the component text, then start
149 ** using our next free buffer as the component
150 ** temp buffer (buffer switching saves an extra
151 ** copy of the component text).
153 if ((cptr = wantcomp[CHASH(name)]))
155 if (!mh_strcasecmp(name, cptr->c_name)) {
156 char_read += msg_count;
157 if (! cptr->c_text) {
158 i = strlen(cptr->c_text = tmpbuf) - 1;
159 if (tmpbuf[i] == '\n')
164 i = strlen(cp = cptr->c_text) - 1;
166 if (cptr->c_type & CT_ADDR) {
168 cp = add(",\n\t", cp);
173 cptr->c_text = add(tmpbuf, cp);
175 while (state == FLDPLUS) {
176 state = m_getfld(state, name, tmpbuf,
178 cptr->c_text = add(tmpbuf, cptr->c_text);
179 char_read += msg_count;
183 } while ((cptr = cptr->c_next));
185 while (state == FLDPLUS)
186 state = m_getfld(state, name, tmpbuf,
197 adios(NULL, "m_getfld() returned %d", state);
202 ** format and output the header lines.
207 ** if there's a "Subject" component, strip any "Re:"s off it
209 FINDCOMP(cptr, "subject")
210 if (cptr && (cp = cptr->c_text)) {
211 register char *sp = cp;
222 if (sp != cptr->c_text) {
224 cptr->c_text = getcpy(sp);
228 i = format_len + char_read + 256;
229 scanl = mh_xmalloc((size_t) i + 2);
233 dat[3] = OUTPUTLINELEN;
235 fmt_scan(fmt, scanl, i, dat);
238 fputs("\nrepl: bad addresses:\n", out);
239 fputs( badaddrs, out);
242 /* Check if we should filter the message */
246 adios(drft, "error writing");
248 replfilter(inb, out, filter);
253 adios(drft, "error writing");
256 /* if we want mime, then add an attachment header */
257 if (!filter && mime && mp) {
260 snprintf(buffer, sizeof buffer, "+%s %s",
261 mp->foldpath, m_name(mp->lowsel));
262 annotate(drft, attach_hdr, buffer, 0, -2, 1);
265 /* return dynamically allocated buffers */
267 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
269 free(cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
271 free(*nxtbuf++); /* free unused nxtbufs */
272 free((char *) compbuffers);
273 free((char *) used_buf);
276 static char *buf; /* our current working buffer */
277 static char *bufend; /* end of working buffer */
278 static char *last_dst; /* buf ptr at end of last call */
279 static unsigned int bufsiz=0; /* current size of buf */
281 #define BUFINCR 512 /* how much to expand buf when if fills */
283 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
286 ** check if there's enough room in buf for str.
287 ** add more mem if needed
289 #define CHECKMEM(str) \
290 if ((len = strlen(str)) >= bufend - dst) {\
292 int n = last_dst - buf;\
293 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
294 buf = mh_xrealloc(buf, bufsiz);\
297 bufend = buf + bufsiz;\
302 ** fmt_scan will call this routine if the user includes the function
303 ** "(formataddr {component})" in a format string. "orig" is the
304 ** original contents of the string register. "str" is the address
305 ** string to be formatted and concatenated onto orig. This routine
306 ** returns a pointer to the concatenated address string.
308 ** We try to not do a lot of malloc/copy/free's (which is why we
309 ** don't call "getcpy") but still place no upper limit on the
310 ** length of the result string.
313 formataddr(char *orig, char *str)
316 char baddr[BUFSIZ], error[BUFSIZ];
317 register int isgroup;
321 register struct mailname *mp = NULL;
323 /* if we don't have a buffer yet, get one */
325 buf = mh_xmalloc(BUFINCR);
326 last_dst = buf; /* XXX */
327 bufsiz = BUFINCR - 6; /* leave some slop */
328 bufend = buf + bufsiz;
331 ** If "orig" points to our buffer we can just pick up where we
332 ** left off. Otherwise we have to copy orig into our buffer.
336 else if (!orig || !*orig) {
340 dst = last_dst; /* XXX */
345 /* concatenate all the new addresses onto 'buf' */
346 for (isgroup = 0; (cp = getname(str)); ) {
347 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
348 snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
350 badaddrs = add(baddr, badaddrs);
353 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
358 /* if we get here we're going to add an address */
364 CHECKMEM(mp->m_gname);
384 insert(struct mailname *np)
387 register struct mailname *mp;
389 if (np->m_mbox == NULL)
392 for (mp = &mq; mp->m_next; mp = mp->m_next) {
393 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
394 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
397 if (!ccme && ismymbox(np))
401 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
403 if (!gans(buffer, anoyes))
420 ** This function expects that argument out has been fflushed by the caller.
424 replfilter(FILE *in, FILE *out, char *filter)
433 if (access(filter, R_OK) == NOTOK)
434 adios(filter, "unable to read");
436 mhl = mhbasename(mhlproc);
439 lseek(fileno(in), (off_t) 0, SEEK_SET);
441 switch (pid = fork()) {
443 adios("fork", "unable to");
446 dup2(fileno(in), fileno(stdin));
447 dup2(fileno(out), fileno(stdout));
450 execlp(mhlproc, mhl, "-form", filter, "-noclear", NULL);
451 errstr = strerror(errno);
452 write(2, "unable to exec ", 15);
453 write(2, mhlproc, strlen(mhlproc));
455 write(2, errstr, strlen(errstr));
460 if (pidXwait(pid, mhl))
462 fseek(out, 0L, SEEK_END);