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