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