]> diplodocus.org Git - nmh/blob - uip/forwsbr.c
Use existing macros min() and max() more.
[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
14 /*
15 * Take from replsbr.c - a buffer big enough to read in data header lines
16 * in reasonable chunks but not enough to slurp in the whole message
17 */
18
19 static char msgbuf[NMH_BUFSIZ];
20 #define COMPFREE(c) mh_xfree(c->c_text)
21
22 /*
23 * A list of components we treat as addresses
24 */
25
26 static char *addrcomps[] = {
27 "from",
28 "sender",
29 "reply-to",
30 "to",
31 "cc",
32 "bcc",
33 "resent-from",
34 "resent-sender",
35 "resent-reply-to",
36 "resent-to",
37 "resent-cc",
38 "resent-bcc",
39 NULL
40 };
41
42 int
43 build_form (char *form, char *digest, int *dat, char *from, char *to,
44 char *cc, char *fcc, char *subject, char *inputfile)
45 {
46 int in;
47 int fmtsize, state;
48 int i;
49 char *nfs;
50 char tmpfil[BUFSIZ], name[NAMESZ], **ap;
51 charstring_t line;
52 FILE *tmp;
53 struct comp *cptr;
54 struct format *fmt;
55 char *cp = NULL;
56 m_getfld_state_t gstate = 0;
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 (;;) {
90 int msg_count = sizeof msgbuf;
91 state = m_getfld (&gstate, 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 while (state == FLDPLUS) {
104 msg_count = sizeof msgbuf;
105 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
106 fmt_appendcomp(i, name, msgbuf);
107 }
108 }
109 while (state == FLDPLUS) {
110 msg_count = sizeof msgbuf;
111 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
112 }
113 break;
114
115 case LENERR:
116 case FMTERR:
117 case BODY:
118 case FILEEOF:
119 goto finished;
120
121 default:
122 adios(NULL, "m_getfld() returned %d", state);
123 }
124 }
125
126 /*
127 * Override any components just in case they were included in the
128 * input message. Also include command-line components given here
129 *
130 * With the memory rework I've changed things so we always get copies
131 * of these strings; I don't like the idea that the caller of this
132 * function has to know to pass in already-allocated memory (and that
133 * it will be free()'d by us).
134 */
135
136 finished:
137 m_getfld_state_destroy (&gstate);
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) {
177 adios(NULL, "unable to create temporary file in %s", get_temp_dir());
178 }
179 strncpy (tmpfil, cp, sizeof(tmpfil));
180 (void) m_unlink (tmpfil);
181 if ((in = dup (fileno (tmp))) == NOTOK)
182 adios ("dup", "unable to");
183
184 line = charstring_create (fmtsize);
185 fmt_scan (fmt, line, fmtsize, dat, NULL);
186 fputs (charstring_buffer (line), tmp);
187 charstring_free (line);
188 if (fclose (tmp))
189 adios (tmpfil, "error writing");
190
191 lseek (in, (off_t) 0, SEEK_SET);
192
193 /*
194 * Free any component buffers that we allocated
195 */
196
197 fmt_free(fmt, 1);
198
199 return in;
200 }