]> diplodocus.org Git - nmh/blob - uip/replsbr.c
93088975b363cc0233d58babfc3b987f742af824
[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/concat.h"
10 #include "sbr/closefds.h"
11 #include "sbr/uprf.h"
12 #include "sbr/escape_addresses.h"
13 #include "sbr/pidstatus.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
16 #include "h/addrsbr.h"
17 #include "h/fmt_scan.h"
18 #include "h/done.h"
19 #include "h/utils.h"
20 #include <sys/file.h>
21 #include "replsbr.h"
22
23 short ccto = -1;
24 short cccc = -1;
25 short ccme = -1;
26 short querysw = 0;
27
28 static int dftype=0;
29
30 static char *badaddrs = NULL;
31 static char *dfhost = NULL;
32
33 static struct mailname mq;
34 static bool nodupcheck; /* If set, no check for duplicates */
35
36 static char *addrcomps[] = {
37 "from",
38 "sender",
39 "reply-to",
40 "to",
41 "cc",
42 "bcc",
43 "resent-from",
44 "resent-sender",
45 "resent-reply-to",
46 "resent-to",
47 "resent-cc",
48 "resent-bcc",
49 NULL
50 };
51
52 /*
53 * static prototypes
54 */
55 static int insert (struct mailname *);
56 static void replfilter (FILE *, FILE *, char *, int);
57 static char *replformataddr(char *, char *);
58 static char *replconcataddr(char *, char *);
59 static char *fix_addresses (char *);
60
61
62 void
63 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
64 int mime, char *form, char *filter, char *fcc, int fmtproc)
65 {
66 int state, i;
67 struct comp *cptr;
68 char tmpbuf[NMH_BUFSIZ];
69 struct format *fmt;
70 char **ap;
71 int char_read = 0, format_len, mask;
72 char name[NAMESZ], *cp;
73 charstring_t scanl;
74 static int dat[5]; /* aux. data for format routine */
75 m_getfld_state_t gstate;
76 struct fmt_callbacks cb;
77
78 FILE *out;
79 NMH_UNUSED (msg);
80
81 mask = umask(~m_gmprot());
82 if ((out = fopen (drft, "w")) == NULL)
83 adios (drft, "unable to create");
84
85 umask(mask);
86
87 /* get new format string */
88 cp = new_fs (form, NULL, NULL);
89 format_len = strlen (cp);
90
91 /* compile format string */
92 fmt_compile (cp, &fmt, 1);
93
94 for (ap = addrcomps; *ap; ap++) {
95 cptr = fmt_findcomp (*ap);
96 if (cptr)
97 cptr->c_type |= CT_ADDR;
98 }
99
100 /*
101 * ignore any components killed by command line switches
102 *
103 * This prevents the component from being found via fmt_findcomp(),
104 * which makes sure no text gets added to it when the message is processed.
105 */
106 if (!ccto) {
107 cptr = fmt_findcomp ("to");
108 if (cptr)
109 cptr->c_name = mh_xstrdup("");
110 }
111 if (!cccc) {
112 cptr = fmt_findcomp("cc");
113 if (cptr)
114 cptr->c_name = mh_xstrdup("");
115 }
116 if (!ccme)
117 ismymbox (NULL);
118
119 /*
120 * pick any interesting stuff out of msg "inb"
121 */
122 gstate = m_getfld_state_init(inb);
123 for (;;) {
124 int msg_count = sizeof tmpbuf;
125 state = m_getfld2(&gstate, name, tmpbuf, &msg_count);
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_getfld2(&gstate, name, tmpbuf, &msg_count);
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_getfld2(&gstate, name, tmpbuf, &msg_count);
150 }
151 break;
152
153 case LENERR:
154 case FMTERR:
155 case BODY:
156 case FILEEOF:
157 goto finished;
158
159 default:
160 die("m_getfld2() 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 free(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 free(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 ZERO(&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 bool 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 = false; (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 = false;
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 = true;
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 = true;
379 cp = replformataddr(orig, str);
380 nodupcheck = false;
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 (FENDNULL(np->m_host),
398 FENDNULL(mp->m_next->m_host)) &&
399 !strcasecmp (FENDNULL(np->m_mbox),
400 FENDNULL(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), 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 arglist_free(mhl, arglist);
489 break;
490 }
491 }
492
493
494 static char *
495 fix_addresses (char *str)
496 {
497 char *fixed_str = NULL;
498 bool fixed_address = false;
499
500 if (str) {
501 /*
502 * Attempt to parse each of the addresses in str. If any fail
503 * and can be fixed with escape_local_part(), do that. This
504 * is extra ugly because getm()'s state can only be reset by
505 * call getname(), and getname() needs to be called repeatedly
506 * until it returns NULL to reset its state.
507 */
508 struct adr_node {
509 char *adr;
510 int escape_local_part;
511 int fixed;
512 struct adr_node *next;
513 } *adrs = NULL;
514 struct adr_node *np = adrs;
515 char *cp;
516
517 /*
518 * First, put each of the addresses in a linked list. Note
519 * invalid addresses that might be fixed by escaping the
520 * local part.
521 */
522 while ((cp = getname (str))) {
523 struct adr_node *adr_nodep;
524 char error[BUFSIZ];
525 struct mailname *mp;
526
527 NEW(adr_nodep);
528 adr_nodep->adr = mh_xstrdup (cp);
529 adr_nodep->escape_local_part = 0;
530 adr_nodep->fixed = 0;
531 adr_nodep->next = NULL;
532
533 /* With AD_NAME, errors are not reported to user. */
534 if ((mp = getm (cp, dfhost, dftype, error,
535 sizeof(error))) == NULL) {
536 const char *no_at_sign = "no at-sign after local-part";
537
538 adr_nodep->escape_local_part =
539 has_prefix(error, no_at_sign);
540 } else {
541 mnfree (mp);
542 }
543
544 if (np) {
545 np = np->next = adr_nodep;
546 } else {
547 np = adrs = adr_nodep;
548 }
549 }
550
551 /*
552 * Walk the list and try to fix broken addresses.
553 */
554 for (np = adrs; np; np = np->next) {
555 char *display_name = mh_xstrdup (np->adr);
556 size_t len = strlen (display_name);
557
558 if (np->escape_local_part) {
559 char *local_part_end = strrchr (display_name, '<');
560 char *angle_addr = mh_xstrdup (local_part_end);
561 struct mailname *mp;
562 char *new_adr, *adr;
563
564 *local_part_end = '\0';
565 /* Trim any trailing whitespace. */
566 while (local_part_end > display_name &&
567 isspace ((unsigned char) *--local_part_end)) {
568 *local_part_end = '\0';
569 }
570 escape_local_part (display_name, len);
571 new_adr = concat (display_name, " ", angle_addr, NULL);
572 adr = getname (new_adr);
573 if (adr != NULL &&
574 (mp = getm (adr, dfhost, dftype, NULL, 0)) != NULL) {
575 fixed_address = true;
576 mnfree (mp);
577 }
578 free (angle_addr);
579 free (new_adr);
580 free (np->adr);
581 np->adr = mh_xstrdup (adr);
582
583 /* Need to flush getname() */
584 while ((cp = getname (""))) continue;
585 } /* else the np->adr is OK, so use it as-is. */
586
587 free (display_name);
588 }
589
590 /*
591 * If any addresses were repaired, build new address string,
592 * replacing broken addresses.
593 */
594 for (np = adrs; np; ) {
595 struct adr_node *next = np->next;
596
597 if (fixed_address) {
598 if (fixed_str) {
599 char *new_str = concat (fixed_str, ", ", np->adr, NULL);
600
601 free (fixed_str);
602 fixed_str = new_str;
603 } else {
604 fixed_str = mh_xstrdup (np->adr);
605 }
606 }
607
608 free (np->adr);
609 free (np);
610 np = next;
611 }
612 }
613
614 if (fixed_address) {
615 return fixed_str;
616 }
617 free (fixed_str);
618 return str ? mh_xstrdup (str) : NULL;
619 }