]> diplodocus.org Git - nmh/blob - uip/replsbr.c
[bug #4296] PUTD() macro can't hack 10 digit values
[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, mask;
80 char name[NAMESZ], *scanl, *cp;
81 FILE *out;
82
83 mask = umask(~m_gmprot());
84 if ((out = fopen (drft, "w")) == NULL)
85 adios (drft, "unable to create");
86
87 umask(mask);
88
89 /* get new format string */
90 cp = new_fs (form, NULL, NULL);
91 format_len = strlen (cp);
92
93 /* compile format string */
94 ncomps = fmt_compile (cp, &fmt) + 1;
95
96 if (!(nxtbuf = compbuffers = (char **)
97 calloc((size_t) ncomps, sizeof(char *))))
98 adios (NULL, "unable to allocate component buffers");
99 if (!(savecomp = used_buf = (struct comp **)
100 calloc((size_t) (ncomps+1), sizeof(struct comp *))))
101 adios (NULL, "unable to allocate component buffer stack");
102 savecomp += ncomps + 1;
103 *--savecomp = NULL; /* point at zero'd end minus 1 */
104
105 for (i = ncomps; i--; )
106 if (!(*nxtbuf++ = malloc(SBUFSIZ)))
107 adios (NULL, "unable to allocate component buffer");
108
109 nxtbuf = compbuffers; /* point at start */
110 tmpbuf = *nxtbuf++;
111
112 for (ap = addrcomps; *ap; ap++) {
113 FINDCOMP (cptr, *ap);
114 if (cptr)
115 cptr->c_type |= CT_ADDR;
116 }
117
118 /*
119 * ignore any components killed by command line switches
120 */
121 if (!ccto) {
122 FINDCOMP (cptr, "to");
123 if (cptr)
124 cptr->c_name = "";
125 }
126 if (!cccc) {
127 FINDCOMP (cptr, "cc");
128 if (cptr)
129 cptr->c_name = "";
130 }
131 /* set up the "fcc" pseudo-component */
132 if (fcc) {
133 FINDCOMP (cptr, "fcc");
134 if (cptr)
135 cptr->c_text = getcpy (fcc);
136 }
137 if ((cp = getenv("USER"))) {
138 FINDCOMP (cptr, "user");
139 if (cptr)
140 cptr->c_text = getcpy(cp);
141 }
142 if (!ccme)
143 ismymbox (NULL);
144
145 /*
146 * pick any interesting stuff out of msg "inb"
147 */
148 for (state = FLD;;) {
149 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
150 switch (state) {
151 case FLD:
152 case FLDPLUS:
153 /*
154 * if we're interested in this component, save a pointer
155 * to the component text, then start using our next free
156 * buffer as the component temp buffer (buffer switching
157 * saves an extra copy of the component text).
158 */
159 if ((cptr = wantcomp[CHASH(name)]))
160 do {
161 if (!strcasecmp(name, cptr->c_name)) {
162 char_read += msg_count;
163 if (! cptr->c_text) {
164 cptr->c_text = tmpbuf;
165 *--savecomp = cptr;
166 tmpbuf = *nxtbuf++;
167 } else {
168 i = strlen (cp = cptr->c_text) - 1;
169 if (cp[i] == '\n') {
170 if (cptr->c_type & CT_ADDR) {
171 cp[i] = '\0';
172 cp = add (",\n\t", cp);
173 } else {
174 cp = add ("\t", cp);
175 }
176 }
177 cptr->c_text = add (tmpbuf, cp);
178 }
179 while (state == FLDPLUS) {
180 state = m_getfld (state, name, tmpbuf,
181 SBUFSIZ, inb);
182 cptr->c_text = add (tmpbuf, cptr->c_text);
183 char_read += msg_count;
184 }
185 break;
186 }
187 } while ((cptr = cptr->c_next));
188
189 while (state == FLDPLUS)
190 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
191 break;
192
193 case LENERR:
194 case FMTERR:
195 case BODY:
196 case FILEEOF:
197 goto finished;
198
199 default:
200 adios (NULL, "m_getfld() returned %d", state);
201 }
202 }
203
204 /*
205 * format and output the header lines.
206 */
207 finished:
208
209 /*
210 * if there's a "Subject" component, strip any "Re:"s off it
211 */
212 FINDCOMP (cptr, "subject")
213 if (cptr && (cp = cptr->c_text)) {
214 register char *sp = cp;
215
216 for (;;) {
217 while (isspace(*cp))
218 cp++;
219 if(uprf(cp, "re:"))
220 cp += 3;
221 else
222 break;
223 sp = cp;
224 }
225 if (sp != cptr->c_text) {
226 cp = cptr->c_text;
227 cptr->c_text = getcpy (sp);
228 free (cp);
229 }
230 }
231 i = format_len + char_read + 256;
232 scanl = malloc ((size_t) i + 2);
233 dat[0] = 0;
234 dat[1] = 0;
235 dat[2] = 0;
236 dat[3] = outputlinelen;
237 dat[4] = 0;
238 fmt_scan (fmt, scanl, i, dat);
239 fputs (scanl, out);
240 if (badaddrs) {
241 fputs ("\nrepl: bad addresses:\n", out);
242 fputs ( badaddrs, out);
243 }
244
245 /*
246 * Check if we should filter the message
247 * or add mhn directives
248 */
249 if (filter) {
250 replfilter (inb, out, filter);
251 } else if (mime && mp) {
252 fprintf (out, "#forw [original message] +%s %s\n",
253 mp->foldpath, m_name (mp->lowsel));
254 }
255
256 if (ferror (out))
257 adios (drft, "error writing");
258 fclose (out);
259
260 /* return dynamically allocated buffers */
261 free (scanl);
262 for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++); nxtbuf++, i--)
263 free (cptr->c_text); /* if not nxtbuf, nxtbuf already freed */
264 while ( i-- > 0)
265 free (*nxtbuf++); /* free unused nxtbufs */
266 free ((char *) compbuffers);
267 free ((char *) used_buf);
268 }
269
270 static char *buf; /* our current working buffer */
271 static char *bufend; /* end of working buffer */
272 static char *last_dst; /* buf ptr at end of last call */
273 static unsigned int bufsiz=0; /* current size of buf */
274
275 #define BUFINCR 512 /* how much to expand buf when if fills */
276
277 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
278
279 /*
280 * check if there's enough room in buf for str.
281 * add more mem if needed
282 */
283 #define CHECKMEM(str) \
284 if ((len = strlen (str)) >= bufend - dst) {\
285 int i = dst - buf;\
286 int n = last_dst - buf;\
287 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
288 buf = realloc (buf, bufsiz);\
289 dst = buf + i;\
290 last_dst = buf + n;\
291 if (! buf)\
292 adios (NULL, "formataddr: couldn't get buffer space");\
293 bufend = buf + bufsiz;\
294 }
295
296
297 /*
298 * fmt_scan will call this routine if the user includes the function
299 * "(formataddr {component})" in a format string. "orig" is the
300 * original contents of the string register. "str" is the address
301 * string to be formatted and concatenated onto orig. This routine
302 * returns a pointer to the concatenated address string.
303 *
304 * We try to not do a lot of malloc/copy/free's (which is why we
305 * don't call "getcpy") but still place no upper limit on the
306 * length of the result string.
307 */
308 char *
309 formataddr (char *orig, char *str)
310 {
311 register int len;
312 char baddr[BUFSIZ], error[BUFSIZ];
313 register int isgroup;
314 register char *dst;
315 register char *cp;
316 register char *sp;
317 register struct mailname *mp = NULL;
318
319 /* if we don't have a buffer yet, get one */
320 if (bufsiz == 0) {
321 buf = malloc (BUFINCR);
322 if (! buf)
323 adios (NULL, "formataddr: couldn't allocate buffer space");
324 last_dst = buf; /* XXX */
325 bufsiz = BUFINCR - 6; /* leave some slop */
326 bufend = buf + bufsiz;
327 }
328 /*
329 * If "orig" points to our buffer we can just pick up where we
330 * left off. Otherwise we have to copy orig into our buffer.
331 */
332 if (orig == buf)
333 dst = last_dst;
334 else if (!orig || !*orig) {
335 dst = buf;
336 *dst = '\0';
337 } else {
338 dst = last_dst; /* XXX */
339 CHECKMEM (orig);
340 CPY (orig);
341 }
342
343 /* concatenate all the new addresses onto 'buf' */
344 for (isgroup = 0; (cp = getname (str)); ) {
345 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
346 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
347 badaddrs = add (baddr, badaddrs);
348 continue;
349 }
350 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
351 *dst++ = ';';
352 isgroup = 0;
353 }
354 if (insert (mp)) {
355 /* if we get here we're going to add an address */
356 if (dst != buf) {
357 *dst++ = ',';
358 *dst++ = ' ';
359 }
360 if (mp->m_gname) {
361 CHECKMEM (mp->m_gname);
362 CPY (mp->m_gname);
363 isgroup++;
364 }
365 sp = adrformat (mp);
366 CHECKMEM (sp);
367 CPY (sp);
368 }
369 }
370
371 if (isgroup)
372 *dst++ = ';';
373
374 *dst = '\0';
375 last_dst = dst;
376 return (buf);
377 }
378
379
380 static int
381 insert (struct mailname *np)
382 {
383 char buffer[BUFSIZ];
384 register struct mailname *mp;
385
386 if (np->m_mbox == NULL)
387 return 0;
388
389 for (mp = &mq; mp->m_next; mp = mp->m_next) {
390 if (!strcasecmp (np->m_host, mp->m_next->m_host)
391 && !strcasecmp (np->m_mbox, mp->m_next->m_mbox))
392 return 0;
393 }
394 if (!ccme && ismymbox (np))
395 return 0;
396
397 if (querysw) {
398 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
399 if (!gans (buffer, anoyes))
400 return 0;
401 }
402 mp->m_next = np;
403
404 #ifdef ISI
405 if (ismymbox (np))
406 ccme = 0;
407 #endif
408
409 return 1;
410 }
411
412
413 /*
414 * Call the mhlproc
415 */
416
417 static void
418 replfilter (FILE *in, FILE *out, char *filter)
419 {
420 int pid;
421 char *mhl;
422
423 if (filter == NULL)
424 return;
425
426 if (access (filter, R_OK) == NOTOK)
427 adios (filter, "unable to read");
428
429 mhl = r1bindex (mhlproc, '/');
430
431 rewind (in);
432 lseek (fileno(in), (off_t) 0, SEEK_SET);
433 fflush (out);
434
435 switch (pid = vfork ()) {
436 case NOTOK:
437 adios ("fork", "unable to");
438
439 case OK:
440 dup2 (fileno (in), fileno (stdin));
441 dup2 (fileno (out), fileno (stdout));
442 closefds (3);
443
444 execlp (mhlproc, mhl, "-form", filter, "-noclear", NULL);
445 fprintf (stderr, "unable to exec ");
446 perror (mhlproc);
447 _exit (-1);
448
449 default:
450 if (pidXwait (pid, mhl))
451 done (1);
452 fseek (out, 0L, SEEK_END);
453 break;
454 }
455 }