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, mask;
80 char name[NAMESZ], *scanl, *cp;
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 if (!(*nxtbuf++ = malloc(SBUFSIZ)))
107 adios (NULL, "unable to allocate component buffer");
109 nxtbuf = compbuffers; /* point at start */
112 for (ap = addrcomps; *ap; ap++) {
113 FINDCOMP (cptr, *ap);
115 cptr->c_type |= CT_ADDR;
119 * ignore any components killed by command line switches
122 FINDCOMP (cptr, "to");
127 FINDCOMP (cptr, "cc");
131 /* set up the "fcc" pseudo-component */
133 FINDCOMP (cptr, "fcc");
135 cptr->c_text = getcpy (fcc);
137 if ((cp = getenv("USER"))) {
138 FINDCOMP (cptr, "user");
140 cptr->c_text = getcpy(cp);
146 * pick any interesting stuff out of msg "inb"
148 for (state = FLD;;) {
149 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
154 * if we're interested in this component, save a pointer
155 * to the component text, then start using our next free
156 * buffer as the component temp buffer (buffer switching
157 * saves an extra copy of the component text).
159 if ((cptr = wantcomp[CHASH(name)]))
161 if (!strcasecmp(name, cptr->c_name)) {
162 char_read += msg_count;
163 if (! cptr->c_text) {
164 cptr->c_text = tmpbuf;
168 i = strlen (cp = cptr->c_text) - 1;
170 if (cptr->c_type & CT_ADDR) {
172 cp = add (",\n\t", cp);
177 cptr->c_text = add (tmpbuf, cp);
179 while (state == FLDPLUS) {
180 state = m_getfld (state, name, tmpbuf,
182 cptr->c_text = add (tmpbuf, cptr->c_text);
183 char_read += msg_count;
187 } while ((cptr = cptr->c_next));
189 while (state == FLDPLUS)
190 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
200 adios (NULL, "m_getfld() returned %d", state);
205 * format and output the header lines.
210 * if there's a "Subject" component, strip any "Re:"s off it
212 FINDCOMP (cptr, "subject")
213 if (cptr && (cp = cptr->c_text)) {
214 register char *sp = cp;
225 if (sp != cptr->c_text) {
227 cptr->c_text = getcpy (sp);
231 i = format_len + char_read + 256;
232 scanl = malloc ((size_t) i + 2);
236 dat[3] = outputlinelen;
238 fmt_scan (fmt, scanl, i, dat);
241 fputs ("\nrepl: bad addresses:\n", out);
242 fputs ( badaddrs, out);
246 * Check if we should filter the message
247 * or add mhn directives
250 replfilter (inb, out, filter);
251 } else if (mime && mp) {
252 fprintf (out, "#forw [original message] +%s %s\n",
253 mp->foldpath, m_name (mp->lowsel));
257 adios (drft, "error writing");
260 /* return dynamically allocated buffers */
262 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
263 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
265 free (*nxtbuf++); /* free unused nxtbufs */
266 free ((char *) compbuffers);
267 free ((char *) used_buf);
270 static char *buf; /* our current working buffer */
271 static char *bufend; /* end of working buffer */
272 static char *last_dst; /* buf ptr at end of last call */
273 static unsigned int bufsiz=0; /* current size of buf */
275 #define BUFINCR 512 /* how much to expand buf when if fills */
277 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
280 * check if there's enough room in buf for str.
281 * add more mem if needed
283 #define CHECKMEM(str) \
284 if ((len = strlen (str)) >= bufend - dst) {\
286 int n = last_dst - buf;\
287 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
288 buf = realloc (buf, bufsiz);\
292 adios (NULL, "formataddr: couldn't get buffer space");\
293 bufend = buf + bufsiz;\
298 * fmt_scan will call this routine if the user includes the function
299 * "(formataddr {component})" in a format string. "orig" is the
300 * original contents of the string register. "str" is the address
301 * string to be formatted and concatenated onto orig. This routine
302 * returns a pointer to the concatenated address string.
304 * We try to not do a lot of malloc/copy/free's (which is why we
305 * don't call "getcpy") but still place no upper limit on the
306 * length of the result string.
309 formataddr (char *orig, char *str)
312 char baddr[BUFSIZ], error[BUFSIZ];
313 register int isgroup;
317 register struct mailname *mp = NULL;
319 /* if we don't have a buffer yet, get one */
321 buf = malloc (BUFINCR);
323 adios (NULL, "formataddr: couldn't allocate buffer space");
324 last_dst = buf; /* XXX */
325 bufsiz = BUFINCR - 6; /* leave some slop */
326 bufend = buf + bufsiz;
329 * If "orig" points to our buffer we can just pick up where we
330 * left off. Otherwise we have to copy orig into our buffer.
334 else if (!orig || !*orig) {
338 dst = last_dst; /* XXX */
343 /* concatenate all the new addresses onto 'buf' */
344 for (isgroup = 0; (cp = getname (str)); ) {
345 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
346 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
347 badaddrs = add (baddr, badaddrs);
350 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
355 /* if we get here we're going to add an address */
361 CHECKMEM (mp->m_gname);
381 insert (struct mailname *np)
384 register struct mailname *mp;
386 if (np->m_mbox == NULL)
389 for (mp = &mq; mp->m_next; mp = mp->m_next) {
390 if (!strcasecmp (np->m_host, mp->m_next->m_host)
391 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox))
394 if (!ccme && ismymbox (np))
398 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
399 if (!gans (buffer, anoyes))
418 replfilter (FILE *in, FILE *out, char *filter)
426 if (access (filter, R_OK) == NOTOK)
427 adios (filter, "unable to read");
429 mhl = r1bindex (mhlproc, '/');
432 lseek (fileno(in), (off_t) 0, SEEK_SET);
435 switch (pid = vfork ()) {
437 adios ("fork", "unable to");
440 dup2 (fileno (in), fileno (stdin));
441 dup2 (fileno (out), fileno (stdout));
444 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
445 fprintf (stderr, "unable to exec ");
450 if (pidXwait (pid, mhl))
452 fseek (out, 0L, SEEK_END);