]>
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.
10 #include <h/fmt_scan.h>
12 #include <sys/file.h> /* L_SET */
14 extern short ccto
; /* from repl.c */
21 static char *badaddrs
= NULL
;
22 static char *dfhost
= NULL
;
24 static struct mailname mq
;
25 static int nodupcheck
= 0; /* If set, no check for duplicates */
27 static char *addrcomps
[] = {
46 static int insert (struct mailname
*);
47 static void replfilter (FILE *, FILE *, char *, int);
48 static char *replformataddr(char *, char *);
49 static char *replconcataddr(char *, char *);
50 static char *fix_addresses (char *);
54 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
55 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
59 char tmpbuf
[NMH_BUFSIZ
];
62 int char_read
= 0, format_len
, mask
;
63 char name
[NAMESZ
], *cp
;
65 static int dat
[5]; /* aux. data for format routine */
66 m_getfld_state_t gstate
= 0;
67 struct fmt_callbacks cb
;
72 mask
= umask(~m_gmprot());
73 if ((out
= fopen (drft
, "w")) == NULL
)
74 adios (drft
, "unable to create");
78 /* get new format string */
79 cp
= new_fs (form
, NULL
, NULL
);
80 format_len
= strlen (cp
);
82 /* compile format string */
83 fmt_compile (cp
, &fmt
, 1);
85 for (ap
= addrcomps
; *ap
; ap
++) {
86 cptr
= fmt_findcomp (*ap
);
88 cptr
->c_type
|= CT_ADDR
;
92 * ignore any components killed by command line switches
94 * This prevents the component from being found via fmt_findcomp(),
95 * which makes sure no text gets added to it when the message is processed.
98 cptr
= fmt_findcomp ("to");
100 cptr
->c_name
= mh_xstrdup("");
103 cptr
= fmt_findcomp("cc");
105 cptr
->c_name
= mh_xstrdup("");
111 * pick any interesting stuff out of msg "inb"
114 int msg_count
= sizeof tmpbuf
;
115 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
120 * if we're interested in this component, save a pointer
121 * to the component text, then start using our next free
122 * buffer as the component temp buffer (buffer switching
123 * saves an extra copy of the component text).
126 i
= fmt_addcomptext(name
, tmpbuf
);
128 char_read
+= msg_count
;
129 while (state
== FLDPLUS
) {
130 msg_count
= sizeof tmpbuf
;
131 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
132 fmt_appendcomp(i
, name
, tmpbuf
);
133 char_read
+= msg_count
;
137 while (state
== FLDPLUS
) {
138 msg_count
= sizeof tmpbuf
;
139 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
150 adios (NULL
, "m_getfld() returned %d", state
);
155 * format and output the header lines.
158 m_getfld_state_destroy (&gstate
);
160 /* set up the "fcc" pseudo-component */
161 cptr
= fmt_findcomp ("fcc");
163 mh_xfree(cptr
->c_text
);
165 cptr
->c_text
= mh_xstrdup(fcc
);
169 cptr
= fmt_findcomp ("user");
171 mh_xfree(cptr
->c_text
);
172 if ((cp
= getenv("USER")))
173 cptr
->c_text
= mh_xstrdup(cp
);
179 * if there's a "Subject" component, strip any "Re:"s off it
181 cptr
= fmt_findcomp ("subject");
182 if (cptr
&& (cp
= cptr
->c_text
)) {
186 while (isspace((unsigned char) *cp
))
194 if (sp
!= cptr
->c_text
) {
196 cptr
->c_text
= mh_xstrdup(sp
);
200 i
= format_len
+ char_read
+ 256;
201 scanl
= charstring_create (i
+ 2);
205 dat
[3] = outputlinelen
;
207 memset(&cb
, 0, sizeof(cb
));
208 cb
.formataddr
= replformataddr
;
209 cb
.concataddr
= replconcataddr
;
210 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
211 fputs (charstring_buffer (scanl
), out
);
213 fputs ("\nrepl: bad addresses:\n", out
);
214 fputs ( badaddrs
, out
);
218 * Check if we should filter the message
219 * or add mhn directives
224 adios (drft
, "error writing");
226 replfilter (inb
, out
, filter
, fmtproc
);
227 } else if (mime
&& mp
) {
228 fprintf (out
, "#forw [original message] +%s %s\n",
229 mp
->foldpath
, m_name (mp
->lowsel
));
234 adios (drft
, "error writing");
237 /* return dynamically allocated buffers */
238 charstring_free (scanl
);
242 static char *buf
; /* our current working buffer */
243 static char *bufend
; /* end of working buffer */
244 static char *last_dst
; /* buf ptr at end of last call */
245 static unsigned int bufsiz
=0; /* current size of buf */
247 #define BUFINCR 512 /* how much to expand buf when if fills */
249 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
252 * check if there's enough room in buf for str.
253 * add more mem if needed
255 #define CHECKMEM(str) \
256 if ((len = strlen (str)) >= bufend - dst) {\
258 int n = last_dst - buf;\
259 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
260 buf = mh_xrealloc (buf, bufsiz);\
263 bufend = buf + bufsiz;\
268 * fmt_scan will call this routine if the user includes the function
269 * "(formataddr {component})" in a format string. "orig" is the
270 * original contents of the string register. "str" is the address
271 * string to be formatted and concatenated onto orig. This routine
272 * returns a pointer to the concatenated address string.
274 * We try to not do a lot of malloc/copy/free's (which is why we
275 * don't call "getcpy") but still place no upper limit on the
276 * length of the result string.
279 replformataddr (char *orig
, char *str
)
282 char baddr
[BUFSIZ
], error
[BUFSIZ
];
287 struct mailname
*mp
= NULL
;
288 char *fixed_str
= fix_addresses (str
);
290 /* if we don't have a buffer yet, get one */
292 buf
= mh_xmalloc (BUFINCR
);
293 last_dst
= buf
; /* XXX */
294 bufsiz
= BUFINCR
- 6; /* leave some slop */
295 bufend
= buf
+ bufsiz
;
298 * If "orig" points to our buffer we can just pick up where we
299 * left off. Otherwise we have to copy orig into our buffer.
303 else if (!orig
|| !*orig
) {
307 dst
= last_dst
; /* XXX */
312 /* concatenate all the new addresses onto 'buf' */
313 for (isgroup
= 0; (cp
= getname (fixed_str
)); ) {
314 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
315 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
316 badaddrs
= add (baddr
, badaddrs
);
319 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
324 /* if we get here we're going to add an address */
330 CHECKMEM (mp
->m_gname
);
352 * fmt_scan will call this routine if the user includes the function
353 * "(concataddr {component})" in a format string. This behaves exactly
354 * like formataddr, except that it does NOT suppress duplicate addresses
357 * As an implementation detail: I thought about splitting out replformataddr()
358 * into the generic part and duplicate-suppressing part, but the call to
359 * insert() was buried deep within a couple of loops and I didn't see a
360 * way to do it easily. So instead we simply set a special flag to stop
361 * the duplicate check and call replformataddr().
364 replconcataddr(char *orig
, char *str
)
369 cp
= replformataddr(orig
, str
);
375 insert (struct mailname
*np
)
383 if (np
->m_mbox
== NULL
)
386 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
387 if (!strcasecmp (FENDNULL(np
->m_host
),
388 FENDNULL(mp
->m_next
->m_host
)) &&
389 !strcasecmp (FENDNULL(np
->m_mbox
),
390 FENDNULL(mp
->m_next
->m_mbox
)))
393 if (!ccme
&& ismymbox (np
))
397 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
398 if (!read_switch (buffer
, anoyes
))
410 * This function expects that argument out has been fflushed by the caller.
414 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
425 if (access (filter
, R_OK
) == NOTOK
)
426 adios (filter
, "unable to read");
429 lseek(fileno(in
), 0, SEEK_SET
);
431 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
433 switch (pid
= fork()) {
435 adios ("fork", "unable to");
438 dup2 (fileno (in
), fileno (stdin
));
439 dup2 (fileno (out
), fileno (stdout
));
443 * We're not allocating the memory for the extra arguments,
444 * because we never call arglist_free(). But if we ever change
445 * that be sure to use getcpy() for the extra arguments.
447 arglist
[argnum
++] = "-form";
448 arglist
[argnum
++] = filter
;
449 arglist
[argnum
++] = "-noclear";
453 arglist
[argnum
++] = "-fmtproc";
454 arglist
[argnum
++] = formatproc
;
457 arglist
[argnum
++] = "-nofmtproc";
461 arglist
[argnum
++] = NULL
;
463 execvp (mhl
, arglist
);
464 errstr
= strerror(errno
);
465 if (write(2, "unable to exec ", 15) < 0 ||
466 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
467 write(2, ": ", 2) < 0 ||
468 write(2, errstr
, strlen(errstr
)) < 0 ||
469 write(2, "\n", 1) < 0) {
470 advise ("stderr", "write");
475 if (pidXwait (pid
, mhl
))
477 fseek (out
, 0L, SEEK_END
);
485 fix_addresses (char *str
) {
486 char *fixed_str
= NULL
;
487 int fixed_address
= 0;
491 * Attempt to parse each of the addresses in str. If any fail
492 * and can be fixed with escape_local_part(), do that. This
493 * is extra ugly because getm()'s state can only be reset by
494 * call getname(), and getname() needs to be called repeatedly
495 * until it returns NULL to reset its state.
499 int escape_local_part
;
501 struct adr_node
*next
;
503 struct adr_node
*np
= adrs
;
507 * First, put each of the addresses in a linked list. Note
508 * invalid addresses that might be fixed by escaping the
511 while ((cp
= getname (str
))) {
512 struct adr_node
*adr_nodep
;
517 adr_nodep
->adr
= mh_xstrdup (cp
);
518 adr_nodep
->escape_local_part
= 0;
519 adr_nodep
->fixed
= 0;
520 adr_nodep
->next
= NULL
;
522 /* With AD_NAME, errors are not reported to user. */
523 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
524 sizeof(error
))) == NULL
) {
525 const char *no_at_sign
= "no at-sign after local-part";
527 adr_nodep
->escape_local_part
=
528 has_prefix(error
, no_at_sign
);
534 np
= np
->next
= adr_nodep
;
536 np
= adrs
= adr_nodep
;
541 * Walk the list and try to fix broken addresses.
543 for (np
= adrs
; np
; np
= np
->next
) {
544 char *display_name
= mh_xstrdup (np
->adr
);
545 size_t len
= strlen (display_name
);
547 if (np
->escape_local_part
) {
548 char *local_part_end
= strrchr (display_name
, '<');
549 char *angle_addr
= mh_xstrdup (local_part_end
);
553 *local_part_end
= '\0';
554 /* Trim any trailing whitespace. */
555 while (local_part_end
> display_name
&&
556 isspace ((unsigned char) *--local_part_end
)) {
557 *local_part_end
= '\0';
559 escape_local_part (display_name
, len
);
560 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
561 adr
= getname (new_adr
);
563 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
570 np
->adr
= mh_xstrdup (adr
);
572 /* Need to flush getname() */
573 while ((cp
= getname (""))) continue;
574 } /* else the np->adr is OK, so use it as-is. */
580 * If any addresses were repaired, build new address string,
581 * replacing broken addresses.
583 for (np
= adrs
; np
; ) {
584 struct adr_node
*next
= np
->next
;
588 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
593 fixed_str
= mh_xstrdup (np
->adr
);
607 return str
? mh_xstrdup (str
) : NULL
;