]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
c3b19f4264423b52cc03dd85581715a42525e0db
1 /* replsbr.c -- routines to help repl along...
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/escape_addresses.h"
10 #include "sbr/pidstatus.h"
11 #include "sbr/arglist.h"
12 #include "sbr/error.h"
13 #include "h/addrsbr.h"
14 #include "h/fmt_scan.h"
27 static char *badaddrs
= NULL
;
28 static char *dfhost
= NULL
;
30 static struct mailname mq
;
31 static bool nodupcheck
; /* If set, no check for duplicates */
33 static char *addrcomps
[] = {
52 static int insert (struct mailname
*);
53 static void replfilter (FILE *, FILE *, char *, int);
54 static char *replformataddr(char *, char *);
55 static char *replconcataddr(char *, char *);
56 static char *fix_addresses (char *);
60 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
61 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
65 char tmpbuf
[NMH_BUFSIZ
];
68 int char_read
= 0, format_len
, mask
;
69 char name
[NAMESZ
], *cp
;
71 static int dat
[5]; /* aux. data for format routine */
72 m_getfld_state_t gstate
;
73 struct fmt_callbacks cb
;
78 mask
= umask(~m_gmprot());
79 if ((out
= fopen (drft
, "w")) == NULL
)
80 adios (drft
, "unable to create");
84 /* get new format string */
85 cp
= new_fs (form
, NULL
, NULL
);
86 format_len
= strlen (cp
);
88 /* compile format string */
89 fmt_compile (cp
, &fmt
, 1);
91 for (ap
= addrcomps
; *ap
; ap
++) {
92 cptr
= fmt_findcomp (*ap
);
94 cptr
->c_type
|= CT_ADDR
;
98 * ignore any components killed by command line switches
100 * This prevents the component from being found via fmt_findcomp(),
101 * which makes sure no text gets added to it when the message is processed.
104 cptr
= fmt_findcomp ("to");
106 cptr
->c_name
= mh_xstrdup("");
109 cptr
= fmt_findcomp("cc");
111 cptr
->c_name
= mh_xstrdup("");
117 * pick any interesting stuff out of msg "inb"
119 gstate
= m_getfld_state_init(inb
);
121 int msg_count
= sizeof tmpbuf
;
122 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
127 * if we're interested in this component, save a pointer
128 * to the component text, then start using our next free
129 * buffer as the component temp buffer (buffer switching
130 * saves an extra copy of the component text).
133 i
= fmt_addcomptext(name
, tmpbuf
);
135 char_read
+= msg_count
;
136 while (state
== FLDPLUS
) {
137 msg_count
= sizeof tmpbuf
;
138 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
139 fmt_appendcomp(i
, name
, tmpbuf
);
140 char_read
+= msg_count
;
144 while (state
== FLDPLUS
) {
145 msg_count
= sizeof tmpbuf
;
146 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
157 die("m_getfld2() returned %d", state
);
162 * format and output the header lines.
165 m_getfld_state_destroy (&gstate
);
167 /* set up the "fcc" pseudo-component */
168 cptr
= fmt_findcomp ("fcc");
172 cptr
->c_text
= mh_xstrdup(fcc
);
176 cptr
= fmt_findcomp ("user");
179 if ((cp
= getenv("USER")))
180 cptr
->c_text
= mh_xstrdup(cp
);
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
;
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
= false; (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 (FENDNULL(np
->m_host
),
395 FENDNULL(mp
->m_next
->m_host
)) &&
396 !strcasecmp (FENDNULL(np
->m_mbox
),
397 FENDNULL(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
), 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
);
485 arglist_free(mhl
, arglist
);
492 fix_addresses (char *str
)
494 char *fixed_str
= NULL
;
495 bool fixed_address
= false;
499 * Attempt to parse each of the addresses in str. If any fail
500 * and can be fixed with escape_local_part(), do that. This
501 * is extra ugly because getm()'s state can only be reset by
502 * call getname(), and getname() needs to be called repeatedly
503 * until it returns NULL to reset its state.
507 int escape_local_part
;
509 struct adr_node
*next
;
511 struct adr_node
*np
= adrs
;
515 * First, put each of the addresses in a linked list. Note
516 * invalid addresses that might be fixed by escaping the
519 while ((cp
= getname (str
))) {
520 struct adr_node
*adr_nodep
;
525 adr_nodep
->adr
= mh_xstrdup (cp
);
526 adr_nodep
->escape_local_part
= 0;
527 adr_nodep
->fixed
= 0;
528 adr_nodep
->next
= NULL
;
530 /* With AD_NAME, errors are not reported to user. */
531 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
532 sizeof(error
))) == NULL
) {
533 const char *no_at_sign
= "no at-sign after local-part";
535 adr_nodep
->escape_local_part
=
536 has_prefix(error
, no_at_sign
);
542 np
= np
->next
= adr_nodep
;
544 np
= adrs
= adr_nodep
;
549 * Walk the list and try to fix broken addresses.
551 for (np
= adrs
; np
; np
= np
->next
) {
552 char *display_name
= mh_xstrdup (np
->adr
);
553 size_t len
= strlen (display_name
);
555 if (np
->escape_local_part
) {
556 char *local_part_end
= strrchr (display_name
, '<');
557 char *angle_addr
= mh_xstrdup (local_part_end
);
561 *local_part_end
= '\0';
562 /* Trim any trailing whitespace. */
563 while (local_part_end
> display_name
&&
564 isspace ((unsigned char) *--local_part_end
)) {
565 *local_part_end
= '\0';
567 escape_local_part (display_name
, len
);
568 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
569 adr
= getname (new_adr
);
571 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
572 fixed_address
= true;
578 np
->adr
= mh_xstrdup (adr
);
580 /* Need to flush getname() */
581 while ((cp
= getname (""))) continue;
582 } /* else the np->adr is OK, so use it as-is. */
588 * If any addresses were repaired, build new address string,
589 * replacing broken addresses.
591 for (np
= adrs
; np
; ) {
592 struct adr_node
*next
= np
->next
;
596 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
601 fixed_str
= mh_xstrdup (np
->adr
);
615 return str
? mh_xstrdup (str
) : NULL
;