]>
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/closefds.h"
11 #include "sbr/escape_addresses.h"
12 #include "sbr/pidstatus.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
15 #include "h/addrsbr.h"
16 #include "h/fmt_scan.h"
29 static char *badaddrs
= NULL
;
30 static char *dfhost
= NULL
;
32 static struct mailname mq
;
33 static bool nodupcheck
; /* If set, no check for duplicates */
35 static char *addrcomps
[] = {
54 static int insert (struct mailname
*);
55 static void replfilter (FILE *, FILE *, char *, int);
56 static char *replformataddr(char *, char *);
57 static char *replconcataddr(char *, char *);
58 static char *fix_addresses (char *);
62 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
63 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
67 char tmpbuf
[NMH_BUFSIZ
];
70 int char_read
= 0, format_len
, mask
;
71 char name
[NAMESZ
], *cp
;
73 static int dat
[5]; /* aux. data for format routine */
74 m_getfld_state_t gstate
;
75 struct fmt_callbacks cb
;
80 mask
= umask(~m_gmprot());
81 if ((out
= fopen (drft
, "w")) == NULL
)
82 adios (drft
, "unable to create");
86 /* get new format string */
87 cp
= new_fs (form
, NULL
, NULL
);
88 format_len
= strlen (cp
);
90 /* compile format string */
91 fmt_compile (cp
, &fmt
, 1);
93 for (ap
= addrcomps
; *ap
; ap
++) {
94 cptr
= fmt_findcomp (*ap
);
96 cptr
->c_type
|= CT_ADDR
;
100 * ignore any components killed by command line switches
102 * This prevents the component from being found via fmt_findcomp(),
103 * which makes sure no text gets added to it when the message is processed.
106 cptr
= fmt_findcomp ("to");
108 cptr
->c_name
= mh_xstrdup("");
111 cptr
= fmt_findcomp("cc");
113 cptr
->c_name
= mh_xstrdup("");
119 * pick any interesting stuff out of msg "inb"
121 gstate
= m_getfld_state_init(inb
);
123 int msg_count
= sizeof tmpbuf
;
124 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
129 * if we're interested in this component, save a pointer
130 * to the component text, then start using our next free
131 * buffer as the component temp buffer (buffer switching
132 * saves an extra copy of the component text).
135 i
= fmt_addcomptext(name
, tmpbuf
);
137 char_read
+= msg_count
;
138 while (state
== FLDPLUS
) {
139 msg_count
= sizeof tmpbuf
;
140 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
141 fmt_appendcomp(i
, name
, tmpbuf
);
142 char_read
+= msg_count
;
146 while (state
== FLDPLUS
) {
147 msg_count
= sizeof tmpbuf
;
148 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
159 die("m_getfld2() returned %d", state
);
164 * format and output the header lines.
167 m_getfld_state_destroy (&gstate
);
169 /* set up the "fcc" pseudo-component */
170 cptr
= fmt_findcomp ("fcc");
174 cptr
->c_text
= mh_xstrdup(fcc
);
178 cptr
= fmt_findcomp ("user");
181 if ((cp
= getenv("USER")))
182 cptr
->c_text
= mh_xstrdup(cp
);
188 * if there's a "Subject" component, strip any "Re:"s off it
190 cptr
= fmt_findcomp ("subject");
191 if (cptr
&& (cp
= cptr
->c_text
)) {
195 while (isspace((unsigned char) *cp
))
203 if (sp
!= cptr
->c_text
) {
205 cptr
->c_text
= mh_xstrdup(sp
);
209 i
= format_len
+ char_read
+ 256;
210 scanl
= charstring_create (i
+ 2);
214 dat
[3] = outputlinelen
;
217 cb
.formataddr
= replformataddr
;
218 cb
.concataddr
= replconcataddr
;
219 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
220 fputs (charstring_buffer (scanl
), out
);
222 fputs ("\nrepl: bad addresses:\n", out
);
223 fputs ( badaddrs
, out
);
227 * Check if we should filter the message
228 * or add mhn directives
233 adios (drft
, "error writing");
235 replfilter (inb
, out
, filter
, fmtproc
);
236 } else if (mime
&& mp
) {
237 fprintf (out
, "#forw [original message] +%s %s\n",
238 mp
->foldpath
, m_name (mp
->lowsel
));
243 adios (drft
, "error writing");
246 /* return dynamically allocated buffers */
247 charstring_free (scanl
);
251 static char *buf
; /* our current working buffer */
252 static char *bufend
; /* end of working buffer */
253 static char *last_dst
; /* buf ptr at end of last call */
254 static unsigned int bufsiz
=0; /* current size of buf */
256 #define BUFINCR 512 /* how much to expand buf when if fills */
258 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
261 * check if there's enough room in buf for str.
262 * add more mem if needed
264 #define CHECKMEM(str) \
265 if ((len = strlen (str)) >= bufend - dst) {\
267 int n = last_dst - buf;\
268 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
269 buf = mh_xrealloc (buf, bufsiz);\
272 bufend = buf + bufsiz;\
277 * fmt_scan will call this routine if the user includes the function
278 * "(formataddr {component})" in a format string. "orig" is the
279 * original contents of the string register. "str" is the address
280 * string to be formatted and concatenated onto orig. This routine
281 * returns a pointer to the concatenated address string.
283 * We try to not do a lot of malloc/copy/free's (which is why we
284 * don't call "getcpy") but still place no upper limit on the
285 * length of the result string.
288 replformataddr (char *orig
, char *str
)
291 char baddr
[BUFSIZ
], error
[BUFSIZ
];
296 struct mailname
*mp
= NULL
;
297 char *fixed_str
= fix_addresses (str
);
299 /* if we don't have a buffer yet, get one */
301 buf
= mh_xmalloc (BUFINCR
);
302 last_dst
= buf
; /* XXX */
303 bufsiz
= BUFINCR
- 6; /* leave some slop */
304 bufend
= buf
+ bufsiz
;
307 * If "orig" points to our buffer we can just pick up where we
308 * left off. Otherwise we have to copy orig into our buffer.
312 else if (!orig
|| !*orig
) {
316 dst
= last_dst
; /* XXX */
321 /* concatenate all the new addresses onto 'buf' */
322 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
323 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
324 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
325 badaddrs
= add (baddr
, badaddrs
);
328 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
333 /* if we get here we're going to add an address */
339 CHECKMEM (mp
->m_gname
);
361 * fmt_scan will call this routine if the user includes the function
362 * "(concataddr {component})" in a format string. This behaves exactly
363 * like formataddr, except that it does NOT suppress duplicate addresses
366 * As an implementation detail: I thought about splitting out replformataddr()
367 * into the generic part and duplicate-suppressing part, but the call to
368 * insert() was buried deep within a couple of loops and I didn't see a
369 * way to do it easily. So instead we simply set a special flag to stop
370 * the duplicate check and call replformataddr().
373 replconcataddr(char *orig
, char *str
)
378 cp
= replformataddr(orig
, str
);
384 insert (struct mailname
*np
)
392 if (np
->m_mbox
== NULL
)
395 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
396 if (!strcasecmp (FENDNULL(np
->m_host
),
397 FENDNULL(mp
->m_next
->m_host
)) &&
398 !strcasecmp (FENDNULL(np
->m_mbox
),
399 FENDNULL(mp
->m_next
->m_mbox
)))
402 if (!ccme
&& ismymbox (np
))
406 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
407 if (!read_switch (buffer
, anoyes
))
419 * This function expects that argument out has been fflushed by the caller.
423 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
434 if (access (filter
, R_OK
) == NOTOK
)
435 adios (filter
, "unable to read");
438 lseek(fileno(in
), 0, SEEK_SET
);
440 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
442 switch (pid
= fork()) {
444 adios ("fork", "unable to");
447 dup2 (fileno (in
), fileno (stdin
));
448 dup2 (fileno (out
), fileno (stdout
));
452 * We're not allocating the memory for the extra arguments,
453 * because we never call arglist_free(). But if we ever change
454 * that be sure to use getcpy() for the extra arguments.
456 arglist
[argnum
++] = "-form";
457 arglist
[argnum
++] = filter
;
458 arglist
[argnum
++] = "-noclear";
462 arglist
[argnum
++] = "-fmtproc";
463 arglist
[argnum
++] = formatproc
;
466 arglist
[argnum
++] = "-nofmtproc";
470 arglist
[argnum
++] = NULL
;
472 execvp (mhl
, arglist
);
473 errstr
= strerror(errno
);
474 if (write(2, "unable to exec ", 15) < 0 ||
475 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
476 write(2, ": ", 2) < 0 ||
477 write(2, errstr
, strlen(errstr
)) < 0 ||
478 write(2, "\n", 1) < 0) {
479 advise ("stderr", "write");
484 if (pidXwait (pid
, mhl
))
486 fseek (out
, 0L, SEEK_END
);
487 arglist_free(mhl
, arglist
);
494 fix_addresses (char *str
)
496 char *fixed_str
= NULL
;
497 bool fixed_address
= false;
501 * Attempt to parse each of the addresses in str. If any fail
502 * and can be fixed with escape_local_part(), do that. This
503 * is extra ugly because getm()'s state can only be reset by
504 * call getname(), and getname() needs to be called repeatedly
505 * until it returns NULL to reset its state.
509 int escape_local_part
;
511 struct adr_node
*next
;
513 struct adr_node
*np
= adrs
;
517 * First, put each of the addresses in a linked list. Note
518 * invalid addresses that might be fixed by escaping the
521 while ((cp
= getname (str
))) {
522 struct adr_node
*adr_nodep
;
527 adr_nodep
->adr
= mh_xstrdup (cp
);
528 adr_nodep
->escape_local_part
= 0;
529 adr_nodep
->fixed
= 0;
530 adr_nodep
->next
= NULL
;
532 /* With AD_NAME, errors are not reported to user. */
533 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
534 sizeof(error
))) == NULL
) {
535 const char *no_at_sign
= "no at-sign after local-part";
537 adr_nodep
->escape_local_part
=
538 has_prefix(error
, no_at_sign
);
544 np
= np
->next
= adr_nodep
;
546 np
= adrs
= adr_nodep
;
551 * Walk the list and try to fix broken addresses.
553 for (np
= adrs
; np
; np
= np
->next
) {
554 char *display_name
= mh_xstrdup (np
->adr
);
555 size_t len
= strlen (display_name
);
557 if (np
->escape_local_part
) {
558 char *local_part_end
= strrchr (display_name
, '<');
559 char *angle_addr
= mh_xstrdup (local_part_end
);
563 *local_part_end
= '\0';
564 /* Trim any trailing whitespace. */
565 while (local_part_end
> display_name
&&
566 isspace ((unsigned char) *--local_part_end
)) {
567 *local_part_end
= '\0';
569 escape_local_part (display_name
, len
);
570 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
571 adr
= getname (new_adr
);
573 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
574 fixed_address
= true;
580 np
->adr
= mh_xstrdup (adr
);
582 /* Need to flush getname() */
583 while ((cp
= getname (""))) continue;
584 } /* else the np->adr is OK, so use it as-is. */
590 * If any addresses were repaired, build new address string,
591 * replacing broken addresses.
593 for (np
= adrs
; np
; ) {
594 struct adr_node
*next
= np
->next
;
598 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
603 fixed_str
= mh_xstrdup (np
->adr
);
617 return str
? mh_xstrdup (str
) : NULL
;