]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
08991178d293f4e5427379edb20348339d58f825
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/arglist.h"
10 #include "sbr/error.h"
11 #include "h/addrsbr.h"
12 #include "h/fmt_scan.h"
25 static char *badaddrs
= NULL
;
26 static char *dfhost
= NULL
;
28 static struct mailname mq
;
29 static bool nodupcheck
; /* If set, no check for duplicates */
31 static char *addrcomps
[] = {
50 static int insert (struct mailname
*);
51 static void replfilter (FILE *, FILE *, char *, int);
52 static char *replformataddr(char *, char *);
53 static char *replconcataddr(char *, char *);
54 static char *fix_addresses (char *);
58 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
59 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
63 char tmpbuf
[NMH_BUFSIZ
];
66 int char_read
= 0, format_len
, mask
;
67 char name
[NAMESZ
], *cp
;
69 static int dat
[5]; /* aux. data for format routine */
70 m_getfld_state_t gstate
;
71 struct fmt_callbacks cb
;
76 mask
= umask(~m_gmprot());
77 if ((out
= fopen (drft
, "w")) == NULL
)
78 adios (drft
, "unable to create");
82 /* get new format string */
83 cp
= new_fs (form
, NULL
, NULL
);
84 format_len
= strlen (cp
);
86 /* compile format string */
87 fmt_compile (cp
, &fmt
, 1);
89 for (ap
= addrcomps
; *ap
; ap
++) {
90 cptr
= fmt_findcomp (*ap
);
92 cptr
->c_type
|= CT_ADDR
;
96 * ignore any components killed by command line switches
98 * This prevents the component from being found via fmt_findcomp(),
99 * which makes sure no text gets added to it when the message is processed.
102 cptr
= fmt_findcomp ("to");
104 cptr
->c_name
= mh_xstrdup("");
107 cptr
= fmt_findcomp("cc");
109 cptr
->c_name
= mh_xstrdup("");
115 * pick any interesting stuff out of msg "inb"
117 gstate
= m_getfld_state_init(inb
);
119 int msg_count
= sizeof tmpbuf
;
120 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
125 * if we're interested in this component, save a pointer
126 * to the component text, then start using our next free
127 * buffer as the component temp buffer (buffer switching
128 * saves an extra copy of the component text).
131 i
= fmt_addcomptext(name
, tmpbuf
);
133 char_read
+= msg_count
;
134 while (state
== FLDPLUS
) {
135 msg_count
= sizeof tmpbuf
;
136 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
137 fmt_appendcomp(i
, name
, tmpbuf
);
138 char_read
+= msg_count
;
142 while (state
== FLDPLUS
) {
143 msg_count
= sizeof tmpbuf
;
144 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
155 die("m_getfld2() returned %d", state
);
160 * format and output the header lines.
163 m_getfld_state_destroy (&gstate
);
165 /* set up the "fcc" pseudo-component */
166 cptr
= fmt_findcomp ("fcc");
170 cptr
->c_text
= mh_xstrdup(fcc
);
174 cptr
= fmt_findcomp ("user");
177 if ((cp
= getenv("USER")))
178 cptr
->c_text
= mh_xstrdup(cp
);
184 * if there's a "Subject" component, strip any "Re:"s off it
186 cptr
= fmt_findcomp ("subject");
187 if (cptr
&& (cp
= cptr
->c_text
)) {
191 while (isspace((unsigned char) *cp
))
199 if (sp
!= cptr
->c_text
) {
201 cptr
->c_text
= mh_xstrdup(sp
);
205 i
= format_len
+ char_read
+ 256;
206 scanl
= charstring_create (i
+ 2);
210 dat
[3] = outputlinelen
;
213 cb
.formataddr
= replformataddr
;
214 cb
.concataddr
= replconcataddr
;
215 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
216 fputs (charstring_buffer (scanl
), out
);
218 fputs ("\nrepl: bad addresses:\n", out
);
219 fputs ( badaddrs
, out
);
223 * Check if we should filter the message
224 * or add mhn directives
229 adios (drft
, "error writing");
231 replfilter (inb
, out
, filter
, fmtproc
);
232 } else if (mime
&& mp
) {
233 fprintf (out
, "#forw [original message] +%s %s\n",
234 mp
->foldpath
, m_name (mp
->lowsel
));
239 adios (drft
, "error writing");
242 /* return dynamically allocated buffers */
243 charstring_free (scanl
);
247 static char *buf
; /* our current working buffer */
248 static char *bufend
; /* end of working buffer */
249 static char *last_dst
; /* buf ptr at end of last call */
250 static unsigned int bufsiz
=0; /* current size of buf */
252 #define BUFINCR 512 /* how much to expand buf when if fills */
254 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
257 * check if there's enough room in buf for str.
258 * add more mem if needed
260 #define CHECKMEM(str) \
261 if ((len = strlen (str)) >= bufend - dst) {\
263 int n = last_dst - buf;\
264 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
265 buf = mh_xrealloc (buf, bufsiz);\
268 bufend = buf + bufsiz;\
273 * fmt_scan will call this routine if the user includes the function
274 * "(formataddr {component})" in a format string. "orig" is the
275 * original contents of the string register. "str" is the address
276 * string to be formatted and concatenated onto orig. This routine
277 * returns a pointer to the concatenated address string.
279 * We try to not do a lot of malloc/copy/free's (which is why we
280 * don't call "getcpy") but still place no upper limit on the
281 * length of the result string.
284 replformataddr (char *orig
, char *str
)
287 char baddr
[BUFSIZ
], error
[BUFSIZ
];
292 struct mailname
*mp
= NULL
;
293 char *fixed_str
= fix_addresses (str
);
295 /* if we don't have a buffer yet, get one */
297 buf
= mh_xmalloc (BUFINCR
);
298 last_dst
= buf
; /* XXX */
299 bufsiz
= BUFINCR
- 6; /* leave some slop */
300 bufend
= buf
+ bufsiz
;
303 * If "orig" points to our buffer we can just pick up where we
304 * left off. Otherwise we have to copy orig into our buffer.
308 else if (!orig
|| !*orig
) {
312 dst
= last_dst
; /* XXX */
317 /* concatenate all the new addresses onto 'buf' */
318 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
319 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
320 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
321 badaddrs
= add (baddr
, badaddrs
);
324 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
329 /* if we get here we're going to add an address */
335 CHECKMEM (mp
->m_gname
);
357 * fmt_scan will call this routine if the user includes the function
358 * "(concataddr {component})" in a format string. This behaves exactly
359 * like formataddr, except that it does NOT suppress duplicate addresses
362 * As an implementation detail: I thought about splitting out replformataddr()
363 * into the generic part and duplicate-suppressing part, but the call to
364 * insert() was buried deep within a couple of loops and I didn't see a
365 * way to do it easily. So instead we simply set a special flag to stop
366 * the duplicate check and call replformataddr().
369 replconcataddr(char *orig
, char *str
)
374 cp
= replformataddr(orig
, str
);
380 insert (struct mailname
*np
)
388 if (np
->m_mbox
== NULL
)
391 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
392 if (!strcasecmp (FENDNULL(np
->m_host
),
393 FENDNULL(mp
->m_next
->m_host
)) &&
394 !strcasecmp (FENDNULL(np
->m_mbox
),
395 FENDNULL(mp
->m_next
->m_mbox
)))
398 if (!ccme
&& ismymbox (np
))
402 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
403 if (!read_switch (buffer
, anoyes
))
415 * This function expects that argument out has been fflushed by the caller.
419 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
430 if (access (filter
, R_OK
) == NOTOK
)
431 adios (filter
, "unable to read");
434 lseek(fileno(in
), 0, SEEK_SET
);
436 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
438 switch (pid
= fork()) {
440 adios ("fork", "unable to");
443 dup2 (fileno (in
), fileno (stdin
));
444 dup2 (fileno (out
), fileno (stdout
));
448 * We're not allocating the memory for the extra arguments,
449 * because we never call arglist_free(). But if we ever change
450 * that be sure to use getcpy() for the extra arguments.
452 arglist
[argnum
++] = "-form";
453 arglist
[argnum
++] = filter
;
454 arglist
[argnum
++] = "-noclear";
458 arglist
[argnum
++] = "-fmtproc";
459 arglist
[argnum
++] = formatproc
;
462 arglist
[argnum
++] = "-nofmtproc";
466 arglist
[argnum
++] = NULL
;
468 execvp (mhl
, arglist
);
469 errstr
= strerror(errno
);
470 if (write(2, "unable to exec ", 15) < 0 ||
471 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
472 write(2, ": ", 2) < 0 ||
473 write(2, errstr
, strlen(errstr
)) < 0 ||
474 write(2, "\n", 1) < 0) {
475 advise ("stderr", "write");
480 if (pidXwait (pid
, mhl
))
482 fseek (out
, 0L, SEEK_END
);
483 arglist_free(mhl
, arglist
);
490 fix_addresses (char *str
)
492 char *fixed_str
= NULL
;
493 bool fixed_address
= false;
497 * Attempt to parse each of the addresses in str. If any fail
498 * and can be fixed with escape_local_part(), do that. This
499 * is extra ugly because getm()'s state can only be reset by
500 * call getname(), and getname() needs to be called repeatedly
501 * until it returns NULL to reset its state.
505 int escape_local_part
;
507 struct adr_node
*next
;
509 struct adr_node
*np
= adrs
;
513 * First, put each of the addresses in a linked list. Note
514 * invalid addresses that might be fixed by escaping the
517 while ((cp
= getname (str
))) {
518 struct adr_node
*adr_nodep
;
523 adr_nodep
->adr
= mh_xstrdup (cp
);
524 adr_nodep
->escape_local_part
= 0;
525 adr_nodep
->fixed
= 0;
526 adr_nodep
->next
= NULL
;
528 /* With AD_NAME, errors are not reported to user. */
529 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
530 sizeof(error
))) == NULL
) {
531 const char *no_at_sign
= "no at-sign after local-part";
533 adr_nodep
->escape_local_part
=
534 has_prefix(error
, no_at_sign
);
540 np
= np
->next
= adr_nodep
;
542 np
= adrs
= adr_nodep
;
547 * Walk the list and try to fix broken addresses.
549 for (np
= adrs
; np
; np
= np
->next
) {
550 char *display_name
= mh_xstrdup (np
->adr
);
551 size_t len
= strlen (display_name
);
553 if (np
->escape_local_part
) {
554 char *local_part_end
= strrchr (display_name
, '<');
555 char *angle_addr
= mh_xstrdup (local_part_end
);
559 *local_part_end
= '\0';
560 /* Trim any trailing whitespace. */
561 while (local_part_end
> display_name
&&
562 isspace ((unsigned char) *--local_part_end
)) {
563 *local_part_end
= '\0';
565 escape_local_part (display_name
, len
);
566 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
567 adr
= getname (new_adr
);
569 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
570 fixed_address
= true;
576 np
->adr
= mh_xstrdup (adr
);
578 /* Need to flush getname() */
579 while ((cp
= getname (""))) continue;
580 } /* else the np->adr is OK, so use it as-is. */
586 * If any addresses were repaired, build new address string,
587 * replacing broken addresses.
589 for (np
= adrs
; np
; ) {
590 struct adr_node
*next
= np
->next
;
594 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
599 fixed_str
= mh_xstrdup (np
->adr
);
613 return str
? mh_xstrdup (str
) : NULL
;