]>
diplodocus.org Git - nmh/blob - uip/replsbr.c
3 * replsbr.c -- routines to help repl along...
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
14 #include <sys/file.h> /* L_SET */
17 extern short ccto
; /* from repl.c */
24 static char *badaddrs
= NULL
;
25 static char *dfhost
= NULL
;
27 static struct mailname mq
= { NULL
};
30 * Buffer size for content part of header fields.
31 * We want this to be large enough so that we don't
32 * do a lot of extra FLDPLUS calls on m_getfld but
33 * small enough so that we don't snarf the entire
34 * message body when we're not going to use any of it.
38 static struct format
*fmt
;
40 static int ncomps
= 0; /* # of interesting components */
41 static char **compbuffers
= NULL
; /* buffers for component text */
42 static struct comp
**used_buf
= NULL
; /* stack for comp that use buffers */
44 static int dat
[5]; /* aux. data for format routine */
46 static char *addrcomps
[] = {
65 static int insert (struct mailname
*);
66 static void replfilter (FILE *, FILE *, char *);
70 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
71 int mime
, char *form
, char *filter
, char *fcc
)
73 register int state
, i
;
74 register struct comp
*cptr
;
75 register char *tmpbuf
;
76 register char **nxtbuf
;
78 register struct comp
**savecomp
;
79 int char_read
= 0, format_len
, mask
;
80 char name
[NAMESZ
], *scanl
;
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 *nxtbuf
++ = mh_xmalloc(SBUFSIZ
);
109 nxtbuf
= compbuffers
; /* point at start */
112 for (ap
= addrcomps
; *ap
; ap
++) {
113 FINDCOMP (cptr
, *ap
);
115 cptr
->c_type
|= CT_ADDR
;
119 * ignore any components killed by command line switches
122 FINDCOMP (cptr
, "to");
127 FINDCOMP (cptr
, "cc");
131 /* set up the "fcc" pseudo-component */
133 FINDCOMP (cptr
, "fcc");
135 cptr
->c_text
= getcpy (fcc
);
137 if ((cp
= getenv("USER"))) {
138 FINDCOMP (cptr
, "user");
140 cptr
->c_text
= getcpy(cp
);
146 * pick any interesting stuff out of msg "inb"
148 for (state
= FLD
;;) {
149 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
154 * if we're interested in this component, save a pointer
155 * to the component text, then start using our next free
156 * buffer as the component temp buffer (buffer switching
157 * saves an extra copy of the component text).
159 if ((cptr
= wantcomp
[CHASH(name
)]))
161 if (!mh_strcasecmp(name
, cptr
->c_name
)) {
162 char_read
+= msg_count
;
163 if (! cptr
->c_text
) {
164 i
= strlen(cptr
->c_text
= tmpbuf
) - 1;
165 if (tmpbuf
[i
] == '\n')
170 i
= strlen (cp
= cptr
->c_text
) - 1;
172 if (cptr
->c_type
& CT_ADDR
) {
174 cp
= add (",\n\t", cp
);
179 cptr
->c_text
= add (tmpbuf
, cp
);
181 while (state
== FLDPLUS
) {
182 state
= m_getfld (state
, name
, tmpbuf
,
184 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
185 char_read
+= msg_count
;
189 } while ((cptr
= cptr
->c_next
));
191 while (state
== FLDPLUS
)
192 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
202 adios (NULL
, "m_getfld() returned %d", state
);
207 * format and output the header lines.
212 * if there's a "Subject" component, strip any "Re:"s off it
214 FINDCOMP (cptr
, "subject")
215 if (cptr
&& (cp
= cptr
->c_text
)) {
216 register char *sp
= cp
;
227 if (sp
!= cptr
->c_text
) {
229 cptr
->c_text
= getcpy (sp
);
233 i
= format_len
+ char_read
+ 256;
234 scanl
= mh_xmalloc ((size_t) i
+ 2);
238 dat
[3] = outputlinelen
;
240 fmt_scan (fmt
, scanl
, i
, dat
);
243 fputs ("\nrepl: bad addresses:\n", out
);
244 fputs ( badaddrs
, out
);
248 * Check if we should filter the message
249 * or add mhn directives
254 adios (drft
, "error writing");
256 replfilter (inb
, out
, filter
);
257 } else if (mime
&& mp
) {
258 fprintf (out
, "#forw [original message] +%s %s\n",
259 mp
->foldpath
, m_name (mp
->lowsel
));
264 adios (drft
, "error writing");
267 /* return dynamically allocated buffers */
269 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
270 free (cptr
->c_text
); /* if not nxtbuf, nxtbuf already freed */
272 free (*nxtbuf
++); /* free unused nxtbufs */
273 free ((char *) compbuffers
);
274 free ((char *) used_buf
);
277 static char *buf
; /* our current working buffer */
278 static char *bufend
; /* end of working buffer */
279 static char *last_dst
; /* buf ptr at end of last call */
280 static unsigned int bufsiz
=0; /* current size of buf */
282 #define BUFINCR 512 /* how much to expand buf when if fills */
284 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
287 * check if there's enough room in buf for str.
288 * add more mem if needed
290 #define CHECKMEM(str) \
291 if ((len = strlen (str)) >= bufend - dst) {\
293 int n = last_dst - buf;\
294 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
295 buf = mh_xrealloc (buf, bufsiz);\
298 bufend = buf + bufsiz;\
303 * fmt_scan will call this routine if the user includes the function
304 * "(formataddr {component})" in a format string. "orig" is the
305 * original contents of the string register. "str" is the address
306 * string to be formatted and concatenated onto orig. This routine
307 * returns a pointer to the concatenated address string.
309 * We try to not do a lot of malloc/copy/free's (which is why we
310 * don't call "getcpy") but still place no upper limit on the
311 * length of the result string.
314 formataddr (char *orig
, char *str
)
317 char baddr
[BUFSIZ
], error
[BUFSIZ
];
318 register int isgroup
;
322 register struct mailname
*mp
= NULL
;
324 /* if we don't have a buffer yet, get one */
326 buf
= mh_xmalloc (BUFINCR
);
327 last_dst
= buf
; /* XXX */
328 bufsiz
= BUFINCR
- 6; /* leave some slop */
329 bufend
= buf
+ bufsiz
;
332 * If "orig" points to our buffer we can just pick up where we
333 * left off. Otherwise we have to copy orig into our buffer.
337 else if (!orig
|| !*orig
) {
341 dst
= last_dst
; /* XXX */
346 /* concatenate all the new addresses onto 'buf' */
347 for (isgroup
= 0; (cp
= getname (str
)); ) {
348 if ((mp
= getm (cp
, dfhost
, dftype
, AD_NAME
, error
)) == NULL
) {
349 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
350 badaddrs
= add (baddr
, badaddrs
);
353 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
358 /* if we get here we're going to add an address */
364 CHECKMEM (mp
->m_gname
);
384 insert (struct mailname
*np
)
387 register struct mailname
*mp
;
389 if (np
->m_mbox
== NULL
)
392 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
393 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
394 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
))
397 if (!ccme
&& ismymbox (np
))
401 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
402 if (!gans (buffer
, anoyes
))
419 * This function expects that argument out has been fflushed by the caller.
423 replfilter (FILE *in
, FILE *out
, char *filter
)
432 if (access (filter
, R_OK
) == NOTOK
)
433 adios (filter
, "unable to read");
435 mhl
= r1bindex (mhlproc
, '/');
438 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
440 switch (pid
= vfork ()) {
442 adios ("fork", "unable to");
445 dup2 (fileno (in
), fileno (stdin
));
446 dup2 (fileno (out
), fileno (stdout
));
449 execlp (mhlproc
, mhl
, "-form", filter
, "-noclear", NULL
);
450 errstr
= strerror(errno
);
451 write(2, "unable to exec ", 15);
452 write(2, mhlproc
, strlen(mhlproc
));
454 write(2, errstr
, strlen(errstr
));
459 if (pidXwait (pid
, mhl
))
461 fseek (out
, 0L, SEEK_END
);