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