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