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