source: trunk/third/nmh/uip/replsbr.c @ 14410

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