]> diplodocus.org Git - nmh/blob - uip/forwsbr.c
pending-release-notes: add mhshow's "-prefer", and mh-format's %(kibi/kilo)
[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;
50 int i;
51 register char *nfs;
52 char tmpfil[BUFSIZ], name[NAMESZ], **ap;
53 charstring_t line;
54 FILE *tmp;
55 register struct comp *cptr;
56 struct format *fmt;
57 char *cp = NULL;
58 m_getfld_state_t gstate = 0;
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 for (;;) {
92 int msg_count = sizeof msgbuf;
93 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
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_getfld (&gstate, name, msgbuf, &msg_count, tmp);
108 fmt_appendcomp(i, name, msgbuf);
109 }
110 }
111 while (state == FLDPLUS) {
112 msg_count = sizeof msgbuf;
113 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
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_getfld() 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, (off_t) 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 }