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