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