]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
9c1cd8eded6af314b363809da6f8fc346001fbc9
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/m_gmprot.h"
10 #include "sbr/m_getfld.h"
11 #include "sbr/read_switch.h"
12 #include "sbr/concat.h"
13 #include "sbr/closefds.h"
15 #include "sbr/escape_addresses.h"
16 #include "sbr/pidstatus.h"
17 #include "sbr/arglist.h"
18 #include "sbr/error.h"
19 #include "h/addrsbr.h"
20 #include "h/fmt_scan.h"
33 static char *badaddrs
= NULL
;
34 static char *dfhost
= NULL
;
36 static struct mailname mq
;
37 static bool nodupcheck
; /* If set, no check for duplicates */
39 static char *addrcomps
[] = {
58 static int insert (struct mailname
*);
59 static void replfilter (FILE *, FILE *, char *, int);
60 static char *replformataddr(char *, char *);
61 static char *replconcataddr(char *, char *);
62 static char *fix_addresses (char *);
66 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
67 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
71 char tmpbuf
[NMH_BUFSIZ
];
74 int char_read
= 0, format_len
, mask
;
75 char name
[NAMESZ
], *cp
;
77 static int dat
[5]; /* aux. data for format routine */
78 m_getfld_state_t gstate
;
79 struct fmt_callbacks cb
;
84 mask
= umask(~m_gmprot());
85 if ((out
= fopen (drft
, "w")) == NULL
)
86 adios (drft
, "unable to create");
90 /* get new format string */
91 cp
= new_fs (form
, NULL
, NULL
);
92 format_len
= strlen (cp
);
94 /* compile format string */
95 fmt_compile (cp
, &fmt
, 1);
97 for (ap
= addrcomps
; *ap
; ap
++) {
98 cptr
= fmt_findcomp (*ap
);
100 cptr
->c_type
|= CT_ADDR
;
104 * ignore any components killed by command line switches
106 * This prevents the component from being found via fmt_findcomp(),
107 * which makes sure no text gets added to it when the message is processed.
110 cptr
= fmt_findcomp ("to");
112 cptr
->c_name
= mh_xstrdup("");
115 cptr
= fmt_findcomp("cc");
117 cptr
->c_name
= mh_xstrdup("");
123 * pick any interesting stuff out of msg "inb"
125 gstate
= m_getfld_state_init(inb
);
127 int msg_count
= sizeof tmpbuf
;
128 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
133 * if we're interested in this component, save a pointer
134 * to the component text, then start using our next free
135 * buffer as the component temp buffer (buffer switching
136 * saves an extra copy of the component text).
139 i
= fmt_addcomptext(name
, tmpbuf
);
141 char_read
+= msg_count
;
142 while (state
== FLDPLUS
) {
143 msg_count
= sizeof tmpbuf
;
144 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
145 fmt_appendcomp(i
, name
, tmpbuf
);
146 char_read
+= msg_count
;
150 while (state
== FLDPLUS
) {
151 msg_count
= sizeof tmpbuf
;
152 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
163 die("m_getfld2() returned %d", state
);
168 * format and output the header lines.
171 m_getfld_state_destroy (&gstate
);
173 /* set up the "fcc" pseudo-component */
174 cptr
= fmt_findcomp ("fcc");
178 cptr
->c_text
= mh_xstrdup(fcc
);
182 cptr
= fmt_findcomp ("user");
185 if ((cp
= getenv("USER")))
186 cptr
->c_text
= mh_xstrdup(cp
);
192 * if there's a "Subject" component, strip any "Re:"s off it
194 cptr
= fmt_findcomp ("subject");
195 if (cptr
&& (cp
= cptr
->c_text
)) {
199 while (isspace((unsigned char) *cp
))
207 if (sp
!= cptr
->c_text
) {
209 cptr
->c_text
= mh_xstrdup(sp
);
213 i
= format_len
+ char_read
+ 256;
214 scanl
= charstring_create (i
+ 2);
218 dat
[3] = outputlinelen
;
221 cb
.formataddr
= replformataddr
;
222 cb
.concataddr
= replconcataddr
;
223 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
224 fputs (charstring_buffer (scanl
), out
);
226 fputs ("\nrepl: bad addresses:\n", out
);
227 fputs ( badaddrs
, out
);
231 * Check if we should filter the message
232 * or add mhn directives
237 adios (drft
, "error writing");
239 replfilter (inb
, out
, filter
, fmtproc
);
240 } else if (mime
&& mp
) {
241 fprintf (out
, "#forw [original message] +%s %s\n",
242 mp
->foldpath
, m_name (mp
->lowsel
));
247 adios (drft
, "error writing");
250 /* return dynamically allocated buffers */
251 charstring_free (scanl
);
255 static char *buf
; /* our current working buffer */
256 static char *bufend
; /* end of working buffer */
257 static char *last_dst
; /* buf ptr at end of last call */
258 static unsigned int bufsiz
=0; /* current size of buf */
260 #define BUFINCR 512 /* how much to expand buf when if fills */
262 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
265 * check if there's enough room in buf for str.
266 * add more mem if needed
268 #define CHECKMEM(str) \
269 if ((len = strlen (str)) >= bufend - dst) {\
271 int n = last_dst - buf;\
272 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
273 buf = mh_xrealloc (buf, bufsiz);\
276 bufend = buf + bufsiz;\
281 * fmt_scan will call this routine if the user includes the function
282 * "(formataddr {component})" in a format string. "orig" is the
283 * original contents of the string register. "str" is the address
284 * string to be formatted and concatenated onto orig. This routine
285 * returns a pointer to the concatenated address string.
287 * We try to not do a lot of malloc/copy/free's (which is why we
288 * don't call "getcpy") but still place no upper limit on the
289 * length of the result string.
292 replformataddr (char *orig
, char *str
)
295 char baddr
[BUFSIZ
], error
[BUFSIZ
];
300 struct mailname
*mp
= NULL
;
301 char *fixed_str
= fix_addresses (str
);
303 /* if we don't have a buffer yet, get one */
305 buf
= mh_xmalloc (BUFINCR
);
306 last_dst
= buf
; /* XXX */
307 bufsiz
= BUFINCR
- 6; /* leave some slop */
308 bufend
= buf
+ bufsiz
;
311 * If "orig" points to our buffer we can just pick up where we
312 * left off. Otherwise we have to copy orig into our buffer.
316 else if (!orig
|| !*orig
) {
320 dst
= last_dst
; /* XXX */
325 /* concatenate all the new addresses onto 'buf' */
326 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
327 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
328 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
329 badaddrs
= add (baddr
, badaddrs
);
332 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
337 /* if we get here we're going to add an address */
343 CHECKMEM (mp
->m_gname
);
365 * fmt_scan will call this routine if the user includes the function
366 * "(concataddr {component})" in a format string. This behaves exactly
367 * like formataddr, except that it does NOT suppress duplicate addresses
370 * As an implementation detail: I thought about splitting out replformataddr()
371 * into the generic part and duplicate-suppressing part, but the call to
372 * insert() was buried deep within a couple of loops and I didn't see a
373 * way to do it easily. So instead we simply set a special flag to stop
374 * the duplicate check and call replformataddr().
377 replconcataddr(char *orig
, char *str
)
382 cp
= replformataddr(orig
, str
);
388 insert (struct mailname
*np
)
396 if (np
->m_mbox
== NULL
)
399 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
400 if (!strcasecmp (FENDNULL(np
->m_host
),
401 FENDNULL(mp
->m_next
->m_host
)) &&
402 !strcasecmp (FENDNULL(np
->m_mbox
),
403 FENDNULL(mp
->m_next
->m_mbox
)))
406 if (!ccme
&& ismymbox (np
))
410 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
411 if (!read_switch (buffer
, anoyes
))
423 * This function expects that argument out has been fflushed by the caller.
427 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
438 if (access (filter
, R_OK
) == NOTOK
)
439 adios (filter
, "unable to read");
442 lseek(fileno(in
), 0, SEEK_SET
);
444 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
446 switch (pid
= fork()) {
448 adios ("fork", "unable to");
451 dup2 (fileno (in
), fileno (stdin
));
452 dup2 (fileno (out
), fileno (stdout
));
456 * We're not allocating the memory for the extra arguments,
457 * because we never call arglist_free(). But if we ever change
458 * that be sure to use getcpy() for the extra arguments.
460 arglist
[argnum
++] = "-form";
461 arglist
[argnum
++] = filter
;
462 arglist
[argnum
++] = "-noclear";
466 arglist
[argnum
++] = "-fmtproc";
467 arglist
[argnum
++] = formatproc
;
470 arglist
[argnum
++] = "-nofmtproc";
474 arglist
[argnum
++] = NULL
;
476 execvp (mhl
, arglist
);
477 errstr
= strerror(errno
);
478 if (write(2, "unable to exec ", 15) < 0 ||
479 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
480 write(2, ": ", 2) < 0 ||
481 write(2, errstr
, strlen(errstr
)) < 0 ||
482 write(2, "\n", 1) < 0) {
483 advise ("stderr", "write");
488 if (pidXwait (pid
, mhl
))
490 fseek (out
, 0L, SEEK_END
);
491 arglist_free(mhl
, arglist
);
498 fix_addresses (char *str
)
500 char *fixed_str
= NULL
;
501 bool fixed_address
= false;
505 * Attempt to parse each of the addresses in str. If any fail
506 * and can be fixed with escape_local_part(), do that. This
507 * is extra ugly because getm()'s state can only be reset by
508 * call getname(), and getname() needs to be called repeatedly
509 * until it returns NULL to reset its state.
513 int escape_local_part
;
515 struct adr_node
*next
;
517 struct adr_node
*np
= adrs
;
521 * First, put each of the addresses in a linked list. Note
522 * invalid addresses that might be fixed by escaping the
525 while ((cp
= getname (str
))) {
526 struct adr_node
*adr_nodep
;
531 adr_nodep
->adr
= mh_xstrdup (cp
);
532 adr_nodep
->escape_local_part
= 0;
533 adr_nodep
->fixed
= 0;
534 adr_nodep
->next
= NULL
;
536 /* With AD_NAME, errors are not reported to user. */
537 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
538 sizeof(error
))) == NULL
) {
539 const char *no_at_sign
= "no at-sign after local-part";
541 adr_nodep
->escape_local_part
=
542 has_prefix(error
, no_at_sign
);
548 np
= np
->next
= adr_nodep
;
550 np
= adrs
= adr_nodep
;
555 * Walk the list and try to fix broken addresses.
557 for (np
= adrs
; np
; np
= np
->next
) {
558 char *display_name
= mh_xstrdup (np
->adr
);
559 size_t len
= strlen (display_name
);
561 if (np
->escape_local_part
) {
562 char *local_part_end
= strrchr (display_name
, '<');
563 char *angle_addr
= mh_xstrdup (local_part_end
);
567 *local_part_end
= '\0';
568 /* Trim any trailing whitespace. */
569 while (local_part_end
> display_name
&&
570 isspace ((unsigned char) *--local_part_end
)) {
571 *local_part_end
= '\0';
573 escape_local_part (display_name
, len
);
574 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
575 adr
= getname (new_adr
);
577 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
578 fixed_address
= true;
584 np
->adr
= mh_xstrdup (adr
);
586 /* Need to flush getname() */
587 while ((cp
= getname (""))) continue;
588 } /* else the np->adr is OK, so use it as-is. */
594 * If any addresses were repaired, build new address string,
595 * replacing broken addresses.
597 for (np
= adrs
; np
; ) {
598 struct adr_node
*next
= np
->next
;
602 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
607 fixed_str
= mh_xstrdup (np
->adr
);
621 return str
? mh_xstrdup (str
) : NULL
;