]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
3 * replsbr.c -- routines to help repl along...
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
15 #include <sys/file.h> /* L_SET */
18 extern short ccto
; /* from repl.c */
25 static char *badaddrs
= NULL
;
26 static char *dfhost
= NULL
;
28 static struct mailname mq
= { NULL
};
31 * Buffer size for content part of header fields.
32 * We want this to be large enough so that we don't
33 * do a lot of extra FLDPLUS calls on m_getfld but
34 * small enough so that we don't snarf the entire
35 * message body when we're not going to use any of it.
39 static struct format
*fmt
;
41 static int ncomps
= 0; /* # of interesting components */
42 static char **compbuffers
= NULL
; /* buffers for component text */
43 static struct comp
**used_buf
= NULL
; /* stack for comp that use buffers */
45 static int dat
[5]; /* aux. data for format routine */
47 static char *addrcomps
[] = {
66 static int insert (struct mailname
*);
67 static void replfilter (FILE *, FILE *, char *);
71 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
72 int mime
, char *form
, char *filter
, char *fcc
)
74 register int state
, i
;
75 register struct comp
*cptr
;
76 register char *tmpbuf
;
77 register char **nxtbuf
;
79 register struct comp
**savecomp
;
80 int char_read
= 0, format_len
, mask
;
81 char name
[NAMESZ
], *scanl
, *cp
;
84 mask
= umask(~m_gmprot());
85 if ((out
= fopen (drft
, "w")) == NULL
)
86 adios (drft
, "unable to create");
90 /* get new format string */
91 cp
= new_fs (form
, NULL
, NULL
);
92 format_len
= strlen (cp
);
94 /* compile format string */
95 ncomps
= fmt_compile (cp
, &fmt
) + 1;
97 if (!(nxtbuf
= compbuffers
= (char **)
98 calloc((size_t) ncomps
, sizeof(char *))))
99 adios (NULL
, "unable to allocate component buffers");
100 if (!(savecomp
= used_buf
= (struct comp
**)
101 calloc((size_t) (ncomps
+1), sizeof(struct comp
*))))
102 adios (NULL
, "unable to allocate component buffer stack");
103 savecomp
+= ncomps
+ 1;
104 *--savecomp
= NULL
; /* point at zero'd end minus 1 */
106 for (i
= ncomps
; i
--; )
107 if (!(*nxtbuf
++ = malloc(SBUFSIZ
)))
108 adios (NULL
, "unable to allocate component buffer");
110 nxtbuf
= compbuffers
; /* point at start */
113 for (ap
= addrcomps
; *ap
; ap
++) {
114 FINDCOMP (cptr
, *ap
);
116 cptr
->c_type
|= CT_ADDR
;
120 * ignore any components killed by command line switches
123 FINDCOMP (cptr
, "to");
128 FINDCOMP (cptr
, "cc");
132 /* set up the "fcc" pseudo-component */
134 FINDCOMP (cptr
, "fcc");
136 cptr
->c_text
= getcpy (fcc
);
138 if ((cp
= getenv("USER"))) {
139 FINDCOMP (cptr
, "user");
141 cptr
->c_text
= getcpy(cp
);
147 * pick any interesting stuff out of msg "inb"
149 for (state
= FLD
;;) {
150 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
155 * if we're interested in this component, save a pointer
156 * to the component text, then start using our next free
157 * buffer as the component temp buffer (buffer switching
158 * saves an extra copy of the component text).
160 if ((cptr
= wantcomp
[CHASH(name
)]))
162 if (!strcasecmp(name
, cptr
->c_name
)) {
163 char_read
+= msg_count
;
164 if (! cptr
->c_text
) {
165 cptr
->c_text
= tmpbuf
;
169 i
= strlen (cp
= cptr
->c_text
) - 1;
171 if (cptr
->c_type
& CT_ADDR
) {
173 cp
= add (",\n\t", cp
);
178 cptr
->c_text
= add (tmpbuf
, cp
);
180 while (state
== FLDPLUS
) {
181 state
= m_getfld (state
, name
, tmpbuf
,
183 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
184 char_read
+= msg_count
;
188 } while ((cptr
= cptr
->c_next
));
190 while (state
== FLDPLUS
)
191 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
201 adios (NULL
, "m_getfld() returned %d", state
);
206 * format and output the header lines.
211 * if there's a "Subject" component, strip any "Re:"s off it
213 FINDCOMP (cptr
, "subject")
214 if (cptr
&& (cp
= cptr
->c_text
)) {
215 register char *sp
= cp
;
226 if (sp
!= cptr
->c_text
) {
228 cptr
->c_text
= getcpy (sp
);
232 i
= format_len
+ char_read
+ 256;
233 scanl
= malloc ((size_t) i
+ 2);
237 dat
[3] = outputlinelen
;
239 fmt_scan (fmt
, scanl
, i
, dat
);
242 fputs ("\nrepl: bad addresses:\n", out
);
243 fputs ( badaddrs
, out
);
247 * Check if we should filter the message
248 * or add mhn directives
253 adios (drft
, "error writing");
255 replfilter (inb
, out
, filter
);
256 } else if (mime
&& mp
) {
257 fprintf (out
, "#forw [original message] +%s %s\n",
258 mp
->foldpath
, m_name (mp
->lowsel
));
263 adios (drft
, "error writing");
266 /* return dynamically allocated buffers */
268 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
269 free (cptr
->c_text
); /* if not nxtbuf, nxtbuf already freed */
271 free (*nxtbuf
++); /* free unused nxtbufs */
272 free ((char *) compbuffers
);
273 free ((char *) used_buf
);
276 static char *buf
; /* our current working buffer */
277 static char *bufend
; /* end of working buffer */
278 static char *last_dst
; /* buf ptr at end of last call */
279 static unsigned int bufsiz
=0; /* current size of buf */
281 #define BUFINCR 512 /* how much to expand buf when if fills */
283 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
286 * check if there's enough room in buf for str.
287 * add more mem if needed
289 #define CHECKMEM(str) \
290 if ((len = strlen (str)) >= bufend - dst) {\
292 int n = last_dst - buf;\
293 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
294 buf = realloc (buf, bufsiz);\
298 adios (NULL, "formataddr: couldn't get buffer space");\
299 bufend = buf + bufsiz;\
304 * fmt_scan will call this routine if the user includes the function
305 * "(formataddr {component})" in a format string. "orig" is the
306 * original contents of the string register. "str" is the address
307 * string to be formatted and concatenated onto orig. This routine
308 * returns a pointer to the concatenated address string.
310 * We try to not do a lot of malloc/copy/free's (which is why we
311 * don't call "getcpy") but still place no upper limit on the
312 * length of the result string.
315 formataddr (char *orig
, char *str
)
318 char baddr
[BUFSIZ
], error
[BUFSIZ
];
319 register int isgroup
;
323 register struct mailname
*mp
= NULL
;
325 /* if we don't have a buffer yet, get one */
327 buf
= malloc (BUFINCR
);
329 adios (NULL
, "formataddr: couldn't allocate buffer space");
330 last_dst
= buf
; /* XXX */
331 bufsiz
= BUFINCR
- 6; /* leave some slop */
332 bufend
= buf
+ bufsiz
;
335 * If "orig" points to our buffer we can just pick up where we
336 * left off. Otherwise we have to copy orig into our buffer.
340 else if (!orig
|| !*orig
) {
344 dst
= last_dst
; /* XXX */
349 /* concatenate all the new addresses onto 'buf' */
350 for (isgroup
= 0; (cp
= getname (str
)); ) {
351 if ((mp
= getm (cp
, dfhost
, dftype
, AD_NAME
, error
)) == NULL
) {
352 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
353 badaddrs
= add (baddr
, badaddrs
);
356 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
361 /* if we get here we're going to add an address */
367 CHECKMEM (mp
->m_gname
);
387 insert (struct mailname
*np
)
390 register struct mailname
*mp
;
392 if (np
->m_mbox
== NULL
)
395 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
396 if (!strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
397 && !strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
))
400 if (!ccme
&& ismymbox (np
))
404 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
405 if (!gans (buffer
, anoyes
))
422 * This function expects that argument out has been fflushed by the caller.
426 replfilter (FILE *in
, FILE *out
, char *filter
)
435 if (access (filter
, R_OK
) == NOTOK
)
436 adios (filter
, "unable to read");
438 mhl
= r1bindex (mhlproc
, '/');
441 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
443 switch (pid
= vfork ()) {
445 adios ("fork", "unable to");
448 dup2 (fileno (in
), fileno (stdin
));
449 dup2 (fileno (out
), fileno (stdout
));
452 execlp (mhlproc
, mhl
, "-form", filter
, "-noclear", NULL
);
453 errstr
= strerror(errno
);
454 write(2, "unable to exec ", 15);
455 write(2, mhlproc
, strlen(mhlproc
));
457 write(2, errstr
, strlen(errstr
));
462 if (pidXwait (pid
, mhl
))
464 fseek (out
, 0L, SEEK_END
);