]>
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/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/read_switch.h"
13 #include "sbr/concat.h"
14 #include "sbr/closefds.h"
16 #include "sbr/escape_addresses.h"
17 #include "sbr/pidstatus.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
20 #include "h/addrsbr.h"
21 #include "h/fmt_scan.h"
34 static char *badaddrs
= NULL
;
35 static char *dfhost
= NULL
;
37 static struct mailname mq
;
38 static bool nodupcheck
; /* If set, no check for duplicates */
40 static char *addrcomps
[] = {
59 static int insert (struct mailname
*);
60 static void replfilter (FILE *, FILE *, char *, int);
61 static char *replformataddr(char *, char *);
62 static char *replconcataddr(char *, char *);
63 static char *fix_addresses (char *);
67 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
68 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
72 char tmpbuf
[NMH_BUFSIZ
];
75 int char_read
= 0, format_len
, mask
;
76 char name
[NAMESZ
], *cp
;
78 static int dat
[5]; /* aux. data for format routine */
79 m_getfld_state_t gstate
;
80 struct fmt_callbacks cb
;
85 mask
= umask(~m_gmprot());
86 if ((out
= fopen (drft
, "w")) == NULL
)
87 adios (drft
, "unable to create");
91 /* get new format string */
92 cp
= new_fs (form
, NULL
, NULL
);
93 format_len
= strlen (cp
);
95 /* compile format string */
96 fmt_compile (cp
, &fmt
, 1);
98 for (ap
= addrcomps
; *ap
; ap
++) {
99 cptr
= fmt_findcomp (*ap
);
101 cptr
->c_type
|= CT_ADDR
;
105 * ignore any components killed by command line switches
107 * This prevents the component from being found via fmt_findcomp(),
108 * which makes sure no text gets added to it when the message is processed.
111 cptr
= fmt_findcomp ("to");
113 cptr
->c_name
= mh_xstrdup("");
116 cptr
= fmt_findcomp("cc");
118 cptr
->c_name
= mh_xstrdup("");
124 * pick any interesting stuff out of msg "inb"
126 gstate
= m_getfld_state_init(inb
);
128 int msg_count
= sizeof tmpbuf
;
129 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
134 * if we're interested in this component, save a pointer
135 * to the component text, then start using our next free
136 * buffer as the component temp buffer (buffer switching
137 * saves an extra copy of the component text).
140 i
= fmt_addcomptext(name
, tmpbuf
);
142 char_read
+= msg_count
;
143 while (state
== FLDPLUS
) {
144 msg_count
= sizeof tmpbuf
;
145 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
146 fmt_appendcomp(i
, name
, tmpbuf
);
147 char_read
+= msg_count
;
151 while (state
== FLDPLUS
) {
152 msg_count
= sizeof tmpbuf
;
153 state
= m_getfld2(&gstate
, name
, tmpbuf
, &msg_count
);
164 die("m_getfld2() returned %d", state
);
169 * format and output the header lines.
172 m_getfld_state_destroy (&gstate
);
174 /* set up the "fcc" pseudo-component */
175 cptr
= fmt_findcomp ("fcc");
179 cptr
->c_text
= mh_xstrdup(fcc
);
183 cptr
= fmt_findcomp ("user");
186 if ((cp
= getenv("USER")))
187 cptr
->c_text
= mh_xstrdup(cp
);
193 * if there's a "Subject" component, strip any "Re:"s off it
195 cptr
= fmt_findcomp ("subject");
196 if (cptr
&& (cp
= cptr
->c_text
)) {
200 while (isspace((unsigned char) *cp
))
208 if (sp
!= cptr
->c_text
) {
210 cptr
->c_text
= mh_xstrdup(sp
);
214 i
= format_len
+ char_read
+ 256;
215 scanl
= charstring_create (i
+ 2);
219 dat
[3] = outputlinelen
;
222 cb
.formataddr
= replformataddr
;
223 cb
.concataddr
= replconcataddr
;
224 fmt_scan (fmt
, scanl
, i
, dat
, &cb
);
225 fputs (charstring_buffer (scanl
), out
);
227 fputs ("\nrepl: bad addresses:\n", out
);
228 fputs ( badaddrs
, out
);
232 * Check if we should filter the message
233 * or add mhn directives
238 adios (drft
, "error writing");
240 replfilter (inb
, out
, filter
, fmtproc
);
241 } else if (mime
&& mp
) {
242 fprintf (out
, "#forw [original message] +%s %s\n",
243 mp
->foldpath
, m_name (mp
->lowsel
));
248 adios (drft
, "error writing");
251 /* return dynamically allocated buffers */
252 charstring_free (scanl
);
256 static char *buf
; /* our current working buffer */
257 static char *bufend
; /* end of working buffer */
258 static char *last_dst
; /* buf ptr at end of last call */
259 static unsigned int bufsiz
=0; /* current size of buf */
261 #define BUFINCR 512 /* how much to expand buf when if fills */
263 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
266 * check if there's enough room in buf for str.
267 * add more mem if needed
269 #define CHECKMEM(str) \
270 if ((len = strlen (str)) >= bufend - dst) {\
272 int n = last_dst - buf;\
273 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
274 buf = mh_xrealloc (buf, bufsiz);\
277 bufend = buf + bufsiz;\
282 * fmt_scan will call this routine if the user includes the function
283 * "(formataddr {component})" in a format string. "orig" is the
284 * original contents of the string register. "str" is the address
285 * string to be formatted and concatenated onto orig. This routine
286 * returns a pointer to the concatenated address string.
288 * We try to not do a lot of malloc/copy/free's (which is why we
289 * don't call "getcpy") but still place no upper limit on the
290 * length of the result string.
293 replformataddr (char *orig
, char *str
)
296 char baddr
[BUFSIZ
], error
[BUFSIZ
];
301 struct mailname
*mp
= NULL
;
302 char *fixed_str
= fix_addresses (str
);
304 /* if we don't have a buffer yet, get one */
306 buf
= mh_xmalloc (BUFINCR
);
307 last_dst
= buf
; /* XXX */
308 bufsiz
= BUFINCR
- 6; /* leave some slop */
309 bufend
= buf
+ bufsiz
;
312 * If "orig" points to our buffer we can just pick up where we
313 * left off. Otherwise we have to copy orig into our buffer.
317 else if (!orig
|| !*orig
) {
321 dst
= last_dst
; /* XXX */
326 /* concatenate all the new addresses onto 'buf' */
327 for (isgroup
= false; (cp
= getname (fixed_str
)); ) {
328 if ((mp
= getm (cp
, dfhost
, dftype
, error
, sizeof(error
))) == NULL
) {
329 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
330 badaddrs
= add (baddr
, badaddrs
);
333 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
338 /* if we get here we're going to add an address */
344 CHECKMEM (mp
->m_gname
);
366 * fmt_scan will call this routine if the user includes the function
367 * "(concataddr {component})" in a format string. This behaves exactly
368 * like formataddr, except that it does NOT suppress duplicate addresses
371 * As an implementation detail: I thought about splitting out replformataddr()
372 * into the generic part and duplicate-suppressing part, but the call to
373 * insert() was buried deep within a couple of loops and I didn't see a
374 * way to do it easily. So instead we simply set a special flag to stop
375 * the duplicate check and call replformataddr().
378 replconcataddr(char *orig
, char *str
)
383 cp
= replformataddr(orig
, str
);
389 insert (struct mailname
*np
)
397 if (np
->m_mbox
== NULL
)
400 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
401 if (!strcasecmp (FENDNULL(np
->m_host
),
402 FENDNULL(mp
->m_next
->m_host
)) &&
403 !strcasecmp (FENDNULL(np
->m_mbox
),
404 FENDNULL(mp
->m_next
->m_mbox
)))
407 if (!ccme
&& ismymbox (np
))
411 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
412 if (!read_switch (buffer
, anoyes
))
424 * This function expects that argument out has been fflushed by the caller.
428 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
439 if (access (filter
, R_OK
) == NOTOK
)
440 adios (filter
, "unable to read");
443 lseek(fileno(in
), 0, SEEK_SET
);
445 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
447 switch (pid
= fork()) {
449 adios ("fork", "unable to");
452 dup2 (fileno (in
), fileno (stdin
));
453 dup2 (fileno (out
), fileno (stdout
));
457 * We're not allocating the memory for the extra arguments,
458 * because we never call arglist_free(). But if we ever change
459 * that be sure to use getcpy() for the extra arguments.
461 arglist
[argnum
++] = "-form";
462 arglist
[argnum
++] = filter
;
463 arglist
[argnum
++] = "-noclear";
467 arglist
[argnum
++] = "-fmtproc";
468 arglist
[argnum
++] = formatproc
;
471 arglist
[argnum
++] = "-nofmtproc";
475 arglist
[argnum
++] = NULL
;
477 execvp (mhl
, arglist
);
478 errstr
= strerror(errno
);
479 if (write(2, "unable to exec ", 15) < 0 ||
480 write(2, mhlproc
, strlen(mhlproc
)) < 0 ||
481 write(2, ": ", 2) < 0 ||
482 write(2, errstr
, strlen(errstr
)) < 0 ||
483 write(2, "\n", 1) < 0) {
484 advise ("stderr", "write");
489 if (pidXwait (pid
, mhl
))
491 fseek (out
, 0L, SEEK_END
);
492 arglist_free(mhl
, arglist
);
499 fix_addresses (char *str
)
501 char *fixed_str
= NULL
;
502 bool fixed_address
= false;
506 * Attempt to parse each of the addresses in str. If any fail
507 * and can be fixed with escape_local_part(), do that. This
508 * is extra ugly because getm()'s state can only be reset by
509 * call getname(), and getname() needs to be called repeatedly
510 * until it returns NULL to reset its state.
514 int escape_local_part
;
516 struct adr_node
*next
;
518 struct adr_node
*np
= adrs
;
522 * First, put each of the addresses in a linked list. Note
523 * invalid addresses that might be fixed by escaping the
526 while ((cp
= getname (str
))) {
527 struct adr_node
*adr_nodep
;
532 adr_nodep
->adr
= mh_xstrdup (cp
);
533 adr_nodep
->escape_local_part
= 0;
534 adr_nodep
->fixed
= 0;
535 adr_nodep
->next
= NULL
;
537 /* With AD_NAME, errors are not reported to user. */
538 if ((mp
= getm (cp
, dfhost
, dftype
, error
,
539 sizeof(error
))) == NULL
) {
540 const char *no_at_sign
= "no at-sign after local-part";
542 adr_nodep
->escape_local_part
=
543 has_prefix(error
, no_at_sign
);
549 np
= np
->next
= adr_nodep
;
551 np
= adrs
= adr_nodep
;
556 * Walk the list and try to fix broken addresses.
558 for (np
= adrs
; np
; np
= np
->next
) {
559 char *display_name
= mh_xstrdup (np
->adr
);
560 size_t len
= strlen (display_name
);
562 if (np
->escape_local_part
) {
563 char *local_part_end
= strrchr (display_name
, '<');
564 char *angle_addr
= mh_xstrdup (local_part_end
);
568 *local_part_end
= '\0';
569 /* Trim any trailing whitespace. */
570 while (local_part_end
> display_name
&&
571 isspace ((unsigned char) *--local_part_end
)) {
572 *local_part_end
= '\0';
574 escape_local_part (display_name
, len
);
575 new_adr
= concat (display_name
, " ", angle_addr
, NULL
);
576 adr
= getname (new_adr
);
578 (mp
= getm (adr
, dfhost
, dftype
, NULL
, 0)) != NULL
) {
579 fixed_address
= true;
585 np
->adr
= mh_xstrdup (adr
);
587 /* Need to flush getname() */
588 while ((cp
= getname (""))) continue;
589 } /* else the np->adr is OK, so use it as-is. */
595 * If any addresses were repaired, build new address string,
596 * replacing broken addresses.
598 for (np
= adrs
; np
; ) {
599 struct adr_node
*next
= np
->next
;
603 char *new_str
= concat (fixed_str
, ", ", np
->adr
, NULL
);
608 fixed_str
= mh_xstrdup (np
->adr
);
622 return str
? mh_xstrdup (str
) : NULL
;