]> diplodocus.org Git - nmh/blob - uip/replsbr.c
Note in dist, mh-profile, and repl man pages that the @ link
[nmh] / uip / replsbr.c
1
2 /*
3 * replsbr.c -- routines to help repl along...
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/utils.h>
14 #include <sys/file.h> /* L_SET */
15 #include <errno.h>
16
17 extern short ccto; /* from repl.c */
18 extern short cccc;
19 extern short ccme;
20 extern short querysw;
21
22 static int dftype=0;
23
24 static char *badaddrs = NULL;
25 static char *dfhost = NULL;
26
27 static struct mailname mq;
28 static int nodupcheck = 0; /* If set, no check for duplicates */
29
30 /*
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.
36 */
37 #define SBUFSIZ 256
38
39 static char *addrcomps[] = {
40 "from",
41 "sender",
42 "reply-to",
43 "to",
44 "cc",
45 "bcc",
46 "resent-from",
47 "resent-sender",
48 "resent-reply-to",
49 "resent-to",
50 "resent-cc",
51 "resent-bcc",
52 NULL
53 };
54
55 /*
56 * static prototypes
57 */
58 static int insert (struct mailname *);
59 static void replfilter (FILE *, FILE *, char *, int);
60
61
62 void
63 replout (FILE *inb, char *msg, char *drft, struct msgs *mp, int outputlinelen,
64 int mime, char *form, char *filter, char *fcc, int fmtproc)
65 {
66 register int state, i;
67 register struct comp *cptr;
68 char tmpbuf[SBUFSIZ];
69 struct format *fmt;
70 register char **ap;
71 int char_read = 0, format_len, mask;
72 char name[NAMESZ], *scanl;
73 unsigned char *cp;
74 static int dat[5]; /* aux. data for format routine */
75
76 FILE *out;
77 NMH_UNUSED (msg);
78
79 mask = umask(~m_gmprot());
80 if ((out = fopen (drft, "w")) == NULL)
81 adios (drft, "unable to create");
82
83 umask(mask);
84
85 /* get new format string */
86 cp = new_fs (form, NULL, NULL);
87 format_len = strlen (cp);
88
89 /* compile format string */
90 fmt_compile (cp, &fmt, 1);
91
92 for (ap = addrcomps; *ap; ap++) {
93 cptr = fmt_findcomp (*ap);
94 if (cptr)
95 cptr->c_type |= CT_ADDR;
96 }
97
98 /*
99 * ignore any components killed by command line switches
100 *
101 * This prevents the component from being found via fmt_findcomp(),
102 * which makes sure no text gets added to it when the message is processed.
103 *
104 * getcpy(NULL) returns a malloc'd zero-length string, so it can safely
105 * be free()'d later.
106 */
107 if (!ccto) {
108 cptr = fmt_findcomp ("to");
109 if (cptr)
110 cptr->c_name = getcpy(NULL);
111 }
112 if (!cccc) {
113 cptr = fmt_findcomp("cc");
114 if (cptr)
115 cptr->c_name = getcpy(NULL);
116 }
117 /* set up the "fcc" pseudo-component */
118 if (fcc) {
119 cptr = fmt_findcomp ("fcc");
120 if (cptr)
121 cptr->c_text = getcpy (fcc);
122 }
123 if ((cp = getenv("USER"))) {
124 cptr = fmt_findcomp ("user");
125 if (cptr)
126 cptr->c_text = getcpy(cp);
127 }
128 if (!ccme)
129 ismymbox (NULL);
130
131 /*
132 * pick any interesting stuff out of msg "inb"
133 */
134 for (state = FLD;;) {
135 state = m_getfld (state, name, tmpbuf, sizeof(tmpbuf), inb);
136 switch (state) {
137 case FLD:
138 case FLDPLUS:
139 /*
140 * if we're interested in this component, save a pointer
141 * to the component text, then start using our next free
142 * buffer as the component temp buffer (buffer switching
143 * saves an extra copy of the component text).
144 */
145
146 i = fmt_addcomptext(name, tmpbuf);
147 if (i != -1) {
148 char_read += msg_count;
149 while (state == FLDPLUS) {
150 state = m_getfld(state, name, tmpbuf,
151 sizeof(tmpbuf), inb);
152 fmt_appendcomp(i, name, tmpbuf);
153 char_read += msg_count;
154 }
155 }
156
157 while (state == FLDPLUS)
158 state = m_getfld (state, name, tmpbuf, SBUFSIZ, inb);
159 break;
160
161 case LENERR:
162 case FMTERR:
163 case BODY:
164 case FILEEOF:
165 goto finished;
166
167 default:
168 adios (NULL, "m_getfld() returned %d", state);
169 }
170 }
171
172 /*
173 * format and output the header lines.
174 */
175 finished:
176
177 /*
178 * if there's a "Subject" component, strip any "Re:"s off it
179 */
180 cptr = fmt_findcomp ("subject");
181 if (cptr && (cp = cptr->c_text)) {
182 register char *sp = cp;
183
184 for (;;) {
185 while (isspace(*cp))
186 cp++;
187 if(uprf(cp, "re:"))
188 cp += 3;
189 else
190 break;
191 sp = cp;
192 }
193 if (sp != cptr->c_text) {
194 cp = cptr->c_text;
195 cptr->c_text = getcpy (sp);
196 free (cp);
197 }
198 }
199 i = format_len + char_read + 256;
200 scanl = mh_xmalloc ((size_t) i + 2);
201 dat[0] = 0;
202 dat[1] = 0;
203 dat[2] = 0;
204 dat[3] = outputlinelen;
205 dat[4] = 0;
206 fmt_scan (fmt, scanl, i + 1, i, dat);
207 fputs (scanl, out);
208 if (badaddrs) {
209 fputs ("\nrepl: bad addresses:\n", out);
210 fputs ( badaddrs, out);
211 }
212
213 /*
214 * Check if we should filter the message
215 * or add mhn directives
216 */
217 if (filter) {
218 fflush(out);
219 if (ferror (out))
220 adios (drft, "error writing");
221
222 replfilter (inb, out, filter, fmtproc);
223 } else if (mime && mp) {
224 fprintf (out, "#forw [original message] +%s %s\n",
225 mp->foldpath, m_name (mp->lowsel));
226 }
227
228 fflush(out);
229 if (ferror (out))
230 adios (drft, "error writing");
231 fclose (out);
232
233 /* return dynamically allocated buffers */
234 free (scanl);
235 fmt_free(fmt, 1);
236 }
237
238 static char *buf; /* our current working buffer */
239 static char *bufend; /* end of working buffer */
240 static char *last_dst; /* buf ptr at end of last call */
241 static unsigned int bufsiz=0; /* current size of buf */
242
243 #define BUFINCR 512 /* how much to expand buf when if fills */
244
245 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
246
247 /*
248 * check if there's enough room in buf for str.
249 * add more mem if needed
250 */
251 #define CHECKMEM(str) \
252 if ((len = strlen (str)) >= bufend - dst) {\
253 int i = dst - buf;\
254 int n = last_dst - buf;\
255 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
256 buf = mh_xrealloc (buf, bufsiz);\
257 dst = buf + i;\
258 last_dst = buf + n;\
259 bufend = buf + bufsiz;\
260 }
261
262
263 /*
264 * fmt_scan will call this routine if the user includes the function
265 * "(formataddr {component})" in a format string. "orig" is the
266 * original contents of the string register. "str" is the address
267 * string to be formatted and concatenated onto orig. This routine
268 * returns a pointer to the concatenated address string.
269 *
270 * We try to not do a lot of malloc/copy/free's (which is why we
271 * don't call "getcpy") but still place no upper limit on the
272 * length of the result string.
273 */
274 char *
275 formataddr (char *orig, char *str)
276 {
277 register int len;
278 char baddr[BUFSIZ], error[BUFSIZ];
279 register int isgroup;
280 register char *dst;
281 register char *cp;
282 register char *sp;
283 register struct mailname *mp = NULL;
284
285 /* if we don't have a buffer yet, get one */
286 if (bufsiz == 0) {
287 buf = mh_xmalloc (BUFINCR);
288 last_dst = buf; /* XXX */
289 bufsiz = BUFINCR - 6; /* leave some slop */
290 bufend = buf + bufsiz;
291 }
292 /*
293 * If "orig" points to our buffer we can just pick up where we
294 * left off. Otherwise we have to copy orig into our buffer.
295 */
296 if (orig == buf)
297 dst = last_dst;
298 else if (!orig || !*orig) {
299 dst = buf;
300 *dst = '\0';
301 } else {
302 dst = last_dst; /* XXX */
303 CHECKMEM (orig);
304 CPY (orig);
305 }
306
307 /* concatenate all the new addresses onto 'buf' */
308 for (isgroup = 0; (cp = getname (str)); ) {
309 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
310 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
311 badaddrs = add (baddr, badaddrs);
312 continue;
313 }
314 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
315 *dst++ = ';';
316 isgroup = 0;
317 }
318 if (insert (mp)) {
319 /* if we get here we're going to add an address */
320 if (dst != buf) {
321 *dst++ = ',';
322 *dst++ = ' ';
323 }
324 if (mp->m_gname) {
325 CHECKMEM (mp->m_gname);
326 CPY (mp->m_gname);
327 isgroup++;
328 }
329 sp = adrformat (mp);
330 CHECKMEM (sp);
331 CPY (sp);
332 }
333 }
334
335 if (isgroup)
336 *dst++ = ';';
337
338 *dst = '\0';
339 last_dst = dst;
340 return (buf);
341 }
342
343
344 /*
345 * fmt_scan will call this routine if the user includes the function
346 * "(concataddr {component})" in a format string. This behaves exactly
347 * like formataddr, except that it does NOT suppress duplicate addresses
348 * between calls.
349 *
350 * As an implementation detail: I thought about splitting out formataddr()
351 * into the generic part and duplicate-suppressing part, but the call to
352 * insert() was buried deep within a couple of loops and I didn't see a
353 * way to do it easily. So instead we simply set a special flag to stop
354 * the duplicate check and call formataddr().
355 */
356 char *
357 concataddr(char *orig, char *str)
358 {
359 char *cp;
360
361 nodupcheck = 1;
362 cp = formataddr(orig, str);
363 nodupcheck = 0;
364 return cp;
365 }
366
367 static int
368 insert (struct mailname *np)
369 {
370 char buffer[BUFSIZ];
371 register struct mailname *mp;
372
373 if (nodupcheck)
374 return 1;
375
376 if (np->m_mbox == NULL)
377 return 0;
378
379 for (mp = &mq; mp->m_next; mp = mp->m_next) {
380 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
381 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox))
382 return 0;
383 }
384 if (!ccme && ismymbox (np))
385 return 0;
386
387 if (querysw) {
388 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
389 if (!gans (buffer, anoyes))
390 return 0;
391 }
392 mp->m_next = np;
393
394 return 1;
395 }
396
397
398 /*
399 * Call the mhlproc
400 *
401 * This function expects that argument out has been fflushed by the caller.
402 */
403
404 static void
405 replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
406 {
407 int pid;
408 char *mhl;
409 char *errstr;
410 char *arglist[7];
411
412 if (filter == NULL)
413 return;
414
415 if (access (filter, R_OK) == NOTOK)
416 adios (filter, "unable to read");
417
418 mhl = r1bindex (mhlproc, '/');
419
420 rewind (in);
421 lseek (fileno(in), (off_t) 0, SEEK_SET);
422
423 switch (pid = vfork()) {
424 case NOTOK:
425 adios ("fork", "unable to");
426
427 case OK:
428 dup2 (fileno (in), fileno (stdin));
429 dup2 (fileno (out), fileno (stdout));
430 closefds (3);
431
432 arglist[0] = mhl;
433 arglist[1] = "-form";
434 arglist[2] = filter;
435 arglist[3] = "-noclear";
436
437 switch (fmtproc) {
438 case 1:
439 arglist[4] = "-fmtproc";
440 arglist[5] = formatproc;
441 arglist[6] = NULL;
442 break;
443 case 0:
444 arglist[4] = "-nofmtproc";
445 arglist[5] = NULL;
446 break;
447 default:
448 arglist[4] = NULL;
449 }
450
451 execvp (mhlproc, arglist);
452 errstr = strerror(errno);
453 write(2, "unable to exec ", 15);
454 write(2, mhlproc, strlen(mhlproc));
455 write(2, ": ", 2);
456 write(2, errstr, strlen(errstr));
457 write(2, "\n", 1);
458 _exit (-1);
459
460 default:
461 if (pidXwait (pid, mhl))
462 done (1);
463 fseek (out, 0L, SEEK_END);
464 break;
465 }
466 }