]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
3 * replsbr.c -- routines to help repl along...
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.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
14 #include <sys/file.h> /* L_SET */
16 extern short ccto
; /* from repl.c */
23 static char *badaddrs
= NULL
;
24 static char *dfhost
= NULL
;
26 static struct mailname mq
;
27 static int nodupcheck
= 0; /* If set, no check for duplicates */
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.
38 static char *addrcomps
[] = {
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 *);
65 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
66 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
73 int char_read
= 0, format_len
, mask
;
74 char name
[NAMESZ
], *cp
;
76 static int dat
[5]; /* aux. data for format routine */
77 m_getfld_state_t gstate
= 0;
78 struct fmt_callbacks cb
;
83 mask
= umask(~m_gmprot());
84 if ((out
= fopen (drft
, "w")) == NULL
)
85 adios (drft
, "unable to create");
89 /* get new format string */
90 cp
= new_fs (form
, NULL
, NULL
);
91 format_len
= strlen (cp
);
93 /* compile format string */
94 fmt_compile (cp
, &fmt
, 1);
96 for (ap
= addrcomps
; *ap
; ap
++) {
97 cptr
= fmt_findcomp (*ap
);
99 cptr
->c_type
|= CT_ADDR
;
103 * ignore any components killed by command line switches
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.
108 * getcpy(NULL) returns a malloc'd zero-length string, so it can safely
112 cptr
= fmt_findcomp ("to");
114 cptr
->c_name
= mh_xstrdup("");
117 cptr
= fmt_findcomp("cc");
119 cptr
->c_name
= mh_xstrdup("");
121 /* set up the "fcc" pseudo-component */
123 cptr
= fmt_findcomp ("fcc");
125 cptr
->c_text
= mh_xstrdup(fcc
);
127 if ((cp
= getenv("USER"))) {
128 cptr
= fmt_findcomp ("user");
130 cptr
->c_text
= mh_xstrdup(cp
);
136 * pick any interesting stuff out of msg "inb"
139 int msg_count
= sizeof tmpbuf
;
140 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
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).
151 i
= fmt_addcomptext(name
, tmpbuf
);
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
;
162 while (state
== FLDPLUS
) {
163 msg_count
= sizeof tmpbuf
;
164 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
175 adios (NULL
, "m_getfld() returned %d", state
);
180 * format and output the header lines.
183 m_getfld_state_destroy (&gstate
);
186 * if there's a "Subject" component, strip any "Re:"s off it
188 cptr
= fmt_findcomp ("subject");
189 if (cptr
&& (cp
= cptr
->c_text
)) {
193 while (isspace((unsigned char) *cp
))
201 if (sp
!= cptr
->c_text
) {
203 cptr
->c_text
= mh_xstrdup(sp
);
207 i
= format_len
+ char_read
+ 256;
208 scanl
= charstring_create (i
+ 2);
212 dat
[3] = outputlinelen
;
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
);
220 fputs ("\nrepl: bad addresses:\n", out
);
221 fputs ( badaddrs
, out
);
225 * Check if we should filter the message
226 * or add mhn directives
231 adios (drft
, "error writing");
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
));
241 adios (drft
, "error writing");
244 /* return dynamically allocated buffers */
245 charstring_free (scanl
);
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 */
254 #define BUFINCR 512 /* how much to expand buf when if fills */
256 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
259 * check if there's enough room in buf for str.
260 * add more mem if needed
262 #define CHECKMEM(str) \
263 if ((len = strlen (str)) >= bufend - dst) {\
265 int n = last_dst - buf;\
266 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
267 buf = mh_xrealloc (buf, bufsiz);\
270 bufend = buf + bufsiz;\
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.
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.
286 replformataddr (char *orig
, char *str
)
289 char baddr
[BUFSIZ
], error
[BUFSIZ
];
294 struct mailname
*mp
= NULL
;
295 char *fixed_str
= fix_addresses (str
);
297 /* if we don't have a buffer yet, get one */
299 buf
= mh_xmalloc (BUFINCR
);
300 last_dst
= buf
; /* XXX */
301 bufsiz
= BUFINCR
- 6; /* leave some slop */
302 bufend
= buf
+ bufsiz
;
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.
310 else if (!orig
|| !*orig
) {
314 dst
= last_dst
; /* XXX */
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
);
326 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
331 /* if we get here we're going to add an address */
337 CHECKMEM (mp
->m_gname
);
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
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().
371 replconcataddr(char *orig
, char *str
)
376 cp
= replformataddr(orig
, str
);
382 insert (struct mailname
*np
)
390 if (np
->m_mbox
== NULL
)
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
: ""))
400 if (!ccme
&& ismymbox (np
))
404 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
405 if (!read_switch (buffer
, anoyes
))
417 * This function expects that argument out has been fflushed by the caller.
421 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
432 if (access (filter
, R_OK
) == NOTOK
)
433 adios (filter
, "unable to read");
436 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
438 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
440 switch (pid
= fork()) {
442 adios ("fork", "unable to");
445 dup2 (fileno (in
), fileno (stdin
));
446 dup2 (fileno (out
), fileno (stdout
));
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.
454 arglist
[argnum
++] = "-form";
455 arglist
[argnum
++] = filter
;
456 arglist
[argnum
++] = "-noclear";
460 arglist
[argnum
++] = "-fmtproc";
461 arglist
[argnum
++] = formatproc
;
464 arglist
[argnum
++] = "-nofmtproc";
468 arglist
[argnum
++] = NULL
;
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");
482 if (pidXwait (pid
, mhl
))
484 fseek (out
, 0L, SEEK_END
);
492 fix_addresses (char *str
) {
493 char *fixed_str
= NULL
;
494 int fixed_address
= 0;
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.
506 int escape_local_part
;
508 struct adr_node
*next
;
510 struct adr_node
*np
= adrs
;
514 * First, put each of the addresses in a linked list. Note
515 * invalid addresses that might be fixed by escaping the
518 while ((cp
= getname (str
))) {
519 struct adr_node
*adr_nodep
;
524 adr_nodep
->adr
= strdup (cp
);
525 adr_nodep
->escape_local_part
= 0;
526 adr_nodep
->fixed
= 0;
527 adr_nodep
->next
= NULL
;
529 /* With AD_NAME, errors are not reported to user. */
530 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
531 sizeof(error
))) == NULL
) {
532 const char *no_at_sign
= "no at-sign after local-part";
534 adr_nodep
->escape_local_part
=
535 HasPrefix(error
, no_at_sign
);
541 np
= np
->next
= adr_nodep
;
543 np
= adrs
= adr_nodep
;
548 * Walk the list and try to fix broken addresses.
550 for (np
= adrs
; np
; np
= np
->next
) {
551 char *display_name
= strdup (np
->adr
);
552 size_t len
= strlen (display_name
);
554 if (np
->escape_local_part
) {
555 char *local_part_end
= strrchr (display_name
, '<');
556 char *angle_addr
= strdup (local_part_end
);
560 *local_part_end
= '\0';
561 /* Trim any trailing whitespace. */
562 while (local_part_end
> display_name
&&
563 isspace ((unsigned char) *--local_part_end
)) {
564 *local_part_end
= '\0';
566 escape_local_part (display_name
, len
);
567 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
568 adr
= getname (new_adr
);
570 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
577 np
->adr
= strdup (adr
);
579 /* Need to flush getname() */
580 while ((cp
= getname (""))) continue;
581 } /* else the np->adr is OK, so use it as-is. */
587 * If any addresses were repaired, build new address string,
588 * replacing broken addresses.
590 for (np
= adrs
; np
; ) {
591 struct adr_node
*next
= np
->next
;
595 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
600 fixed_str
= strdup (np
->adr
);
614 return str
? strdup (str
) : NULL
;