]>
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"
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
21 static char msgbuf
[NMH_BUFSIZ
];
22 #define COMPFREE(c) free(c->c_text)
25 * A list of components we treat as addresses
28 static char *addrcomps
[] = {
45 build_form (char *form
, char *digest
, int *dat
, char *from
, char *to
,
46 char *cc
, char *fcc
, char *subject
, char *inputfile
)
52 char tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
58 m_getfld_state_t gstate
;
61 * Open the message we'll be scanning for components
64 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
65 adios (inputfile
, "Unable to open");
67 /* Get new format string */
68 nfs
= new_fs (form
, NULL
, NULL
);
69 fmtsize
= strlen (nfs
) + 256;
71 /* Compile format string */
72 (void) fmt_compile (nfs
, &fmt
, 1);
75 * Mark any components tagged as address components
78 for (ap
= addrcomps
; *ap
; ap
++) {
79 cptr
= fmt_findcomp (*ap
);
81 cptr
->c_type
|= CT_ADDR
;
85 * Process our message and save all relevant components
87 * A lot of this is taken from replsbr.c; should we try to merge
91 gstate
= m_getfld_state_init(tmp
);
93 int msg_count
= sizeof msgbuf
;
94 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
99 * If we find a component that we're interested in, save
100 * a copy. We don't do all of that weird buffer switching
104 i
= fmt_addcomptext(name
, msgbuf
);
106 while (state
== FLDPLUS
) {
107 msg_count
= sizeof msgbuf
;
108 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
109 fmt_appendcomp(i
, name
, msgbuf
);
112 while (state
== FLDPLUS
) {
113 msg_count
= sizeof msgbuf
;
114 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
125 die("m_getfld2() returned %d", state
);
130 * Override any components just in case they were included in the
131 * input message. Also include command-line components given here
133 * With the memory rework I've changed things so we always get copies
134 * of these strings; I don't like the idea that the caller of this
135 * function has to know to pass in already-allocated memory (and that
136 * it will be free()'d by us).
140 m_getfld_state_destroy (&gstate
);
142 cptr
= fmt_findcomp ("digest");
145 cptr
->c_text
= getcpy(digest
);
147 cptr
= fmt_findcomp ("nmh-date");
150 cptr
->c_text
= getcpy(dtimenow (0));
152 cptr
= fmt_findcomp ("nmh-from");
155 cptr
->c_text
= getcpy(from
);
157 cptr
= fmt_findcomp ("nmh-to");
160 cptr
->c_text
= getcpy(to
);
162 cptr
= fmt_findcomp ("nmh-cc");
165 cptr
->c_text
= getcpy(cc
);
167 cptr
= fmt_findcomp ("nmh-subject");
170 cptr
->c_text
= getcpy(subject
);
172 cptr
= fmt_findcomp ("fcc");
175 cptr
->c_text
= getcpy(fcc
);
178 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
180 die("unable to create temporary file in %s", get_temp_dir());
182 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
183 (void) m_unlink (tmpfil
);
184 if ((in
= dup (fileno (tmp
))) == NOTOK
)
185 adios ("dup", "unable to");
187 line
= charstring_create (fmtsize
);
188 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
189 fputs (charstring_buffer (line
), tmp
);
190 charstring_free (line
);
192 adios (tmpfil
, "error writing");
194 lseek(in
, 0, SEEK_SET
);
197 * Free any component buffers that we allocated