]> diplodocus.org Git - nmh/blob - uip/replsbr.c
picksbr.c: fprintf(3) function pointer using `%p' and union.
[nmh] / uip / replsbr.c
1 /* replsbr.c -- routines to help repl along...
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/addrsbr.h>
10 #include <h/fmt_scan.h>
11 #include "h/done.h"
12 #include <h/utils.h>
13 #include <sys/file.h> /* L_SET */
14
15 extern short ccto; /* from repl.c */
16 extern short cccc;
17 extern short ccme;
18 extern short querysw;
19
20 static int dftype=0;
21
22 static char *badaddrs = NULL;
23 static char *dfhost = NULL;
24
25 static struct mailname mq;
26 static int nodupcheck = 0; /* If set, no check for duplicates */
27
28 static char *addrcomps[] = {
29 "from",
30 "sender",
31 "reply-to",
32 "to",
33 "cc",
34 "bcc",
35 "resent-from",
36 "resent-sender",
37 "resent-reply-to",
38 "resent-to",
39 "resent-cc",
40 "resent-bcc",
41 NULL
42 };
43
44 /*
45 * static prototypes
46 */
47 static int insert (struct mailname *);
48 static void replfilter (FILE *, FILE *, char *, int);
49 static char *replformataddr(char *, char *);
50 static char *replconcataddr(char *, char *);
51 static char *fix_addresses (char *);
52
53
54 void
55 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
56 int mime, char *form, char *filter, char *fcc, int fmtproc)
57 {
58 int state, i;
59 struct comp *cptr;
60 char tmpbuf[NMH_BUFSIZ];
61 struct format *fmt;
62 char **ap;
63 int char_read = 0, format_len, mask;
64 char name[NAMESZ], *cp;
65 charstring_t scanl;
66 static int dat[5]; /* aux. data for format routine */
67 m_getfld_state_t gstate;
68 struct fmt_callbacks cb;
69
70 FILE *out;
71 NMH_UNUSED (msg);
72
73 mask = umask(~m_gmprot());
74 if ((out = fopen (drft, "w")) == NULL)
75 adios (drft, "unable to create");
76
77 umask(mask);
78
79 /* get new format string */
80 cp = new_fs (form, NULL, NULL);
81 format_len = strlen (cp);
82
83 /* compile format string */
84 fmt_compile (cp, &fmt, 1);
85
86 for (ap = addrcomps; *ap; ap++) {
87 cptr = fmt_findcomp (*ap);
88 if (cptr)
89 cptr->c_type |= CT_ADDR;
90 }
91
92 /*
93 * ignore any components killed by command line switches
94 *
95 * This prevents the component from being found via fmt_findcomp(),
96 * which makes sure no text gets added to it when the message is processed.
97 */
98 if (!ccto) {
99 cptr = fmt_findcomp ("to");
100 if (cptr)
101 cptr->c_name = mh_xstrdup("");
102 }
103 if (!cccc) {
104 cptr = fmt_findcomp("cc");
105 if (cptr)
106 cptr->c_name = mh_xstrdup("");
107 }
108 if (!ccme)
109 ismymbox (NULL);
110
111 /*
112 * pick any interesting stuff out of msg "inb"
113 */
114 gstate = m_getfld_state_init(inb);
115 for (;;) {
116 int msg_count = sizeof tmpbuf;
117 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
118 switch (state) {
119 case FLD:
120 case FLDPLUS:
121 /*
122 * if we're interested in this component, save a pointer
123 * to the component text, then start using our next free
124 * buffer as the component temp buffer (buffer switching
125 * saves an extra copy of the component text).
126 */
127
128 i = fmt_addcomptext(name, tmpbuf);
129 if (i != -1) {
130 char_read += msg_count;
131 while (state == FLDPLUS) {
132 msg_count= sizeof tmpbuf;
133 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
134 fmt_appendcomp(i, name, tmpbuf);
135 char_read += msg_count;
136 }
137 }
138
139 while (state == FLDPLUS) {
140 msg_count= sizeof tmpbuf;
141 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
142 }
143 break;
144
145 case LENERR:
146 case FMTERR:
147 case BODY:
148 case FILEEOF:
149 goto finished;
150
151 default:
152 adios (NULL, "m_getfld2() returned %d", state);
153 }
154 }
155
156 /*
157 * format and output the header lines.
158 */
159 finished:
160 m_getfld_state_destroy (&gstate);
161
162 /* set up the "fcc" pseudo-component */
163 cptr = fmt_findcomp ("fcc");
164 if (cptr) {
165 free(cptr->c_text);
166 if (fcc)
167 cptr->c_text = mh_xstrdup(fcc);
168 else
169 cptr->c_text = NULL;
170 }
171 cptr = fmt_findcomp ("user");
172 if (cptr) {
173 free(cptr->c_text);
174 if ((cp = getenv("USER")))
175 cptr->c_text = mh_xstrdup(cp);
176 else
177 cptr = NULL;
178 }
179
180 /*
181 * if there's a "Subject" component, strip any "Re:"s off it
182 */
183 cptr = fmt_findcomp ("subject");
184 if (cptr && (cp = cptr->c_text)) {
185 char *sp = cp;
186
187 for (;;) {
188 while (isspace((unsigned char) *cp))
189 cp++;
190 if(uprf(cp, "re:"))
191 cp += 3;
192 else
193 break;
194 sp = cp;
195 }
196 if (sp != cptr->c_text) {
197 cp = cptr->c_text;
198 cptr->c_text = mh_xstrdup(sp);
199 free (cp);
200 }
201 }
202 i = format_len + char_read + 256;
203 scanl = charstring_create (i + 2);
204 dat[0] = 0;
205 dat[1] = 0;
206 dat[2] = 0;
207 dat[3] = outputlinelen;
208 dat[4] = 0;
209 ZERO(&cb);
210 cb.formataddr = replformataddr;
211 cb.concataddr = replconcataddr;
212 fmt_scan (fmt, scanl, i, dat, &cb);
213 fputs (charstring_buffer (scanl), out);
214 if (badaddrs) {
215 fputs ("\nrepl: bad addresses:\n", out);
216 fputs ( badaddrs, out);
217 }
218
219 /*
220 * Check if we should filter the message
221 * or add mhn directives
222 */
223 if (filter) {
224 fflush(out);
225 if (ferror (out))
226 adios (drft, "error writing");
227
228 replfilter (inb, out, filter, fmtproc);
229 } else if (mime && mp) {
230 fprintf (out, "#forw [original message] +%s %s\n",
231 mp->foldpath, m_name (mp->lowsel));
232 }
233
234 fflush(out);
235 if (ferror (out))
236 adios (drft, "error writing");
237 fclose (out);
238
239 /* return dynamically allocated buffers */
240 charstring_free (scanl);
241 fmt_free(fmt, 1);
242 }
243
244 static char *buf; /* our current working buffer */
245 static char *bufend; /* end of working buffer */
246 static char *last_dst; /* buf ptr at end of last call */
247 static unsigned int bufsiz=0; /* current size of buf */
248
249 #define BUFINCR 512 /* how much to expand buf when if fills */
250
251 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
252
253 /*
254 * check if there's enough room in buf for str.
255 * add more mem if needed
256 */
257 #define CHECKMEM(str) \
258 if ((len = strlen (str)) >= bufend - dst) {\
259 int i = dst - buf;\
260 int n = last_dst - buf;\
261 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
262 buf = mh_xrealloc (buf, bufsiz);\
263 dst = buf + i;\
264 last_dst = buf + n;\
265 bufend = buf + bufsiz;\
266 }
267
268
269 /*
270 * fmt_scan will call this routine if the user includes the function
271 * "(formataddr {component})" in a format string. "orig" is the
272 * original contents of the string register. "str" is the address
273 * string to be formatted and concatenated onto orig. This routine
274 * returns a pointer to the concatenated address string.
275 *
276 * We try to not do a lot of malloc/copy/free's (which is why we
277 * don't call "getcpy") but still place no upper limit on the
278 * length of the result string.
279 */
280 static char *
281 replformataddr (char *orig, char *str)
282 {
283 int len;
284 char baddr[BUFSIZ], error[BUFSIZ];
285 int isgroup;
286 char *dst;
287 char *cp;
288 char *sp;
289 struct mailname *mp = NULL;
290 char *fixed_str = fix_addresses (str);
291
292 /* if we don't have a buffer yet, get one */
293 if (bufsiz == 0) {
294 buf = mh_xmalloc (BUFINCR);
295 last_dst = buf; /* XXX */
296 bufsiz = BUFINCR - 6; /* leave some slop */
297 bufend = buf + bufsiz;
298 }
299 /*
300 * If "orig" points to our buffer we can just pick up where we
301 * left off. Otherwise we have to copy orig into our buffer.
302 */
303 if (orig == buf)
304 dst = last_dst;
305 else if (!orig || !*orig) {
306 dst = buf;
307 *dst = '\0';
308 } else {
309 dst = last_dst; /* XXX */
310 CHECKMEM (orig);
311 CPY (orig);
312 }
313
314 /* concatenate all the new addresses onto 'buf' */
315 for (isgroup = 0; (cp = getname (fixed_str)); ) {
316 if ((mp = getm (cp, dfhost, dftype, error, sizeof(error))) == NULL) {
317 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
318 badaddrs = add (baddr, badaddrs);
319 continue;
320 }
321 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
322 *dst++ = ';';
323 isgroup = 0;
324 }
325 if (insert (mp)) {
326 /* if we get here we're going to add an address */
327 if (dst != buf) {
328 *dst++ = ',';
329 *dst++ = ' ';
330 }
331 if (mp->m_gname) {
332 CHECKMEM (mp->m_gname);
333 CPY (mp->m_gname);
334 isgroup++;
335 }
336 sp = adrformat (mp);
337 CHECKMEM (sp);
338 CPY (sp);
339 }
340 }
341
342 free (fixed_str);
343
344 if (isgroup)
345 *dst++ = ';';
346
347 *dst = '\0';
348 last_dst = dst;
349 return buf;
350 }
351
352
353 /*
354 * fmt_scan will call this routine if the user includes the function
355 * "(concataddr {component})" in a format string. This behaves exactly
356 * like formataddr, except that it does NOT suppress duplicate addresses
357 * between calls.
358 *
359 * As an implementation detail: I thought about splitting out replformataddr()
360 * into the generic part and duplicate-suppressing part, but the call to
361 * insert() was buried deep within a couple of loops and I didn't see a
362 * way to do it easily. So instead we simply set a special flag to stop
363 * the duplicate check and call replformataddr().
364 */
365 static char *
366 replconcataddr(char *orig, char *str)
367 {
368 char *cp;
369
370 nodupcheck = 1;
371 cp = replformataddr(orig, str);
372 nodupcheck = 0;
373 return cp;
374 }
375
376 static int
377 insert (struct mailname *np)
378 {
379 char buffer[BUFSIZ];
380 struct mailname *mp;
381
382 if (nodupcheck)
383 return 1;
384
385 if (np->m_mbox == NULL)
386 return 0;
387
388 for (mp = &mq; mp->m_next; mp = mp->m_next) {
389 if (!strcasecmp (FENDNULL(np->m_host),
390 FENDNULL(mp->m_next->m_host)) &&
391 !strcasecmp (FENDNULL(np->m_mbox),
392 FENDNULL(mp->m_next->m_mbox)))
393 return 0;
394 }
395 if (!ccme && ismymbox (np))
396 return 0;
397
398 if (querysw) {
399 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
400 if (!read_switch (buffer, anoyes))
401 return 0;
402 }
403 mp->m_next = np;
404
405 return 1;
406 }
407
408
409 /*
410 * Call the mhlproc
411 *
412 * This function expects that argument out has been fflushed by the caller.
413 */
414
415 static void
416 replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
417 {
418 int pid;
419 char *mhl;
420 char *errstr;
421 char **arglist;
422 int argnum;
423
424 if (filter == NULL)
425 return;
426
427 if (access (filter, R_OK) == NOTOK)
428 adios (filter, "unable to read");
429
430 rewind (in);
431 lseek(fileno(in), 0, SEEK_SET);
432
433 arglist = argsplit(mhlproc, &mhl, &argnum);
434
435 switch (pid = fork()) {
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 /*
445 * We're not allocating the memory for the extra arguments,
446 * because we never call arglist_free(). But if we ever change
447 * that be sure to use getcpy() for the extra arguments.
448 */
449 arglist[argnum++] = "-form";
450 arglist[argnum++] = filter;
451 arglist[argnum++] = "-noclear";
452
453 switch (fmtproc) {
454 case 1:
455 arglist[argnum++] = "-fmtproc";
456 arglist[argnum++] = formatproc;
457 break;
458 case 0:
459 arglist[argnum++] = "-nofmtproc";
460 break;
461 }
462
463 arglist[argnum++] = NULL;
464
465 execvp (mhl, arglist);
466 errstr = strerror(errno);
467 if (write(2, "unable to exec ", 15) < 0 ||
468 write(2, mhlproc, strlen(mhlproc)) < 0 ||
469 write(2, ": ", 2) < 0 ||
470 write(2, errstr, strlen(errstr)) < 0 ||
471 write(2, "\n", 1) < 0) {
472 advise ("stderr", "write");
473 }
474 _exit(1);
475
476 default:
477 if (pidXwait (pid, mhl))
478 done (1);
479 fseek (out, 0L, SEEK_END);
480 arglist_free(mhl, arglist);
481 break;
482 }
483 }
484
485
486 static
487 char *
488 fix_addresses (char *str) {
489 char *fixed_str = NULL;
490 int fixed_address = 0;
491
492 if (str) {
493 /*
494 * Attempt to parse each of the addresses in str. If any fail
495 * and can be fixed with escape_local_part(), do that. This
496 * is extra ugly because getm()'s state can only be reset by
497 * call getname(), and getname() needs to be called repeatedly
498 * until it returns NULL to reset its state.
499 */
500 struct adr_node {
501 char *adr;
502 int escape_local_part;
503 int fixed;
504 struct adr_node *next;
505 } *adrs = NULL;
506 struct adr_node *np = adrs;
507 char *cp;
508
509 /*
510 * First, put each of the addresses in a linked list. Note
511 * invalid addresses that might be fixed by escaping the
512 * local part.
513 */
514 while ((cp = getname (str))) {
515 struct adr_node *adr_nodep;
516 char error[BUFSIZ];
517 struct mailname *mp;
518
519 NEW(adr_nodep);
520 adr_nodep->adr = mh_xstrdup (cp);
521 adr_nodep->escape_local_part = 0;
522 adr_nodep->fixed = 0;
523 adr_nodep->next = NULL;
524
525 /* With AD_NAME, errors are not reported to user. */
526 if ((mp = getm (cp, dfhost, dftype, error,
527 sizeof(error))) == NULL) {
528 const char *no_at_sign = "no at-sign after local-part";
529
530 adr_nodep->escape_local_part =
531 has_prefix(error, no_at_sign);
532 } else {
533 mnfree (mp);
534 }
535
536 if (np) {
537 np = np->next = adr_nodep;
538 } else {
539 np = adrs = adr_nodep;
540 }
541 }
542
543 /*
544 * Walk the list and try to fix broken addresses.
545 */
546 for (np = adrs; np; np = np->next) {
547 char *display_name = mh_xstrdup (np->adr);
548 size_t len = strlen (display_name);
549
550 if (np->escape_local_part) {
551 char *local_part_end = strrchr (display_name, '<');
552 char *angle_addr = mh_xstrdup (local_part_end);
553 struct mailname *mp;
554 char *new_adr, *adr;
555
556 *local_part_end = '\0';
557 /* Trim any trailing whitespace. */
558 while (local_part_end > display_name &&
559 isspace ((unsigned char) *--local_part_end)) {
560 *local_part_end = '\0';
561 }
562 escape_local_part (display_name, len);
563 new_adr = concat (display_name, " ", angle_addr, NULL);
564 adr = getname (new_adr);
565 if (adr != NULL &&
566 (mp = getm (adr, dfhost, dftype, NULL, 0)) != NULL) {
567 fixed_address = 1;
568 mnfree (mp);
569 }
570 free (angle_addr);
571 free (new_adr);
572 free (np->adr);
573 np->adr = mh_xstrdup (adr);
574
575 /* Need to flush getname() */
576 while ((cp = getname (""))) continue;
577 } /* else the np->adr is OK, so use it as-is. */
578
579 free (display_name);
580 }
581
582 /*
583 * If any addresses were repaired, build new address string,
584 * replacing broken addresses.
585 */
586 for (np = adrs; np; ) {
587 struct adr_node *next = np->next;
588
589 if (fixed_address) {
590 if (fixed_str) {
591 char *new_str = concat (fixed_str, ", ", np->adr, NULL);
592
593 free (fixed_str);
594 fixed_str = new_str;
595 } else {
596 fixed_str = mh_xstrdup (np->adr);
597 }
598 }
599
600 free (np->adr);
601 free (np);
602 np = next;
603 }
604 }
605
606 if (fixed_address) {
607 return fixed_str;
608 }
609 free (fixed_str);
610 return str ? mh_xstrdup (str) : NULL;
611 }