]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
1c9a8c36b7ff2def6531768b6b2e055fc5fcf3c9
2 * replsbr.c -- routines to help repl along...
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
13 #include <sys/file.h> /* L_SET */
15 extern short ccto
; /* from repl.c */
22 static char *badaddrs
= NULL
;
23 static char *dfhost
= NULL
;
25 static struct mailname mq
;
26 static int nodupcheck
= 0; /* If set, no check for duplicates */
29 * Buffer size for content part of header fields.
30 * We want this to be large enough so that we don't
31 * do a lot of extra FLDPLUS calls on m_getfld but
32 * small enough so that we don't snarf the entire
33 * message body when we're not going to use any of it.
37 static char *addrcomps
[] = {
56 static int insert (struct mailname
*);
57 static void replfilter (FILE *, FILE *, char *, int);
58 static char *replformataddr(char *, char *);
59 static char *replconcataddr(char *, char *);
60 static char *fix_addresses (char *);
64 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
65 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
72 int char_read
= 0, format_len
, mask
;
73 char name
[NAMESZ
], *cp
;
75 static int dat
[5]; /* aux. data for format routine */
76 m_getfld_state_t gstate
= 0;
77 struct fmt_callbacks cb
;
82 mask
= umask(~m_gmprot());
83 if ((out
= fopen (drft
, "w")) == NULL
)
84 adios (drft
, "unable to create");
88 /* get new format string */
89 cp
= new_fs (form
, NULL
, NULL
);
90 format_len
= strlen (cp
);
92 /* compile format string */
93 fmt_compile (cp
, &fmt
, 1);
95 for (ap
= addrcomps
; *ap
; ap
++) {
96 cptr
= fmt_findcomp (*ap
);
98 cptr
->c_type
|= CT_ADDR
;
102 * ignore any components killed by command line switches
104 * This prevents the component from being found via fmt_findcomp(),
105 * which makes sure no text gets added to it when the message is processed.
108 cptr
= fmt_findcomp ("to");
110 cptr
->c_name
= mh_xstrdup("");
113 cptr
= fmt_findcomp("cc");
115 cptr
->c_name
= mh_xstrdup("");
121 * pick any interesting stuff out of msg "inb"
124 int msg_count
= sizeof tmpbuf
;
125 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
130 * if we're interested in this component, save a pointer
131 * to the component text, then start using our next free
132 * buffer as the component temp buffer (buffer switching
133 * saves an extra copy of the component text).
136 i
= fmt_addcomptext(name
, tmpbuf
);
138 char_read
+= msg_count
;
139 while (state
== FLDPLUS
) {
140 msg_count
= sizeof tmpbuf
;
141 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
142 fmt_appendcomp(i
, name
, tmpbuf
);
143 char_read
+= msg_count
;
147 while (state
== FLDPLUS
) {
148 msg_count
= sizeof tmpbuf
;
149 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
160 adios (NULL
, "m_getfld() returned %d", state
);
165 * format and output the header lines.
168 m_getfld_state_destroy (&gstate
);
170 /* set up the "fcc" pseudo-component */
171 cptr
= fmt_findcomp ("fcc");
173 mh_xfree(cptr
->c_text
);
175 cptr
->c_text
= mh_xstrdup(fcc
);
179 cptr
= fmt_findcomp ("user");
181 mh_xfree(cptr
->c_text
);
182 if ((cp
= getenv("USER")))
183 cptr
->c_text
= mh_xstrdup(cp
);
189 * if there's a "Subject" component, strip any "Re:"s off it
191 cptr
= fmt_findcomp ("subject");
192 if (cptr
&& (cp
= cptr
->c_text
)) {
196 while (isspace((unsigned char) *cp
))
204 if (sp
!= cptr
->c_text
) {
206 cptr
->c_text
= mh_xstrdup(sp
);
210 i
= format_len
+ char_read
+ 256;
211 scanl
= charstring_create (i
+ 2);
215 dat
[3] = outputlinelen
;
217 memset(&cb
, 0, sizeof(cb
));
218 cb
.formataddr
= replformataddr
;
219 cb
.concataddr
= replconcataddr
;
220 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
221 fputs (charstring_buffer (scanl
), out
);
223 fputs ("\nrepl: bad addresses:\n", out
);
224 fputs ( badaddrs
, out
);
228 * Check if we should filter the message
229 * or add mhn directives
234 adios (drft
, "error writing");
236 replfilter (inb
, out
, filter
, fmtproc
);
237 } else if (mime
&& mp
) {
238 fprintf (out
, "#forw [original message] +%s %s\n",
239 mp
->foldpath
, m_name (mp
->lowsel
));
244 adios (drft
, "error writing");
247 /* return dynamically allocated buffers */
248 charstring_free (scanl
);
252 static char *buf
; /* our current working buffer */
253 static char *bufend
; /* end of working buffer */
254 static char *last_dst
; /* buf ptr at end of last call */
255 static unsigned int bufsiz
=0; /* current size of buf */
257 #define BUFINCR 512 /* how much to expand buf when if fills */
259 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
262 * check if there's enough room in buf for str.
263 * add more mem if needed
265 #define CHECKMEM(str) \
266 if ((len = strlen (str)) >= bufend - dst) {\
268 int n = last_dst - buf;\
269 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
270 buf = mh_xrealloc (buf, bufsiz);\
273 bufend = buf + bufsiz;\
278 * fmt_scan will call this routine if the user includes the function
279 * "(formataddr {component})" in a format string. "orig" is the
280 * original contents of the string register. "str" is the address
281 * string to be formatted and concatenated onto orig. This routine
282 * returns a pointer to the concatenated address string.
284 * We try to not do a lot of malloc/copy/free's (which is why we
285 * don't call "getcpy") but still place no upper limit on the
286 * length of the result string.
289 replformataddr (char *orig
, char *str
)
292 char baddr
[BUFSIZ
], error
[BUFSIZ
];
297 struct mailname
*mp
= NULL
;
298 char *fixed_str
= fix_addresses (str
);
300 /* if we don't have a buffer yet, get one */
302 buf
= mh_xmalloc (BUFINCR
);
303 last_dst
= buf
; /* XXX */
304 bufsiz
= BUFINCR
- 6; /* leave some slop */
305 bufend
= buf
+ bufsiz
;
308 * If "orig" points to our buffer we can just pick up where we
309 * left off. Otherwise we have to copy orig into our buffer.
313 else if (!orig
|| !*orig
) {
317 dst
= last_dst
; /* XXX */
322 /* concatenate all the new addresses onto 'buf' */
323 for (isgroup
= 0; (cp
= getname (fixed_str
)); ) {
324 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
325 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
326 badaddrs
= add (baddr
, badaddrs
);
329 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
334 /* if we get here we're going to add an address */
340 CHECKMEM (mp
->m_gname
);
362 * fmt_scan will call this routine if the user includes the function
363 * "(concataddr {component})" in a format string. This behaves exactly
364 * like formataddr, except that it does NOT suppress duplicate addresses
367 * As an implementation detail: I thought about splitting out replformataddr()
368 * into the generic part and duplicate-suppressing part, but the call to
369 * insert() was buried deep within a couple of loops and I didn't see a
370 * way to do it easily. So instead we simply set a special flag to stop
371 * the duplicate check and call replformataddr().
374 replconcataddr(char *orig
, char *str
)
379 cp
= replformataddr(orig
, str
);
385 insert (struct mailname
*np
)
393 if (np
->m_mbox
== NULL
)
396 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
397 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
398 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
399 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
400 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: ""))
403 if (!ccme
&& ismymbox (np
))
407 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
408 if (!read_switch (buffer
, anoyes
))
420 * This function expects that argument out has been fflushed by the caller.
424 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
435 if (access (filter
, R_OK
) == NOTOK
)
436 adios (filter
, "unable to read");
439 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
441 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
443 switch (pid
= fork()) {
445 adios ("fork", "unable to");
448 dup2 (fileno (in
), fileno (stdin
));
449 dup2 (fileno (out
), fileno (stdout
));
453 * We're not allocating the memory for the extra arguments,
454 * because we never call arglist_free(). But if we ever change
455 * that be sure to use getcpy() for the extra arguments.
457 arglist
[argnum
++] = "-form";
458 arglist
[argnum
++] = filter
;
459 arglist
[argnum
++] = "-noclear";
463 arglist
[argnum
++] = "-fmtproc";
464 arglist
[argnum
++] = formatproc
;
467 arglist
[argnum
++] = "-nofmtproc";
471 arglist
[argnum
++] = NULL
;
473 execvp (mhl
, arglist
);
474 errstr
= strerror(errno
);
475 if (write(2, "unable to exec ", 15) < 0 ||
476 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
477 write(2, ": ", 2) < 0 ||
478 write(2, errstr
, strlen(errstr
)) < 0 ||
479 write(2, "\n", 1) < 0) {
480 advise ("stderr", "write");
485 if (pidXwait (pid
, mhl
))
487 fseek (out
, 0L, SEEK_END
);
495 fix_addresses (char *str
) {
496 char *fixed_str
= NULL
;
497 int fixed_address
= 0;
501 * Attempt to parse each of the addresses in str. If any fail
502 * and can be fixed with escape_local_part(), do that. This
503 * is extra ugly because getm()'s state can only be reset by
504 * call getname(), and getname() needs to be called repeatedly
505 * until it returns NULL to reset its state.
509 int escape_local_part
;
511 struct adr_node
*next
;
513 struct adr_node
*np
= adrs
;
517 * First, put each of the addresses in a linked list. Note
518 * invalid addresses that might be fixed by escaping the
521 while ((cp
= getname (str
))) {
522 struct adr_node
*adr_nodep
;
527 adr_nodep
->adr
= mh_xstrdup (cp
);
528 adr_nodep
->escape_local_part
= 0;
529 adr_nodep
->fixed
= 0;
530 adr_nodep
->next
= NULL
;
532 /* With AD_NAME, errors are not reported to user. */
533 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
534 sizeof(error
))) == NULL
) {
535 const char *no_at_sign
= "no at-sign after local-part";
537 adr_nodep
->escape_local_part
=
538 has_prefix(error
, no_at_sign
);
544 np
= np
->next
= adr_nodep
;
546 np
= adrs
= adr_nodep
;
551 * Walk the list and try to fix broken addresses.
553 for (np
= adrs
; np
; np
= np
->next
) {
554 char *display_name
= mh_xstrdup (np
->adr
);
555 size_t len
= strlen (display_name
);
557 if (np
->escape_local_part
) {
558 char *local_part_end
= strrchr (display_name
, '<');
559 char *angle_addr
= mh_xstrdup (local_part_end
);
563 *local_part_end
= '\0';
564 /* Trim any trailing whitespace. */
565 while (local_part_end
> display_name
&&
566 isspace ((unsigned char) *--local_part_end
)) {
567 *local_part_end
= '\0';
569 escape_local_part (display_name
, len
);
570 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
571 adr
= getname (new_adr
);
573 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
580 np
->adr
= mh_xstrdup (adr
);
582 /* Need to flush getname() */
583 while ((cp
= getname (""))) continue;
584 } /* else the np->adr is OK, so use it as-is. */
590 * If any addresses were repaired, build new address string,
591 * replacing broken addresses.
593 for (np
= adrs
; np
; ) {
594 struct adr_node
*next
= np
->next
;
598 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
603 fixed_str
= mh_xstrdup (np
->adr
);
617 return str
? mh_xstrdup (str
) : NULL
;