]>
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 */
16 extern short ccto
; /* from repl.c */
23 static char *badaddrs
= NULL
;
24 static char *dfhost
= NULL
;
26 static struct mailname mq
;
27 static int nodupcheck
= 0; /* If set, no check for duplicates */
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 char *addrcomps
[] = {
57 static int insert (struct mailname
*);
58 static void replfilter (FILE *, FILE *, char *, int);
59 static char *replformataddr(char *, char *);
60 static char *replconcataddr(char *, char *);
64 replout (FILE *inb
, char *msg
, char *drft
, struct msgs
*mp
, int outputlinelen
,
65 int mime
, char *form
, char *filter
, char *fcc
, int fmtproc
)
67 register int state
, i
;
68 register struct comp
*cptr
;
72 int char_read
= 0, format_len
, mask
;
73 char name
[NAMESZ
], *scanl
, *cp
;
74 static int dat
[5]; /* aux. data for format routine */
75 m_getfld_state_t gstate
= 0;
76 struct fmt_callbacks cb
;
81 mask
= umask(~m_gmprot());
82 if ((out
= fopen (drft
, "w")) == NULL
)
83 adios (drft
, "unable to create");
87 /* get new format string */
88 cp
= new_fs (form
, NULL
, NULL
);
89 format_len
= strlen (cp
);
91 /* compile format string */
92 fmt_compile (cp
, &fmt
, 1);
94 for (ap
= addrcomps
; *ap
; ap
++) {
95 cptr
= fmt_findcomp (*ap
);
97 cptr
->c_type
|= CT_ADDR
;
101 * ignore any components killed by command line switches
103 * This prevents the component from being found via fmt_findcomp(),
104 * which makes sure no text gets added to it when the message is processed.
106 * getcpy(NULL) returns a malloc'd zero-length string, so it can safely
110 cptr
= fmt_findcomp ("to");
112 cptr
->c_name
= getcpy(NULL
);
115 cptr
= fmt_findcomp("cc");
117 cptr
->c_name
= getcpy(NULL
);
119 /* set up the "fcc" pseudo-component */
121 cptr
= fmt_findcomp ("fcc");
123 cptr
->c_text
= getcpy (fcc
);
125 if ((cp
= getenv("USER"))) {
126 cptr
= fmt_findcomp ("user");
128 cptr
->c_text
= getcpy(cp
);
134 * pick any interesting stuff out of msg "inb"
137 int msg_count
= sizeof tmpbuf
;
138 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, 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 msg_count
= sizeof tmpbuf
;
154 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
155 fmt_appendcomp(i
, name
, tmpbuf
);
156 char_read
+= msg_count
;
160 while (state
== FLDPLUS
) {
161 msg_count
= sizeof tmpbuf
;
162 state
= m_getfld (&gstate
, name
, tmpbuf
, &msg_count
, inb
);
173 adios (NULL
, "m_getfld() returned %d", state
);
178 * format and output the header lines.
181 m_getfld_state_destroy (&gstate
);
184 * if there's a "Subject" component, strip any "Re:"s off it
186 cptr
= fmt_findcomp ("subject");
187 if (cptr
&& (cp
= cptr
->c_text
)) {
188 register char *sp
= cp
;
191 while (isspace((unsigned char) *cp
))
199 if (sp
!= cptr
->c_text
) {
201 cptr
->c_text
= getcpy (sp
);
205 i
= format_len
+ char_read
+ 256;
206 scanl
= mh_xmalloc ((size_t) i
+ 2);
210 dat
[3] = outputlinelen
;
212 memset(&cb
, 0, sizeof(cb
));
213 cb
.formataddr
= replformataddr
;
214 cb
.concataddr
= replconcataddr
;
215 fmt_scan (fmt
, scanl
, i
+ 1, i
, dat
, &cb
);
218 fputs ("\nrepl: bad addresses:\n", out
);
219 fputs ( badaddrs
, out
);
223 * Check if we should filter the message
224 * or add mhn directives
229 adios (drft
, "error writing");
231 replfilter (inb
, out
, filter
, fmtproc
);
232 } else if (mime
&& mp
) {
233 fprintf (out
, "#forw [original message] +%s %s\n",
234 mp
->foldpath
, m_name (mp
->lowsel
));
239 adios (drft
, "error writing");
242 /* return dynamically allocated buffers */
247 static char *buf
; /* our current working buffer */
248 static char *bufend
; /* end of working buffer */
249 static char *last_dst
; /* buf ptr at end of last call */
250 static unsigned int bufsiz
=0; /* current size of buf */
252 #define BUFINCR 512 /* how much to expand buf when if fills */
254 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
257 * check if there's enough room in buf for str.
258 * add more mem if needed
260 #define CHECKMEM(str) \
261 if ((len = strlen (str)) >= bufend - dst) {\
263 int n = last_dst - buf;\
264 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
265 buf = mh_xrealloc (buf, bufsiz);\
268 bufend = buf + bufsiz;\
273 * fmt_scan will call this routine if the user includes the function
274 * "(formataddr {component})" in a format string. "orig" is the
275 * original contents of the string register. "str" is the address
276 * string to be formatted and concatenated onto orig. This routine
277 * returns a pointer to the concatenated address string.
279 * We try to not do a lot of malloc/copy/free's (which is why we
280 * don't call "getcpy") but still place no upper limit on the
281 * length of the result string.
284 replformataddr (char *orig
, char *str
)
287 char baddr
[BUFSIZ
], error
[BUFSIZ
];
288 register int isgroup
;
292 register struct mailname
*mp
= NULL
;
294 /* if we don't have a buffer yet, get one */
296 buf
= mh_xmalloc (BUFINCR
);
297 last_dst
= buf
; /* XXX */
298 bufsiz
= BUFINCR
- 6; /* leave some slop */
299 bufend
= buf
+ bufsiz
;
302 * If "orig" points to our buffer we can just pick up where we
303 * left off. Otherwise we have to copy orig into our buffer.
307 else if (!orig
|| !*orig
) {
311 dst
= last_dst
; /* XXX */
316 /* concatenate all the new addresses onto 'buf' */
317 for (isgroup
= 0; (cp
= getname (str
)); ) {
318 if ((mp
= getm (cp
, dfhost
, dftype
, AD_NAME
, error
)) == NULL
) {
319 snprintf (baddr
, sizeof(baddr
), "\t%s -- %s\n", cp
, error
);
320 badaddrs
= add (baddr
, badaddrs
);
323 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
328 /* if we get here we're going to add an address */
334 CHECKMEM (mp
->m_gname
);
354 * fmt_scan will call this routine if the user includes the function
355 * "(concataddr {component})" in a format string. This behaves exactly
356 * like formataddr, except that it does NOT suppress duplicate addresses
359 * As an implementation detail: I thought about splitting out replformataddr()
360 * into the generic part and duplicate-suppressing part, but the call to
361 * insert() was buried deep within a couple of loops and I didn't see a
362 * way to do it easily. So instead we simply set a special flag to stop
363 * the duplicate check and call replformataddr().
366 replconcataddr(char *orig
, char *str
)
371 cp
= replformataddr(orig
, str
);
377 insert (struct mailname
*np
)
380 register struct mailname
*mp
;
385 if (np
->m_mbox
== NULL
)
388 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
389 if (!strcasecmp (np
->m_host
? np
->m_host
: "",
390 mp
->m_next
->m_host
? mp
->m_next
->m_host
: "") &&
391 !strcasecmp (np
->m_mbox
? np
->m_mbox
: "",
392 mp
->m_next
->m_mbox
? mp
->m_next
->m_mbox
: ""))
395 if (!ccme
&& ismymbox (np
))
399 snprintf (buffer
, sizeof(buffer
), "Reply to %s? ", adrformat (np
));
400 if (!gans (buffer
, anoyes
))
412 * This function expects that argument out has been fflushed by the caller.
416 replfilter (FILE *in
, FILE *out
, char *filter
, int fmtproc
)
427 if (access (filter
, R_OK
) == NOTOK
)
428 adios (filter
, "unable to read");
431 lseek (fileno(in
), (off_t
) 0, SEEK_SET
);
433 switch (pid
= fork()) {
435 adios ("fork", "unable to");
438 dup2 (fileno (in
), fileno (stdin
));
439 dup2 (fileno (out
), fileno (stdout
));
443 * We're not allocating the memory for the extra arguments,
444 * because we never call arglist_free(). But if we ever change
445 * that be sure to use getcpy() for the extra arguments.
447 arglist
= argsplit(mhlproc
, &mhl
, &argnum
);
448 arglist
[argnum
++] = "-form";
449 arglist
[argnum
++] = filter
;
450 arglist
[argnum
++] = "-noclear";
454 arglist
[argnum
++] = "-fmtproc";
455 arglist
[argnum
++] = formatproc
;
458 arglist
[argnum
++] = "-nofmtproc";
462 arglist
[argnum
++] = NULL
;
464 execvp (mhl
, arglist
);
465 errstr
= strerror(errno
);
466 write(2, "unable to exec ", 15);
467 write(2, mhlproc
, strlen(mhlproc
));
469 write(2, errstr
, strlen(errstr
));
474 if (pidXwait (pid
, mhl
))
476 fseek (out
, 0L, SEEK_END
);