File: forwsbr.c

package info (click to toggle)
nmh 1.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,204 kB
  • ctags: 3,851
  • sloc: ansic: 48,922; sh: 16,422; makefile: 559; perl: 509; lex: 402; awk: 74
file content (201 lines) | stat: -rw-r--r-- 4,687 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

/*
 * forwsbr.c -- subroutine to build a draft from a component file
 *
 * This code is Copyright (c) 2012, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include <h/mh.h>
#include <fcntl.h>
#include <h/fmt_scan.h>
#include <h/tws.h>
#include <h/utils.h>

/*
 * Take from replsbr.c - a buffer big enough to read in data header lines
 * in reasonable chunks but not enough to slurp in the whole message
 */

static char msgbuf[256];
#define COMPFREE(c) if (c->c_text) free(c->c_text)

/*
 * A list of components we treat as addresses
 */

static char *addrcomps[] = {
    "from",
    "sender",
    "reply-to",
    "to",
    "cc",
    "bcc",
    "resent-from",
    "resent-sender",
    "resent-reply-to",
    "resent-to",
    "resent-cc",
    "resent-bcc",
    NULL
};

int
build_form (char *form, char *digest, int *dat, char *from, char *to,
	    char *cc, char *fcc, char *subject, char *inputfile)
{
    int	in;
    int fmtsize, state;
    int i;
    register char *nfs;
    char *line, tmpfil[BUFSIZ], name[NAMESZ], **ap;
    FILE *tmp;
    register struct comp *cptr;
    struct format *fmt;
    char *cp = NULL;
    m_getfld_state_t gstate = 0;

    /*
     * Open the message we'll be scanning for components
     */

    if ((tmp = fopen(inputfile, "r")) == NULL)
    	adios (inputfile, "Unable to open");

    /* Get new format string */
    nfs = new_fs (form, NULL, NULL);
    fmtsize = strlen (nfs) + 256;

    /* Compile format string */
    (void) fmt_compile (nfs, &fmt, 1);

    /*
     * Mark any components tagged as address components
     */

    for (ap = addrcomps; *ap; ap++) {
	cptr = fmt_findcomp (*ap);
	if (cptr)
	    cptr->c_type |= CT_ADDR;
    }

    /*
     * Process our message and save all relevant components
     *
     * A lot of this is taken from replsbr.c; should we try to merge
     * these routines?
     */

    for (;;) {
	int msg_count = sizeof msgbuf;
	state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
	switch (state) {
	    case FLD:
	    case FLDPLUS:
	    	/*
		 * If we find a component that we're interested in, save
		 * a copy.  We don't do all of that weird buffer switching
		 * that replout does.
		 */

		i = fmt_addcomptext(name, msgbuf);
		if (i != -1) {
		    while (state == FLDPLUS) {
			msg_count = sizeof msgbuf;
			state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
			fmt_appendcomp(i, name, msgbuf);
		    }
		}
		while (state == FLDPLUS) {
		    msg_count = sizeof msgbuf;
		    state = m_getfld (&gstate, name, msgbuf, &msg_count, tmp);
		}
		break;

	    case LENERR:
	    case FMTERR:
	    case BODY:
	    case FILEEOF:
	    	goto finished;

	    default:
	    	adios(NULL, "m_getfld() returned %d", state);
	}
    }

    /*
     * Override any components just in case they were included in the
     * input message.  Also include command-line components given here
     *
     * With the memory rework I've changed things so we always get copies
     * of these strings; I don't like the idea that the caller of this
     * function has to know to pass in already-allocated memory (and that
     * it will be free()'d by us).
     */

finished:
    m_getfld_state_destroy (&gstate);

    cptr = fmt_findcomp ("digest");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(digest);
    }
    cptr = fmt_findcomp ("nmh-date");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(dtimenow (0));
    }
    cptr = fmt_findcomp ("nmh-from");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(from);
    }
    cptr = fmt_findcomp ("nmh-to");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(to);
    }
    cptr = fmt_findcomp ("nmh-cc");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(cc);
    }
    cptr = fmt_findcomp ("nmh-subject");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(subject);
    }
    cptr = fmt_findcomp ("fcc");
    if (cptr) {
    	COMPFREE(cptr);
	cptr->c_text = getcpy(fcc);
    }

    cp = m_mktemp2(NULL, invo_name, NULL, &tmp);
    if (cp == NULL) {
	adios(NULL, "unable to create temporary file in %s", get_temp_dir());
    }
    strncpy (tmpfil, cp, sizeof(tmpfil));
    (void) m_unlink (tmpfil);
    if ((in = dup (fileno (tmp))) == NOTOK)
	adios ("dup", "unable to");

    line = mh_xmalloc ((unsigned) fmtsize);
    fmt_scan (fmt, line, fmtsize - 1, fmtsize, dat, NULL);
    fputs (line, tmp);
    free (line);
    if (fclose (tmp))
	adios (tmpfil, "error writing");

    lseek (in, (off_t) 0, SEEK_SET);

    /*
     * Free any component buffers that we allocated
     */

    fmt_free(fmt, 1);

    return in;
}