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