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