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