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