]>
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/fmt_new.h"
10 #include "sbr/m_name.h"
11 #include "sbr/m_gmprot.h"
12 #include "sbr/m_getfld.h"
13 #include "sbr/read_switch.h"
14 #include "sbr/concat.h"
15 #include "sbr/closefds.h"
17 #include "sbr/escape_addresses.h"
18 #include "sbr/pidstatus.h"
19 #include "sbr/arglist.h"
20 #include "sbr/error.h"
21 #include "h/addrsbr.h"
22 #include "h/fmt_scan.h"
35 static char *badaddrs
= NULL
;
36 static char *dfhost
= NULL
;
38 static struct mailname mq
;
39 static bool nodupcheck
; /* If set, no check for duplicates */
41 static char *addrcomps
[] = {
60 static int insert (struct mailname
*);
61 static void replfilter (FILE *, FILE *, char *, int);
62 static char *replformataddr(char *, char *);
63 static char *replconcataddr(char *, char *);
64 static char *fix_addresses (char *);
68 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
69 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
73 char tmpbuf
[NMH_BUFSIZ
];
76 int char_read
= 0, format_len
, mask
;
77 char name
[NAMESZ
], *cp
;
79 static int dat
[5]; /* aux. data for format routine */
80 m_getfld_state_t gstate
;
81 struct fmt_callbacks cb
;
86 mask
= umask(~m_gmprot());
87 if ((out
= fopen (drft
, "w")) == NULL
)
88 adios (drft
, "unable to create");
92 /* get new format string */
93 cp
= new_fs (form
, NULL
, NULL
);
94 format_len
= strlen (cp
);
96 /* compile format string */
97 fmt_compile (cp
, &fmt
, 1);
99 for (ap
= addrcomps
; *ap
; ap
++) {
100 cptr
= fmt_findcomp (*ap
);
102 cptr
->c_type
|= CT_ADDR
;
106 * ignore any components killed by command line switches
108 * This prevents the component from being found via fmt_findcomp(),
109 * which makes sure no text gets added to it when the message is processed.
112 cptr
= fmt_findcomp ("to");
114 cptr
->c_name
= mh_xstrdup("");
117 cptr
= fmt_findcomp("cc");
119 cptr
->c_name
= mh_xstrdup("");
125 * pick any interesting stuff out of msg "inb"
127 gstate
= m_getfld_state_init(inb
);
129 int msg_count
= sizeof tmpbuf
;
130 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
135 * if we're interested in this component, save a pointer
136 * to the component text, then start using our next free
137 * buffer as the component temp buffer (buffer switching
138 * saves an extra copy of the component text).
141 i
= fmt_addcomptext(name
, tmpbuf
);
143 char_read
+= msg_count
;
144 while (state
== FLDPLUS
) {
145 msg_count
= sizeof tmpbuf
;
146 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
147 fmt_appendcomp(i
, name
, tmpbuf
);
148 char_read
+= msg_count
;
152 while (state
== FLDPLUS
) {
153 msg_count
= sizeof tmpbuf
;
154 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
165 die("m_getfld2() returned %d", state
);
170 * format and output the header lines.
173 m_getfld_state_destroy (&gstate
);
175 /* set up the "fcc" pseudo-component */
176 cptr
= fmt_findcomp ("fcc");
180 cptr
->c_text
= mh_xstrdup(fcc
);
184 cptr
= fmt_findcomp ("user");
187 if ((cp
= getenv("USER")))
188 cptr
->c_text
= mh_xstrdup(cp
);
194 * if there's a "Subject" component, strip any "Re:"s off it
196 cptr
= fmt_findcomp ("subject");
197 if (cptr
&& (cp
= cptr
->c_text
)) {
201 while (isspace((unsigned char) *cp
))
209 if (sp
!= cptr
->c_text
) {
211 cptr
->c_text
= mh_xstrdup(sp
);
215 i
= format_len
+ char_read
+ 256;
216 scanl
= charstring_create (i
+ 2);
220 dat
[3] = outputlinelen
;
223 cb
.formataddr
= replformataddr
;
224 cb
.concataddr
= replconcataddr
;
225 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
226 fputs (charstring_buffer (scanl
), out
);
228 fputs ("\nrepl: bad addresses:\n", out
);
229 fputs ( badaddrs
, out
);
233 * Check if we should filter the message
234 * or add mhn directives
239 adios (drft
, "error writing");
241 replfilter (inb
, out
, filter
, fmtproc
);
242 } else if (mime
&& mp
) {
243 fprintf (out
, "#forw [original message] +%s %s\n",
244 mp
->foldpath
, m_name (mp
->lowsel
));
249 adios (drft
, "error writing");
252 /* return dynamically allocated buffers */
253 charstring_free (scanl
);
257 static char *buf
; /* our current working buffer */
258 static char *bufend
; /* end of working buffer */
259 static char *last_dst
; /* buf ptr at end of last call */
260 static unsigned int bufsiz
=0; /* current size of buf */
262 #define BUFINCR 512 /* how much to expand buf when if fills */
264 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
267 * check if there's enough room in buf for str.
268 * add more mem if needed
270 #define CHECKMEM(str) \
271 if ((len = strlen (str)) >= bufend - dst) {\
273 int n = last_dst - buf;\
274 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
275 buf = mh_xrealloc (buf, bufsiz);\
278 bufend = buf + bufsiz;\
283 * fmt_scan will call this routine if the user includes the function
284 * "(formataddr {component})" in a format string. "orig" is the
285 * original contents of the string register. "str" is the address
286 * string to be formatted and concatenated onto orig. This routine
287 * returns a pointer to the concatenated address string.
289 * We try to not do a lot of malloc/copy/free's (which is why we
290 * don't call "getcpy") but still place no upper limit on the
291 * length of the result string.
294 replformataddr (char *orig
, char *str
)
297 char baddr
[BUFSIZ
], error
[BUFSIZ
];
302 struct mailname
*mp
= NULL
;
303 char *fixed_str
= fix_addresses (str
);
305 /* if we don't have a buffer yet, get one */
307 buf
= mh_xmalloc (BUFINCR
);
308 last_dst
= buf
; /* XXX */
309 bufsiz
= BUFINCR
- 6; /* leave some slop */
310 bufend
= buf
+ bufsiz
;
313 * If "orig" points to our buffer we can just pick up where we
314 * left off. Otherwise we have to copy orig into our buffer.
318 else if (!orig
|| !*orig
) {
322 dst
= last_dst
; /* XXX */
327 /* concatenate all the new addresses onto 'buf' */
328 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
329 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
330 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
331 badaddrs
= add (baddr
, badaddrs
);
334 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
339 /* if we get here we're going to add an address */
345 CHECKMEM (mp
->m_gname
);
367 * fmt_scan will call this routine if the user includes the function
368 * "(concataddr {component})" in a format string. This behaves exactly
369 * like formataddr, except that it does NOT suppress duplicate addresses
372 * As an implementation detail: I thought about splitting out replformataddr()
373 * into the generic part and duplicate-suppressing part, but the call to
374 * insert() was buried deep within a couple of loops and I didn't see a
375 * way to do it easily. So instead we simply set a special flag to stop
376 * the duplicate check and call replformataddr().
379 replconcataddr(char *orig
, char *str
)
384 cp
= replformataddr(orig
, str
);
390 insert (struct mailname
*np
)
398 if (np
->m_mbox
== NULL
)
401 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
402 if (!strcasecmp (FENDNULL(np
->m_host
),
403 FENDNULL(mp
->m_next
->m_host
)) &&
404 !strcasecmp (FENDNULL(np
->m_mbox
),
405 FENDNULL(mp
->m_next
->m_mbox
)))
408 if (!ccme
&& ismymbox (np
))
412 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
413 if (!read_switch (buffer
, anoyes
))
425 * This function expects that argument out has been fflushed by the caller.
429 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
440 if (access (filter
, R_OK
) == NOTOK
)
441 adios (filter
, "unable to read");
444 lseek(fileno(in
), 0, SEEK_SET
);
446 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
448 switch (pid
= fork()) {
450 adios ("fork", "unable to");
453 dup2 (fileno (in
), fileno (stdin
));
454 dup2 (fileno (out
), fileno (stdout
));
458 * We're not allocating the memory for the extra arguments,
459 * because we never call arglist_free(). But if we ever change
460 * that be sure to use getcpy() for the extra arguments.
462 arglist
[argnum
++] = "-form";
463 arglist
[argnum
++] = filter
;
464 arglist
[argnum
++] = "-noclear";
468 arglist
[argnum
++] = "-fmtproc";
469 arglist
[argnum
++] = formatproc
;
472 arglist
[argnum
++] = "-nofmtproc";
476 arglist
[argnum
++] = NULL
;
478 execvp (mhl
, arglist
);
479 errstr
= strerror(errno
);
480 if (write(2, "unable to exec ", 15) < 0 ||
481 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
482 write(2, ": ", 2) < 0 ||
483 write(2, errstr
, strlen(errstr
)) < 0 ||
484 write(2, "\n", 1) < 0) {
485 advise ("stderr", "write");
490 if (pidXwait (pid
, mhl
))
492 fseek (out
, 0L, SEEK_END
);
493 arglist_free(mhl
, arglist
);
500 fix_addresses (char *str
)
502 char *fixed_str
= NULL
;
503 bool fixed_address
= false;
507 * Attempt to parse each of the addresses in str. If any fail
508 * and can be fixed with escape_local_part(), do that. This
509 * is extra ugly because getm()'s state can only be reset by
510 * call getname(), and getname() needs to be called repeatedly
511 * until it returns NULL to reset its state.
515 int escape_local_part
;
517 struct adr_node
*next
;
519 struct adr_node
*np
= adrs
;
523 * First, put each of the addresses in a linked list. Note
524 * invalid addresses that might be fixed by escaping the
527 while ((cp
= getname (str
))) {
528 struct adr_node
*adr_nodep
;
533 adr_nodep
->adr
= mh_xstrdup (cp
);
534 adr_nodep
->escape_local_part
= 0;
535 adr_nodep
->fixed
= 0;
536 adr_nodep
->next
= NULL
;
538 /* With AD_NAME, errors are not reported to user. */
539 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
540 sizeof(error
))) == NULL
) {
541 const char *no_at_sign
= "no at-sign after local-part";
543 adr_nodep
->escape_local_part
=
544 has_prefix(error
, no_at_sign
);
550 np
= np
->next
= adr_nodep
;
552 np
= adrs
= adr_nodep
;
557 * Walk the list and try to fix broken addresses.
559 for (np
= adrs
; np
; np
= np
->next
) {
560 char *display_name
= mh_xstrdup (np
->adr
);
561 size_t len
= strlen (display_name
);
563 if (np
->escape_local_part
) {
564 char *local_part_end
= strrchr (display_name
, '<');
565 char *angle_addr
= mh_xstrdup (local_part_end
);
569 *local_part_end
= '\0';
570 /* Trim any trailing whitespace. */
571 while (local_part_end
> display_name
&&
572 isspace ((unsigned char) *--local_part_end
)) {
573 *local_part_end
= '\0';
575 escape_local_part (display_name
, len
);
576 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
577 adr
= getname (new_adr
);
579 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
580 fixed_address
= true;
586 np
->adr
= mh_xstrdup (adr
);
588 /* Need to flush getname() */
589 while ((cp
= getname (""))) continue;
590 } /* else the np->adr is OK, so use it as-is. */
596 * If any addresses were repaired, build new address string,
597 * replacing broken addresses.
599 for (np
= adrs
; np
; ) {
600 struct adr_node
*next
= np
->next
;
604 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
609 fixed_str
= mh_xstrdup (np
->adr
);
623 return str
? mh_xstrdup (str
) : NULL
;