]>
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
;
28 static int nodupcheck
= 0; /* If set, no check for duplicates */
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
)
76 register int state
, i
;
77 register struct comp
*cptr
;
78 register char *tmpbuf
;
79 register char **nxtbuf
;
81 register struct comp
**savecomp
;
82 int char_read
= 0, format_len
, mask
;
83 char name
[NAMESZ
], *scanl
;
87 mask
= umask(~m_gmprot());
88 if ((out
= fopen (drft
, "w")) == NULL
)
89 adios (drft
, "unable to create");
93 /* get new format string */
94 cp
= new_fs (form
, NULL
, NULL
);
95 format_len
= strlen (cp
);
97 /* compile format string */
98 ncomps
= fmt_compile (cp
, &fmt
) + 1;
100 if (!(nxtbuf
= compbuffers
= (char **)
101 calloc((size_t) ncomps
, sizeof(char *))))
102 adios (NULL
, "unable to allocate component buffers");
103 if (!(savecomp
= used_buf
= (struct comp
**)
104 calloc((size_t) (ncomps
+1), sizeof(struct comp
*))))
105 adios (NULL
, "unable to allocate component buffer stack");
106 savecomp
+= ncomps
+ 1;
107 *--savecomp
= NULL
; /* point at zero'd end minus 1 */
109 for (i
= ncomps
; i
--; )
110 *nxtbuf
++ = mh_xmalloc(SBUFSIZ
);
112 nxtbuf
= compbuffers
; /* point at start */
115 for (ap
= addrcomps
; *ap
; ap
++) {
116 FINDCOMP (cptr
, *ap
);
118 cptr
->c_type
|= CT_ADDR
;
122 * ignore any components killed by command line switches
125 FINDCOMP (cptr
, "to");
130 FINDCOMP (cptr
, "cc");
134 /* set up the "fcc" pseudo-component */
136 FINDCOMP (cptr
, "fcc");
138 cptr
->c_text
= getcpy (fcc
);
140 if ((cp
= getenv("USER"))) {
141 FINDCOMP (cptr
, "user");
143 cptr
->c_text
= getcpy(cp
);
149 * pick any interesting stuff out of msg "inb"
151 for (state
= FLD
;;) {
152 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
157 * if we're interested in this component, save a pointer
158 * to the component text, then start using our next free
159 * buffer as the component temp buffer (buffer switching
160 * saves an extra copy of the component text).
162 if ((cptr
= wantcomp
[CHASH(name
)]))
164 if (!mh_strcasecmp(name
, cptr
->c_name
)) {
165 char_read
+= msg_count
;
166 if (! cptr
->c_text
) {
167 i
= strlen(cptr
->c_text
= tmpbuf
) - 1;
168 if (tmpbuf
[i
] == '\n')
173 i
= strlen (cp
= cptr
->c_text
) - 1;
175 if (cptr
->c_type
& CT_ADDR
) {
177 cp
= add (",\n\t", cp
);
182 cptr
->c_text
= add (tmpbuf
, cp
);
184 while (state
== FLDPLUS
) {
185 state
= m_getfld (state
, name
, tmpbuf
,
187 cptr
->c_text
= add (tmpbuf
, cptr
->c_text
);
188 char_read
+= msg_count
;
192 } while ((cptr
= cptr
->c_next
));
194 while (state
== FLDPLUS
)
195 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
205 adios (NULL
, "m_getfld() returned %d", state
);
210 * format and output the header lines.
215 * if there's a "Subject" component, strip any "Re:"s off it
217 FINDCOMP (cptr
, "subject")
218 if (cptr
&& (cp
= cptr
->c_text
)) {
219 register char *sp
= cp
;
230 if (sp
!= cptr
->c_text
) {
232 cptr
->c_text
= getcpy (sp
);
236 i
= format_len
+ char_read
+ 256;
237 scanl
= mh_xmalloc ((size_t) i
+ 2);
241 dat
[3] = outputlinelen
;
243 fmt_scan (fmt
, scanl
, i
, dat
);
246 fputs ("\nrepl: bad addresses:\n", out
);
247 fputs ( badaddrs
, out
);
251 * Check if we should filter the message
252 * or add mhn directives
257 adios (drft
, "error writing");
259 replfilter (inb
, out
, filter
);
260 } else if (mime
&& mp
) {
261 fprintf (out
, "#forw [original message] +%s %s\n",
262 mp
->foldpath
, m_name (mp
->lowsel
));
267 adios (drft
, "error writing");
270 /* return dynamically allocated buffers */
272 for (nxtbuf
= compbuffers
, i
= ncomps
; (cptr
= *savecomp
++); nxtbuf
++, i
--)
273 free (cptr
->c_text
); /* if not nxtbuf, nxtbuf already freed */
275 free (*nxtbuf
++); /* free unused nxtbufs */
276 free ((char *) compbuffers
);
277 free ((char *) used_buf
);
280 static char *buf
; /* our current working buffer */
281 static char *bufend
; /* end of working buffer */
282 static char *last_dst
; /* buf ptr at end of last call */
283 static unsigned int bufsiz
=0; /* current size of buf */
285 #define BUFINCR 512 /* how much to expand buf when if fills */
287 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
290 * check if there's enough room in buf for str.
291 * add more mem if needed
293 #define CHECKMEM(str) \
294 if ((len = strlen (str)) >= bufend - dst) {\
296 int n = last_dst - buf;\
297 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
298 buf = mh_xrealloc (buf, bufsiz);\
301 bufend = buf + bufsiz;\
306 * fmt_scan will call this routine if the user includes the function
307 * "(formataddr {component})" in a format string. "orig" is the
308 * original contents of the string register. "str" is the address
309 * string to be formatted and concatenated onto orig. This routine
310 * returns a pointer to the concatenated address string.
312 * We try to not do a lot of malloc/copy/free's (which is why we
313 * don't call "getcpy") but still place no upper limit on the
314 * length of the result string.
317 formataddr (char *orig
, char *str
)
320 char baddr
[BUFSIZ
], error
[BUFSIZ
];
321 register int isgroup
;
325 register struct mailname
*mp
= NULL
;
327 /* if we don't have a buffer yet, get one */
329 buf
= mh_xmalloc (BUFINCR
);
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 * fmt_scan will call this routine if the user includes the function
388 * "(concataddr {component})" in a format string. This behaves exactly
389 * like formataddr, except that it does NOT suppress duplicate addresses
392 * As an implementation detail: I thought about splitting out formataddr()
393 * into the generic part and duplicate-suppressing part, but the call to
394 * insert() was buried deep within a couple of loops and I didn't see a
395 * way to do it easily. So instead we simply set a special flag to stop
396 * the duplicate check and call formataddr().
399 concataddr(char *orig
, char *str
)
404 cp
= formataddr(orig
, str
);
410 insert (struct mailname
*np
)
413 register struct mailname
*mp
;
418 if (np
->m_mbox
== NULL
)
421 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
422 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
423 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
))
426 if (!ccme
&& ismymbox (np
))
430 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
431 if (!gans (buffer
, anoyes
))
448 * This function expects that argument out has been fflushed by the caller.
452 replfilter (FILE *in
, FILE *out
, char *filter
)
461 if (access (filter
, R_OK
) == NOTOK
)
462 adios (filter
, "unable to read");
464 mhl
= r1bindex (mhlproc
, '/');
467 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
469 switch (pid
= vfork()) {
471 adios ("fork", "unable to");
474 dup2 (fileno (in
), fileno (stdin
));
475 dup2 (fileno (out
), fileno (stdout
));
478 execlp (mhlproc
, mhl
, "-form", filter
, "-noclear", NULL
);
479 errstr
= strerror(errno
);
480 write(2, "unable to exec ", 15);
481 write(2, mhlproc
, strlen(mhlproc
));
483 write(2, errstr
, strlen(errstr
));
488 if (pidXwait (pid
, mhl
))
490 fseek (out
, 0L, SEEK_END
);