]>
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
, *cp
;
75 static int dat
[5]; /* aux. data for format routine */
76 m_getfld_state_t gstate
= 0;
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"
138 int msg_count
= sizeof tmpbuf
;
139 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
144 * if we're interested in this component, save a pointer
145 * to the component text, then start using our next free
146 * buffer as the component temp buffer (buffer switching
147 * saves an extra copy of the component text).
150 i
= fmt_addcomptext(name
, tmpbuf
);
152 char_read
+= msg_count
;
153 while (state
== FLDPLUS
) {
154 msg_count
= sizeof tmpbuf
;
155 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
156 fmt_appendcomp(i
, name
, tmpbuf
);
157 char_read
+= msg_count
;
161 while (state
== FLDPLUS
) {
162 msg_count
= sizeof tmpbuf
;
163 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
174 adios (NULL
, "m_getfld() returned %d", state
);
179 * format and output the header lines.
182 m_getfld_state_destroy (&gstate
);
185 * if there's a "Subject" component, strip any "Re:"s off it
187 cptr
= fmt_findcomp ("subject");
188 if (cptr
&& (cp
= cptr
->c_text
)) {
189 register char *sp
= cp
;
192 while (isspace((unsigned char) *cp
))
200 if (sp
!= cptr
->c_text
) {
202 cptr
->c_text
= getcpy (sp
);
206 i
= format_len
+ char_read
+ 256;
207 scanl
= mh_xmalloc ((size_t) i
+ 2);
211 dat
[3] = outputlinelen
;
213 memset(&cb
, 0, sizeof(cb
));
214 cb
.formataddr
= replformataddr
;
215 cb
.concataddr
= replconcataddr
;
216 fmt_scan (fmt
, scanl
, i
+ 1, i
, dat
, &cb
);
219 fputs ("\nrepl: bad addresses:\n", out
);
220 fputs ( badaddrs
, out
);
224 * Check if we should filter the message
225 * or add mhn directives
230 adios (drft
, "error writing");
232 replfilter (inb
, out
, filter
, fmtproc
);
233 } else if (mime
&& mp
) {
234 fprintf (out
, "#forw [original message] +%s %s\n",
235 mp
->foldpath
, m_name (mp
->lowsel
));
240 adios (drft
, "error writing");
243 /* return dynamically allocated buffers */
248 static char *buf
; /* our current working buffer */
249 static char *bufend
; /* end of working buffer */
250 static char *last_dst
; /* buf ptr at end of last call */
251 static unsigned int bufsiz
=0; /* current size of buf */
253 #define BUFINCR 512 /* how much to expand buf when if fills */
255 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
258 * check if there's enough room in buf for str.
259 * add more mem if needed
261 #define CHECKMEM(str) \
262 if ((len = strlen (str)) >= bufend - dst) {\
264 int n = last_dst - buf;\
265 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
266 buf = mh_xrealloc (buf, bufsiz);\
269 bufend = buf + bufsiz;\
274 * fmt_scan will call this routine if the user includes the function
275 * "(formataddr {component})" in a format string. "orig" is the
276 * original contents of the string register. "str" is the address
277 * string to be formatted and concatenated onto orig. This routine
278 * returns a pointer to the concatenated address string.
280 * We try to not do a lot of malloc/copy/free's (which is why we
281 * don't call "getcpy") but still place no upper limit on the
282 * length of the result string.
285 replformataddr (char *orig
, char *str
)
288 char baddr
[BUFSIZ
], error
[BUFSIZ
];
289 register int isgroup
;
293 register struct mailname
*mp
= NULL
;
295 /* if we don't have a buffer yet, get one */
297 buf
= mh_xmalloc (BUFINCR
);
298 last_dst
= buf
; /* XXX */
299 bufsiz
= BUFINCR
- 6; /* leave some slop */
300 bufend
= buf
+ bufsiz
;
303 * If "orig" points to our buffer we can just pick up where we
304 * left off. Otherwise we have to copy orig into our buffer.
308 else if (!orig
|| !*orig
) {
312 dst
= last_dst
; /* XXX */
317 /* concatenate all the new addresses onto 'buf' */
318 for (isgroup
= 0; (cp
= getname (str
)); ) {
319 if ((mp
= getm (cp
, dfhost
, dftype
, AD_NAME
, error
)) == NULL
) {
320 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
321 badaddrs
= add (baddr
, badaddrs
);
324 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
329 /* if we get here we're going to add an address */
335 CHECKMEM (mp
->m_gname
);
355 * fmt_scan will call this routine if the user includes the function
356 * "(concataddr {component})" in a format string. This behaves exactly
357 * like formataddr, except that it does NOT suppress duplicate addresses
360 * As an implementation detail: I thought about splitting out replformataddr()
361 * into the generic part and duplicate-suppressing part, but the call to
362 * insert() was buried deep within a couple of loops and I didn't see a
363 * way to do it easily. So instead we simply set a special flag to stop
364 * the duplicate check and call replformataddr().
367 replconcataddr(char *orig
, char *str
)
372 cp
= replformataddr(orig
, str
);
378 insert (struct mailname
*np
)
381 register struct mailname
*mp
;
386 if (np
->m_mbox
== NULL
)
389 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
390 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
391 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
392 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
393 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: ""))
396 if (!ccme
&& ismymbox (np
))
400 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
401 if (!gans (buffer
, anoyes
))
413 * This function expects that argument out has been fflushed by the caller.
417 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
428 if (access (filter
, R_OK
) == NOTOK
)
429 adios (filter
, "unable to read");
432 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
434 switch (pid
= fork()) {
436 adios ("fork", "unable to");
439 dup2 (fileno (in
), fileno (stdin
));
440 dup2 (fileno (out
), fileno (stdout
));
444 * We're not allocating the memory for the extra arguments,
445 * because we never call arglist_free(). But if we ever change
446 * that be sure to use getcpy() for the extra arguments.
448 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
449 arglist
[argnum
++] = "-form";
450 arglist
[argnum
++] = filter
;
451 arglist
[argnum
++] = "-noclear";
455 arglist
[argnum
++] = "-fmtproc";
456 arglist
[argnum
++] = formatproc
;
459 arglist
[argnum
++] = "-nofmtproc";
463 arglist
[argnum
++] = NULL
;
465 execvp (mhl
, arglist
);
466 errstr
= strerror(errno
);
467 write(2, "unable to exec ", 15);
468 write(2, mhlproc
, strlen(mhlproc
));
470 write(2, errstr
, strlen(errstr
));
475 if (pidXwait (pid
, mhl
))
477 fseek (out
, 0L, SEEK_END
);