File: mimeparser_test.c

package info (click to toggle)
libcitadel 917-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,808 kB
  • sloc: ansic: 15,131; sh: 8,304; makefile: 302
file content (273 lines) | stat: -rw-r--r-- 5,721 bytes parent folder | download | duplicates (6)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>

#include <fcntl.h>

#include <unistd.h>
#include <stddef.h>


#include "../lib/libcitadel.h"

/* shamelesly copied from msgbase.h */
struct ma_info {
	int is_ma;		/* Set to 1 if we are using this stuff */
	int freeze;		/* Freeze the replacement chain because we're
				 * digging through a subsection */
	int did_print;		/* One alternative has been displayed */
	char chosen_part[128];	/* Which part of a m/a did we choose? */
	const char *printme;
	int chosen_pref;	/* Chosen part preference level (lower is better) */
	int use_fo_hooks;	/* Use fixed output hooks */
	int dont_decode;        /* should we call the decoder or not? */
};


/*
 * Callback function for mime parser that simply lists the part
 */
static void list_this_part(char *name, 
			   char *filename, 
			   char *partnum, 
			   char *disp,
			   void *content, 
			   char *cbtype, 
			   char *cbcharset, 
			   size_t length, 
			   char *encoding,
			   char *cbid, 
			   void *cbuserdata)
{
	struct ma_info *ma;
	
	ma = (struct ma_info *)cbuserdata;
	if (ma->is_ma == 0) {
		printf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
			name, 
			filename, 
			partnum, 
			disp, 
			cbtype, 
			(long)length, 
			cbid, 
			cbcharset);
	}
}

/* 
 * Callback function for multipart prefix
 */
static void list_this_pref(char *name, 
			   char *filename, 
			   char *partnum, 
			   char *disp,
			   void *content, 
			   char *cbtype, 
			   char *cbcharset, 
			   size_t length, 
			   char *encoding,
			   char *cbid, 
			   void *cbuserdata)
{
	struct ma_info *ma;
	
	ma = (struct ma_info *)cbuserdata;
	if (!strcasecmp(cbtype, "multipart/alternative")) {
		++ma->is_ma;
	}

	if (ma->is_ma == 0) {
		printf("pref=%s|%s\n", partnum, cbtype);
	}
}

/* 
 * Callback function for multipart sufffix
 */
static void list_this_suff(char *name, 
			   char *filename, 
			   char *partnum, 
			   char *disp,
			   void *content, 
			   char *cbtype, 
			   char *cbcharset, 
			   size_t length, 
			   char *encoding,
			   char *cbid, 
			   void *cbuserdata)
{
	struct ma_info *ma;
	
	ma = (struct ma_info *)cbuserdata;
	if (ma->is_ma == 0) {
		printf("suff=%s|%s\n", partnum, cbtype);
	}
	if (!strcasecmp(cbtype, "multipart/alternative")) {
		--ma->is_ma;
	}
}


/*
 * Callback function for mime parser that opens a section for downloading
 */
static void mime_download(char *name, 
			  char *filename, 
			  char *partnum, 
			  char *disp,
			  void *content, 
			  char *cbtype, 
			  char *cbcharset, 
			  size_t length,
			  char *encoding, 
			  char *cbid, 
			  void *cbuserdata)
{
	int rc = 0;

	/* Silently go away if there's already a download open. */

	struct ma_info *ma;
	
	ma = (struct ma_info *)cbuserdata;

	if ((!IsEmptyStr(partnum) && (!strcasecmp(ma->printme, partnum)))) {
		char *decoded = NULL;
		size_t bytes_decoded;
		rc = mime_decode_now (content, 
				      length,
				      encoding,
				      &decoded,
				      &bytes_decoded);
		if (rc < 0) {
			printf("failed to decode content\n");
			return;
		}
		if (rc == 0){
			rc = write(STDOUT_FILENO, content, length);
		}
		else {
			rc = write(STDOUT_FILENO, decoded, bytes_decoded);
			free(decoded);
		}
	}
}



/*
 * Callback function for mime parser that outputs a section all at once.
 * We can specify the desired section by part number *or* content-id.
 * /
void mime_spew_section(char *name, 
		       char *filename, 
		       char *partnum, 
		       char *disp,
		       void *content, 
		       char *cbtype, 
		       char *cbcharset, 
		       size_t length,
		       char *encoding, 
		       char *cbid, 
		       void *cbuserdata)
{
	int *found_it = (int *)cbuserdata;

	if (
		(!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
	||	(!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
	) {
		*found_it = 1;
		printf("%d %d|-1|%s|%s|%s\n",
			BINARY_FOLLOWS,
			(int)length,
			filename,
			cbtype,
			cbcharset
		);
		fwrite(STDOUT, content, length);
	}
}

*/




int main(int argc, char* argv[])
{
	char a;
	int fd;
	char *filename = NULL;
	struct stat statbuf;
	const char *Err;

	StrBuf *MimeBuf;
	long MimeLen;
	char *MimeStr;
	struct ma_info ma;
	int do_proto = 0;
	int dont_decode = 1;

	setvbuf(stdout, NULL, _IONBF, 0);
	memset(&ma, 0, sizeof(struct ma_info));

	while ((a = getopt(argc, argv, "dpf:P:")) != EOF)
	{
		switch (a) {
		case 'f':
			filename = optarg;
			break;
		case 'p':
			do_proto = 1;
			break;
		case 'd':
			dont_decode = 0;
			break;
		case 'P':
			ma.printme = optarg;
		}
	}
	StartLibCitadel(8);

	if (filename == NULL) {
		printf("Filename requried! -f\n");
		return 1;
	}
	fd = open(filename, 0);
	if (fd < 0) {
		printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
		return 1;
	}
	if (fstat(fd, &statbuf) == -1) {
		printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
		return 1;
	}
	MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
	if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
		printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
		FreeStrBuf(&MimeBuf);
		return 1;
	}
	MimeLen = StrLength(MimeBuf);
	MimeStr = SmashStrBuf(&MimeBuf);

	if (ma.printme == NULL)
		mime_parser(MimeStr, MimeStr + MimeLen,
			    (do_proto ? *list_this_part : NULL),
			    (do_proto ? *list_this_pref : NULL),
			    (do_proto ? *list_this_suff : NULL),
			    (void *)&ma, dont_decode);
	else 
		mime_parser(MimeStr, MimeStr + MimeLen,
			    *mime_download, NULL, NULL, (void *)&ma, dont_decode);

	free(MimeStr);
	return 0;
}