]> diplodocus.org Git - nmh/blob - uip/forwsbr.c
Removed temporary probes added in commit
[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 *line, tmpfil[BUFSIZ], name[NAMESZ], **ap;
53 FILE *tmp;
54 register struct comp *cptr;
55 struct format *fmt;
56 char *cp = NULL;
57 m_getfld_state_t gstate = 0;
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 for (;;) {
91 int msg_count = sizeof msgbuf;
92 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
93 switch (state) {
94 case FLD:
95 case FLDPLUS:
96 /*
97 * If we find a component that we're interested in, save
98 * a copy. We don't do all of that weird buffer switching
99 * that replout does.
100 */
101
102 i = fmt_addcomptext(name, msgbuf);
103 if (i != -1) {
104 while (state == FLDPLUS) {
105 msg_count = sizeof msgbuf;
106 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
107 fmt_appendcomp(i, name, msgbuf);
108 }
109 }
110 while (state == FLDPLUS) {
111 msg_count = sizeof msgbuf;
112 state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
113 }
114 break;
115
116 case LENERR:
117 case FMTERR:
118 case BODY:
119 case FILEEOF:
120 goto finished;
121
122 default:
123 adios(NULL, "m_getfld() returned %d", state);
124 }
125 }
126
127 /*
128 * Override any components just in case they were included in the
129 * input message. Also include command-line components given here
130 *
131 * With the memory rework I've changed things so we always get copies
132 * of these strings; I don't like the idea that the caller of this
133 * function has to know to pass in already-allocated memory (and that
134 * it will be free()'d by us).
135 */
136
137 finished:
138 m_getfld_state_destroy (&gstate);
139
140 cptr = fmt_findcomp ("digest");
141 if (cptr) {
142 COMPFREE(cptr);
143 cptr->c_text = getcpy(digest);
144 }
145 cptr = fmt_findcomp ("nmh-date");
146 if (cptr) {
147 COMPFREE(cptr);
148 cptr->c_text = getcpy(dtimenow (0));
149 }
150 cptr = fmt_findcomp ("nmh-from");
151 if (cptr) {
152 COMPFREE(cptr);
153 cptr->c_text = getcpy(from);
154 }
155 cptr = fmt_findcomp ("nmh-to");
156 if (cptr) {
157 COMPFREE(cptr);
158 cptr->c_text = getcpy(to);
159 }
160 cptr = fmt_findcomp ("nmh-cc");
161 if (cptr) {
162 COMPFREE(cptr);
163 cptr->c_text = getcpy(cc);
164 }
165 cptr = fmt_findcomp ("nmh-subject");
166 if (cptr) {
167 COMPFREE(cptr);
168 cptr->c_text = getcpy(subject);
169 }
170 cptr = fmt_findcomp ("fcc");
171 if (cptr) {
172 COMPFREE(cptr);
173 cptr->c_text = getcpy(fcc);
174 }
175
176 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
177 if (cp == NULL) adios("forw", "unable to create temporary file");
178 strncpy (tmpfil, cp, sizeof(tmpfil));
179 unlink (tmpfil);
180 if ((in = dup (fileno (tmp))) == NOTOK)
181 adios ("dup", "unable to");
182
183 line = mh_xmalloc ((unsigned) fmtsize);
184 fmt_scan (fmt, line, fmtsize - 1, fmtsize, dat, NULL);
185 fputs (line, tmp);
186 free (line);
187 if (fclose (tmp))
188 adios (tmpfil, "error writing");
189
190 lseek (in, (off_t) 0, SEEK_SET);
191
192 /*
193 * Free any component buffers that we allocated
194 */
195
196 fmt_free(fmt, 1);
197
198 return in;
199 }