]>
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) free(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
;
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
90 gstate
= m_getfld_state_init(tmp
);
92 int msg_count
= sizeof msgbuf
;
93 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
98 * If we find a component that we're interested in, save
99 * a copy. We don't do all of that weird buffer switching
103 i
= fmt_addcomptext(name
, msgbuf
);
105 while (state
== FLDPLUS
) {
106 msg_count
= sizeof msgbuf
;
107 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
108 fmt_appendcomp(i
, name
, msgbuf
);
111 while (state
== FLDPLUS
) {
112 msg_count
= sizeof msgbuf
;
113 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
124 adios(NULL
, "m_getfld2() returned %d", state
);
129 * Override any components just in case they were included in the
130 * input message. Also include command-line components given here
132 * With the memory rework I've changed things so we always get copies
133 * of these strings; I don't like the idea that the caller of this
134 * function has to know to pass in already-allocated memory (and that
135 * it will be free()'d by us).
139 m_getfld_state_destroy (&gstate
);
141 cptr
= fmt_findcomp ("digest");
144 cptr
->c_text
= getcpy(digest
);
146 cptr
= fmt_findcomp ("nmh-date");
149 cptr
->c_text
= getcpy(dtimenow (0));
151 cptr
= fmt_findcomp ("nmh-from");
154 cptr
->c_text
= getcpy(from
);
156 cptr
= fmt_findcomp ("nmh-to");
159 cptr
->c_text
= getcpy(to
);
161 cptr
= fmt_findcomp ("nmh-cc");
164 cptr
->c_text
= getcpy(cc
);
166 cptr
= fmt_findcomp ("nmh-subject");
169 cptr
->c_text
= getcpy(subject
);
171 cptr
= fmt_findcomp ("fcc");
174 cptr
->c_text
= getcpy(fcc
);
177 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
179 adios(NULL
, "unable to create temporary file in %s", get_temp_dir());
181 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
182 (void) m_unlink (tmpfil
);
183 if ((in
= dup (fileno (tmp
))) == NOTOK
)
184 adios ("dup", "unable to");
186 line
= charstring_create (fmtsize
);
187 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
188 fputs (charstring_buffer (line
), tmp
);
189 charstring_free (line
);
191 adios (tmpfil
, "error writing");
193 lseek(in
, 0, SEEK_SET
);
196 * Free any component buffers that we allocated