4 * This is the MIME parser for Citadel. Sometimes it actually works.
6 * Copyright (c) 1998-2001 by Art Cancro
7 * This code is distributed under the terms of the GNU General Public License.
15 #include <sys/types.h>
22 #include "mime_parser.h"
25 void extract_key(char *target, char *source, char *key)
29 strcpy(target, source);
30 for (a = 0; a < strlen(target); ++a) {
31 if ((!strncasecmp(&target[a], key, strlen(key)))
32 && (target[a + strlen(key)] == '=')) {
33 strcpy(target, &target[a + strlen(key) + 1]);
35 strcpy(target, &target[1]);
36 for (b = 0; b < strlen(target); ++b)
47 * For non-multipart messages, we need to generate a quickie partnum of "1"
48 * to return to callback functions. Some callbacks demand it.
50 char *fixed_partnum(char *supplied_partnum) {
51 if (supplied_partnum == NULL) return "1";
52 if (strlen(supplied_partnum)==0) return "1";
53 return supplied_partnum;
58 * Given a message or message-part body and a length, handle any necessary
59 * decoding and pass the request up the stack.
61 void mime_decode(char *partnum,
62 char *part_start, size_t length,
63 char *content_type, char *encoding,
65 char *name, char *filename,
76 void (*PreMultiPartCallBack)
86 void (*PostMultiPartCallBack)
106 size_t bytes_sent = 0;
107 size_t bytes_recv = 0;
111 fprintf(stderr, "mime_decode() called\n");
113 /* Some encodings aren't really encodings */
114 if (!strcasecmp(encoding, "7bit"))
115 strcpy(encoding, "");
116 if (!strcasecmp(encoding, "8bit"))
117 strcpy(encoding, "");
118 if (!strcasecmp(encoding, "binary"))
119 strcpy(encoding, "");
121 /* If this part is not encoded, send as-is */
122 if ( (strlen(encoding) == 0) || (dont_decode)) {
123 if (CallBack != NULL) {
124 CallBack(name, filename, fixed_partnum(partnum),
125 disposition, part_start,
126 content_type, length, encoding, userdata);
130 if ((strcasecmp(encoding, "base64"))
131 && (strcasecmp(encoding, "quoted-printable"))) {
132 fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
136 * Allocate a buffer for the decoded data. The output buffer is the
137 * same size as the input buffer; this assumes that the decoded data
138 * will never be larger than the encoded data. This is a safe
139 * assumption with base64, uuencode, and quoted-printable. Just to
140 * be safe, we still pad the buffer a bit.
142 decoded = malloc(length + 1024);
143 if (decoded == NULL) {
144 fprintf(stderr, "ERROR: cannot allocate memory.\n");
147 if (pipe(sendpipe) != 0)
149 if (pipe(recvpipe) != 0)
159 /* send stdio to the pipes */
160 if (dup2(sendpipe[0], 0) < 0)
161 fprintf(stderr, "ERROR dup2()\n");
162 if (dup2(recvpipe[1], 1) < 0)
163 fprintf(stderr, "ERROR dup2()\n");
164 close(sendpipe[1]); /* Close the ends we're not using */
166 if (!strcasecmp(encoding, "base64"))
167 execlp("./base64", "base64", "-d", NULL);
168 else if (!strcasecmp(encoding, "quoted-printable"))
169 execlp("./qpdecode", "qpdecode", NULL);
170 fprintf(stderr, "ERROR: cannot exec decoder for %s\n", encoding);
173 close(sendpipe[0]); /* Close the ends we're not using */
176 while ((bytes_sent < length) && (write_error == 0)) {
177 /* Empty the input pipe FIRST */
178 while (fstat(recvpipe[0], &statbuf), (statbuf.st_size > 0)) {
179 blocksize = read(recvpipe[0], &decoded[bytes_recv],
182 fprintf(stderr, "ERROR: cannot read from pipe\n");
184 bytes_recv = bytes_recv + blocksize;
186 /* Then put some data into the output pipe */
187 blocksize = length - bytes_sent;
188 if (blocksize > 2048)
190 if (write(sendpipe[1], &part_start[bytes_sent], blocksize) < 0) {
191 fprintf(stderr, "ERROR: cannot write to pipe: %s\n",
195 bytes_sent = bytes_sent + blocksize;
198 /* Empty the input pipe */
199 while ((blocksize = read(recvpipe[0], &decoded[bytes_recv], 1)),
201 bytes_recv = bytes_recv + blocksize;
204 if (bytes_recv > 0) if (CallBack != NULL) {
205 CallBack(name, filename, fixed_partnum(partnum),
206 disposition, decoded,
207 content_type, bytes_recv, "binary", userdata);
214 * Break out the components of a multipart message
215 * (This function expects to be fed HEADERS + CONTENT)
216 * Note: NULL can be supplied as content_end; in this case, the message is
217 * considered to have ended when the parser encounters a 0x00 byte.
219 void the_mime_parser(char *partnum,
220 char *content_start, char *content_end,
231 void (*PreMultiPartCallBack)
241 void (*PostMultiPartCallBack)
257 char *part_start, *part_end = NULL;
263 char content_type[SIZ];
264 size_t content_length;
266 char disposition[SIZ];
273 char nested_partnum[SIZ];
275 fprintf(stderr, "the_mime_parser() called\n");
277 memset(boundary, 0, sizeof boundary);
278 memset(content_type, 0, sizeof content_type);
279 memset(encoding, 0, sizeof encoding);
280 memset(name, 0, sizeof name);
281 memset(filename, 0, sizeof filename);
282 memset(disposition, 0, sizeof disposition);
285 /* If the caller didn't supply an endpointer, generate one by measure */
286 if (content_end == NULL) {
287 content_end = &content_start[strlen(content_start)];
290 /* Learn interesting things from the headers */
293 ptr = memreadline(ptr, buf, sizeof buf);
294 if (ptr >= content_end)
297 for (i = 0; i < strlen(buf); ++i)
300 if (!isspace(buf[0])) {
301 if (!strncasecmp(header, "Content-type: ", 14)) {
302 strcpy(content_type, &header[14]);
303 extract_key(name, content_type, "name");
305 if (!strncasecmp(header, "Content-Disposition: ", 21)) {
306 strcpy(disposition, &header[21]);
307 extract_key(filename, disposition, "filename");
309 if (!strncasecmp(header, "Content-length: ", 16)) {
310 content_length = (size_t) atol(&header[16]);
312 if (!strncasecmp(header,
313 "Content-transfer-encoding: ", 27))
314 strcpy(encoding, &header[27]);
315 if (strlen(boundary) == 0)
316 extract_key(boundary, header, "boundary");
319 if ((strlen(header) + strlen(buf) + 2) < sizeof(header))
321 } while ((strlen(buf) > 0) && (*ptr != 0));
323 for (i = 0; i < strlen(disposition); ++i)
324 if (disposition[i] == ';')
326 while (isspace(disposition[0]))
327 strcpy(disposition, &disposition[1]);
328 for (i = 0; i < strlen(content_type); ++i)
329 if (content_type[i] == ';')
331 while (isspace(content_type[0]))
332 strcpy(content_type, &content_type[1]);
334 if (strlen(boundary) > 0) {
340 fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
341 is_multipart, boundary);
343 /* If this is a multipart message, then recursively process it */
347 /* Tell the client about this message's multipartedness */
348 if (PreMultiPartCallBack != NULL) {
349 PreMultiPartCallBack("", "", partnum, "",
351 0, encoding, userdata);
354 /* Figure out where the boundaries are */
355 sprintf(startary, "--%s", boundary);
356 sprintf(endary, "--%s--", boundary);
358 /* if (ptr >= content_end) goto END_MULTI; */
360 if ( (!strncasecmp(ptr, startary, strlen(startary)))
361 || (!strncasecmp(ptr, endary, strlen(endary))) ) {
362 fprintf(stderr, "hit boundary!\n");
363 if (part_start != NULL) {
364 if (strlen(partnum) > 0) {
365 sprintf(nested_partnum, "%s.%d",
366 partnum, ++part_seq);
369 sprintf(nested_partnum, "%d",
372 the_mime_parser(nested_partnum,
373 part_start, part_end,
375 PreMultiPartCallBack,
376 PostMultiPartCallBack,
380 ptr = memreadline(ptr, buf, sizeof(buf));
387 } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
388 if (PostMultiPartCallBack != NULL) {
389 PostMultiPartCallBack("", "", partnum, "", NULL,
390 content_type, 0, encoding, userdata);
395 /* If it's not a multipart message, then do something with it */
397 fprintf(stderr, "doing non-multipart thing\n");
400 while (ptr < content_end) {
404 part_end = content_end;
406 /* Truncate if the header told us to */
407 if ( (content_length > 0) && (length > content_length) ) {
408 length = content_length;
409 fprintf(stderr, "truncated to %d\n", content_length);
414 content_type, encoding, disposition,
416 CallBack, NULL, NULL,
417 userdata, dont_decode);
424 * Entry point for the MIME parser.
425 * (This function expects to be fed HEADERS + CONTENT)
426 * Note: NULL can be supplied as content_end; in this case, the message is
427 * considered to have ended when the parser encounters a 0x00 byte.
429 void mime_parser(char *content_start,
443 void (*PreMultiPartCallBack)
454 void (*PostMultiPartCallBack)
470 fprintf(stderr, "mime_parser() called\n");
471 the_mime_parser("", content_start, content_end,
473 PreMultiPartCallBack,
474 PostMultiPartCallBack,
475 userdata, dont_decode);