]>
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 "sbr/m_getfld.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/error.h"
14 #include "h/fmt_scan.h"
17 #include "sbr/m_mktemp.h"
21 * Take from replsbr.c - a buffer big enough to read in data header lines
22 * in reasonable chunks but not enough to slurp in the whole message
25 static char msgbuf
[NMH_BUFSIZ
];
26 #define COMPFREE(c) free(c->c_text)
29 * A list of components we treat as addresses
32 static char *addrcomps
[] = {
49 build_form (char *form
, char *digest
, int *dat
, char *from
, char *to
,
50 char *cc
, char *fcc
, char *subject
, char *inputfile
)
56 char tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
62 m_getfld_state_t gstate
;
65 * Open the message we'll be scanning for components
68 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
69 adios (inputfile
, "Unable to open");
71 /* Get new format string */
72 nfs
= new_fs (form
, NULL
, NULL
);
73 fmtsize
= strlen (nfs
) + 256;
75 /* Compile format string */
76 (void) fmt_compile (nfs
, &fmt
, 1);
79 * Mark any components tagged as address components
82 for (ap
= addrcomps
; *ap
; ap
++) {
83 cptr
= fmt_findcomp (*ap
);
85 cptr
->c_type
|= CT_ADDR
;
89 * Process our message and save all relevant components
91 * A lot of this is taken from replsbr.c; should we try to merge
95 gstate
= m_getfld_state_init(tmp
);
97 int msg_count
= sizeof msgbuf
;
98 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
103 * If we find a component that we're interested in, save
104 * a copy. We don't do all of that weird buffer switching
108 i
= fmt_addcomptext(name
, msgbuf
);
110 while (state
== FLDPLUS
) {
111 msg_count
= sizeof msgbuf
;
112 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
113 fmt_appendcomp(i
, name
, msgbuf
);
116 while (state
== FLDPLUS
) {
117 msg_count
= sizeof msgbuf
;
118 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
129 die("m_getfld2() returned %d", state
);
134 * Override any components just in case they were included in the
135 * input message. Also include command-line components given here
137 * With the memory rework I've changed things so we always get copies
138 * of these strings; I don't like the idea that the caller of this
139 * function has to know to pass in already-allocated memory (and that
140 * it will be free()'d by us).
144 m_getfld_state_destroy (&gstate
);
146 cptr
= fmt_findcomp ("digest");
149 cptr
->c_text
= getcpy(digest
);
151 cptr
= fmt_findcomp ("nmh-date");
154 cptr
->c_text
= getcpy(dtimenow (0));
156 cptr
= fmt_findcomp ("nmh-from");
159 cptr
->c_text
= getcpy(from
);
161 cptr
= fmt_findcomp ("nmh-to");
164 cptr
->c_text
= getcpy(to
);
166 cptr
= fmt_findcomp ("nmh-cc");
169 cptr
->c_text
= getcpy(cc
);
171 cptr
= fmt_findcomp ("nmh-subject");
174 cptr
->c_text
= getcpy(subject
);
176 cptr
= fmt_findcomp ("fcc");
179 cptr
->c_text
= getcpy(fcc
);
182 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
184 die("unable to create temporary file in %s", get_temp_dir());
186 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
187 (void) m_unlink (tmpfil
);
188 if ((in
= dup (fileno (tmp
))) == NOTOK
)
189 adios ("dup", "unable to");
191 line
= charstring_create (fmtsize
);
192 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
193 fputs (charstring_buffer (line
), tmp
);
194 charstring_free (line
);
196 adios (tmpfil
, "error writing");
198 lseek(in
, 0, SEEK_SET
);
201 * Free any component buffers that we allocated