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