]>
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
;
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"
113 gstate
= m_getfld_state_init(inb
);
115 int msg_count
= sizeof tmpbuf
;
116 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
121 * if we're interested in this component, save a pointer
122 * to the component text, then start using our next free
123 * buffer as the component temp buffer (buffer switching
124 * saves an extra copy of the component text).
127 i
= fmt_addcomptext(name
, tmpbuf
);
129 char_read
+= msg_count
;
130 while (state
== FLDPLUS
) {
131 msg_count
= sizeof tmpbuf
;
132 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
133 fmt_appendcomp(i
, name
, tmpbuf
);
134 char_read
+= msg_count
;
138 while (state
== FLDPLUS
) {
139 msg_count
= sizeof tmpbuf
;
140 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
151 adios (NULL
, "m_getfld2() returned %d", state
);
156 * format and output the header lines.
159 m_getfld_state_destroy (&gstate
);
161 /* set up the "fcc" pseudo-component */
162 cptr
= fmt_findcomp ("fcc");
166 cptr
->c_text
= mh_xstrdup(fcc
);
170 cptr
= fmt_findcomp ("user");
173 if ((cp
= getenv("USER")))
174 cptr
->c_text
= mh_xstrdup(cp
);
180 * if there's a "Subject" component, strip any "Re:"s off it
182 cptr
= fmt_findcomp ("subject");
183 if (cptr
&& (cp
= cptr
->c_text
)) {
187 while (isspace((unsigned char) *cp
))
195 if (sp
!= cptr
->c_text
) {
197 cptr
->c_text
= mh_xstrdup(sp
);
201 i
= format_len
+ char_read
+ 256;
202 scanl
= charstring_create (i
+ 2);
206 dat
[3] = outputlinelen
;
209 cb
.formataddr
= replformataddr
;
210 cb
.concataddr
= replconcataddr
;
211 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
212 fputs (charstring_buffer (scanl
), out
);
214 fputs ("\nrepl: bad addresses:\n", out
);
215 fputs ( badaddrs
, out
);
219 * Check if we should filter the message
220 * or add mhn directives
225 adios (drft
, "error writing");
227 replfilter (inb
, out
, filter
, fmtproc
);
228 } else if (mime
&& mp
) {
229 fprintf (out
, "#forw [original message] +%s %s\n",
230 mp
->foldpath
, m_name (mp
->lowsel
));
235 adios (drft
, "error writing");
238 /* return dynamically allocated buffers */
239 charstring_free (scanl
);
243 static char *buf
; /* our current working buffer */
244 static char *bufend
; /* end of working buffer */
245 static char *last_dst
; /* buf ptr at end of last call */
246 static unsigned int bufsiz
=0; /* current size of buf */
248 #define BUFINCR 512 /* how much to expand buf when if fills */
250 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
253 * check if there's enough room in buf for str.
254 * add more mem if needed
256 #define CHECKMEM(str) \
257 if ((len = strlen (str)) >= bufend - dst) {\
259 int n = last_dst - buf;\
260 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
261 buf = mh_xrealloc (buf, bufsiz);\
264 bufend = buf + bufsiz;\
269 * fmt_scan will call this routine if the user includes the function
270 * "(formataddr {component})" in a format string. "orig" is the
271 * original contents of the string register. "str" is the address
272 * string to be formatted and concatenated onto orig. This routine
273 * returns a pointer to the concatenated address string.
275 * We try to not do a lot of malloc/copy/free's (which is why we
276 * don't call "getcpy") but still place no upper limit on the
277 * length of the result string.
280 replformataddr (char *orig
, char *str
)
283 char baddr
[BUFSIZ
], error
[BUFSIZ
];
288 struct mailname
*mp
= NULL
;
289 char *fixed_str
= fix_addresses (str
);
291 /* if we don't have a buffer yet, get one */
293 buf
= mh_xmalloc (BUFINCR
);
294 last_dst
= buf
; /* XXX */
295 bufsiz
= BUFINCR
- 6; /* leave some slop */
296 bufend
= buf
+ bufsiz
;
299 * If "orig" points to our buffer we can just pick up where we
300 * left off. Otherwise we have to copy orig into our buffer.
304 else if (!orig
|| !*orig
) {
308 dst
= last_dst
; /* XXX */
313 /* concatenate all the new addresses onto 'buf' */
314 for (isgroup
= 0; (cp
= getname (fixed_str
)); ) {
315 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
316 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
317 badaddrs
= add (baddr
, badaddrs
);
320 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
325 /* if we get here we're going to add an address */
331 CHECKMEM (mp
->m_gname
);
353 * fmt_scan will call this routine if the user includes the function
354 * "(concataddr {component})" in a format string. This behaves exactly
355 * like formataddr, except that it does NOT suppress duplicate addresses
358 * As an implementation detail: I thought about splitting out replformataddr()
359 * into the generic part and duplicate-suppressing part, but the call to
360 * insert() was buried deep within a couple of loops and I didn't see a
361 * way to do it easily. So instead we simply set a special flag to stop
362 * the duplicate check and call replformataddr().
365 replconcataddr(char *orig
, char *str
)
370 cp
= replformataddr(orig
, str
);
376 insert (struct mailname
*np
)
384 if (np
->m_mbox
== NULL
)
387 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
388 if (!strcasecmp (FENDNULL(np
->m_host
),
389 FENDNULL(mp
->m_next
->m_host
)) &&
390 !strcasecmp (FENDNULL(np
->m_mbox
),
391 FENDNULL(mp
->m_next
->m_mbox
)))
394 if (!ccme
&& ismymbox (np
))
398 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
399 if (!read_switch (buffer
, anoyes
))
411 * This function expects that argument out has been fflushed by the caller.
415 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
426 if (access (filter
, R_OK
) == NOTOK
)
427 adios (filter
, "unable to read");
430 lseek(fileno(in
), 0, SEEK_SET
);
432 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
434 switch (pid
= fork()) {
436 adios ("fork", "unable to");
439 dup2 (fileno (in
), fileno (stdin
));
440 dup2 (fileno (out
), fileno (stdout
));
444 * We're not allocating the memory for the extra arguments,
445 * because we never call arglist_free(). But if we ever change
446 * that be sure to use getcpy() for the extra arguments.
448 arglist
[argnum
++] = "-form";
449 arglist
[argnum
++] = filter
;
450 arglist
[argnum
++] = "-noclear";
454 arglist
[argnum
++] = "-fmtproc";
455 arglist
[argnum
++] = formatproc
;
458 arglist
[argnum
++] = "-nofmtproc";
462 arglist
[argnum
++] = NULL
;
464 execvp (mhl
, arglist
);
465 errstr
= strerror(errno
);
466 if (write(2, "unable to exec ", 15) < 0 ||
467 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
468 write(2, ": ", 2) < 0 ||
469 write(2, errstr
, strlen(errstr
)) < 0 ||
470 write(2, "\n", 1) < 0) {
471 advise ("stderr", "write");
476 if (pidXwait (pid
, mhl
))
478 fseek (out
, 0L, SEEK_END
);
479 arglist_free(mhl
, arglist
);
487 fix_addresses (char *str
) {
488 char *fixed_str
= NULL
;
489 int fixed_address
= 0;
493 * Attempt to parse each of the addresses in str. If any fail
494 * and can be fixed with escape_local_part(), do that. This
495 * is extra ugly because getm()'s state can only be reset by
496 * call getname(), and getname() needs to be called repeatedly
497 * until it returns NULL to reset its state.
501 int escape_local_part
;
503 struct adr_node
*next
;
505 struct adr_node
*np
= adrs
;
509 * First, put each of the addresses in a linked list. Note
510 * invalid addresses that might be fixed by escaping the
513 while ((cp
= getname (str
))) {
514 struct adr_node
*adr_nodep
;
519 adr_nodep
->adr
= mh_xstrdup (cp
);
520 adr_nodep
->escape_local_part
= 0;
521 adr_nodep
->fixed
= 0;
522 adr_nodep
->next
= NULL
;
524 /* With AD_NAME, errors are not reported to user. */
525 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
526 sizeof(error
))) == NULL
) {
527 const char *no_at_sign
= "no at-sign after local-part";
529 adr_nodep
->escape_local_part
=
530 has_prefix(error
, no_at_sign
);
536 np
= np
->next
= adr_nodep
;
538 np
= adrs
= adr_nodep
;
543 * Walk the list and try to fix broken addresses.
545 for (np
= adrs
; np
; np
= np
->next
) {
546 char *display_name
= mh_xstrdup (np
->adr
);
547 size_t len
= strlen (display_name
);
549 if (np
->escape_local_part
) {
550 char *local_part_end
= strrchr (display_name
, '<');
551 char *angle_addr
= mh_xstrdup (local_part_end
);
555 *local_part_end
= '\0';
556 /* Trim any trailing whitespace. */
557 while (local_part_end
> display_name
&&
558 isspace ((unsigned char) *--local_part_end
)) {
559 *local_part_end
= '\0';
561 escape_local_part (display_name
, len
);
562 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
563 adr
= getname (new_adr
);
565 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
572 np
->adr
= mh_xstrdup (adr
);
574 /* Need to flush getname() */
575 while ((cp
= getname (""))) continue;
576 } /* else the np->adr is OK, so use it as-is. */
582 * If any addresses were repaired, build new address string,
583 * replacing broken addresses.
585 for (np
= adrs
; np
; ) {
586 struct adr_node
*next
= np
->next
;
590 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
595 fixed_str
= mh_xstrdup (np
->adr
);
609 return str
? mh_xstrdup (str
) : NULL
;