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