3 * replsbr.c -- routines to help repl along...
10 #include <h/fmt_scan.h>
11 #include <sys/file.h> /* L_SET */
13 extern short ccto; /* from repl.c */
20 static char *badaddrs = NULL;
21 static char *dfhost = NULL;
23 static struct mailname mq = { NULL };
26 * Buffer size for content part of header fields.
27 * We want this to be large enough so that we don't
28 * do a lot of extra FLDPLUS calls on m_getfld but
29 * small enough so that we don't snarf the entire
30 * message body when we're not going to use any of it.
34 static struct format *fmt;
36 static int ncomps = 0; /* # of interesting components */
37 static char **compbuffers = NULL; /* buffers for component text */
38 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
40 static int dat[5]; /* aux. data for format routine */
42 static char *addrcomps[] = {
61 static int insert (struct mailname *);
62 static void replfilter (FILE *, FILE *, char *);
66 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
67 int mime, char *form, char *filter, char *fcc)
69 register int state, i;
70 register struct comp *cptr;
71 register char *tmpbuf;
72 register char **nxtbuf;
74 register struct comp **savecomp;
75 int char_read = 0, format_len;
76 char name[NAMESZ], *scanl, *cp;
80 if ((out = fopen (drft, "w")) == NULL)
81 adios (drft, "unable to create");
83 /* get new format string */
84 cp = new_fs (form, NULL, NULL);
85 format_len = strlen (cp);
87 /* compile format string */
88 ncomps = fmt_compile (cp, &fmt) + 1;
90 if (!(nxtbuf = compbuffers = (char **)
91 calloc((size_t) ncomps, sizeof(char *))))
92 adios (NULL, "unable to allocate component buffers");
93 if (!(savecomp = used_buf = (struct comp **)
94 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
95 adios (NULL, "unable to allocate component buffer stack");
96 savecomp += ncomps + 1;
97 *--savecomp = NULL; /* point at zero'd end minus 1 */
99 for (i = ncomps; i--; )
100 if (!(*nxtbuf++ = malloc(SBUFSIZ)))
101 adios (NULL, "unable to allocate component buffer");
103 nxtbuf = compbuffers; /* point at start */
106 for (ap = addrcomps; *ap; ap++) {
107 FINDCOMP (cptr, *ap);
109 cptr->c_type |= CT_ADDR;
113 * ignore any components killed by command line switches
116 FINDCOMP (cptr, "to");
121 FINDCOMP (cptr, "cc");
125 /* set up the "fcc" pseudo-component */
127 FINDCOMP (cptr, "fcc");
129 cptr->c_text = getcpy (fcc);
131 if ((cp = getenv("USER"))) {
132 FINDCOMP (cptr, "user");
134 cptr->c_text = getcpy(cp);
140 * pick any interesting stuff out of msg "inb"
142 for (state = FLD;;) {
143 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
148 * if we're interested in this component, save a pointer
149 * to the component text, then start using our next free
150 * buffer as the component temp buffer (buffer switching
151 * saves an extra copy of the component text).
153 if ((cptr = wantcomp[CHASH(name)]))
155 if (!strcasecmp(name, cptr->c_name)) {
156 char_read += msg_count;
157 if (! cptr->c_text) {
158 cptr->c_text = tmpbuf;
162 i = strlen (cp = cptr->c_text) - 1;
164 if (cptr->c_type & CT_ADDR) {
166 cp = add (",\n\t", cp);
170 cptr->c_text = add (tmpbuf, cp);
172 while (state == FLDPLUS) {
173 state = m_getfld (state, name, tmpbuf,
175 cptr->c_text = add (tmpbuf, cptr->c_text);
176 char_read += msg_count;
180 } while ((cptr = cptr->c_next));
182 while (state == FLDPLUS)
183 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
193 adios (NULL, "m_getfld() returned %d", state);
198 * format and output the header lines.
203 * if there's a "Subject" component, strip any "Re:"s off it
205 FINDCOMP (cptr, "subject")
206 if (cptr && (cp = cptr->c_text)) {
207 register char *sp = cp;
218 if (sp != cptr->c_text) {
220 cptr->c_text = getcpy (sp);
224 i = format_len + char_read + 256;
225 scanl = malloc ((size_t) i + 2);
229 dat[3] = outputlinelen;
231 fmt_scan (fmt, scanl, i, dat);
234 fputs ("\nrepl: bad addresses:\n", out);
235 fputs ( badaddrs, out);
239 * Check if we should filter the message
240 * or add mhn directives
243 replfilter (inb, out, filter);
244 } else if (mime && mp) {
245 fprintf (out, "#forw [original message] +%s %s\n",
246 mp->foldpath, m_name (mp->lowsel));
250 adios (drft, "error writing");
253 /* return dynamically allocated buffers */
255 for (nxtbuf = compbuffers, i = ncomps; cptr = *savecomp++; nxtbuf++, i--)
256 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
258 free (*nxtbuf++); /* free unused nxtbufs */
259 free ((char *) compbuffers);
260 free ((char *) used_buf);
263 static char *buf; /* our current working buffer */
264 static char *bufend; /* end of working buffer */
265 static char *last_dst; /* buf ptr at end of last call */
266 static unsigned int bufsiz=0; /* current size of buf */
268 #define BUFINCR 512 /* how much to expand buf when if fills */
270 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
273 * check if there's enough room in buf for str.
274 * add more mem if needed
276 #define CHECKMEM(str) \
277 if ((len = strlen (str)) >= bufend - dst) {\
279 int n = last_dst - buf;\
280 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
281 buf = realloc (buf, bufsiz);\
285 adios (NULL, "formataddr: couldn't get buffer space");\
286 bufend = buf + bufsiz;\
291 * fmt_scan will call this routine if the user includes the function
292 * "(formataddr {component})" in a format string. "orig" is the
293 * original contents of the string register. "str" is the address
294 * string to be formatted and concatenated onto orig. This routine
295 * returns a pointer to the concatenated address string.
297 * We try to not do a lot of malloc/copy/free's (which is why we
298 * don't call "getcpy") but still place no upper limit on the
299 * length of the result string.
302 formataddr (char *orig, char *str)
305 char baddr[BUFSIZ], error[BUFSIZ];
306 register int isgroup;
310 register struct mailname *mp = NULL;
312 /* if we don't have a buffer yet, get one */
314 buf = malloc (BUFINCR);
316 adios (NULL, "formataddr: couldn't allocate buffer space");
317 last_dst = buf; /* XXX */
318 bufsiz = BUFINCR - 6; /* leave some slop */
319 bufend = buf + bufsiz;
322 * If "orig" points to our buffer we can just pick up where we
323 * left off. Otherwise we have to copy orig into our buffer.
327 else if (!orig || !*orig) {
331 dst = last_dst; /* XXX */
336 /* concatenate all the new addresses onto 'buf' */
337 for (isgroup = 0; cp = getname (str); ) {
338 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
339 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
340 badaddrs = add (baddr, badaddrs);
343 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
348 /* if we get here we're going to add an address */
354 CHECKMEM (mp->m_gname);
374 insert (struct mailname *np)
377 register struct mailname *mp;
379 if (np->m_mbox == NULL)
382 for (mp = &mq; mp->m_next; mp = mp->m_next) {
383 if (!strcasecmp (np->m_host, mp->m_next->m_host)
384 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox))
387 if (!ccme && ismymbox (np))
391 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
392 if (!gans (buffer, anoyes))
411 replfilter (FILE *in, FILE *out, char *filter)
419 if (access (filter, R_OK) == NOTOK)
420 adios (filter, "unable to read");
422 mhl = r1bindex (mhlproc, '/');
425 lseek (fileno(in), (off_t) 0, SEEK_SET);
428 switch (pid = vfork ()) {
430 adios ("fork", "unable to");
433 dup2 (fileno (in), fileno (stdin));
434 dup2 (fileno (out), fileno (stdout));
437 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
438 fprintf (stderr, "unable to exec ");
443 if (pidXwait (pid, mhl))
445 fseek (out, 0L, SEEK_END);