]> diplodocus.org Git - nmh/blob - uip/replsbr.c
Document argsplit changes in mh-profile man page.
[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, *cp;
73 static int dat[5]; /* aux. data for format routine */
74 m_getfld_state_t gstate = 0;
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 (;;) {
135 int msg_count = sizeof tmpbuf;
136 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
137 switch (state) {
138 case FLD:
139 case FLDPLUS:
140 /*
141 * if we're interested in this component, save a pointer
142 * to the component text, then start using our next free
143 * buffer as the component temp buffer (buffer switching
144 * saves an extra copy of the component text).
145 */
146
147 i = fmt_addcomptext(name, tmpbuf);
148 if (i != -1) {
149 char_read += msg_count;
150 while (state == FLDPLUS) {
151 msg_count= sizeof tmpbuf;
152 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
153 fmt_appendcomp(i, name, tmpbuf);
154 char_read += msg_count;
155 }
156 }
157
158 while (state == FLDPLUS) {
159 msg_count= sizeof tmpbuf;
160 state = m_getfld (&gstate, name, tmpbuf, &msg_count, inb);
161 }
162 break;
163
164 case LENERR:
165 case FMTERR:
166 case BODY:
167 case FILEEOF:
168 goto finished;
169
170 default:
171 adios (NULL, "m_getfld() returned %d", state);
172 }
173 }
174
175 /*
176 * format and output the header lines.
177 */
178 finished:
179 m_getfld_state_destroy (&gstate);
180
181 /*
182 * if there's a "Subject" component, strip any "Re:"s off it
183 */
184 cptr = fmt_findcomp ("subject");
185 if (cptr && (cp = cptr->c_text)) {
186 register char *sp = cp;
187
188 for (;;) {
189 while (isspace((unsigned char) *cp))
190 cp++;
191 if(uprf(cp, "re:"))
192 cp += 3;
193 else
194 break;
195 sp = cp;
196 }
197 if (sp != cptr->c_text) {
198 cp = cptr->c_text;
199 cptr->c_text = getcpy (sp);
200 free (cp);
201 }
202 }
203 i = format_len + char_read + 256;
204 scanl = mh_xmalloc ((size_t) i + 2);
205 dat[0] = 0;
206 dat[1] = 0;
207 dat[2] = 0;
208 dat[3] = outputlinelen;
209 dat[4] = 0;
210 fmt_scan (fmt, scanl, i + 1, i, dat);
211 fputs (scanl, out);
212 if (badaddrs) {
213 fputs ("\nrepl: bad addresses:\n", out);
214 fputs ( badaddrs, out);
215 }
216
217 /*
218 * Check if we should filter the message
219 * or add mhn directives
220 */
221 if (filter) {
222 fflush(out);
223 if (ferror (out))
224 adios (drft, "error writing");
225
226 replfilter (inb, out, filter, fmtproc);
227 } else if (mime && mp) {
228 fprintf (out, "#forw [original message] +%s %s\n",
229 mp->foldpath, m_name (mp->lowsel));
230 }
231
232 fflush(out);
233 if (ferror (out))
234 adios (drft, "error writing");
235 fclose (out);
236
237 /* return dynamically allocated buffers */
238 free (scanl);
239 fmt_free(fmt, 1);
240 }
241
242 static char *buf; /* our current working buffer */
243 static char *bufend; /* end of working buffer */
244 static char *last_dst; /* buf ptr at end of last call */
245 static unsigned int bufsiz=0; /* current size of buf */
246
247 #define BUFINCR 512 /* how much to expand buf when if fills */
248
249 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
250
251 /*
252 * check if there's enough room in buf for str.
253 * add more mem if needed
254 */
255 #define CHECKMEM(str) \
256 if ((len = strlen (str)) >= bufend - dst) {\
257 int i = dst - buf;\
258 int n = last_dst - buf;\
259 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
260 buf = mh_xrealloc (buf, bufsiz);\
261 dst = buf + i;\
262 last_dst = buf + n;\
263 bufend = buf + bufsiz;\
264 }
265
266
267 /*
268 * fmt_scan will call this routine if the user includes the function
269 * "(formataddr {component})" in a format string. "orig" is the
270 * original contents of the string register. "str" is the address
271 * string to be formatted and concatenated onto orig. This routine
272 * returns a pointer to the concatenated address string.
273 *
274 * We try to not do a lot of malloc/copy/free's (which is why we
275 * don't call "getcpy") but still place no upper limit on the
276 * length of the result string.
277 */
278 char *
279 formataddr (char *orig, char *str)
280 {
281 register int len;
282 char baddr[BUFSIZ], error[BUFSIZ];
283 register int isgroup;
284 register char *dst;
285 register char *cp;
286 register char *sp;
287 register struct mailname *mp = NULL;
288
289 /* if we don't have a buffer yet, get one */
290 if (bufsiz == 0) {
291 buf = mh_xmalloc (BUFINCR);
292 last_dst = buf; /* XXX */
293 bufsiz = BUFINCR - 6; /* leave some slop */
294 bufend = buf + bufsiz;
295 }
296 /*
297 * If "orig" points to our buffer we can just pick up where we
298 * left off. Otherwise we have to copy orig into our buffer.
299 */
300 if (orig == buf)
301 dst = last_dst;
302 else if (!orig || !*orig) {
303 dst = buf;
304 *dst = '\0';
305 } else {
306 dst = last_dst; /* XXX */
307 CHECKMEM (orig);
308 CPY (orig);
309 }
310
311 /* concatenate all the new addresses onto 'buf' */
312 for (isgroup = 0; (cp = getname (str)); ) {
313 if ((mp = getm (cp, dfhost, dftype, AD_NAME, error)) == NULL) {
314 snprintf (baddr, sizeof(baddr), "\t%s -- %s\n", cp, error);
315 badaddrs = add (baddr, badaddrs);
316 continue;
317 }
318 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
319 *dst++ = ';';
320 isgroup = 0;
321 }
322 if (insert (mp)) {
323 /* if we get here we're going to add an address */
324 if (dst != buf) {
325 *dst++ = ',';
326 *dst++ = ' ';
327 }
328 if (mp->m_gname) {
329 CHECKMEM (mp->m_gname);
330 CPY (mp->m_gname);
331 isgroup++;
332 }
333 sp = adrformat (mp);
334 CHECKMEM (sp);
335 CPY (sp);
336 }
337 }
338
339 if (isgroup)
340 *dst++ = ';';
341
342 *dst = '\0';
343 last_dst = dst;
344 return (buf);
345 }
346
347
348 /*
349 * fmt_scan will call this routine if the user includes the function
350 * "(concataddr {component})" in a format string. This behaves exactly
351 * like formataddr, except that it does NOT suppress duplicate addresses
352 * between calls.
353 *
354 * As an implementation detail: I thought about splitting out formataddr()
355 * into the generic part and duplicate-suppressing part, but the call to
356 * insert() was buried deep within a couple of loops and I didn't see a
357 * way to do it easily. So instead we simply set a special flag to stop
358 * the duplicate check and call formataddr().
359 */
360 char *
361 concataddr(char *orig, char *str)
362 {
363 char *cp;
364
365 nodupcheck = 1;
366 cp = formataddr(orig, str);
367 nodupcheck = 0;
368 return cp;
369 }
370
371 static int
372 insert (struct mailname *np)
373 {
374 char buffer[BUFSIZ];
375 register struct mailname *mp;
376
377 if (nodupcheck)
378 return 1;
379
380 if (np->m_mbox == NULL)
381 return 0;
382
383 for (mp = &mq; mp->m_next; mp = mp->m_next) {
384 if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
385 && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox))
386 return 0;
387 }
388 if (!ccme && ismymbox (np))
389 return 0;
390
391 if (querysw) {
392 snprintf (buffer, sizeof(buffer), "Reply to %s? ", adrformat (np));
393 if (!gans (buffer, anoyes))
394 return 0;
395 }
396 mp->m_next = np;
397
398 return 1;
399 }
400
401
402 /*
403 * Call the mhlproc
404 *
405 * This function expects that argument out has been fflushed by the caller.
406 */
407
408 static void
409 replfilter (FILE *in, FILE *out, char *filter, int fmtproc)
410 {
411 int pid;
412 char *mhl;
413 char *errstr;
414 char **arglist;
415 int argnum;
416
417 if (filter == NULL)
418 return;
419
420 if (access (filter, R_OK) == NOTOK)
421 adios (filter, "unable to read");
422
423 rewind (in);
424 lseek (fileno(in), (off_t) 0, SEEK_SET);
425
426 switch (pid = fork()) {
427 case NOTOK:
428 adios ("fork", "unable to");
429
430 case OK:
431 dup2 (fileno (in), fileno (stdin));
432 dup2 (fileno (out), fileno (stdout));
433 closefds (3);
434
435 /*
436 * We're not allocating the memory for the extra arguments,
437 * because we never call arglist_free(). But if we ever change
438 * that be sure to use getcpy() for the extra arguments.
439 */
440 arglist = argsplit(mhlproc, &mhl, &argnum);
441 arglist[argnum++] = "-form";
442 arglist[argnum++] = filter;
443 arglist[argnum++] = "-noclear";
444
445 switch (fmtproc) {
446 case 1:
447 arglist[argnum++] = "-fmtproc";
448 arglist[argnum++] = formatproc;
449 break;
450 case 0:
451 arglist[argnum++] = "-nofmtproc";
452 break;
453 }
454
455 arglist[argnum++] = NULL;
456
457 execvp (mhl, arglist);
458 errstr = strerror(errno);
459 write(2, "unable to exec ", 15);
460 write(2, mhlproc, strlen(mhlproc));
461 write(2, ": ", 2);
462 write(2, errstr, strlen(errstr));
463 write(2, "\n", 1);
464 _exit (-1);
465
466 default:
467 if (pidXwait (pid, mhl))
468 done (1);
469 fseek (out, 0L, SEEK_END);
470 break;
471 }
472 }