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