]> diplodocus.org Git - nmh/blob - uip/replsbr.c
Escape literal leading full stop in man/new.man.
[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 int state, i;
69 struct comp *cptr;
70 char tmpbuf[SBUFSIZ];
71 struct format *fmt;
72 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 if (!ccto) {
109 cptr = fmt_findcomp ("to");
110 if (cptr)
111 cptr->c_name = mh_xstrdup("");
112 }
113 if (!cccc) {
114 cptr = fmt_findcomp("cc");
115 if (cptr)
116 cptr->c_name = mh_xstrdup("");
117 }
118 if (!ccme)
119 ismymbox (NULL);
120
121 /*
122 * pick any interesting stuff out of msg "inb"
123 */
124 for (;;) {
125 int msg_count = sizeof tmpbuf;
126 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
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_getfld (&gstate, name, tmpbuf, &msg_count, inb);
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_getfld (&gstate, name, tmpbuf, &msg_count, inb);
151 }
152 break;
153
154 case LENERR:
155 case FMTERR:
156 case BODY:
157 case FILEEOF:
158 goto finished;
159
160 default:
161 adios (NULL, "m_getfld() 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 mh_xfree(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 mh_xfree(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 memset(&cb, 0, sizeof(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 int 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 = 0; (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 = 0;
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++;
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 = 1;
380 cp = replformataddr(orig, str);
381 nodupcheck = 0;
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 (np->m_host ? np->m_host : "",
399 mp->m_next->m_host ? mp->m_next->m_host : "") &&
400 !strcasecmp (np->m_mbox ? np->m_mbox : "",
401 mp->m_next->m_mbox ? 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), (off_t) 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 break;
490 }
491 }
492
493
494 static
495 char *
496 fix_addresses (char *str) {
497 char *fixed_str = NULL;
498 int fixed_address = 0;
499
500 if (str) {
501 /*
502 * Attempt to parse each of the addresses in str. If any fail
503 * and can be fixed with escape_local_part(), do that. This
504 * is extra ugly because getm()'s state can only be reset by
505 * call getname(), and getname() needs to be called repeatedly
506 * until it returns NULL to reset its state.
507 */
508 struct adr_node {
509 char *adr;
510 int escape_local_part;
511 int fixed;
512 struct adr_node *next;
513 } *adrs = NULL;
514 struct adr_node *np = adrs;
515 char *cp;
516
517 /*
518 * First, put each of the addresses in a linked list. Note
519 * invalid addresses that might be fixed by escaping the
520 * local part.
521 */
522 while ((cp = getname (str))) {
523 struct adr_node *adr_nodep;
524 char error[BUFSIZ];
525 struct mailname *mp;
526
527 NEW(adr_nodep);
528 adr_nodep->adr = strdup (cp);
529 adr_nodep->escape_local_part = 0;
530 adr_nodep->fixed = 0;
531 adr_nodep->next = NULL;
532
533 /* With AD_NAME, errors are not reported to user. */
534 if ((mp = getm (cp, dfhost, dftype, error,
535 sizeof(error))) == NULL) {
536 const char *no_at_sign = "no at-sign after local-part";
537
538 adr_nodep->escape_local_part =
539 has_prefix(error, no_at_sign);
540 } else {
541 mnfree (mp);
542 }
543
544 if (np) {
545 np = np->next = adr_nodep;
546 } else {
547 np = adrs = adr_nodep;
548 }
549 }
550
551 /*
552 * Walk the list and try to fix broken addresses.
553 */
554 for (np = adrs; np; np = np->next) {
555 char *display_name = strdup (np->adr);
556 size_t len = strlen (display_name);
557
558 if (np->escape_local_part) {
559 char *local_part_end = strrchr (display_name, '<');
560 char *angle_addr = strdup (local_part_end);
561 struct mailname *mp;
562 char *new_adr, *adr;
563
564 *local_part_end = '\0';
565 /* Trim any trailing whitespace. */
566 while (local_part_end > display_name &&
567 isspace ((unsigned char) *--local_part_end)) {
568 *local_part_end = '\0';
569 }
570 escape_local_part (display_name, len);
571 new_adr = concat (display_name, " ", angle_addr, NULL);
572 adr = getname (new_adr);
573 if (adr != NULL &&
574 (mp = getm (adr, dfhost, dftype, NULL, 0)) != NULL) {
575 fixed_address = 1;
576 mnfree (mp);
577 }
578 free (angle_addr);
579 free (new_adr);
580 free (np->adr);
581 np->adr = strdup (adr);
582
583 /* Need to flush getname() */
584 while ((cp = getname (""))) continue;
585 } /* else the np->adr is OK, so use it as-is. */
586
587 free (display_name);
588 }
589
590 /*
591 * If any addresses were repaired, build new address string,
592 * replacing broken addresses.
593 */
594 for (np = adrs; np; ) {
595 struct adr_node *next = np->next;
596
597 if (fixed_address) {
598 if (fixed_str) {
599 char *new_str = concat (fixed_str, ", ", np->adr, NULL);
600
601 free (fixed_str);
602 fixed_str = new_str;
603 } else {
604 fixed_str = strdup (np->adr);
605 }
606 }
607
608 free (np->adr);
609 free (np);
610 np = next;
611 }
612 }
613
614 if (fixed_address) {
615 return fixed_str;
616 }
617 free (fixed_str);
618 return str ? strdup (str) : NULL;
619 }