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