]>
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.
109 cptr
= fmt_findcomp ("to");
111 cptr
->c_name
= mh_xstrdup("");
114 cptr
= fmt_findcomp("cc");
116 cptr
->c_name
= mh_xstrdup("");
118 /* set up the "fcc" pseudo-component */
120 cptr
= fmt_findcomp ("fcc");
122 cptr
->c_text
= mh_xstrdup(fcc
);
124 if ((cp
= getenv("USER"))) {
125 cptr
= fmt_findcomp ("user");
127 cptr
->c_text
= mh_xstrdup(cp
);
133 * pick any interesting stuff out of msg "inb"
136 int msg_count
= sizeof tmpbuf
;
137 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
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).
148 i
= fmt_addcomptext(name
, tmpbuf
);
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
;
159 while (state
== FLDPLUS
) {
160 msg_count
= sizeof tmpbuf
;
161 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
172 adios (NULL
, "m_getfld() returned %d", state
);
177 * format and output the header lines.
180 m_getfld_state_destroy (&gstate
);
183 * if there's a "Subject" component, strip any "Re:"s off it
185 cptr
= fmt_findcomp ("subject");
186 if (cptr
&& (cp
= cptr
->c_text
)) {
190 while (isspace((unsigned char) *cp
))
198 if (sp
!= cptr
->c_text
) {
200 cptr
->c_text
= mh_xstrdup(sp
);
204 i
= format_len
+ char_read
+ 256;
205 scanl
= charstring_create (i
+ 2);
209 dat
[3] = outputlinelen
;
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
);
217 fputs ("\nrepl: bad addresses:\n", out
);
218 fputs ( badaddrs
, out
);
222 * Check if we should filter the message
223 * or add mhn directives
228 adios (drft
, "error writing");
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
));
238 adios (drft
, "error writing");
241 /* return dynamically allocated buffers */
242 charstring_free (scanl
);
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 */
251 #define BUFINCR 512 /* how much to expand buf when if fills */
253 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
256 * check if there's enough room in buf for str.
257 * add more mem if needed
259 #define CHECKMEM(str) \
260 if ((len = strlen (str)) >= bufend - dst) {\
262 int n = last_dst - buf;\
263 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
264 buf = mh_xrealloc (buf, bufsiz);\
267 bufend = buf + bufsiz;\
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.
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.
283 replformataddr (char *orig
, char *str
)
286 char baddr
[BUFSIZ
], error
[BUFSIZ
];
291 struct mailname
*mp
= NULL
;
292 char *fixed_str
= fix_addresses (str
);
294 /* if we don't have a buffer yet, get one */
296 buf
= mh_xmalloc (BUFINCR
);
297 last_dst
= buf
; /* XXX */
298 bufsiz
= BUFINCR
- 6; /* leave some slop */
299 bufend
= buf
+ bufsiz
;
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.
307 else if (!orig
|| !*orig
) {
311 dst
= last_dst
; /* XXX */
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
);
323 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
328 /* if we get here we're going to add an address */
334 CHECKMEM (mp
->m_gname
);
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
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().
368 replconcataddr(char *orig
, char *str
)
373 cp
= replformataddr(orig
, str
);
379 insert (struct mailname
*np
)
387 if (np
->m_mbox
== NULL
)
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
: ""))
397 if (!ccme
&& ismymbox (np
))
401 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
402 if (!read_switch (buffer
, anoyes
))
414 * This function expects that argument out has been fflushed by the caller.
418 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
429 if (access (filter
, R_OK
) == NOTOK
)
430 adios (filter
, "unable to read");
433 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
435 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
437 switch (pid
= fork()) {
439 adios ("fork", "unable to");
442 dup2 (fileno (in
), fileno (stdin
));
443 dup2 (fileno (out
), fileno (stdout
));
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.
451 arglist
[argnum
++] = "-form";
452 arglist
[argnum
++] = filter
;
453 arglist
[argnum
++] = "-noclear";
457 arglist
[argnum
++] = "-fmtproc";
458 arglist
[argnum
++] = formatproc
;
461 arglist
[argnum
++] = "-nofmtproc";
465 arglist
[argnum
++] = NULL
;
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");
479 if (pidXwait (pid
, mhl
))
481 fseek (out
, 0L, SEEK_END
);
489 fix_addresses (char *str
) {
490 char *fixed_str
= NULL
;
491 int fixed_address
= 0;
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.
503 int escape_local_part
;
505 struct adr_node
*next
;
507 struct adr_node
*np
= adrs
;
511 * First, put each of the addresses in a linked list. Note
512 * invalid addresses that might be fixed by escaping the
515 while ((cp
= getname (str
))) {
516 struct adr_node
*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
;
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";
531 adr_nodep
->escape_local_part
=
532 has_prefix(error
, no_at_sign
);
538 np
= np
->next
= adr_nodep
;
540 np
= adrs
= adr_nodep
;
545 * Walk the list and try to fix broken addresses.
547 for (np
= adrs
; np
; np
= np
->next
) {
548 char *display_name
= strdup (np
->adr
);
549 size_t len
= strlen (display_name
);
551 if (np
->escape_local_part
) {
552 char *local_part_end
= strrchr (display_name
, '<');
553 char *angle_addr
= strdup (local_part_end
);
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';
563 escape_local_part (display_name
, len
);
564 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
565 adr
= getname (new_adr
);
567 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
574 np
->adr
= strdup (adr
);
576 /* Need to flush getname() */
577 while ((cp
= getname (""))) continue;
578 } /* else the np->adr is OK, so use it as-is. */
584 * If any addresses were repaired, build new address string,
585 * replacing broken addresses.
587 for (np
= adrs
; np
; ) {
588 struct adr_node
*next
= np
->next
;
592 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
597 fixed_str
= strdup (np
->adr
);
611 return str
? strdup (str
) : NULL
;