Finally, some support for mh-format when using forw(1). Still needs some
[mmh] / uip / forwsbr.c
1
2 /*
3  * forwsbr.c -- subroutine to build a draft from a component file
4  *
5  * This code is Copyright (c) 2012, 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 <fcntl.h>
12 #include <h/fmt_scan.h>
13 #include <h/tws.h>
14 #include <h/utils.h>
15
16 /*
17  * Take from replsbr.c - a buffer big enough to read in data header lines
18  * in reasonable chunks but not enough to slurp in the whole message
19  */
20
21 static char msgbuf[256];
22 #define COMPFREE(c) if (c->c_text) free(c->c_text)
23
24 int
25 build_form (char *form, char *digest, int *dat, char *from, char *to,
26             char *cc, char *fcc, char *subject, char *inputfile)
27 {
28     int in;
29     int i, fmtsize, ncomps, state, char_read = 0;
30     register char *nfs;
31     char *line, tmpfil[BUFSIZ], name[NAMESZ];
32     FILE *tmp;
33     register struct comp *cptr;
34     struct format *fmt;
35     char *cp = NULL;
36
37     /*
38      * Open the message we'll be scanning for components
39      */
40
41     if ((tmp = fopen(inputfile, "r")) == NULL)
42         adios (inputfile, "Unable to open");
43
44     /* Get new format string */
45     nfs = new_fs (form, NULL, NULL);
46     fmtsize = strlen (nfs) + 256;
47
48     /* Compile format string */
49     ncomps = fmt_compile (nfs, &fmt);
50
51     /*
52      * Process our message and save all relevant components
53      *
54      * A lot of this is taken from replsbr.c; should we try to merge
55      * these routines?
56      */
57
58     for (state = FLD;;) {
59         state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
60         switch (state) {
61             case FLD:
62             case FLDPLUS:
63                 /*
64                  * If we find a component that we're interested in, save
65                  * a copy.  We don't do all of that weird buffer switching
66                  * that replout does.
67                  */
68                 if ((cptr = wantcomp[CHASH(name)]))
69                     do {
70                         if (mh_strcasecmp(name, cptr->c_name) == 0) {
71                             char_read += msg_count;
72                             if (! cptr->c_text) {
73                                 i = strlen(cptr->c_text = strdup(msgbuf)) - 1;
74                                 if (cptr->c_text[i] == '\n')
75                                     cptr->c_text[i] = '\0';
76                             } else {
77                                 i = strlen(cptr->c_text) - 1;
78                                 if (cptr->c_text[i] == '\n') {
79                                     if (cptr->c_type & CT_ADDR) {
80                                         cptr->c_text[i] = '\0';
81                                         cptr->c_text = add(",\n\t",
82                                                                cptr->c_text);
83                                     } else {
84                                         cptr->c_text = add ("\t", cptr->c_text);
85                                     }
86                                 }
87                                 cptr->c_text = add(msgbuf, cptr->c_text);
88                             }
89                             while (state == FLDPLUS) {
90                                 state = m_getfld(state, name, msgbuf,
91                                                  sizeof(msgbuf), tmp);
92                                 cptr->c_text = add(msgbuf, cptr->c_text);
93                                 char_read += msg_count;
94                             }
95                             break;
96                         }
97                     } while ((cptr = cptr->c_next));
98
99                 while (state == FLDPLUS)
100                     state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
101                 break;
102
103             case LENERR:
104             case FMTERR:
105             case BODY:
106             case FILEEOF:
107                 goto finished;
108
109             default:
110                 adios(NULL, "m_getfld() returned %d", state);
111         }
112     }
113
114     /*
115      * Override any components just in case they were included in the
116      * input message.  Also include command-line components given here
117      */
118
119 finished:
120
121     FINDCOMP (cptr, "digest");
122     if (cptr) {
123         COMPFREE(cptr);
124         cptr->c_text = digest;
125     }
126     FINDCOMP (cptr, "nmh-date");
127     if (cptr) {
128         COMPFREE(cptr);
129         cptr->c_text = getcpy(dtimenow (0));
130     }
131     FINDCOMP (cptr, "nmh-from");
132     if (cptr) {
133         COMPFREE(cptr);
134         cptr->c_text = from;
135     }
136     FINDCOMP (cptr, "nmh-to");
137     if (cptr) {
138         COMPFREE(cptr);
139         cptr->c_text = to;
140     }
141     FINDCOMP (cptr, "nmh-cc");
142     if (cptr) {
143         COMPFREE(cptr);
144         cptr->c_text = cc;
145     }
146     FINDCOMP (cptr, "nmh-subject");
147     if (cptr) {
148         COMPFREE(cptr);
149         cptr->c_text = subject;
150     }
151     FINDCOMP (cptr, "fcc");
152     if (cptr) {
153         COMPFREE(cptr);
154         cptr->c_text = fcc;
155     }
156
157     cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
158     if (cp == NULL) adios("forw", "unable to create temporary file");
159     strncpy (tmpfil, cp, sizeof(tmpfil));
160     unlink (tmpfil);
161     if ((in = dup (fileno (tmp))) == NOTOK)
162         adios ("dup", "unable to");
163
164     line = mh_xmalloc ((unsigned) fmtsize);
165     fmt_scan (fmt, line, fmtsize, dat);
166     fputs (line, tmp);
167     free (line);
168     if (fclose (tmp))
169         adios (tmpfil, "error writing");
170
171     lseek (in, (off_t) 0, SEEK_SET);
172
173     /*
174      * Free any component buffers that we allocated
175      */
176
177     for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++)
178         for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next)
179             COMPFREE(cptr);
180
181     return in;
182 }