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