3 * replsbr.c -- routines to help repl along...
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <sys/file.h> /* L_SET */
17 extern short ccto; /* from repl.c */
24 static char *badaddrs = NULL;
25 static char *dfhost = NULL;
27 static struct mailname mq = { NULL };
30 * Buffer size for content part of header fields.
31 * We want this to be large enough so that we don't
32 * do a lot of extra FLDPLUS calls on m_getfld but
33 * small enough so that we don't snarf the entire
34 * message body when we're not going to use any of it.
38 static struct format *fmt;
40 static int ncomps = 0; /* # of interesting components */
41 static char **compbuffers = NULL; /* buffers for component text */
42 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
44 static int dat[5]; /* aux. data for format routine */
46 static char *addrcomps[] = {
65 static int insert (struct mailname *);
66 static void replfilter (FILE *, FILE *, char *);
70 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
71 int mime, char *form, char *filter, char *fcc)
73 register int state, i;
74 register struct comp *cptr;
75 register char *tmpbuf;
76 register char **nxtbuf;
78 register struct comp **savecomp;
79 int char_read = 0, format_len;
80 char name[NAMESZ], *scanl, *cp;
84 if ((out = fopen (drft, "w")) == NULL)
85 adios (drft, "unable to create");
87 /* get new format string */
88 cp = new_fs (form, NULL, NULL);
89 format_len = strlen (cp);
91 /* compile format string */
92 ncomps = fmt_compile (cp, &fmt) + 1;
94 if (!(nxtbuf = compbuffers = (char **)
95 calloc((size_t) ncomps, sizeof(char *))))
96 adios (NULL, "unable to allocate component buffers");
97 if (!(savecomp = used_buf = (struct comp **)
98 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
99 adios (NULL, "unable to allocate component buffer stack");
100 savecomp += ncomps + 1;
101 *--savecomp = NULL; /* point at zero'd end minus 1 */
103 for (i = ncomps; i--; )
104 if (!(*nxtbuf++ = malloc(SBUFSIZ)))
105 adios (NULL, "unable to allocate component buffer");
107 nxtbuf = compbuffers; /* point at start */
110 for (ap = addrcomps; *ap; ap++) {
111 FINDCOMP (cptr, *ap);
113 cptr->c_type |= CT_ADDR;
117 * ignore any components killed by command line switches
120 FINDCOMP (cptr, "to");
125 FINDCOMP (cptr, "cc");
129 /* set up the "fcc" pseudo-component */
131 FINDCOMP (cptr, "fcc");
133 cptr->c_text = getcpy (fcc);
135 if ((cp = getenv("USER"))) {
136 FINDCOMP (cptr, "user");
138 cptr->c_text = getcpy(cp);
144 * pick any interesting stuff out of msg "inb"
146 for (state = FLD;;) {
147 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
152 * if we're interested in this component, save a pointer
153 * to the component text, then start using our next free
154 * buffer as the component temp buffer (buffer switching
155 * saves an extra copy of the component text).
157 if ((cptr = wantcomp[CHASH(name)]))
159 if (!strcasecmp(name, cptr->c_name)) {
160 char_read += msg_count;
161 if (! cptr->c_text) {
162 cptr->c_text = tmpbuf;
166 i = strlen (cp = cptr->c_text) - 1;
168 if (cptr->c_type & CT_ADDR) {
170 cp = add (",\n\t", cp);
175 cptr->c_text = add (tmpbuf, cp);
177 while (state == FLDPLUS) {
178 state = m_getfld (state, name, tmpbuf,
180 cptr->c_text = add (tmpbuf, cptr->c_text);
181 char_read += msg_count;
185 } while ((cptr = cptr->c_next));
187 while (state == FLDPLUS)
188 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
198 adios (NULL, "m_getfld() returned %d", state);
203 * format and output the header lines.
208 * if there's a "Subject" component, strip any "Re:"s off it
210 FINDCOMP (cptr, "subject")
211 if (cptr && (cp = cptr->c_text)) {
212 register char *sp = cp;
223 if (sp != cptr->c_text) {
225 cptr->c_text = getcpy (sp);
229 i = format_len + char_read + 256;
230 scanl = malloc ((size_t) i + 2);
234 dat[3] = outputlinelen;
236 fmt_scan (fmt, scanl, i, dat);
239 fputs ("\nrepl: bad addresses:\n", out);
240 fputs ( badaddrs, out);
244 * Check if we should filter the message
245 * or add mhn directives
248 replfilter (inb, out, filter);
249 } else if (mime && mp) {
250 fprintf (out, "#forw [original message] +%s %s\n",
251 mp->foldpath, m_name (mp->lowsel));
255 adios (drft, "error writing");
258 /* return dynamically allocated buffers */
260 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
261 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
263 free (*nxtbuf++); /* free unused nxtbufs */
264 free ((char *) compbuffers);
265 free ((char *) used_buf);
268 static char *buf; /* our current working buffer */
269 static char *bufend; /* end of working buffer */
270 static char *last_dst; /* buf ptr at end of last call */
271 static unsigned int bufsiz=0; /* current size of buf */
273 #define BUFINCR 512 /* how much to expand buf when if fills */
275 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
278 * check if there's enough room in buf for str.
279 * add more mem if needed
281 #define CHECKMEM(str) \
282 if ((len = strlen (str)) >= bufend - dst) {\
284 int n = last_dst - buf;\
285 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
286 buf = realloc (buf, bufsiz);\
290 adios (NULL, "formataddr: couldn't get buffer space");\
291 bufend = buf + bufsiz;\
296 * fmt_scan will call this routine if the user includes the function
297 * "(formataddr {component})" in a format string. "orig" is the
298 * original contents of the string register. "str" is the address
299 * string to be formatted and concatenated onto orig. This routine
300 * returns a pointer to the concatenated address string.
302 * We try to not do a lot of malloc/copy/free's (which is why we
303 * don't call "getcpy") but still place no upper limit on the
304 * length of the result string.
307 formataddr (char *orig, char *str)
310 char baddr[BUFSIZ], error[BUFSIZ];
311 register int isgroup;
315 register struct mailname *mp = NULL;
317 /* if we don't have a buffer yet, get one */
319 buf = malloc (BUFINCR);
321 adios (NULL, "formataddr: couldn't allocate buffer space");
322 last_dst = buf; /* XXX */
323 bufsiz = BUFINCR - 6; /* leave some slop */
324 bufend = buf + bufsiz;
327 * If "orig" points to our buffer we can just pick up where we
328 * left off. Otherwise we have to copy orig into our buffer.
332 else if (!orig || !*orig) {
336 dst = last_dst; /* XXX */
341 /* concatenate all the new addresses onto 'buf' */
342 for (isgroup = 0; (cp = getname (str)); ) {
343 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
344 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
345 badaddrs = add (baddr, badaddrs);
348 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
353 /* if we get here we're going to add an address */
359 CHECKMEM (mp->m_gname);
379 insert (struct mailname *np)
382 register struct mailname *mp;
384 if (np->m_mbox == NULL)
387 for (mp = &mq; mp->m_next; mp = mp->m_next) {
388 if (!strcasecmp (np->m_host, mp->m_next->m_host)
389 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox))
392 if (!ccme && ismymbox (np))
396 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
397 if (!gans (buffer, anoyes))
416 replfilter (FILE *in, FILE *out, char *filter)
424 if (access (filter, R_OK) == NOTOK)
425 adios (filter, "unable to read");
427 mhl = r1bindex (mhlproc, '/');
430 lseek (fileno(in), (off_t) 0, SEEK_SET);
433 switch (pid = vfork ()) {
435 adios ("fork", "unable to");
438 dup2 (fileno (in), fileno (stdin));
439 dup2 (fileno (out), fileno (stdout));
442 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
443 fprintf (stderr, "unable to exec ");
448 if (pidXwait (pid, mhl))
450 fseek (out, 0L, SEEK_END);