]> diplodocus.org Git - nmh/blob - uip/replsbr.c
Formatting cleanup.
[nmh] / uip / replsbr.c
1
2 /*
3 * replsbr.c -- routines to help repl along...
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/utils.h>
14 #include <sys/file.h> /* L_SET */
15 #include <errno.h>
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;
28 static int nodupcheck = 0; /* If set, no check for duplicates */
29
30 /*
31 * Buffer size for content part of header fields.
32 * We want this to be large enough so that we don't
33 * do a lot of extra FLDPLUS calls on m_getfld but
34 * small enough so that we don't snarf the entire
35 * message body when we're not going to use any of it.
36 */
37 #define SBUFSIZ 256
38
39 static struct format *fmt;
40
41 static int ncomps = 0; /* # of interesting components */
42 static char **compbuffers = NULL; /* buffers for component text */
43 static struct comp **used_buf = NULL; /* stack for comp that use buffers */
44
45 static int dat[5]; /* aux. data for format routine */
46
47 static char *addrcomps[] = {
48 "from",
49 "sender",
50 "reply-to",
51 "to",
52 "cc",
53 "bcc",
54 "resent-from",
55 "resent-sender",
56 "resent-reply-to",
57 "resent-to",
58 "resent-cc",
59 "resent-bcc",
60 NULL
61 };
62
63 /*
64 * static prototypes
65 */
66 static int insert (struct mailname *);
67 static void replfilter (FILE *, FILE *, char *, int);
68
69
70 void
71 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
72 int mime, char *form, char *filter, char *fcc, int fmtproc)
73 {
74 NMH_UNUSED (msg);
75
76 register int state, i;
77 register struct comp *cptr;
78 register char *tmpbuf;
79 register char **nxtbuf;
80 register char **ap;
81 register struct comp **savecomp;
82 int char_read = 0, format_len, mask;
83 char name[NAMESZ], *scanl;
84 unsigned char *cp;
85 FILE *out;
86
87 mask = umask(~m_gmprot());
88 if ((out = fopen (drft, "w")) == NULL)
89 adios (drft, "unable to create");
90
91 umask(mask);
92
93 /* get new format string */
94 cp = new_fs (form, NULL, NULL);
95 format_len = strlen (cp);
96
97 /* compile format string */
98 ncomps = fmt_compile (cp, &fmt) + 1;
99
100 if (!(nxtbuf = compbuffers = (char **)
101 calloc((size_t) ncomps, sizeof(char *))))
102 adios (NULL, "unable to allocate component buffers");
103 if (!(savecomp = used_buf = (struct comp **)
104 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
105 adios (NULL, "unable to allocate component buffer stack");
106 savecomp += ncomps + 1;
107 *--savecomp = NULL; /* point at zero'd end minus 1 */
108
109 for (i = ncomps; i--; )
110 *nxtbuf++ = mh_xmalloc(SBUFSIZ);
111
112 nxtbuf = compbuffers; /* point at start */
113 tmpbuf = *nxtbuf++;
114
115 for (ap = addrcomps; *ap; ap++) {
116 FINDCOMP (cptr, *ap);
117 if (cptr)
118 cptr->c_type |= CT_ADDR;
119 }
120
121 /*
122 * ignore any components killed by command line switches
123 */
124 if (!ccto) {
125 FINDCOMP (cptr, "to");
126 if (cptr)
127 cptr->c_name = "";
128 }
129 if (!cccc) {
130 FINDCOMP (cptr, "cc");
131 if (cptr)
132 cptr->c_name = "";
133 }
134 /* set up the "fcc" pseudo-component */
135 if (fcc) {
136 FINDCOMP (cptr, "fcc");
137 if (cptr)
138 cptr->c_text = getcpy (fcc);
139 }
140 if ((cp = getenv("USER"))) {
141 FINDCOMP (cptr, "user");
142 if (cptr)
143 cptr->c_text = getcpy(cp);
144 }
145 if (!ccme)
146 ismymbox (NULL);
147
148 /*
149 * pick any interesting stuff out of msg "inb"
150 */
151 for (state = FLD;;) {
152 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
153 switch (state) {
154 case FLD:
155 case FLDPLUS:
156 /*
157 * if we're interested in this component, save a pointer
158 * to the component text, then start using our next free
159 * buffer as the component temp buffer (buffer switching
160 * saves an extra copy of the component text).
161 */
162 if ((cptr = wantcomp[CHASH(name)]))
163 do {
164 if (!mh_strcasecmp(name, cptr->c_name)) {
165 char_read += msg_count;
166 if (! cptr->c_text) {
167 cptr->c_text = tmpbuf;
168 *--savecomp = cptr;
169 tmpbuf = *nxtbuf++;
170 } else {
171 i = strlen (cp = cptr->c_text) - 1;
172 if (cp[i] == '\n') {
173 if (cptr->c_type & CT_ADDR) {
174 cp[i] = '\0';
175 cp = add (",\n\t", cp);
176 } else {
177 cp = add ("\t", cp);
178 }
179 }
180 cptr->c_text = add (tmpbuf, cp);
181 }
182 while (state == FLDPLUS) {
183 state = m_getfld (state, name, tmpbuf,
184 SBUFSIZ, inb);
185 cptr->c_text = add (tmpbuf, cptr->c_text);
186 char_read += msg_count;
187 }
188 break;
189 }
190 } while ((cptr = cptr->c_next));
191
192 while (state == FLDPLUS)
193 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
194 break;
195
196 case LENERR:
197 case FMTERR:
198 case BODY:
199 case FILEEOF:
200 goto finished;
201
202 default:
203 adios (NULL, "m_getfld() returned %d", state);
204 }
205 }
206
207 /*
208 * format and output the header lines.
209 */
210 finished:
211
212 /*
213 * if there's a "Subject" component, strip any "Re:"s off it
214 */
215 FINDCOMP (cptr, "subject")
216 if (cptr && (cp = cptr->c_text)) {
217 register char *sp = cp;
218
219 for (;;) {
220 while (isspace(*cp))
221 cp++;
222 if(uprf(cp, "re:"))
223 cp += 3;
224 else
225 break;
226 sp = cp;
227 }
228 if (sp != cptr->c_text) {
229 cp = cptr->c_text;
230 cptr->c_text = getcpy (sp);
231 free (cp);
232 }
233 }
234 i = format_len + char_read + 256;
235 scanl = mh_xmalloc ((size_t) i + 2);
236 dat[0] = 0;
237 dat[1] = 0;
238 dat[2] = 0;
239 dat[3] = outputlinelen;
240 dat[4] = 0;
241 fmt_scan (fmt, scanl, i, dat);
242 fputs (scanl, out);
243 if (badaddrs) {
244 fputs ("\nrepl: bad addresses:\n", out);
245 fputs ( badaddrs, out);
246 }
247
248 /*
249 * Check if we should filter the message
250 * or add mhn directives
251 */
252 if (filter) {
253 fflush(out);
254 if (ferror (out))
255 adios (drft, "error writing");
256
257 replfilter (inb, out, filter, fmtproc);
258 } else if (mime && mp) {
259 fprintf (out, "#forw [original message] +%s %s\n",
260 mp->foldpath, m_name (mp->lowsel));
261 }
262
263 fflush(out);
264 if (ferror (out))
265 adios (drft, "error writing");
266 fclose (out);
267
268 /* return dynamically allocated buffers */
269 free (scanl);
270 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
271 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
272 while ( i-- > 0)
273 free (*nxtbuf++); /* free unused nxtbufs */
274 free ((char *) compbuffers);
275 free ((char *) used_buf);
276 }
277
278 static char *buf; /* our current working buffer */
279 static char *bufend; /* end of working buffer */
280 static char *last_dst; /* buf ptr at end of last call */
281 static unsigned int bufsiz=0; /* current size of buf */
282
283 #define BUFINCR 512 /* how much to expand buf when if fills */
284
285 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
286
287 /*
288 * check if there's enough room in buf for str.
289 * add more mem if needed
290 */
291 #define CHECKMEM(str) \
292 if ((len = strlen (str)) >= bufend - dst) {\
293 int i = dst - buf;\
294 int n = last_dst - buf;\
295 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
296 buf = mh_xrealloc (buf, bufsiz);\
297 dst = buf + i;\
298 last_dst = buf + n;\
299 bufend = buf + bufsiz;\
300 }
301
302
303 /*
304 * fmt_scan will call this routine if the user includes the function
305 * "(formataddr {component})" in a format string. "orig" is the
306 * original contents of the string register. "str" is the address
307 * string to be formatted and concatenated onto orig. This routine
308 * returns a pointer to the concatenated address string.
309 *
310 * We try to not do a lot of malloc/copy/free's (which is why we
311 * don't call "getcpy") but still place no upper limit on the
312 * length of the result string.
313 */
314 char *
315 formataddr (char *orig, char *str)
316 {
317 register int len;
318 char baddr[BUFSIZ], error[BUFSIZ];
319 register int isgroup;
320 register char *dst;
321 register char *cp;
322 register char *sp;
323 register struct mailname *mp = NULL;
324
325 /* if we don't have a buffer yet, get one */
326 if (bufsiz == 0) {
327 buf = mh_xmalloc (BUFINCR);
328 last_dst = buf; /* XXX */
329 bufsiz = BUFINCR - 6; /* leave some slop */
330 bufend = buf + bufsiz;
331 }
332 /*
333 * If "orig" points to our buffer we can just pick up where we
334 * left off. Otherwise we have to copy orig into our buffer.
335 */
336 if (orig == buf)
337 dst = last_dst;
338 else if (!orig || !*orig) {
339 dst = buf;
340 *dst = '\0';
341 } else {
342 dst = last_dst; /* XXX */
343 CHECKMEM (orig);
344 CPY (orig);
345 }
346
347 /* concatenate all the new addresses onto 'buf' */
348 for (isgroup = 0; (cp = getname (str)); ) {
349 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
350 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
351 badaddrs = add (baddr, badaddrs);
352 continue;
353 }
354 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
355 *dst++ = ';';
356 isgroup = 0;
357 }
358 if (insert (mp)) {
359 /* if we get here we're going to add an address */
360 if (dst != buf) {
361 *dst++ = ',';
362 *dst++ = ' ';
363 }
364 if (mp->m_gname) {
365 CHECKMEM (mp->m_gname);
366 CPY (mp->m_gname);
367 isgroup++;
368 }
369 sp = adrformat (mp);
370 CHECKMEM (sp);
371 CPY (sp);
372 }
373 }
374
375 if (isgroup)
376 *dst++ = ';';
377
378 *dst = '\0';
379 last_dst = dst;
380 return (buf);
381 }
382
383
384 /*
385 * fmt_scan will call this routine if the user includes the function
386 * "(concataddr {component})" in a format string. This behaves exactly
387 * like formataddr, except that it does NOT suppress duplicate addresses
388 * between calls.
389 *
390 * As an implementation detail: I thought about splitting out formataddr()
391 * into the generic part and duplicate-suppressing part, but the call to
392 * insert() was buried deep within a couple of loops and I didn't see a
393 * way to do it easily. So instead we simply set a special flag to stop
394 * the duplicate check and call formataddr().
395 */
396 char *
397 concataddr(char *orig, char *str)
398 {
399 char *cp;
400
401 nodupcheck = 1;
402 cp = formataddr(orig, str);
403 nodupcheck = 0;
404 return cp;
405 }
406
407 static int
408 insert (struct mailname *np)
409 {
410 char buffer[BUFSIZ];
411 register struct mailname *mp;
412
413 if (nodupcheck)
414 return 1;
415
416 if (np->m_mbox == NULL)
417 return 0;
418
419 for (mp = &mq; mp->m_next; mp = mp->m_next) {
420 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
421 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox))
422 return 0;
423 }
424 if (!ccme && ismymbox (np))
425 return 0;
426
427 if (querysw) {
428 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
429 if (!gans (buffer, anoyes))
430 return 0;
431 }
432 mp->m_next = np;
433
434 #ifdef ISI
435 if (ismymbox (np))
436 ccme = 0;
437 #endif
438
439 return 1;
440 }
441
442
443 /*
444 * Call the mhlproc
445 *
446 * This function expects that argument out has been fflushed by the caller.
447 */
448
449 static void
450 replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
451 {
452 int pid;
453 char *mhl;
454 char *errstr;
455 char *arglist[7];
456
457 if (filter == NULL)
458 return;
459
460 if (access (filter, R_OK) == NOTOK)
461 adios (filter, "unable to read");
462
463 mhl = r1bindex (mhlproc, '/');
464
465 rewind (in);
466 lseek (fileno(in), (off_t) 0, SEEK_SET);
467
468 switch (pid = vfork()) {
469 case NOTOK:
470 adios ("fork", "unable to");
471
472 case OK:
473 dup2 (fileno (in), fileno (stdin));
474 dup2 (fileno (out), fileno (stdout));
475 closefds (3);
476
477 arglist[0] = mhl;
478 arglist[1] = "-form";
479 arglist[2] = filter;
480 arglist[3] = "-noclear";
481
482 switch (fmtproc) {
483 case 1:
484 arglist[4] = "-fmtproc";
485 arglist[5] = formatproc;
486 arglist[6] = NULL;
487 break;
488 case 0:
489 arglist[4] = "-nofmtproc";
490 arglist[5] = NULL;
491 break;
492 default:
493 arglist[4] = NULL;
494 }
495
496 execvp (mhlproc, arglist);
497 errstr = strerror(errno);
498 write(2, "unable to exec ", 15);
499 write(2, mhlproc, strlen(mhlproc));
500 write(2, ": ", 2);
501 write(2, errstr, strlen(errstr));
502 write(2, "\n", 1);
503 _exit (-1);
504
505 default:
506 if (pidXwait (pid, mhl))
507 done (1);
508 fseek (out, 0L, SEEK_END);
509 break;
510 }
511 }