]>
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.
11 #include "h/fmt_scan.h"
14 #include "sbr/m_mktemp.h"
18 * Take from replsbr.c - a buffer big enough to read in data header lines
19 * in reasonable chunks but not enough to slurp in the whole message
22 static char msgbuf
[NMH_BUFSIZ
];
23 #define COMPFREE(c) free(c->c_text)
26 * A list of components we treat as addresses
29 static char *addrcomps
[] = {
46 build_form (char *form
, char *digest
, int *dat
, char *from
, char *to
,
47 char *cc
, char *fcc
, char *subject
, char *inputfile
)
53 char tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
59 m_getfld_state_t gstate
;
62 * Open the message we'll be scanning for components
65 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
66 adios (inputfile
, "Unable to open");
68 /* Get new format string */
69 nfs
= new_fs (form
, NULL
, NULL
);
70 fmtsize
= strlen (nfs
) + 256;
72 /* Compile format string */
73 (void) fmt_compile (nfs
, &fmt
, 1);
76 * Mark any components tagged as address components
79 for (ap
= addrcomps
; *ap
; ap
++) {
80 cptr
= fmt_findcomp (*ap
);
82 cptr
->c_type
|= CT_ADDR
;
86 * Process our message and save all relevant components
88 * A lot of this is taken from replsbr.c; should we try to merge
92 gstate
= m_getfld_state_init(tmp
);
94 int msg_count
= sizeof msgbuf
;
95 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
100 * If we find a component that we're interested in, save
101 * a copy. We don't do all of that weird buffer switching
105 i
= fmt_addcomptext(name
, msgbuf
);
107 while (state
== FLDPLUS
) {
108 msg_count
= sizeof msgbuf
;
109 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
110 fmt_appendcomp(i
, name
, msgbuf
);
113 while (state
== FLDPLUS
) {
114 msg_count
= sizeof msgbuf
;
115 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
126 die("m_getfld2() returned %d", state
);
131 * Override any components just in case they were included in the
132 * input message. Also include command-line components given here
134 * With the memory rework I've changed things so we always get copies
135 * of these strings; I don't like the idea that the caller of this
136 * function has to know to pass in already-allocated memory (and that
137 * it will be free()'d by us).
141 m_getfld_state_destroy (&gstate
);
143 cptr
= fmt_findcomp ("digest");
146 cptr
->c_text
= getcpy(digest
);
148 cptr
= fmt_findcomp ("nmh-date");
151 cptr
->c_text
= getcpy(dtimenow (0));
153 cptr
= fmt_findcomp ("nmh-from");
156 cptr
->c_text
= getcpy(from
);
158 cptr
= fmt_findcomp ("nmh-to");
161 cptr
->c_text
= getcpy(to
);
163 cptr
= fmt_findcomp ("nmh-cc");
166 cptr
->c_text
= getcpy(cc
);
168 cptr
= fmt_findcomp ("nmh-subject");
171 cptr
->c_text
= getcpy(subject
);
173 cptr
= fmt_findcomp ("fcc");
176 cptr
->c_text
= getcpy(fcc
);
179 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
181 die("unable to create temporary file in %s", get_temp_dir());
183 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
184 (void) m_unlink (tmpfil
);
185 if ((in
= dup (fileno (tmp
))) == NOTOK
)
186 adios ("dup", "unable to");
188 line
= charstring_create (fmtsize
);
189 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
190 fputs (charstring_buffer (line
), tmp
);
191 charstring_free (line
);
193 adios (tmpfil
, "error writing");
195 lseek(in
, 0, SEEK_SET
);
198 * Free any component buffers that we allocated