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