]> diplodocus.org Git - nmh/blob - uip/forwsbr.c
Started revising m_getfld() code to replace direct buffer
[nmh] / 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 /*
25 * A list of components we treat as addresses
26 */
27
28 static char *addrcomps[] = {
29 "from",
30 "sender",
31 "reply-to",
32 "to",
33 "cc",
34 "bcc",
35 "resent-from",
36 "resent-sender",
37 "resent-reply-to",
38 "resent-to",
39 "resent-cc",
40 "resent-bcc",
41 NULL
42 };
43
44 int
45 build_form (char *form, char *digest, int *dat, char *from, char *to,
46 char *cc, char *fcc, char *subject, char *inputfile)
47 {
48 int in;
49 int fmtsize, state, char_read = 0;
50 int i;
51 register char *nfs;
52 char *line, tmpfil[BUFSIZ], name[NAMESZ], **ap;
53 FILE *tmp;
54 register struct comp *cptr;
55 struct format *fmt;
56 char *cp = NULL;
57
58 /*
59 * Open the message we'll be scanning for components
60 */
61
62 if ((tmp = fopen(inputfile, "r")) == NULL)
63 adios (inputfile, "Unable to open");
64
65 /* Get new format string */
66 nfs = new_fs (form, NULL, NULL);
67 fmtsize = strlen (nfs) + 256;
68
69 /* Compile format string */
70 (void) fmt_compile (nfs, &fmt, 1);
71
72 /*
73 * Mark any components tagged as address components
74 */
75
76 for (ap = addrcomps; *ap; ap++) {
77 cptr = fmt_findcomp (*ap);
78 if (cptr)
79 cptr->c_type |= CT_ADDR;
80 }
81
82 /*
83 * Process our message and save all relevant components
84 *
85 * A lot of this is taken from replsbr.c; should we try to merge
86 * these routines?
87 */
88
89 for (state = FLD;;) {
90 int msg_count = sizeof msgbuf;
91 state = m_getfld(state, name, msgbuf, &msg_count, tmp);
92 switch (state) {
93 case FLD:
94 case FLDPLUS:
95 /*
96 * If we find a component that we're interested in, save
97 * a copy. We don't do all of that weird buffer switching
98 * that replout does.
99 */
100
101 i = fmt_addcomptext(name, msgbuf);
102 if (i != -1) {
103 char_read += msg_count;
104 while (state == FLDPLUS) {
105 msg_count = sizeof msgbuf;
106 state = m_getfld(state, name, msgbuf, &msg_count, tmp);
107 fmt_appendcomp(i, name, msgbuf);
108 char_read += msg_count;
109 }
110 }
111 while (state == FLDPLUS)
112 msg_count = sizeof msgbuf;
113 state = m_getfld(state, name, msgbuf, &msg_count, tmp);
114 break;
115
116 case LENERR:
117 case FMTERR:
118 case BODY:
119 case FILEEOF:
120 goto finished;
121
122 default:
123 adios(NULL, "m_getfld() returned %d", state);
124 }
125 }
126
127 /*
128 * Override any components just in case they were included in the
129 * input message. Also include command-line components given here
130 *
131 * With the memory rework I've changed things so we always get copies
132 * of these strings; I don't like the idea that the caller of this
133 * function has to know to pass in already-allocated memory (and that
134 * it will be free()'d by us).
135 */
136
137 finished:
138
139 cptr = fmt_findcomp ("digest");
140 if (cptr) {
141 COMPFREE(cptr);
142 cptr->c_text = getcpy(digest);
143 }
144 cptr = fmt_findcomp ("nmh-date");
145 if (cptr) {
146 COMPFREE(cptr);
147 cptr->c_text = getcpy(dtimenow (0));
148 }
149 cptr = fmt_findcomp ("nmh-from");
150 if (cptr) {
151 COMPFREE(cptr);
152 cptr->c_text = getcpy(from);
153 }
154 cptr = fmt_findcomp ("nmh-to");
155 if (cptr) {
156 COMPFREE(cptr);
157 cptr->c_text = getcpy(to);
158 }
159 cptr = fmt_findcomp ("nmh-cc");
160 if (cptr) {
161 COMPFREE(cptr);
162 cptr->c_text = getcpy(cc);
163 }
164 cptr = fmt_findcomp ("nmh-subject");
165 if (cptr) {
166 COMPFREE(cptr);
167 cptr->c_text = getcpy(subject);
168 }
169 cptr = fmt_findcomp ("fcc");
170 if (cptr) {
171 COMPFREE(cptr);
172 cptr->c_text = getcpy(fcc);
173 }
174
175 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
176 if (cp == NULL) adios("forw", "unable to create temporary file");
177 strncpy (tmpfil, cp, sizeof(tmpfil));
178 unlink (tmpfil);
179 if ((in = dup (fileno (tmp))) == NOTOK)
180 adios ("dup", "unable to");
181
182 line = mh_xmalloc ((unsigned) fmtsize);
183 fmt_scan (fmt, line, fmtsize - 1, fmtsize, dat);
184 fputs (line, tmp);
185 free (line);
186 if (fclose (tmp))
187 adios (tmpfil, "error writing");
188
189 lseek (in, (off_t) 0, SEEK_SET);
190
191 /*
192 * Free any component buffers that we allocated
193 */
194
195 fmt_free(fmt, 1);
196
197 return in;
198 }