]>
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("");
122 * pick any interesting stuff out of msg "inb"
125 int msg_count
= sizeof tmpbuf
;
126 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
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_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
143 fmt_appendcomp(i
, name
, tmpbuf
);
144 char_read
+= msg_count
;
148 while (state
== FLDPLUS
) {
149 msg_count
= sizeof tmpbuf
;
150 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
161 adios (NULL
, "m_getfld() 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");
174 mh_xfree(cptr
->c_text
);
176 cptr
->c_text
= mh_xstrdup(fcc
);
180 cptr
= fmt_findcomp ("user");
182 mh_xfree(cptr
->c_text
);
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
;
218 memset(&cb
, 0, sizeof(cb
));
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
= 0; (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 (np
->m_host
? np
->m_host
: "",
399 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
400 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
401 mp
->m_next
->m_mbox
? 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
), (off_t
) 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
);
496 fix_addresses (char *str
) {
497 char *fixed_str
= NULL
;
498 int fixed_address
= 0;
502 * Attempt to parse each of the addresses in str. If any fail
503 * and can be fixed with escape_local_part(), do that. This
504 * is extra ugly because getm()'s state can only be reset by
505 * call getname(), and getname() needs to be called repeatedly
506 * until it returns NULL to reset its state.
510 int escape_local_part
;
512 struct adr_node
*next
;
514 struct adr_node
*np
= adrs
;
518 * First, put each of the addresses in a linked list. Note
519 * invalid addresses that might be fixed by escaping the
522 while ((cp
= getname (str
))) {
523 struct adr_node
*adr_nodep
;
528 adr_nodep
->adr
= strdup (cp
);
529 adr_nodep
->escape_local_part
= 0;
530 adr_nodep
->fixed
= 0;
531 adr_nodep
->next
= NULL
;
533 /* With AD_NAME, errors are not reported to user. */
534 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
535 sizeof(error
))) == NULL
) {
536 const char *no_at_sign
= "no at-sign after local-part";
538 adr_nodep
->escape_local_part
=
539 has_prefix(error
, no_at_sign
);
545 np
= np
->next
= adr_nodep
;
547 np
= adrs
= adr_nodep
;
552 * Walk the list and try to fix broken addresses.
554 for (np
= adrs
; np
; np
= np
->next
) {
555 char *display_name
= strdup (np
->adr
);
556 size_t len
= strlen (display_name
);
558 if (np
->escape_local_part
) {
559 char *local_part_end
= strrchr (display_name
, '<');
560 char *angle_addr
= strdup (local_part_end
);
564 *local_part_end
= '\0';
565 /* Trim any trailing whitespace. */
566 while (local_part_end
> display_name
&&
567 isspace ((unsigned char) *--local_part_end
)) {
568 *local_part_end
= '\0';
570 escape_local_part (display_name
, len
);
571 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
572 adr
= getname (new_adr
);
574 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
581 np
->adr
= strdup (adr
);
583 /* Need to flush getname() */
584 while ((cp
= getname (""))) continue;
585 } /* else the np->adr is OK, so use it as-is. */
591 * If any addresses were repaired, build new address string,
592 * replacing broken addresses.
594 for (np
= adrs
; np
; ) {
595 struct adr_node
*next
= np
->next
;
599 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
604 fixed_str
= strdup (np
->adr
);
618 return str
? strdup (str
) : NULL
;