]> diplodocus.org Git - nmh/blob - uip/replsbr.c
Bring these changes over from the branch.
[nmh] / uip / replsbr.c
1
2 /*
3 * replsbr.c -- routines to help repl along...
4 *
5 * $Id$
6 *
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.
10 */
11
12 #include <h/mh.h>
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <sys/file.h> /* L_SET */
16
17 extern short ccto; /* from repl.c */
18 extern short cccc;
19 extern short ccme;
20 extern short querysw;
21
22 static int dftype=0;
23
24 static char *badaddrs = NULL;
25 static char *dfhost = NULL;
26
27 static struct mailname mq = { NULL };
28
29 /*
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.
35 */
36 #define SBUFSIZ 256
37
38 static struct format *fmt;
39
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 */
43
44 static int dat[5]; /* aux. data for format routine */
45
46 static char *addrcomps[] = {
47 "from",
48 "sender",
49 "reply-to",
50 "to",
51 "cc",
52 "bcc",
53 "resent-from",
54 "resent-sender",
55 "resent-reply-to",
56 "resent-to",
57 "resent-cc",
58 "resent-bcc",
59 NULL
60 };
61
62 /*
63 * static prototypes
64 */
65 static int insert (struct mailname *);
66 static void replfilter (FILE *, FILE *, char *);
67
68
69 void
70 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
71 int mime, char *form, char *filter, char *fcc)
72 {
73 register int state, i;
74 register struct comp *cptr;
75 register char *tmpbuf;
76 register char **nxtbuf;
77 register char **ap;
78 register struct comp **savecomp;
79 int char_read = 0, format_len;
80 char name[NAMESZ], *scanl, *cp;
81 FILE *out;
82
83 umask(~m_gmprot());
84 if ((out = fopen (drft, "w")) == NULL)
85 adios (drft, "unable to create");
86
87 /* get new format string */
88 cp = new_fs (form, NULL, NULL);
89 format_len = strlen (cp);
90
91 /* compile format string */
92 ncomps = fmt_compile (cp, &fmt) + 1;
93
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 */
102
103 for (i = ncomps; i--; )
104 if (!(*nxtbuf++ = malloc(SBUFSIZ)))
105 adios (NULL, "unable to allocate component buffer");
106
107 nxtbuf = compbuffers; /* point at start */
108 tmpbuf = *nxtbuf++;
109
110 for (ap = addrcomps; *ap; ap++) {
111 FINDCOMP (cptr, *ap);
112 if (cptr)
113 cptr->c_type |= CT_ADDR;
114 }
115
116 /*
117 * ignore any components killed by command line switches
118 */
119 if (!ccto) {
120 FINDCOMP (cptr, "to");
121 if (cptr)
122 cptr->c_name = "";
123 }
124 if (!cccc) {
125 FINDCOMP (cptr, "cc");
126 if (cptr)
127 cptr->c_name = "";
128 }
129 /* set up the "fcc" pseudo-component */
130 if (fcc) {
131 FINDCOMP (cptr, "fcc");
132 if (cptr)
133 cptr->c_text = getcpy (fcc);
134 }
135 if ((cp = getenv("USER"))) {
136 FINDCOMP (cptr, "user");
137 if (cptr)
138 cptr->c_text = getcpy(cp);
139 }
140 if (!ccme)
141 ismymbox (NULL);
142
143 /*
144 * pick any interesting stuff out of msg "inb"
145 */
146 for (state = FLD;;) {
147 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
148 switch (state) {
149 case FLD:
150 case FLDPLUS:
151 /*
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).
156 */
157 if ((cptr = wantcomp[CHASH(name)]))
158 do {
159 if (!strcasecmp(name, cptr->c_name)) {
160 char_read += msg_count;
161 if (! cptr->c_text) {
162 cptr->c_text = tmpbuf;
163 *--savecomp = cptr;
164 tmpbuf = *nxtbuf++;
165 } else {
166 i = strlen (cp = cptr->c_text) - 1;
167 if (cp[i] == '\n') {
168 if (cptr->c_type & CT_ADDR) {
169 cp[i] = '\0';
170 cp = add (",\n\t", cp);
171 } else {
172 cp = add ("\t", cp);
173 }
174 }
175 cptr->c_text = add (tmpbuf, cp);
176 }
177 while (state == FLDPLUS) {
178 state = m_getfld (state, name, tmpbuf,
179 SBUFSIZ, inb);
180 cptr->c_text = add (tmpbuf, cptr->c_text);
181 char_read += msg_count;
182 }
183 break;
184 }
185 } while ((cptr = cptr->c_next));
186
187 while (state == FLDPLUS)
188 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
189 break;
190
191 case LENERR:
192 case FMTERR:
193 case BODY:
194 case FILEEOF:
195 goto finished;
196
197 default:
198 adios (NULL, "m_getfld() returned %d", state);
199 }
200 }
201
202 /*
203 * format and output the header lines.
204 */
205 finished:
206
207 /*
208 * if there's a "Subject" component, strip any "Re:"s off it
209 */
210 FINDCOMP (cptr, "subject")
211 if (cptr && (cp = cptr->c_text)) {
212 register char *sp = cp;
213
214 for (;;) {
215 while (isspace(*cp))
216 cp++;
217 if(uprf(cp, "re:"))
218 cp += 3;
219 else
220 break;
221 sp = cp;
222 }
223 if (sp != cptr->c_text) {
224 cp = cptr->c_text;
225 cptr->c_text = getcpy (sp);
226 free (cp);
227 }
228 }
229 i = format_len + char_read + 256;
230 scanl = malloc ((size_t) i + 2);
231 dat[0] = 0;
232 dat[1] = 0;
233 dat[2] = 0;
234 dat[3] = outputlinelen;
235 dat[4] = 0;
236 fmt_scan (fmt, scanl, i, dat);
237 fputs (scanl, out);
238 if (badaddrs) {
239 fputs ("\nrepl: bad addresses:\n", out);
240 fputs ( badaddrs, out);
241 }
242
243 /*
244 * Check if we should filter the message
245 * or add mhn directives
246 */
247 if (filter) {
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));
252 }
253
254 if (ferror (out))
255 adios (drft, "error writing");
256 fclose (out);
257
258 /* return dynamically allocated buffers */
259 free (scanl);
260 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
261 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
262 while ( i-- > 0)
263 free (*nxtbuf++); /* free unused nxtbufs */
264 free ((char *) compbuffers);
265 free ((char *) used_buf);
266 }
267
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 */
272
273 #define BUFINCR 512 /* how much to expand buf when if fills */
274
275 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
276
277 /*
278 * check if there's enough room in buf for str.
279 * add more mem if needed
280 */
281 #define CHECKMEM(str) \
282 if ((len = strlen (str)) >= bufend - dst) {\
283 int i = dst - buf;\
284 int n = last_dst - buf;\
285 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
286 buf = realloc (buf, bufsiz);\
287 dst = buf + i;\
288 last_dst = buf + n;\
289 if (! buf)\
290 adios (NULL, "formataddr: couldn't get buffer space");\
291 bufend = buf + bufsiz;\
292 }
293
294
295 /*
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.
301 *
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.
305 */
306 char *
307 formataddr (char *orig, char *str)
308 {
309 register int len;
310 char baddr[BUFSIZ], error[BUFSIZ];
311 register int isgroup;
312 register char *dst;
313 register char *cp;
314 register char *sp;
315 register struct mailname *mp = NULL;
316
317 /* if we don't have a buffer yet, get one */
318 if (bufsiz == 0) {
319 buf = malloc (BUFINCR);
320 if (! buf)
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;
325 }
326 /*
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.
329 */
330 if (orig == buf)
331 dst = last_dst;
332 else if (!orig || !*orig) {
333 dst = buf;
334 *dst = '\0';
335 } else {
336 dst = last_dst; /* XXX */
337 CHECKMEM (orig);
338 CPY (orig);
339 }
340
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);
346 continue;
347 }
348 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
349 *dst++ = ';';
350 isgroup = 0;
351 }
352 if (insert (mp)) {
353 /* if we get here we're going to add an address */
354 if (dst != buf) {
355 *dst++ = ',';
356 *dst++ = ' ';
357 }
358 if (mp->m_gname) {
359 CHECKMEM (mp->m_gname);
360 CPY (mp->m_gname);
361 isgroup++;
362 }
363 sp = adrformat (mp);
364 CHECKMEM (sp);
365 CPY (sp);
366 }
367 }
368
369 if (isgroup)
370 *dst++ = ';';
371
372 *dst = '\0';
373 last_dst = dst;
374 return (buf);
375 }
376
377
378 static int
379 insert (struct mailname *np)
380 {
381 char buffer[BUFSIZ];
382 register struct mailname *mp;
383
384 if (np->m_mbox == NULL)
385 return 0;
386
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))
390 return 0;
391 }
392 if (!ccme && ismymbox (np))
393 return 0;
394
395 if (querysw) {
396 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
397 if (!gans (buffer, anoyes))
398 return 0;
399 }
400 mp->m_next = np;
401
402 #ifdef ISI
403 if (ismymbox (np))
404 ccme = 0;
405 #endif
406
407 return 1;
408 }
409
410
411 /*
412 * Call the mhlproc
413 */
414
415 static void
416 replfilter (FILE *in, FILE *out, char *filter)
417 {
418 int pid;
419 char *mhl;
420
421 if (filter == NULL)
422 return;
423
424 if (access (filter, R_OK) == NOTOK)
425 adios (filter, "unable to read");
426
427 mhl = r1bindex (mhlproc, '/');
428
429 rewind (in);
430 lseek (fileno(in), (off_t) 0, SEEK_SET);
431 fflush (out);
432
433 switch (pid = vfork ()) {
434 case NOTOK:
435 adios ("fork", "unable to");
436
437 case OK:
438 dup2 (fileno (in), fileno (stdin));
439 dup2 (fileno (out), fileno (stdout));
440 closefds (3);
441
442 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
443 fprintf (stderr, "unable to exec ");
444 perror (mhlproc);
445 _exit (-1);
446
447 default:
448 if (pidXwait (pid, mhl))
449 done (1);
450 fseek (out, 0L, SEEK_END);
451 break;
452 }
453 }