First Step: chop mimeparser into pieces.
[citadel.git] / libcitadel / lib / mime_parser.c
1 /*
2  * This is the MIME parser for Citadel.
3  *
4  * Copyright (c) 1998-2010 by the citadel.org development team.
5  * This code is distributed under the GNU General Public License v3.
6  *
7  */
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <dirent.h>
19 #include <errno.h>
20
21 #include "xdgmime/xdgmime.h"
22 #include "libcitadel.h"
23 #include "libcitadellocal.h"
24
25 const unsigned char FromHexTable [256] = {
26         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //  0
27         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 10
28         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 20
29         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 30
30         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01, // 40
31         0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, // 50
32         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 60
33         0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 70
34         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 80
35         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, // 90
36         0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //100
37         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //110
38         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //120
39         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //130
40         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //140
41         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //150
42         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //160
43         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //170
44         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //180
45         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //190
46         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //200
47         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //210
48         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //220
49         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //230
50         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, //240
51         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF                          //250
52 };
53
54
55 long extract_key(char *target, char *source, long sourcelen, char *key, long keylen, char KeyEnd)
56 {
57         char *sptr, *ptr = NULL;
58         int double_quotes = 0;
59         long RealKeyLen = keylen;
60
61         sptr = source;
62
63         while (sptr != NULL)
64         {
65                 ptr = bmstrcasestr_len(sptr, sourcelen - (sptr - source), 
66                                        key, keylen);
67                 if(ptr != NULL)
68                 {
69                         while (isspace(*(ptr + RealKeyLen)))
70                                 RealKeyLen ++;
71                         if (*(ptr + RealKeyLen) == KeyEnd)
72                         {
73                                 sptr = NULL;
74                                 RealKeyLen ++;                          
75                         }
76                         else
77                         {
78                                 sptr = ptr + RealKeyLen + 1;
79                         }
80                 }
81                 else 
82                         sptr = ptr;
83         }
84         if (ptr == NULL) {
85                 *target = '\0';
86                 return 0;
87         }
88         strcpy(target, (ptr + RealKeyLen));
89
90         for (ptr=target; (*ptr != 0); ptr++) {
91
92                 /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
93                 if ( (double_quotes != 1) && (*ptr == ';')) {
94                         *ptr = 0;
95                 }
96
97                 /* if we find double quotes, we've got a great set of string boundaries */
98                 if (*ptr == '\"') {
99                         ++double_quotes;
100                         if (double_quotes == 1) {
101                                 strcpy(ptr, ptr+1);
102                         }
103                         else {
104                                 *ptr = 0;
105                         }
106                 }
107         }
108         *ptr = '\0';
109         return ptr - target;
110 }
111
112
113 /*
114  * For non-multipart messages, we need to generate a quickie partnum of "1"
115  * to return to callback functions.  Some callbacks demand it.
116  */
117 char *fixed_partnum(char *supplied_partnum) {
118         if (supplied_partnum == NULL) return "1";
119         if (strlen(supplied_partnum)==0) return "1";
120         return supplied_partnum;
121 }
122
123
124 static inline unsigned int _decode_hex(const char *Source)
125 {
126         int ret = '?';
127         unsigned char LO_NIBBLE;
128         unsigned char HI_NIBBLE;
129
130         HI_NIBBLE = FromHexTable[(unsigned char) *Source];
131         LO_NIBBLE = FromHexTable[(unsigned char) *(Source+1)];
132         
133         if ((LO_NIBBLE == 0xFF) || (LO_NIBBLE == 0xFF))
134                 return ret;
135         ret = HI_NIBBLE;
136         ret = ret << 4;
137         ret = ret | LO_NIBBLE;
138         return ret;
139 }
140
141 unsigned int decode_hex(char *Source) {return _decode_hex(Source);}
142
143 /*
144  * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
145  * according to RFC2045 section 6.7
146  */
147 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
148         unsigned int ch;
149         int decoded_length = 0;
150         int pos = 0;
151
152         while (pos < sourcelen)
153         {
154                 if (*(encoded + pos) == '=')
155                 {
156                         pos ++;
157                         if (*(encoded + pos) == '\n')
158                         {
159                                 pos ++;
160                         }
161                         else if (*(encoded + pos) == '\r')
162                         {
163                                 pos ++;
164                                 if (*(encoded + pos) == '\n')
165                                         pos++;
166                         }
167                         else
168                         {
169                                 ch = 0;
170                                 ch = _decode_hex(&encoded[pos]);
171                                 pos += 2;
172                                 decoded[decoded_length++] = ch;
173                         }
174                 }
175                 else
176                 {
177                         decoded[decoded_length++] = encoded[pos];
178                         pos += 1;
179                 }
180         }
181         decoded[decoded_length] = 0;
182         return(decoded_length);
183 }
184
185
186 /*
187  * Given a message or message-part body and a length, handle any necessary
188  * decoding and pass the request up the stack.
189  */
190 void mime_decode(char *partnum,
191                  char *part_start, size_t length,
192                  char *content_type, char *charset, char *encoding,
193                  char *disposition,
194                  char *id,
195                  char *name, char *filename,
196                  MimeParserCallBackType CallBack,
197                  MimeParserCallBackType PreMultiPartCallBack,
198                  MimeParserCallBackType PostMultiPartCallBack,
199                  void *userdata,
200                  int dont_decode)
201 {
202
203         char *decoded;
204         size_t bytes_decoded = 0;
205
206         /* Some encodings aren't really encodings */
207         if (!strcasecmp(encoding, "7bit"))
208                 strcpy(encoding, "");
209         if (!strcasecmp(encoding, "8bit"))
210                 strcpy(encoding, "");
211         if (!strcasecmp(encoding, "binary"))
212                 strcpy(encoding, "");
213
214         /* If this part is not encoded, send as-is */
215         if ( (strlen(encoding) == 0) || (dont_decode)) {
216                 if (CallBack != NULL) {
217                         CallBack(name, 
218                                  filename, 
219                                  fixed_partnum(partnum),
220                                  disposition, 
221                                  part_start,
222                                  content_type, 
223                                  charset, 
224                                  length, 
225                                  encoding, 
226                                  id,
227                                  userdata);
228                         }
229                 return;
230         }
231         
232         /* Fail silently if we hit an unknown encoding. */
233         if ((strcasecmp(encoding, "base64"))
234             && (strcasecmp(encoding, "quoted-printable"))) {
235                 return;
236         }
237
238         /*
239          * Allocate a buffer for the decoded data.  The output buffer is slightly
240          * larger than the input buffer; this assumes that the decoded data
241          * will never be significantly larger than the encoded data.  This is a
242          * safe assumption with base64, uuencode, and quoted-printable.
243          */
244         decoded = malloc(length + 32768);
245         if (decoded == NULL) {
246                 return;
247         }
248
249         if (!strcasecmp(encoding, "base64")) {
250                 bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
251         }
252         else if (!strcasecmp(encoding, "quoted-printable")) {
253                 bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
254         }
255
256         if (bytes_decoded > 0) if (CallBack != NULL) {
257                 CallBack(name, filename, fixed_partnum(partnum),
258                         disposition, decoded,
259                         content_type, charset, bytes_decoded, "binary", id, userdata);
260         }
261
262         free(decoded);
263 }
264
265 /*
266  * this is the extract of mime_decode which can be called if 'dont_decode' was set; 
267  * to save the cpu intense process of decoding to the time when it realy wants the content. 
268  * returns: 
269  *   - > 0 we decoded something, its on *decoded, you need to free it.
270  *   - = 0 no need to decode stuff. *decoded will be NULL.
271  *   - < 0 an error occured, either an unknown encoding, or alloc failed. no need to free.
272  */
273 int mime_decode_now (char *part_start, 
274                      size_t length,
275                      char *encoding,
276                      char **decoded,
277                      size_t *bytes_decoded)
278 {
279         *bytes_decoded = 0;
280         *decoded = NULL;
281         /* Some encodings aren't really encodings */
282         if (!strcasecmp(encoding, "7bit"))
283                 strcpy(encoding, "");
284         if (!strcasecmp(encoding, "8bit"))
285                 strcpy(encoding, "");
286         if (!strcasecmp(encoding, "binary"))
287                 strcpy(encoding, "");
288
289         /* If this part is not encoded, send as-is */
290         if (strlen(encoding) == 0) {
291                 return 0;
292         }
293         
294
295         /* Fail if we hit an unknown encoding. */
296         if ((strcasecmp(encoding, "base64"))
297             && (strcasecmp(encoding, "quoted-printable"))) {
298                 return -1;
299         }
300
301         /*
302          * Allocate a buffer for the decoded data.  The output buffer is slightly
303          * larger than the input buffer; this assumes that the decoded data
304          * will never be significantly larger than the encoded data.  This is a
305          * safe assumption with base64, uuencode, and quoted-printable.
306          */
307         *decoded = malloc(length + 32768);
308         if (decoded == NULL) {
309                 return -1;
310         }
311
312         if (!strcasecmp(encoding, "base64")) {
313                 *bytes_decoded = CtdlDecodeBase64(*decoded, part_start, length);
314                 return 1;
315         }
316         else if (!strcasecmp(encoding, "quoted-printable")) {
317                 *bytes_decoded = CtdlDecodeQuotedPrintable(*decoded, part_start, length);
318                 return 1;
319         }
320         return -1;
321 }
322
323 typedef enum _eIntMimeHdrs {
324         boundary,
325         startary,
326         endary,
327         content_type,
328         charset,
329         encoding,
330         content_type_name,
331         content_disposition_name,
332         filename,
333         disposition,
334         id,
335         eMax /* don't move ! */
336 } eIntMimeHdrs;
337
338 typedef struct _CBufStr {
339         char Key[SIZ];
340         long len;
341 }CBufStr;
342
343 typedef struct _interesting_mime_headers {
344         CBufStr b[eMax];
345         long content_length;
346         long is_multipart;
347 } interesting_mime_headers;
348
349 interesting_mime_headers *InitInterestingMimes(void)
350 {
351         int i;
352         interesting_mime_headers *m;
353         m = (interesting_mime_headers*) malloc( sizeof(interesting_mime_headers));
354         
355         for (i = 0; i < eMax; i++) {
356              m->b[i].Key[0] = '\0';
357              m->b[i].len = 0;
358         }
359         m->content_length = -1;
360         return m;
361 }
362
363
364
365 long parse_MimeHeaders(interesting_mime_headers *m, char* content_start, char *content_end)
366 {
367         char buf[SIZ];
368         char header[SIZ];
369         long headerlen;
370         char *ptr;
371         int buflen;
372         int i;
373
374         /* Learn interesting things from the headers */
375         ptr = content_start;
376         *header = '\0';
377         headerlen = 0;
378         do {
379                 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
380                 if (ptr >= content_end) {
381                         return -1;
382                 }
383
384                 for (i = 0; i < buflen; ++i) {
385                         if (isspace(buf[i])) {
386                                 buf[i] = ' ';
387                         }
388                 }
389
390                 if (!isspace(buf[0])) {
391                         if (!strncasecmp(header, "Content-type:", 13)) {
392                                 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
393                                 m->b[content_type].len = striplt (m->b[content_type].Key);
394
395                                 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
396                                 m->b[charset].len           = extract_key(m->b[charset].Key,           CKEY(m->b[content_type]), HKEY("charset"), '=');
397                                 m->b[boundary].len          = extract_key(m->b[boundary].Key,          header,       headerlen,  HKEY("boundary"), '=');
398
399                                 /* Deal with weird headers */
400                                 if (strchr(m->b[content_type].Key, ' '))
401                                         *(strchr(m->b[content_type].Key, ' ')) = '\0';
402                                 if (strchr(m->b[content_type].Key, ';'))
403                                         *(strchr(m->b[content_type].Key, ';')) = '\0';
404                         }
405                         else if (!strncasecmp(header, "Content-Disposition:", 20)) {
406                                 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
407                                 m->b[disposition].len = striplt(m->b[disposition].Key);
408
409                                 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
410                                 m->b[filename].len                 = extract_key(m->b[filename].Key,                 CKEY(m->b[disposition]), HKEY("filename"), '=');
411                         }
412                         else if (!strncasecmp(header, "Content-ID:", 11)) {
413                                 memcpy(m->b[id].Key, &header[11], headerlen);
414                                 striplt(m->b[id].Key);
415                                 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
416                         }
417                         else if (!strncasecmp(header, "Content-length: ", 15)) {
418                                 char *clbuf;
419                                 clbuf = &header[15];
420                                 while (isspace(clbuf))
421                                         clbuf ++;
422                                 m->content_length = (size_t) atol(clbuf);
423                         }
424                         else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
425                                 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
426                                 m->b[encoding].len = striplt(m->b[encoding].Key);
427                         }
428                         *header = '\0';
429                         headerlen = 0;
430                 }
431                 if ((headerlen + buflen + 2) < SIZ) {
432                         memcpy(&header[headerlen], buf, buflen);
433                         headerlen += buflen;
434                         header[headerlen] = '\0';
435                 }
436         } while ((!IsEmptyStr(buf)) && (*ptr != 0));
437
438         ptr = strchr(m->b[disposition].Key, ';');
439         if (ptr != NULL) *ptr = '\0';
440         m->b[disposition].len = striplt(m->b[disposition].Key);
441
442         ptr = strchr(m->b[content_type].Key, ';');
443         if (ptr != NULL) *ptr = '\0';
444         m->b[content_type].len = striplt(m->b[content_type].Key);
445
446         m->is_multipart = m->b[boundary].len != 0;
447
448         return 0;
449 }
450
451 /*
452  * Break out the components of a multipart message
453  * (This function expects to be fed HEADERS + CONTENT)
454  * Note: NULL can be supplied as content_end; in this case, the message is
455  * considered to have ended when the parser encounters a 0x00 byte.
456  */
457 void the_mime_parser(char *partnum,
458                      char *content_start, char *content_end,
459                      MimeParserCallBackType CallBack,
460                      MimeParserCallBackType PreMultiPartCallBack,
461                      MimeParserCallBackType PostMultiPartCallBack,
462                      void *userdata,
463                      int dont_decode)
464 {
465
466         char *ptr;
467         char *part_start, *part_end = NULL;
468         char *next_boundary;
469         
470         size_t content_length;
471         int part_seq = 0;
472         size_t length;
473         char nested_partnum[256];
474         int crlf_in_use = 0;
475         char *evaluate_crlf_ptr = NULL;
476         
477         interesting_mime_headers *m;
478         CBufStr *chosen_name;
479
480         ptr = content_start;
481         content_length = 0;
482
483         m = InitInterestingMimes();
484
485
486         /* If the caller didn't supply an endpointer, generate one by measure */
487         if (content_end == NULL) {
488                 content_end = &content_start[strlen(content_start)];
489         }
490
491
492         if (parse_MimeHeaders(m, content_start, content_end) != 0)
493                 goto end_parser;
494         
495         /* If this is a multipart message, then recursively process it */
496         part_start = NULL;
497         if (m->is_multipart) {
498
499                 /* Tell the client about this message's multipartedness */
500                 if (PreMultiPartCallBack != NULL) {
501                         PreMultiPartCallBack("", 
502                                              "", 
503                                              partnum, 
504                                              "",
505                                              NULL, 
506                                              m->b[content_type].Key, 
507                                              m->b[charset].Key,
508                                              0, 
509                                              m->b[encoding].Key, 
510                                              m->b[id].Key, 
511                                              userdata);
512                 }
513
514                 /* Figure out where the boundaries are */
515                 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
516
517                 part_start = NULL;
518                 do {
519                         char tmp;
520
521                         tmp = *content_end;
522                         *content_end = '\0';
523                         
524                         next_boundary = strstr(ptr, m->b[startary].Key);
525                         *content_end = tmp;
526
527                         if ( (part_start != NULL) && (next_boundary != NULL) ) {
528                                 part_end = next_boundary;
529                                 --part_end;             /* omit the trailing LF */
530                                 if (crlf_in_use) {
531                                         --part_end;     /* omit the trailing CR */
532                                 }
533
534                                 if (!IsEmptyStr(partnum)) {
535                                         snprintf(nested_partnum,
536                                                  sizeof nested_partnum,
537                                                  "%s.%d", partnum,
538                                                  ++part_seq);
539                                 }
540                                 else {
541                                         snprintf(nested_partnum,
542                                                  sizeof nested_partnum,
543                                                  "%d", ++part_seq);
544                                 }
545                                 the_mime_parser(nested_partnum,
546                                                 part_start, 
547                                                 part_end,
548                                                 CallBack,
549                                                 PreMultiPartCallBack,
550                                                 PostMultiPartCallBack,
551                                                 userdata,
552                                                 dont_decode);
553                         }
554
555                         if (next_boundary != NULL) {
556                                 /* If we pass out of scope, don't attempt to
557                                  * read past the end boundary. */
558                                 if ((*(next_boundary + m->b[startary].len + 1) == '-') && 
559                                     (*(next_boundary + m->b[startary].len + 2) == '-') ){
560                                         ptr = content_end;
561                                 }
562                                 else {
563                                         /* Set up for the next part. */
564                                         part_start = strstr(next_boundary, "\n");
565                                         
566                                         /* Determine whether newlines are LF or CRLF */
567                                         evaluate_crlf_ptr = part_start;
568                                         --evaluate_crlf_ptr;
569                                         if ((*evaluate_crlf_ptr == '\r') && 
570                                             (*(evaluate_crlf_ptr + 1) == '\n'))
571                                         {
572                                                 crlf_in_use = 1;
573                                         }
574                                         else {
575                                                 crlf_in_use = 0;
576                                         }
577
578                                         /* Advance past the LF ... now we're in the next part */
579                                         ++part_start;
580                                         ptr = part_start;
581                                 }
582                         }
583                         else {
584                                 /* Invalid end of multipart.  Bail out! */
585                                 ptr = content_end;
586                         }
587                 } while ( (ptr < content_end) && (next_boundary != NULL) );
588
589                 if (PostMultiPartCallBack != NULL) {
590                         PostMultiPartCallBack("", 
591                                               "", 
592                                               partnum, 
593                                               "", 
594                                               NULL,
595                                               m->b[content_type].Key, 
596                                               m->b[charset].Key,
597                                               0, 
598                                               m->b[encoding].Key, 
599                                               m->b[id].Key, 
600                                               userdata);
601                 }
602                 goto end_parser;
603         }
604
605         /* If it's not a multipart message, then do something with it */
606         if (!m->is_multipart) {
607                 part_start = ptr;
608                 length = 0;
609                 while (ptr < content_end) {
610                         ++ptr;
611                         ++length;
612                 }
613                 part_end = content_end;
614
615
616                 /* The following code will truncate the MIME part to the size
617                  * specified by the Content-length: header.   We have commented it
618                  * out because these headers have a tendency to be wrong.
619                  *
620                  *      if ( (content_length > 0) && (length > content_length) ) {
621                  *              length = content_length;
622                  *      }
623                  */
624
625                 /* Sometimes the "name" field is tacked on to Content-type,
626                  * and sometimes it's tacked on to Content-disposition.  Use
627                  * whichever one we have.
628                  */
629                 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
630                         chosen_name = &m->b[content_disposition_name];
631                 }
632                 else {
633                         chosen_name = &m->b[content_type_name];
634                 }
635         
636                 /* Ok, we've got a non-multipart part here, so do something with it.
637                  */
638                 mime_decode(partnum,
639                             part_start, 
640                             length,
641                             m->b[content_type].Key, 
642                             m->b[charset].Key,
643                             m->b[encoding].Key, 
644                             m->b[disposition].Key, 
645                             m->b[id].Key, 
646                             chosen_name->Key, 
647                             m->b[filename].Key,
648                             CallBack, 
649                             NULL, NULL,
650                             userdata, 
651                             dont_decode
652                         );
653
654                 /*
655                  * Now if it's an encapsulated message/rfc822 then we have to recurse into it
656                  */
657                 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
658
659                         if (PreMultiPartCallBack != NULL) {
660                                 PreMultiPartCallBack("", 
661                                                      "", 
662                                                      partnum, 
663                                                      "",
664                                                      NULL, 
665                                                      m->b[content_type].Key, 
666                                                      m->b[charset].Key,
667                                                      0, 
668                                                      m->b[encoding].Key, 
669                                                      m->b[id].Key, 
670                                                      userdata);
671                         }
672                         if (CallBack != NULL) {
673                                 if (strlen(partnum) > 0) {
674                                         snprintf(nested_partnum,
675                                                  sizeof nested_partnum,
676                                                  "%s.%d", partnum,
677                                                  ++part_seq);
678                                 }
679                                 else {
680                                         snprintf(nested_partnum,
681                                                  sizeof nested_partnum,
682                                                  "%d", ++part_seq);
683                                 }
684                                 the_mime_parser(nested_partnum,
685                                                 part_start, 
686                                                 part_end,
687                                                 CallBack,
688                                                 PreMultiPartCallBack,
689                                                 PostMultiPartCallBack,
690                                                 userdata,
691                                                 dont_decode
692                                         );
693                         }
694                         if (PostMultiPartCallBack != NULL) {
695                                 PostMultiPartCallBack("", 
696                                                       "", 
697                                                       partnum, 
698                                                       "", 
699                                                       NULL,
700                                                       m->b[content_type].Key, 
701                                                       m->b[charset].Key,
702                                                       0, 
703                                                       m->b[encoding].Key, 
704                                                       m->b[id].Key, 
705                                                       userdata);
706                         }
707
708
709                 }
710
711         }
712
713 end_parser:     /* free the buffers!  end the oppression!! */
714         free(m);
715 }
716
717
718
719 /*
720  * Entry point for the MIME parser.
721  * (This function expects to be fed HEADERS + CONTENT)
722  * Note: NULL can be supplied as content_end; in this case, the message is
723  * considered to have ended when the parser encounters a 0x00 byte.
724  */
725 void mime_parser(char *content_start,
726                  char *content_end,
727                  MimeParserCallBackType CallBack,
728                  MimeParserCallBackType PreMultiPartCallBack,
729                  MimeParserCallBackType PostMultiPartCallBack,
730                  void *userdata,
731                  int dont_decode)
732 {
733
734         the_mime_parser("", content_start, content_end,
735                         CallBack,
736                         PreMultiPartCallBack,
737                         PostMultiPartCallBack,
738                         userdata, dont_decode);
739 }
740
741
742
743
744
745
746 typedef struct _MimeGuess {
747         const char *Pattern;
748         size_t PatternLen;
749         long PatternOffset;
750         const char *MimeString;
751 } MimeGuess;
752
753 MimeGuess MyMimes [] = {
754         {
755                 "GIF",
756                 3,
757                 0,
758                 "image/gif"
759         },
760         {
761                 "\xff\xd8",
762                 2,
763                 0,
764                 "image/jpeg"
765         },
766         {
767                 "\x89PNG",
768                 4,
769                 0,
770                 "image/png"
771         },
772         { // last...
773                 "",
774                 0,
775                 0,
776                 ""
777         }
778 };
779
780
781 const char *GuessMimeType(const char *data, size_t dlen)
782 {
783         int MimeIndex = 0;
784
785         while (MyMimes[MimeIndex].PatternLen != 0)
786         {
787                 if ((MyMimes[MimeIndex].PatternLen + 
788                      MyMimes[MimeIndex].PatternOffset < dlen) &&
789                     strncmp(MyMimes[MimeIndex].Pattern, 
790                             &data[MyMimes[MimeIndex].PatternOffset], 
791                             MyMimes[MimeIndex].PatternLen) == 0)
792                 {
793                         return MyMimes[MimeIndex].MimeString;
794                 }
795                 MimeIndex ++;
796         }
797         /* 
798          * ok, our simple minded algorythm didn't find anything, 
799          * let the big chegger try it, he wil default to application/octet-stream
800          */
801         return (xdg_mime_get_mime_type_for_data(data, dlen));
802 }
803
804
805 const char* GuessMimeByFilename(const char *what, size_t len)
806 {
807         /* we know some hardcoded on our own, try them... */
808         if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
809                 return "image/gif";
810         else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
811                 return  "text/javascript";
812         else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
813                 return "text/plain";
814         else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
815                 return "text/css";
816         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
817                 return "text/x-component";
818         else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
819                 return "image/jpeg";
820         else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
821                 return "image/png";
822         else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
823                 return "image/x-icon";
824         else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
825                 return "text/x-vcard";
826         else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
827                 return "text/html";
828         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
829                 return "text/html";
830         else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
831                 return "text/vnd.wap.wml";
832         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
833                 return "text/vnd.wap.wmlscript";
834         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
835                 return "application/vnd.wap.wmlc";
836         else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
837                 return "application/vnd.wap.wmlscriptc";
838         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
839                 return "image/vnd.wap.wbmp";
840         else
841                 /* and let xdgmime do the fallback. */
842                 return xdg_mime_get_mime_type_from_file_name(what);
843 }
844
845 static HashList *IconHash = NULL;
846
847 typedef struct IconName IconName;
848
849 struct IconName {
850         char *FlatName;
851         char *FileName;
852 };
853
854 static void DeleteIcon(void *IconNamePtr)
855 {
856         IconName *Icon = (IconName*) IconNamePtr;
857         free(Icon->FlatName);
858         free(Icon->FileName);
859         free(Icon);
860 }
861
862 /*
863 static const char *PrintFlat(void *IconNamePtr)
864 {
865         IconName *Icon = (IconName*) IconNamePtr;
866         return Icon->FlatName;
867 }
868 static const char *PrintFile(void *IconNamePtr)
869 {
870         IconName *Icon = (IconName*) IconNamePtr;
871         return Icon->FileName;
872 }
873 */
874
875 #define GENSTR "x-generic"
876 #define IGNORE_PREFIX_1 "gnome-mime"
877 int LoadIconDir(const char *DirName)
878 {
879         DIR *filedir = NULL;
880         struct dirent *filedir_entry;
881         int d_namelen;
882         int d_without_ext;
883         IconName *Icon;
884
885         filedir = opendir (DirName);
886         IconHash = NewHash(1, NULL);
887         if (filedir == NULL) {
888                 return 0;
889         }
890
891         while ((filedir_entry = readdir(filedir)))
892         {
893                 char *MinorPtr;
894                 char *PStart;
895 #ifdef _DIRENT_HAVE_D_NAMELEN
896                 d_namelen = filedir_entry->d_namelen;
897 #else
898                 d_namelen = strlen(filedir_entry->d_name);
899 #endif
900                 d_without_ext = d_namelen;
901                 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
902                         d_without_ext --;
903                 if ((d_without_ext == 0) || (d_namelen < 3))
904                         continue;
905
906                 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
907                     (strncmp(IGNORE_PREFIX_1, 
908                              filedir_entry->d_name, 
909                              sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
910                         PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
911                         d_without_ext -= sizeof(IGNORE_PREFIX_1);
912                 }
913                 else {
914                         PStart = filedir_entry->d_name;
915                 }
916                 Icon = malloc(sizeof(IconName));
917
918                 Icon->FileName = malloc(d_namelen + 1);
919                 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
920
921                 Icon->FlatName = malloc(d_without_ext + 1);
922                 memcpy(Icon->FlatName, PStart, d_without_ext);
923                 Icon->FlatName[d_without_ext] = '\0';
924                 /* Try to find Minor type in image-jpeg */
925                 MinorPtr = strchr(Icon->FlatName, '-');
926                 if (MinorPtr != NULL) {
927                         size_t MinorLen;
928                         MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
929                         if ((MinorLen == sizeof(GENSTR)) && 
930                             (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
931                                 /* ok, we found a generic filename. cut the generic. */
932                                 *MinorPtr = '\0';
933                                 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
934                         }
935                         else { /* Map the major / minor separator to / */
936                                 *MinorPtr = '/';
937                         }
938                 }
939
940 //              PrintHash(IconHash, PrintFlat, PrintFile);
941 //              printf("%s - %s\n", Icon->FlatName, Icon->FileName);
942                 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
943 //              PrintHash(IconHash, PrintFlat, PrintFile);
944         }
945         closedir(filedir);
946         return 1;
947 }
948
949 const char *GetIconFilename(char *MimeType, size_t len)
950 {
951         void *vIcon;
952         IconName *Icon;
953         
954         if(IconHash == NULL)
955                 return NULL;
956
957         GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
958         /* didn't find the exact mimetype? try major only. */
959         if (Icon == NULL) {
960                 char * pMinor;
961                 pMinor = strchr(MimeType, '/');
962                 if (pMinor != NULL) {
963                         *pMinor = '\0';
964                         GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
965                                 Icon = (IconName*) vIcon;
966                 }
967         }
968         if (Icon == NULL) {
969                 return NULL;
970         }
971
972         /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
973         return Icon->FileName;
974 }
975
976 void ShutDownLibCitadelMime(void)
977 {
978         DeleteHash(&IconHash);
979 }