]>
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 char *addrcomps
[] = {
58 static int insert (struct mailname
*);
59 static void replfilter (FILE *, FILE *, char *, int);
60 static char *replformataddr(char *, char *);
61 static char *replconcataddr(char *, char *);
65 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
66 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
68 register int state
, i
;
69 register struct comp
*cptr
;
73 int char_read
= 0, format_len
, mask
;
74 char name
[NAMESZ
], *scanl
;
76 static int dat
[5]; /* aux. data for format routine */
77 struct fmt_callbacks cb
;
82 mask
= umask(~m_gmprot());
83 if ((out
= fopen (drft
, "w")) == NULL
)
84 adios (drft
, "unable to create");
88 /* get new format string */
89 cp
= new_fs (form
, NULL
, NULL
);
90 format_len
= strlen (cp
);
92 /* compile format string */
93 fmt_compile (cp
, &fmt
, 1);
95 for (ap
= addrcomps
; *ap
; ap
++) {
96 cptr
= fmt_findcomp (*ap
);
98 cptr
->c_type
|= CT_ADDR
;
102 * ignore any components killed by command line switches
104 * This prevents the component from being found via fmt_findcomp(),
105 * which makes sure no text gets added to it when the message is processed.
107 * getcpy(NULL) returns a malloc'd zero-length string, so it can safely
111 cptr
= fmt_findcomp ("to");
113 cptr
->c_name
= getcpy(NULL
);
116 cptr
= fmt_findcomp("cc");
118 cptr
->c_name
= getcpy(NULL
);
120 /* set up the "fcc" pseudo-component */
122 cptr
= fmt_findcomp ("fcc");
124 cptr
->c_text
= getcpy (fcc
);
126 if ((cp
= getenv("USER"))) {
127 cptr
= fmt_findcomp ("user");
129 cptr
->c_text
= getcpy(cp
);
135 * pick any interesting stuff out of msg "inb"
137 for (state
= FLD
;;) {
138 state
= m_getfld (state
, name
, tmpbuf
, sizeof(tmpbuf
), inb
);
143 * if we're interested in this component, save a pointer
144 * to the component text, then start using our next free
145 * buffer as the component temp buffer (buffer switching
146 * saves an extra copy of the component text).
149 i
= fmt_addcomptext(name
, tmpbuf
);
151 char_read
+= msg_count
;
152 while (state
== FLDPLUS
) {
153 state
= m_getfld(state
, name
, tmpbuf
,
154 sizeof(tmpbuf
), inb
);
155 fmt_appendcomp(i
, name
, tmpbuf
);
156 char_read
+= msg_count
;
160 while (state
== FLDPLUS
)
161 state
= m_getfld (state
, name
, tmpbuf
, SBUFSIZ
, inb
);
171 adios (NULL
, "m_getfld() returned %d", state
);
176 * format and output the header lines.
181 * if there's a "Subject" component, strip any "Re:"s off it
183 cptr
= fmt_findcomp ("subject");
184 if (cptr
&& (cp
= cptr
->c_text
)) {
185 register char *sp
= cp
;
196 if (sp
!= cptr
->c_text
) {
198 cptr
->c_text
= getcpy (sp
);
202 i
= format_len
+ char_read
+ 256;
203 scanl
= mh_xmalloc ((size_t) i
+ 2);
207 dat
[3] = outputlinelen
;
209 memset(&cb
, 0, sizeof(cb
));
210 cb
.formataddr
= replformataddr
;
211 cb
.concataddr
= replconcataddr
;
212 fmt_scan (fmt
, scanl
, i
+ 1, i
, dat
, &cb
);
215 fputs ("\nrepl: bad addresses:\n", out
);
216 fputs ( badaddrs
, out
);
220 * Check if we should filter the message
221 * or add mhn directives
226 adios (drft
, "error writing");
228 replfilter (inb
, out
, filter
, fmtproc
);
229 } else if (mime
&& mp
) {
230 fprintf (out
, "#forw [original message] +%s %s\n",
231 mp
->foldpath
, m_name (mp
->lowsel
));
236 adios (drft
, "error writing");
239 /* return dynamically allocated buffers */
244 static char *buf
; /* our current working buffer */
245 static char *bufend
; /* end of working buffer */
246 static char *last_dst
; /* buf ptr at end of last call */
247 static unsigned int bufsiz
=0; /* current size of buf */
249 #define BUFINCR 512 /* how much to expand buf when if fills */
251 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
254 * check if there's enough room in buf for str.
255 * add more mem if needed
257 #define CHECKMEM(str) \
258 if ((len = strlen (str)) >= bufend - dst) {\
260 int n = last_dst - buf;\
261 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
262 buf = mh_xrealloc (buf, bufsiz);\
265 bufend = buf + bufsiz;\
270 * fmt_scan will call this routine if the user includes the function
271 * "(formataddr {component})" in a format string. "orig" is the
272 * original contents of the string register. "str" is the address
273 * string to be formatted and concatenated onto orig. This routine
274 * returns a pointer to the concatenated address string.
276 * We try to not do a lot of malloc/copy/free's (which is why we
277 * don't call "getcpy") but still place no upper limit on the
278 * length of the result string.
281 replformataddr (char *orig
, char *str
)
284 char baddr
[BUFSIZ
], error
[BUFSIZ
];
285 register int isgroup
;
289 register struct mailname
*mp
= NULL
;
291 /* if we don't have a buffer yet, get one */
293 buf
= mh_xmalloc (BUFINCR
);
294 last_dst
= buf
; /* XXX */
295 bufsiz
= BUFINCR
- 6; /* leave some slop */
296 bufend
= buf
+ bufsiz
;
299 * If "orig" points to our buffer we can just pick up where we
300 * left off. Otherwise we have to copy orig into our buffer.
304 else if (!orig
|| !*orig
) {
308 dst
= last_dst
; /* XXX */
313 /* concatenate all the new addresses onto 'buf' */
314 for (isgroup
= 0; (cp
= getname (str
)); ) {
315 if ((mp
= getm (cp
, dfhost
, dftype
, AD_NAME
, error
)) == NULL
) {
316 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
317 badaddrs
= add (baddr
, badaddrs
);
320 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
325 /* if we get here we're going to add an address */
331 CHECKMEM (mp
->m_gname
);
351 * fmt_scan will call this routine if the user includes the function
352 * "(concataddr {component})" in a format string. This behaves exactly
353 * like formataddr, except that it does NOT suppress duplicate addresses
356 * As an implementation detail: I thought about splitting out replformataddr()
357 * into the generic part and duplicate-suppressing part, but the call to
358 * insert() was buried deep within a couple of loops and I didn't see a
359 * way to do it easily. So instead we simply set a special flag to stop
360 * the duplicate check and call replformataddr().
363 replconcataddr(char *orig
, char *str
)
368 cp
= replformataddr(orig
, str
);
374 insert (struct mailname
*np
)
377 register struct mailname
*mp
;
382 if (np
->m_mbox
== NULL
)
385 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
386 if (!mh_strcasecmp (np
->m_host
, mp
->m_next
->m_host
)
387 && !mh_strcasecmp (np
->m_mbox
, mp
->m_next
->m_mbox
))
390 if (!ccme
&& ismymbox (np
))
394 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
395 if (!gans (buffer
, anoyes
))
407 * This function expects that argument out has been fflushed by the caller.
411 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
421 if (access (filter
, R_OK
) == NOTOK
)
422 adios (filter
, "unable to read");
424 mhl
= r1bindex (mhlproc
, '/');
427 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
429 switch (pid
= vfork()) {
431 adios ("fork", "unable to");
434 dup2 (fileno (in
), fileno (stdin
));
435 dup2 (fileno (out
), fileno (stdout
));
439 arglist
[1] = "-form";
441 arglist
[3] = "-noclear";
445 arglist
[4] = "-fmtproc";
446 arglist
[5] = formatproc
;
450 arglist
[4] = "-nofmtproc";
457 execvp (mhlproc
, arglist
);
458 errstr
= strerror(errno
);
459 write(2, "unable to exec ", 15);
460 write(2, mhlproc
, strlen(mhlproc
));
462 write(2, errstr
, strlen(errstr
));
467 if (pidXwait (pid
, mhl
))
469 fseek (out
, 0L, SEEK_END
);