]> diplodocus.org Git - nmh/blob - uip/forwsbr.c
With -messageid random, make the part after the @ more resemble a
[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, char_read = 0;
50 unsigned 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
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);
71
72 /*
73 * Mark any components tagged as address components
74 */
75
76 for (ap = addrcomps; *ap; ap++) {
77 FINDCOMP (cptr, *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 (state = FLD;;) {
90 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
91 switch (state) {
92 case FLD:
93 case FLDPLUS:
94 /*
95 * If we find a component that we're interested in, save
96 * a copy. We don't do all of that weird buffer switching
97 * that replout does.
98 */
99 if ((cptr = wantcomp[CHASH(name)]))
100 do {
101 if (mh_strcasecmp(name, cptr->c_name) == 0) {
102 char_read += msg_count;
103 if (! cptr->c_text) {
104 cptr->c_text = strdup(msgbuf);
105 } else {
106 i = strlen(cptr->c_text) - 1;
107 if (cptr->c_text[i] == '\n') {
108 if (cptr->c_type & CT_ADDR) {
109 cptr->c_text[i] = '\0';
110 cptr->c_text = add(",\n\t",
111 cptr->c_text);
112 } else {
113 cptr->c_text = add ("\t", cptr->c_text);
114 }
115 }
116 cptr->c_text = add(msgbuf, cptr->c_text);
117 }
118 while (state == FLDPLUS) {
119 state = m_getfld(state, name, msgbuf,
120 sizeof(msgbuf), tmp);
121 cptr->c_text = add(msgbuf, cptr->c_text);
122 char_read += msg_count;
123 }
124 break;
125 }
126 } while ((cptr = cptr->c_next));
127
128 while (state == FLDPLUS)
129 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
130 break;
131
132 case LENERR:
133 case FMTERR:
134 case BODY:
135 case FILEEOF:
136 goto finished;
137
138 default:
139 adios(NULL, "m_getfld() returned %d", state);
140 }
141 }
142
143 /*
144 * Override any components just in case they were included in the
145 * input message. Also include command-line components given here
146 */
147
148 finished:
149
150 FINDCOMP (cptr, "digest");
151 if (cptr) {
152 COMPFREE(cptr);
153 cptr->c_text = digest;
154 }
155 FINDCOMP (cptr, "nmh-date");
156 if (cptr) {
157 COMPFREE(cptr);
158 cptr->c_text = getcpy(dtimenow (0));
159 }
160 FINDCOMP (cptr, "nmh-from");
161 if (cptr) {
162 COMPFREE(cptr);
163 cptr->c_text = from;
164 }
165 FINDCOMP (cptr, "nmh-to");
166 if (cptr) {
167 COMPFREE(cptr);
168 cptr->c_text = to;
169 }
170 FINDCOMP (cptr, "nmh-cc");
171 if (cptr) {
172 COMPFREE(cptr);
173 cptr->c_text = cc;
174 }
175 FINDCOMP (cptr, "nmh-subject");
176 if (cptr) {
177 COMPFREE(cptr);
178 cptr->c_text = subject;
179 }
180 FINDCOMP (cptr, "fcc");
181 if (cptr) {
182 COMPFREE(cptr);
183 cptr->c_text = fcc;
184 }
185
186 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
187 if (cp == NULL) adios("forw", "unable to create temporary file");
188 strncpy (tmpfil, cp, sizeof(tmpfil));
189 unlink (tmpfil);
190 if ((in = dup (fileno (tmp))) == NOTOK)
191 adios ("dup", "unable to");
192
193 line = mh_xmalloc ((unsigned) fmtsize);
194 fmt_scan (fmt, line, fmtsize, dat);
195 fputs (line, tmp);
196 free (line);
197 if (fclose (tmp))
198 adios (tmpfil, "error writing");
199
200 lseek (in, (off_t) 0, SEEK_SET);
201
202 /*
203 * Free any component buffers that we allocated
204 */
205
206 for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++)
207 for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next)
208 COMPFREE(cptr);
209
210 return in;
211 }