]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
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/read_switch.h"
10 #include "sbr/concat.h"
11 #include "sbr/closefds.h"
13 #include "sbr/escape_addresses.h"
14 #include "sbr/pidstatus.h"
15 #include "sbr/arglist.h"
16 #include "sbr/error.h"
17 #include "h/addrsbr.h"
18 #include "h/fmt_scan.h"
31 static char *badaddrs
= NULL
;
32 static char *dfhost
= NULL
;
34 static struct mailname mq
;
35 static bool nodupcheck
; /* If set, no check for duplicates */
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
)
69 char tmpbuf
[NMH_BUFSIZ
];
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
;
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"
123 gstate
= m_getfld_state_init(inb
);
125 int msg_count
= sizeof tmpbuf
;
126 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
131 * if we're interested in this component, save a pointer
132 * to the component text, then start using our next free
133 * buffer as the component temp buffer (buffer switching
134 * saves an extra copy of the component text).
137 i
= fmt_addcomptext(name
, tmpbuf
);
139 char_read
+= msg_count
;
140 while (state
== FLDPLUS
) {
141 msg_count
= sizeof tmpbuf
;
142 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
143 fmt_appendcomp(i
, name
, tmpbuf
);
144 char_read
+= msg_count
;
148 while (state
== FLDPLUS
) {
149 msg_count
= sizeof tmpbuf
;
150 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
161 die("m_getfld2() returned %d", state
);
166 * format and output the header lines.
169 m_getfld_state_destroy (&gstate
);
171 /* set up the "fcc" pseudo-component */
172 cptr
= fmt_findcomp ("fcc");
176 cptr
->c_text
= mh_xstrdup(fcc
);
180 cptr
= fmt_findcomp ("user");
183 if ((cp
= getenv("USER")))
184 cptr
->c_text
= mh_xstrdup(cp
);
190 * if there's a "Subject" component, strip any "Re:"s off it
192 cptr
= fmt_findcomp ("subject");
193 if (cptr
&& (cp
= cptr
->c_text
)) {
197 while (isspace((unsigned char) *cp
))
205 if (sp
!= cptr
->c_text
) {
207 cptr
->c_text
= mh_xstrdup(sp
);
211 i
= format_len
+ char_read
+ 256;
212 scanl
= charstring_create (i
+ 2);
216 dat
[3] = outputlinelen
;
219 cb
.formataddr
= replformataddr
;
220 cb
.concataddr
= replconcataddr
;
221 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
222 fputs (charstring_buffer (scanl
), out
);
224 fputs ("\nrepl: bad addresses:\n", out
);
225 fputs ( badaddrs
, out
);
229 * Check if we should filter the message
230 * or add mhn directives
235 adios (drft
, "error writing");
237 replfilter (inb
, out
, filter
, fmtproc
);
238 } else if (mime
&& mp
) {
239 fprintf (out
, "#forw [original message] +%s %s\n",
240 mp
->foldpath
, m_name (mp
->lowsel
));
245 adios (drft
, "error writing");
248 /* return dynamically allocated buffers */
249 charstring_free (scanl
);
253 static char *buf
; /* our current working buffer */
254 static char *bufend
; /* end of working buffer */
255 static char *last_dst
; /* buf ptr at end of last call */
256 static unsigned int bufsiz
=0; /* current size of buf */
258 #define BUFINCR 512 /* how much to expand buf when if fills */
260 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
263 * check if there's enough room in buf for str.
264 * add more mem if needed
266 #define CHECKMEM(str) \
267 if ((len = strlen (str)) >= bufend - dst) {\
269 int n = last_dst - buf;\
270 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
271 buf = mh_xrealloc (buf, bufsiz);\
274 bufend = buf + bufsiz;\
279 * fmt_scan will call this routine if the user includes the function
280 * "(formataddr {component})" in a format string. "orig" is the
281 * original contents of the string register. "str" is the address
282 * string to be formatted and concatenated onto orig. This routine
283 * returns a pointer to the concatenated address string.
285 * We try to not do a lot of malloc/copy/free's (which is why we
286 * don't call "getcpy") but still place no upper limit on the
287 * length of the result string.
290 replformataddr (char *orig
, char *str
)
293 char baddr
[BUFSIZ
], error
[BUFSIZ
];
298 struct mailname
*mp
= NULL
;
299 char *fixed_str
= fix_addresses (str
);
301 /* if we don't have a buffer yet, get one */
303 buf
= mh_xmalloc (BUFINCR
);
304 last_dst
= buf
; /* XXX */
305 bufsiz
= BUFINCR
- 6; /* leave some slop */
306 bufend
= buf
+ bufsiz
;
309 * If "orig" points to our buffer we can just pick up where we
310 * left off. Otherwise we have to copy orig into our buffer.
314 else if (!orig
|| !*orig
) {
318 dst
= last_dst
; /* XXX */
323 /* concatenate all the new addresses onto 'buf' */
324 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
325 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
326 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
327 badaddrs
= add (baddr
, badaddrs
);
330 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
335 /* if we get here we're going to add an address */
341 CHECKMEM (mp
->m_gname
);
363 * fmt_scan will call this routine if the user includes the function
364 * "(concataddr {component})" in a format string. This behaves exactly
365 * like formataddr, except that it does NOT suppress duplicate addresses
368 * As an implementation detail: I thought about splitting out replformataddr()
369 * into the generic part and duplicate-suppressing part, but the call to
370 * insert() was buried deep within a couple of loops and I didn't see a
371 * way to do it easily. So instead we simply set a special flag to stop
372 * the duplicate check and call replformataddr().
375 replconcataddr(char *orig
, char *str
)
380 cp
= replformataddr(orig
, str
);
386 insert (struct mailname
*np
)
394 if (np
->m_mbox
== NULL
)
397 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
398 if (!strcasecmp (FENDNULL(np
->m_host
),
399 FENDNULL(mp
->m_next
->m_host
)) &&
400 !strcasecmp (FENDNULL(np
->m_mbox
),
401 FENDNULL(mp
->m_next
->m_mbox
)))
404 if (!ccme
&& ismymbox (np
))
408 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
409 if (!read_switch (buffer
, anoyes
))
421 * This function expects that argument out has been fflushed by the caller.
425 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
436 if (access (filter
, R_OK
) == NOTOK
)
437 adios (filter
, "unable to read");
440 lseek(fileno(in
), 0, SEEK_SET
);
442 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
444 switch (pid
= fork()) {
446 adios ("fork", "unable to");
449 dup2 (fileno (in
), fileno (stdin
));
450 dup2 (fileno (out
), fileno (stdout
));
454 * We're not allocating the memory for the extra arguments,
455 * because we never call arglist_free(). But if we ever change
456 * that be sure to use getcpy() for the extra arguments.
458 arglist
[argnum
++] = "-form";
459 arglist
[argnum
++] = filter
;
460 arglist
[argnum
++] = "-noclear";
464 arglist
[argnum
++] = "-fmtproc";
465 arglist
[argnum
++] = formatproc
;
468 arglist
[argnum
++] = "-nofmtproc";
472 arglist
[argnum
++] = NULL
;
474 execvp (mhl
, arglist
);
475 errstr
= strerror(errno
);
476 if (write(2, "unable to exec ", 15) < 0 ||
477 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
478 write(2, ": ", 2) < 0 ||
479 write(2, errstr
, strlen(errstr
)) < 0 ||
480 write(2, "\n", 1) < 0) {
481 advise ("stderr", "write");
486 if (pidXwait (pid
, mhl
))
488 fseek (out
, 0L, SEEK_END
);
489 arglist_free(mhl
, arglist
);
496 fix_addresses (char *str
)
498 char *fixed_str
= NULL
;
499 bool fixed_address
= false;
503 * Attempt to parse each of the addresses in str. If any fail
504 * and can be fixed with escape_local_part(), do that. This
505 * is extra ugly because getm()'s state can only be reset by
506 * call getname(), and getname() needs to be called repeatedly
507 * until it returns NULL to reset its state.
511 int escape_local_part
;
513 struct adr_node
*next
;
515 struct adr_node
*np
= adrs
;
519 * First, put each of the addresses in a linked list. Note
520 * invalid addresses that might be fixed by escaping the
523 while ((cp
= getname (str
))) {
524 struct adr_node
*adr_nodep
;
529 adr_nodep
->adr
= mh_xstrdup (cp
);
530 adr_nodep
->escape_local_part
= 0;
531 adr_nodep
->fixed
= 0;
532 adr_nodep
->next
= NULL
;
534 /* With AD_NAME, errors are not reported to user. */
535 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
536 sizeof(error
))) == NULL
) {
537 const char *no_at_sign
= "no at-sign after local-part";
539 adr_nodep
->escape_local_part
=
540 has_prefix(error
, no_at_sign
);
546 np
= np
->next
= adr_nodep
;
548 np
= adrs
= adr_nodep
;
553 * Walk the list and try to fix broken addresses.
555 for (np
= adrs
; np
; np
= np
->next
) {
556 char *display_name
= mh_xstrdup (np
->adr
);
557 size_t len
= strlen (display_name
);
559 if (np
->escape_local_part
) {
560 char *local_part_end
= strrchr (display_name
, '<');
561 char *angle_addr
= mh_xstrdup (local_part_end
);
565 *local_part_end
= '\0';
566 /* Trim any trailing whitespace. */
567 while (local_part_end
> display_name
&&
568 isspace ((unsigned char) *--local_part_end
)) {
569 *local_part_end
= '\0';
571 escape_local_part (display_name
, len
);
572 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
573 adr
= getname (new_adr
);
575 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
576 fixed_address
= true;
582 np
->adr
= mh_xstrdup (adr
);
584 /* Need to flush getname() */
585 while ((cp
= getname (""))) continue;
586 } /* else the np->adr is OK, so use it as-is. */
592 * If any addresses were repaired, build new address string,
593 * replacing broken addresses.
595 for (np
= adrs
; np
; ) {
596 struct adr_node
*next
= np
->next
;
600 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
605 fixed_str
= mh_xstrdup (np
->adr
);
619 return str
? mh_xstrdup (str
) : NULL
;