]>
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.
9 #include "sbr/getcpy.h"
10 #include "sbr/error.h"
12 #include "h/fmt_scan.h"
15 #include "sbr/m_mktemp.h"
19 * Take from replsbr.c - a buffer big enough to read in data header lines
20 * in reasonable chunks but not enough to slurp in the whole message
23 static char msgbuf
[NMH_BUFSIZ
];
24 #define COMPFREE(c) free(c->c_text)
27 * A list of components we treat as addresses
30 static char *addrcomps
[] = {
47 build_form (char *form
, char *digest
, int *dat
, char *from
, char *to
,
48 char *cc
, char *fcc
, char *subject
, char *inputfile
)
54 char tmpfil
[BUFSIZ
], name
[NAMESZ
], **ap
;
60 m_getfld_state_t gstate
;
63 * Open the message we'll be scanning for components
66 if ((tmp
= fopen(inputfile
, "r")) == NULL
)
67 adios (inputfile
, "Unable to open");
69 /* Get new format string */
70 nfs
= new_fs (form
, NULL
, NULL
);
71 fmtsize
= strlen (nfs
) + 256;
73 /* Compile format string */
74 (void) fmt_compile (nfs
, &fmt
, 1);
77 * Mark any components tagged as address components
80 for (ap
= addrcomps
; *ap
; ap
++) {
81 cptr
= fmt_findcomp (*ap
);
83 cptr
->c_type
|= CT_ADDR
;
87 * Process our message and save all relevant components
89 * A lot of this is taken from replsbr.c; should we try to merge
93 gstate
= m_getfld_state_init(tmp
);
95 int msg_count
= sizeof msgbuf
;
96 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
101 * If we find a component that we're interested in, save
102 * a copy. We don't do all of that weird buffer switching
106 i
= fmt_addcomptext(name
, msgbuf
);
108 while (state
== FLDPLUS
) {
109 msg_count
= sizeof msgbuf
;
110 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
111 fmt_appendcomp(i
, name
, msgbuf
);
114 while (state
== FLDPLUS
) {
115 msg_count
= sizeof msgbuf
;
116 state
= m_getfld2(&gstate
, name
, msgbuf
, &msg_count
);
127 die("m_getfld2() returned %d", state
);
132 * Override any components just in case they were included in the
133 * input message. Also include command-line components given here
135 * With the memory rework I've changed things so we always get copies
136 * of these strings; I don't like the idea that the caller of this
137 * function has to know to pass in already-allocated memory (and that
138 * it will be free()'d by us).
142 m_getfld_state_destroy (&gstate
);
144 cptr
= fmt_findcomp ("digest");
147 cptr
->c_text
= getcpy(digest
);
149 cptr
= fmt_findcomp ("nmh-date");
152 cptr
->c_text
= getcpy(dtimenow (0));
154 cptr
= fmt_findcomp ("nmh-from");
157 cptr
->c_text
= getcpy(from
);
159 cptr
= fmt_findcomp ("nmh-to");
162 cptr
->c_text
= getcpy(to
);
164 cptr
= fmt_findcomp ("nmh-cc");
167 cptr
->c_text
= getcpy(cc
);
169 cptr
= fmt_findcomp ("nmh-subject");
172 cptr
->c_text
= getcpy(subject
);
174 cptr
= fmt_findcomp ("fcc");
177 cptr
->c_text
= getcpy(fcc
);
180 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &tmp
);
182 die("unable to create temporary file in %s", get_temp_dir());
184 strncpy (tmpfil
, cp
, sizeof(tmpfil
));
185 (void) m_unlink (tmpfil
);
186 if ((in
= dup (fileno (tmp
))) == NOTOK
)
187 adios ("dup", "unable to");
189 line
= charstring_create (fmtsize
);
190 fmt_scan (fmt
, line
, fmtsize
, dat
, NULL
);
191 fputs (charstring_buffer (line
), tmp
);
192 charstring_free (line
);
194 adios (tmpfil
, "error writing");
196 lseek(in
, 0, SEEK_SET
);
199 * Free any component buffers that we allocated