]>
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.
10 #include <h/fmt_scan.h>
13 #include "../sbr/m_mktemp.h"
16 * Take from replsbr.c - a buffer big enough to read in data header lines
17 * in reasonable chunks but not enough to slurp in the whole message
20 static char msgbuf
[NMH_BUFSIZ
];
21 #define COMPFREE(c) mh_xfree(c->c_text)
24 * A list of components we treat as addresses
27 static char *addrcomps
[] = {
44 build_form (char *form
, char *digest
, int *dat
, char *from
, char *to
,
45 char *cc
, char *fcc
, char *subject
, char *inputfile
)
51 char tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
57 m_getfld_state_t gstate
= 0;
60 * Open the message we'll be scanning for components
63 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
64 adios (inputfile
, "Unable to open");
66 /* Get new format string */
67 nfs
= new_fs (form
, NULL
, NULL
);
68 fmtsize
= strlen (nfs
) + 256;
70 /* Compile format string */
71 (void) fmt_compile (nfs
, &fmt
, 1);
74 * Mark any components tagged as address components
77 for (ap
= addrcomps
; *ap
; ap
++) {
78 cptr
= fmt_findcomp (*ap
);
80 cptr
->c_type
|= CT_ADDR
;
84 * Process our message and save all relevant components
86 * A lot of this is taken from replsbr.c; should we try to merge
91 int msg_count
= sizeof msgbuf
;
92 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
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
102 i
= fmt_addcomptext(name
, msgbuf
);
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
);
110 while (state
== FLDPLUS
) {
111 msg_count
= sizeof msgbuf
;
112 state
= m_getfld (&gstate
, name
, msgbuf
, &msg_count
, tmp
);
123 adios(NULL
, "m_getfld() returned %d", state
);
128 * Override any components just in case they were included in the
129 * input message. Also include command-line components given here
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).
138 m_getfld_state_destroy (&gstate
);
140 cptr
= fmt_findcomp ("digest");
143 cptr
->c_text
= getcpy(digest
);
145 cptr
= fmt_findcomp ("nmh-date");
148 cptr
->c_text
= getcpy(dtimenow (0));
150 cptr
= fmt_findcomp ("nmh-from");
153 cptr
->c_text
= getcpy(from
);
155 cptr
= fmt_findcomp ("nmh-to");
158 cptr
->c_text
= getcpy(to
);
160 cptr
= fmt_findcomp ("nmh-cc");
163 cptr
->c_text
= getcpy(cc
);
165 cptr
= fmt_findcomp ("nmh-subject");
168 cptr
->c_text
= getcpy(subject
);
170 cptr
= fmt_findcomp ("fcc");
173 cptr
->c_text
= getcpy(fcc
);
176 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
178 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
180 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
181 (void) m_unlink (tmpfil
);
182 if ((in
= dup (fileno (tmp
))) == NOTOK
)
183 adios ("dup", "unable to");
185 line
= charstring_create (fmtsize
);
186 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
187 fputs (charstring_buffer (line
), tmp
);
188 charstring_free (line
);
190 adios (tmpfil
, "error writing");
192 lseek(in
, 0, SEEK_SET
);
195 * Free any component buffers that we allocated