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>
16 #include <sys/file.h> /* L_SET */
19 extern short ccto; /* from repl.c */
26 static char *badaddrs = NULL;
27 static char *dfhost = NULL;
29 static struct mailname mq = { NULL };
32 * Buffer size for content part of header fields.
33 * We want this to be large enough so that we don't
34 * do a lot of extra FLDPLUS calls on m_getfld but
35 * small enough so that we don't snarf the entire
36 * message body when we're not going to use any of it.
40 static struct format *fmt;
42 static int ncomps = 0; /* # of interesting components */
43 static char **compbuffers = NULL; /* buffers for component text */
44 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
46 static int dat[5]; /* aux. data for format routine */
48 static char *addrcomps[] = {
67 static int insert (struct mailname *);
68 static void replfilter (FILE *, FILE *, char *);
72 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
73 int mime, char *form, char *filter, char *fcc)
75 register int state, i;
76 register struct comp *cptr;
77 register char *tmpbuf;
78 register char **nxtbuf;
80 register struct comp **savecomp;
81 int char_read = 0, format_len, mask;
82 char name[NAMESZ], *scanl;
86 mask = umask(~m_gmprot());
87 if ((out = fopen (drft, "w")) == NULL)
88 adios (drft, "unable to create");
92 /* get new format string */
93 cp = new_fs (form, NULL, NULL);
94 format_len = strlen (cp);
96 /* compile format string */
97 ncomps = fmt_compile (cp, &fmt) + 1;
99 if (!(nxtbuf = compbuffers = (char **)
100 calloc((size_t) ncomps, sizeof(char *))))
101 adios (NULL, "unable to allocate component buffers");
102 if (!(savecomp = used_buf = (struct comp **)
103 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
104 adios (NULL, "unable to allocate component buffer stack");
105 savecomp += ncomps + 1;
106 *--savecomp = NULL; /* point at zero'd end minus 1 */
108 for (i = ncomps; i--; )
109 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
111 nxtbuf = compbuffers; /* point at start */
114 for (ap = addrcomps; *ap; ap++) {
115 FINDCOMP (cptr, *ap);
117 cptr->c_type |= CT_ADDR;
121 * ignore any components killed by command line switches
124 FINDCOMP (cptr, "to");
129 FINDCOMP (cptr, "cc");
133 /* set up the "fcc" pseudo-component */
135 FINDCOMP (cptr, "fcc");
137 cptr->c_text = getcpy (fcc);
139 if ((cp = getenv("USER"))) {
140 FINDCOMP (cptr, "user");
142 cptr->c_text = getcpy(cp);
148 * pick any interesting stuff out of msg "inb"
150 for (state = FLD;;) {
151 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
156 * if we're interested in this component, save a pointer
157 * to the component text, then start using our next free
158 * buffer as the component temp buffer (buffer switching
159 * saves an extra copy of the component text).
161 if ((cptr = wantcomp[CHASH(name)]))
163 if (!mh_strcasecmp(name, cptr->c_name)) {
164 char_read += msg_count;
165 if (! cptr->c_text) {
166 i = strlen(cptr->c_text = tmpbuf) - 1;
167 if (tmpbuf[i] == '\n')
172 i = strlen (cp = cptr->c_text) - 1;
174 if (cptr->c_type & CT_ADDR) {
176 cp = add (",\n\t", cp);
181 cptr->c_text = add (tmpbuf, cp);
183 while (state == FLDPLUS) {
184 state = m_getfld (state, name, tmpbuf,
186 cptr->c_text = add (tmpbuf, cptr->c_text);
187 char_read += msg_count;
191 } while ((cptr = cptr->c_next));
193 while (state == FLDPLUS)
194 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
204 adios (NULL, "m_getfld() returned %d", state);
209 * format and output the header lines.
214 * if there's a "Subject" component, strip any "Re:"s off it
216 FINDCOMP (cptr, "subject")
217 if (cptr && (cp = cptr->c_text)) {
218 register char *sp = cp;
229 if (sp != cptr->c_text) {
231 cptr->c_text = getcpy (sp);
235 i = format_len + char_read + 256;
236 scanl = mh_xmalloc ((size_t) i + 2);
240 dat[3] = outputlinelen;
242 fmt_scan (fmt, scanl, i, dat);
245 fputs ("\nrepl: bad addresses:\n", out);
246 fputs ( badaddrs, out);
250 * Check if we should filter the message
251 * or add mhn directives
256 adios (drft, "error writing");
258 replfilter (inb, out, filter);
259 } else if (mime && mp) {
260 fprintf (out, "#forw [original message] +%s %s\n",
261 mp->foldpath, m_name (mp->lowsel));
266 adios (drft, "error writing");
269 /* return dynamically allocated buffers */
271 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
272 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
274 free (*nxtbuf++); /* free unused nxtbufs */
275 free ((char *) compbuffers);
276 free ((char *) used_buf);
279 static char *buf; /* our current working buffer */
280 static char *bufend; /* end of working buffer */
281 static char *last_dst; /* buf ptr at end of last call */
282 static unsigned int bufsiz=0; /* current size of buf */
284 #define BUFINCR 512 /* how much to expand buf when if fills */
286 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
289 * check if there's enough room in buf for str.
290 * add more mem if needed
292 #define CHECKMEM(str) \
293 if ((len = strlen (str)) >= bufend - dst) {\
295 int n = last_dst - buf;\
296 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
297 buf = mh_xrealloc (buf, bufsiz);\
300 bufend = buf + bufsiz;\
305 * fmt_scan will call this routine if the user includes the function
306 * "(formataddr {component})" in a format string. "orig" is the
307 * original contents of the string register. "str" is the address
308 * string to be formatted and concatenated onto orig. This routine
309 * returns a pointer to the concatenated address string.
311 * We try to not do a lot of malloc/copy/free's (which is why we
312 * don't call "getcpy") but still place no upper limit on the
313 * length of the result string.
316 formataddr (char *orig, char *str)
319 char baddr[BUFSIZ], error[BUFSIZ];
320 register int isgroup;
324 register struct mailname *mp = NULL;
326 /* if we don't have a buffer yet, get one */
328 buf = mh_xmalloc (BUFINCR);
329 last_dst = buf; /* XXX */
330 bufsiz = BUFINCR - 6; /* leave some slop */
331 bufend = buf + bufsiz;
334 * If "orig" points to our buffer we can just pick up where we
335 * left off. Otherwise we have to copy orig into our buffer.
339 else if (!orig || !*orig) {
343 dst = last_dst; /* XXX */
348 /* concatenate all the new addresses onto 'buf' */
349 for (isgroup = 0; (cp = getname (str)); ) {
350 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
351 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
352 badaddrs = add (baddr, badaddrs);
355 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
360 /* if we get here we're going to add an address */
366 CHECKMEM (mp->m_gname);
386 insert (struct mailname *np)
389 register struct mailname *mp;
391 if (np->m_mbox == NULL)
394 for (mp = &mq; mp->m_next; mp = mp->m_next) {
395 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
396 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox))
399 if (!ccme && ismymbox (np))
403 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
404 if (!gans (buffer, anoyes))
421 * This function expects that argument out has been fflushed by the caller.
425 replfilter (FILE *in, FILE *out, char *filter)
434 if (access (filter, R_OK) == NOTOK)
435 adios (filter, "unable to read");
437 mhl = r1bindex (mhlproc, '/');
440 lseek (fileno(in), (off_t) 0, SEEK_SET);
442 switch (pid = vfork ()) {
444 adios ("fork", "unable to");
447 dup2 (fileno (in), fileno (stdin));
448 dup2 (fileno (out), fileno (stdout));
451 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
452 errstr = strerror(errno);
453 write(2, "unable to exec ", 15);
454 write(2, mhlproc, strlen(mhlproc));
456 write(2, errstr, strlen(errstr));
461 if (pidXwait (pid, mhl))
463 fseek (out, 0L, SEEK_END);