]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
76d027f846746aa185c6b49dab9aa7f381b9f7ec
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/addrsbr.h>
11 #include <h/fmt_scan.h>
14 #include <sys/file.h> /* L_SET */
24 static char *badaddrs
= NULL
;
25 static char *dfhost
= NULL
;
27 static struct mailname mq
;
28 static bool nodupcheck
; /* If set, no check for duplicates */
30 static char *addrcomps
[] = {
49 static int insert (struct mailname
*);
50 static void replfilter (FILE *, FILE *, char *, int);
51 static char *replformataddr(char *, char *);
52 static char *replconcataddr(char *, char *);
53 static char *fix_addresses (char *);
57 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
58 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
62 char tmpbuf
[NMH_BUFSIZ
];
65 int char_read
= 0, format_len
, mask
;
66 char name
[NAMESZ
], *cp
;
68 static int dat
[5]; /* aux. data for format routine */
69 m_getfld_state_t gstate
;
70 struct fmt_callbacks cb
;
75 mask
= umask(~m_gmprot());
76 if ((out
= fopen (drft
, "w")) == NULL
)
77 adios (drft
, "unable to create");
81 /* get new format string */
82 cp
= new_fs (form
, NULL
, NULL
);
83 format_len
= strlen (cp
);
85 /* compile format string */
86 fmt_compile (cp
, &fmt
, 1);
88 for (ap
= addrcomps
; *ap
; ap
++) {
89 cptr
= fmt_findcomp (*ap
);
91 cptr
->c_type
|= CT_ADDR
;
95 * ignore any components killed by command line switches
97 * This prevents the component from being found via fmt_findcomp(),
98 * which makes sure no text gets added to it when the message is processed.
101 cptr
= fmt_findcomp ("to");
103 cptr
->c_name
= mh_xstrdup("");
106 cptr
= fmt_findcomp("cc");
108 cptr
->c_name
= mh_xstrdup("");
114 * pick any interesting stuff out of msg "inb"
116 gstate
= m_getfld_state_init(inb
);
118 int msg_count
= sizeof tmpbuf
;
119 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
124 * if we're interested in this component, save a pointer
125 * to the component text, then start using our next free
126 * buffer as the component temp buffer (buffer switching
127 * saves an extra copy of the component text).
130 i
= fmt_addcomptext(name
, tmpbuf
);
132 char_read
+= msg_count
;
133 while (state
== FLDPLUS
) {
134 msg_count
= sizeof tmpbuf
;
135 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
136 fmt_appendcomp(i
, name
, tmpbuf
);
137 char_read
+= msg_count
;
141 while (state
== FLDPLUS
) {
142 msg_count
= sizeof tmpbuf
;
143 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
154 die("m_getfld2() returned %d", state
);
159 * format and output the header lines.
162 m_getfld_state_destroy (&gstate
);
164 /* set up the "fcc" pseudo-component */
165 cptr
= fmt_findcomp ("fcc");
169 cptr
->c_text
= mh_xstrdup(fcc
);
173 cptr
= fmt_findcomp ("user");
176 if ((cp
= getenv("USER")))
177 cptr
->c_text
= mh_xstrdup(cp
);
183 * if there's a "Subject" component, strip any "Re:"s off it
185 cptr
= fmt_findcomp ("subject");
186 if (cptr
&& (cp
= cptr
->c_text
)) {
190 while (isspace((unsigned char) *cp
))
198 if (sp
!= cptr
->c_text
) {
200 cptr
->c_text
= mh_xstrdup(sp
);
204 i
= format_len
+ char_read
+ 256;
205 scanl
= charstring_create (i
+ 2);
209 dat
[3] = outputlinelen
;
212 cb
.formataddr
= replformataddr
;
213 cb
.concataddr
= replconcataddr
;
214 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
215 fputs (charstring_buffer (scanl
), out
);
217 fputs ("\nrepl: bad addresses:\n", out
);
218 fputs ( badaddrs
, out
);
222 * Check if we should filter the message
223 * or add mhn directives
228 adios (drft
, "error writing");
230 replfilter (inb
, out
, filter
, fmtproc
);
231 } else if (mime
&& mp
) {
232 fprintf (out
, "#forw [original message] +%s %s\n",
233 mp
->foldpath
, m_name (mp
->lowsel
));
238 adios (drft
, "error writing");
241 /* return dynamically allocated buffers */
242 charstring_free (scanl
);
246 static char *buf
; /* our current working buffer */
247 static char *bufend
; /* end of working buffer */
248 static char *last_dst
; /* buf ptr at end of last call */
249 static unsigned int bufsiz
=0; /* current size of buf */
251 #define BUFINCR 512 /* how much to expand buf when if fills */
253 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
256 * check if there's enough room in buf for str.
257 * add more mem if needed
259 #define CHECKMEM(str) \
260 if ((len = strlen (str)) >= bufend - dst) {\
262 int n = last_dst - buf;\
263 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
264 buf = mh_xrealloc (buf, bufsiz);\
267 bufend = buf + bufsiz;\
272 * fmt_scan will call this routine if the user includes the function
273 * "(formataddr {component})" in a format string. "orig" is the
274 * original contents of the string register. "str" is the address
275 * string to be formatted and concatenated onto orig. This routine
276 * returns a pointer to the concatenated address string.
278 * We try to not do a lot of malloc/copy/free's (which is why we
279 * don't call "getcpy") but still place no upper limit on the
280 * length of the result string.
283 replformataddr (char *orig
, char *str
)
286 char baddr
[BUFSIZ
], error
[BUFSIZ
];
291 struct mailname
*mp
= NULL
;
292 char *fixed_str
= fix_addresses (str
);
294 /* if we don't have a buffer yet, get one */
296 buf
= mh_xmalloc (BUFINCR
);
297 last_dst
= buf
; /* XXX */
298 bufsiz
= BUFINCR
- 6; /* leave some slop */
299 bufend
= buf
+ bufsiz
;
302 * If "orig" points to our buffer we can just pick up where we
303 * left off. Otherwise we have to copy orig into our buffer.
307 else if (!orig
|| !*orig
) {
311 dst
= last_dst
; /* XXX */
316 /* concatenate all the new addresses onto 'buf' */
317 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
318 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
319 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
320 badaddrs
= add (baddr
, badaddrs
);
323 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
328 /* if we get here we're going to add an address */
334 CHECKMEM (mp
->m_gname
);
356 * fmt_scan will call this routine if the user includes the function
357 * "(concataddr {component})" in a format string. This behaves exactly
358 * like formataddr, except that it does NOT suppress duplicate addresses
361 * As an implementation detail: I thought about splitting out replformataddr()
362 * into the generic part and duplicate-suppressing part, but the call to
363 * insert() was buried deep within a couple of loops and I didn't see a
364 * way to do it easily. So instead we simply set a special flag to stop
365 * the duplicate check and call replformataddr().
368 replconcataddr(char *orig
, char *str
)
373 cp
= replformataddr(orig
, str
);
379 insert (struct mailname
*np
)
387 if (np
->m_mbox
== NULL
)
390 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
391 if (!strcasecmp (FENDNULL(np
->m_host
),
392 FENDNULL(mp
->m_next
->m_host
)) &&
393 !strcasecmp (FENDNULL(np
->m_mbox
),
394 FENDNULL(mp
->m_next
->m_mbox
)))
397 if (!ccme
&& ismymbox (np
))
401 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
402 if (!read_switch (buffer
, anoyes
))
414 * This function expects that argument out has been fflushed by the caller.
418 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
429 if (access (filter
, R_OK
) == NOTOK
)
430 adios (filter
, "unable to read");
433 lseek(fileno(in
), 0, SEEK_SET
);
435 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
437 switch (pid
= fork()) {
439 adios ("fork", "unable to");
442 dup2 (fileno (in
), fileno (stdin
));
443 dup2 (fileno (out
), fileno (stdout
));
447 * We're not allocating the memory for the extra arguments,
448 * because we never call arglist_free(). But if we ever change
449 * that be sure to use getcpy() for the extra arguments.
451 arglist
[argnum
++] = "-form";
452 arglist
[argnum
++] = filter
;
453 arglist
[argnum
++] = "-noclear";
457 arglist
[argnum
++] = "-fmtproc";
458 arglist
[argnum
++] = formatproc
;
461 arglist
[argnum
++] = "-nofmtproc";
465 arglist
[argnum
++] = NULL
;
467 execvp (mhl
, arglist
);
468 errstr
= strerror(errno
);
469 if (write(2, "unable to exec ", 15) < 0 ||
470 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
471 write(2, ": ", 2) < 0 ||
472 write(2, errstr
, strlen(errstr
)) < 0 ||
473 write(2, "\n", 1) < 0) {
474 advise ("stderr", "write");
479 if (pidXwait (pid
, mhl
))
481 fseek (out
, 0L, SEEK_END
);
482 arglist_free(mhl
, arglist
);
489 fix_addresses (char *str
)
491 char *fixed_str
= NULL
;
492 bool fixed_address
= false;
496 * Attempt to parse each of the addresses in str. If any fail
497 * and can be fixed with escape_local_part(), do that. This
498 * is extra ugly because getm()'s state can only be reset by
499 * call getname(), and getname() needs to be called repeatedly
500 * until it returns NULL to reset its state.
504 int escape_local_part
;
506 struct adr_node
*next
;
508 struct adr_node
*np
= adrs
;
512 * First, put each of the addresses in a linked list. Note
513 * invalid addresses that might be fixed by escaping the
516 while ((cp
= getname (str
))) {
517 struct adr_node
*adr_nodep
;
522 adr_nodep
->adr
= mh_xstrdup (cp
);
523 adr_nodep
->escape_local_part
= 0;
524 adr_nodep
->fixed
= 0;
525 adr_nodep
->next
= NULL
;
527 /* With AD_NAME, errors are not reported to user. */
528 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
529 sizeof(error
))) == NULL
) {
530 const char *no_at_sign
= "no at-sign after local-part";
532 adr_nodep
->escape_local_part
=
533 has_prefix(error
, no_at_sign
);
539 np
= np
->next
= adr_nodep
;
541 np
= adrs
= adr_nodep
;
546 * Walk the list and try to fix broken addresses.
548 for (np
= adrs
; np
; np
= np
->next
) {
549 char *display_name
= mh_xstrdup (np
->adr
);
550 size_t len
= strlen (display_name
);
552 if (np
->escape_local_part
) {
553 char *local_part_end
= strrchr (display_name
, '<');
554 char *angle_addr
= mh_xstrdup (local_part_end
);
558 *local_part_end
= '\0';
559 /* Trim any trailing whitespace. */
560 while (local_part_end
> display_name
&&
561 isspace ((unsigned char) *--local_part_end
)) {
562 *local_part_end
= '\0';
564 escape_local_part (display_name
, len
);
565 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
566 adr
= getname (new_adr
);
568 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
569 fixed_address
= true;
575 np
->adr
= mh_xstrdup (adr
);
577 /* Need to flush getname() */
578 while ((cp
= getname (""))) continue;
579 } /* else the np->adr is OK, so use it as-is. */
585 * If any addresses were repaired, build new address string,
586 * replacing broken addresses.
588 for (np
= adrs
; np
; ) {
589 struct adr_node
*next
= np
->next
;
593 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
598 fixed_str
= mh_xstrdup (np
->adr
);
612 return str
? mh_xstrdup (str
) : NULL
;