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