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