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