]> diplodocus.org Git - nmh/blob - uip/forwsbr.c
Fix this man page for the New World Order.
[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 int
25 build_form (char *form, char *digest, int *dat, char *from, char *to,
26 char *cc, char *fcc, char *subject, char *inputfile)
27 {
28 int in;
29 int fmtsize, state, char_read = 0;
30 unsigned i;
31 register char *nfs;
32 char *line, tmpfil[BUFSIZ], name[NAMESZ];
33 FILE *tmp;
34 register struct comp *cptr;
35 struct format *fmt;
36 char *cp = NULL;
37
38 /*
39 * Open the message we'll be scanning for components
40 */
41
42 if ((tmp = fopen(inputfile, "r")) == NULL)
43 adios (inputfile, "Unable to open");
44
45 /* Get new format string */
46 nfs = new_fs (form, NULL, NULL);
47 fmtsize = strlen (nfs) + 256;
48
49 /* Compile format string */
50 (void) fmt_compile (nfs, &fmt);
51
52 /*
53 * Process our message and save all relevant components
54 *
55 * A lot of this is taken from replsbr.c; should we try to merge
56 * these routines?
57 */
58
59 for (state = FLD;;) {
60 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
61 switch (state) {
62 case FLD:
63 case FLDPLUS:
64 /*
65 * If we find a component that we're interested in, save
66 * a copy. We don't do all of that weird buffer switching
67 * that replout does.
68 */
69 if ((cptr = wantcomp[CHASH(name)]))
70 do {
71 if (mh_strcasecmp(name, cptr->c_name) == 0) {
72 char_read += msg_count;
73 if (! cptr->c_text) {
74 i = strlen(cptr->c_text = strdup(msgbuf)) - 1;
75 if (cptr->c_text[i] == '\n')
76 cptr->c_text[i] = '\0';
77 } else {
78 i = strlen(cptr->c_text) - 1;
79 if (cptr->c_text[i] == '\n') {
80 if (cptr->c_type & CT_ADDR) {
81 cptr->c_text[i] = '\0';
82 cptr->c_text = add(",\n\t",
83 cptr->c_text);
84 } else {
85 cptr->c_text = add ("\t", cptr->c_text);
86 }
87 }
88 cptr->c_text = add(msgbuf, cptr->c_text);
89 }
90 while (state == FLDPLUS) {
91 state = m_getfld(state, name, msgbuf,
92 sizeof(msgbuf), tmp);
93 cptr->c_text = add(msgbuf, cptr->c_text);
94 char_read += msg_count;
95 }
96 break;
97 }
98 } while ((cptr = cptr->c_next));
99
100 while (state == FLDPLUS)
101 state = m_getfld(state, name, msgbuf, sizeof(msgbuf), tmp);
102 break;
103
104 case LENERR:
105 case FMTERR:
106 case BODY:
107 case FILEEOF:
108 goto finished;
109
110 default:
111 adios(NULL, "m_getfld() returned %d", state);
112 }
113 }
114
115 /*
116 * Override any components just in case they were included in the
117 * input message. Also include command-line components given here
118 */
119
120 finished:
121
122 FINDCOMP (cptr, "digest");
123 if (cptr) {
124 COMPFREE(cptr);
125 cptr->c_text = digest;
126 }
127 FINDCOMP (cptr, "nmh-date");
128 if (cptr) {
129 COMPFREE(cptr);
130 cptr->c_text = getcpy(dtimenow (0));
131 }
132 FINDCOMP (cptr, "nmh-from");
133 if (cptr) {
134 COMPFREE(cptr);
135 cptr->c_text = from;
136 }
137 FINDCOMP (cptr, "nmh-to");
138 if (cptr) {
139 COMPFREE(cptr);
140 cptr->c_text = to;
141 }
142 FINDCOMP (cptr, "nmh-cc");
143 if (cptr) {
144 COMPFREE(cptr);
145 cptr->c_text = cc;
146 }
147 FINDCOMP (cptr, "nmh-subject");
148 if (cptr) {
149 COMPFREE(cptr);
150 cptr->c_text = subject;
151 }
152 FINDCOMP (cptr, "fcc");
153 if (cptr) {
154 COMPFREE(cptr);
155 cptr->c_text = fcc;
156 }
157
158 cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
159 if (cp == NULL) adios("forw", "unable to create temporary file");
160 strncpy (tmpfil, cp, sizeof(tmpfil));
161 unlink (tmpfil);
162 if ((in = dup (fileno (tmp))) == NOTOK)
163 adios ("dup", "unable to");
164
165 line = mh_xmalloc ((unsigned) fmtsize);
166 fmt_scan (fmt, line, fmtsize, dat);
167 fputs (line, tmp);
168 free (line);
169 if (fclose (tmp))
170 adios (tmpfil, "error writing");
171
172 lseek (in, (off_t) 0, SEEK_SET);
173
174 /*
175 * Free any component buffers that we allocated
176 */
177
178 for (i = 0; i < (sizeof(wantcomp) / sizeof(struct comp)); i++)
179 for (cptr = wantcomp[i]; cptr != NULL; cptr = cptr->c_next)
180 COMPFREE(cptr);
181
182 return in;
183 }