]>
diplodocus.org Git - nmh/blob - uip/forwsbr.c
1 /* forwsbr.c -- subroutine to build a draft from a component file
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.
9 #include "sbr/fmt_new.h"
10 #include "sbr/dtime.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getcpy.h"
13 #include "sbr/error.h"
15 #include "h/fmt_scan.h"
18 #include "sbr/m_mktemp.h"
22 * Take from replsbr.c - a buffer big enough to read in data header lines
23 * in reasonable chunks but not enough to slurp in the whole message
26 static char msgbuf
[NMH_BUFSIZ
];
27 #define COMPFREE(c) free(c->c_text)
30 * A list of components we treat as addresses
33 static char *addrcomps
[] = {
50 build_form (char *form
, char *digest
, int *dat
, char *from
, char *to
,
51 char *cc
, char *fcc
, char *subject
, char *inputfile
)
57 char tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
63 m_getfld_state_t gstate
;
66 * Open the message we'll be scanning for components
69 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
70 adios (inputfile
, "Unable to open");
72 /* Get new format string */
73 nfs
= new_fs (form
, NULL
, NULL
);
74 fmtsize
= strlen (nfs
) + 256;
76 /* Compile format string */
77 (void) fmt_compile (nfs
, &fmt
, 1);
80 * Mark any components tagged as address components
83 for (ap
= addrcomps
; *ap
; ap
++) {
84 cptr
= fmt_findcomp (*ap
);
86 cptr
->c_type
|= CT_ADDR
;
90 * Process our message and save all relevant components
92 * A lot of this is taken from replsbr.c; should we try to merge
96 gstate
= m_getfld_state_init(tmp
);
98 int msg_count
= sizeof msgbuf
;
99 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
104 * If we find a component that we're interested in, save
105 * a copy. We don't do all of that weird buffer switching
109 i
= fmt_addcomptext(name
, msgbuf
);
111 while (state
== FLDPLUS
) {
112 msg_count
= sizeof msgbuf
;
113 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
114 fmt_appendcomp(i
, name
, msgbuf
);
117 while (state
== FLDPLUS
) {
118 msg_count
= sizeof msgbuf
;
119 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
130 die("m_getfld2() returned %d", state
);
135 * Override any components just in case they were included in the
136 * input message. Also include command-line components given here
138 * With the memory rework I've changed things so we always get copies
139 * of these strings; I don't like the idea that the caller of this
140 * function has to know to pass in already-allocated memory (and that
141 * it will be free()'d by us).
145 m_getfld_state_destroy (&gstate
);
147 cptr
= fmt_findcomp ("digest");
150 cptr
->c_text
= getcpy(digest
);
152 cptr
= fmt_findcomp ("nmh-date");
155 cptr
->c_text
= getcpy(dtimenow (0));
157 cptr
= fmt_findcomp ("nmh-from");
160 cptr
->c_text
= getcpy(from
);
162 cptr
= fmt_findcomp ("nmh-to");
165 cptr
->c_text
= getcpy(to
);
167 cptr
= fmt_findcomp ("nmh-cc");
170 cptr
->c_text
= getcpy(cc
);
172 cptr
= fmt_findcomp ("nmh-subject");
175 cptr
->c_text
= getcpy(subject
);
177 cptr
= fmt_findcomp ("fcc");
180 cptr
->c_text
= getcpy(fcc
);
183 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
185 die("unable to create temporary file in %s", get_temp_dir());
187 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
188 (void) m_unlink (tmpfil
);
189 if ((in
= dup (fileno (tmp
))) == NOTOK
)
190 adios ("dup", "unable to");
192 line
= charstring_create (fmtsize
);
193 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
194 fputs (charstring_buffer (line
), tmp
);
195 charstring_free (line
);
197 adios (tmpfil
, "error writing");
199 lseek(in
, 0, SEEK_SET
);
202 * Free any component buffers that we allocated