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);
171 cptr->c_text = add (tmpbuf, cp);
173 while (state == FLDPLUS) {
174 state = m_getfld (state, name, tmpbuf,
176 cptr->c_text = add (tmpbuf, cptr->c_text);
177 char_read += msg_count;
181 } while ((cptr = cptr->c_next));
183 while (state == FLDPLUS)
184 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
194 adios (NULL, "m_getfld() returned %d", state);
199 * format and output the header lines.
204 * if there's a "Subject" component, strip any "Re:"s off it
206 FINDCOMP (cptr, "subject")
207 if (cptr && (cp = cptr->c_text)) {
208 register char *sp = cp;
219 if (sp != cptr->c_text) {
221 cptr->c_text = getcpy (sp);
225 i = format_len + char_read + 256;
226 scanl = malloc ((size_t) i + 2);
230 dat[3] = outputlinelen;
232 fmt_scan (fmt, scanl, i, dat);
235 fputs ("\nrepl: bad addresses:\n", out);
236 fputs ( badaddrs, out);
240 * Check if we should filter the message
241 * or add mhn directives
244 replfilter (inb, out, filter);
245 } else if (mime && mp) {
246 fprintf (out, "#forw [original message] +%s %s\n",
247 mp->foldpath, m_name (mp->lowsel));
251 adios (drft, "error writing");
254 /* return dynamically allocated buffers */
256 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
257 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
259 free (*nxtbuf++); /* free unused nxtbufs */
260 free ((char *) compbuffers);
261 free ((char *) used_buf);
264 static char *buf; /* our current working buffer */
265 static char *bufend; /* end of working buffer */
266 static char *last_dst; /* buf ptr at end of last call */
267 static unsigned int bufsiz=0; /* current size of buf */
269 #define BUFINCR 512 /* how much to expand buf when if fills */
271 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
274 * check if there's enough room in buf for str.
275 * add more mem if needed
277 #define CHECKMEM(str) \
278 if ((len = strlen (str)) >= bufend - dst) {\
280 int n = last_dst - buf;\
281 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
282 buf = realloc (buf, bufsiz);\
286 adios (NULL, "formataddr: couldn't get buffer space");\
287 bufend = buf + bufsiz;\
292 * fmt_scan will call this routine if the user includes the function
293 * "(formataddr {component})" in a format string. "orig" is the
294 * original contents of the string register. "str" is the address
295 * string to be formatted and concatenated onto orig. This routine
296 * returns a pointer to the concatenated address string.
298 * We try to not do a lot of malloc/copy/free's (which is why we
299 * don't call "getcpy") but still place no upper limit on the
300 * length of the result string.
303 formataddr (char *orig, char *str)
306 char baddr[BUFSIZ], error[BUFSIZ];
307 register int isgroup;
311 register struct mailname *mp = NULL;
313 /* if we don't have a buffer yet, get one */
315 buf = malloc (BUFINCR);
317 adios (NULL, "formataddr: couldn't allocate buffer space");
318 last_dst = buf; /* XXX */
319 bufsiz = BUFINCR - 6; /* leave some slop */
320 bufend = buf + bufsiz;
323 * If "orig" points to our buffer we can just pick up where we
324 * left off. Otherwise we have to copy orig into our buffer.
328 else if (!orig || !*orig) {
332 dst = last_dst; /* XXX */
337 /* concatenate all the new addresses onto 'buf' */
338 for (isgroup = 0; (cp = getname (str)); ) {
339 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
340 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
341 badaddrs = add (baddr, badaddrs);
344 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
349 /* if we get here we're going to add an address */
355 CHECKMEM (mp->m_gname);
375 insert (struct mailname *np)
378 register struct mailname *mp;
380 if (np->m_mbox == NULL)
383 for (mp = &mq; mp->m_next; mp = mp->m_next) {
384 if (!strcasecmp (np->m_host, mp->m_next->m_host)
385 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox))
388 if (!ccme && ismymbox (np))
392 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
393 if (!gans (buffer, anoyes))
412 replfilter (FILE *in, FILE *out, char *filter)
420 if (access (filter, R_OK) == NOTOK)
421 adios (filter, "unable to read");
423 mhl = r1bindex (mhlproc, '/');
426 lseek (fileno(in), (off_t) 0, SEEK_SET);
429 switch (pid = vfork ()) {
431 adios ("fork", "unable to");
434 dup2 (fileno (in), fileno (stdin));
435 dup2 (fileno (out), fileno (stdout));
438 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
439 fprintf (stderr, "unable to exec ");
444 if (pidXwait (pid, mhl))
446 fseek (out, 0L, SEEK_END);