1468375b0bf7227eb5d3a9c6132c426c801c6a9f
[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** pcontent_start, char *content_end)
366 {
367         char buf[SIZ];
368         char header[SIZ];
369         long headerlen;
370         char *ptr, *pch;
371         int buflen = 0;
372         int i;
373
374         /* Learn interesting things from the headers */
375         ptr = *pcontent_start;
376         *header = '\0';
377         headerlen = 0;
378         do {
379                 ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
380
381                 for (i = 0; i < buflen; ++i) {
382                         if (isspace(buf[i])) {
383                                 buf[i] = ' ';
384                         }
385                 }
386
387                 if (!isspace(buf[0]) && (headerlen > 0)) {
388                         if (!strncasecmp(header, "Content-type:", 13)) {
389                                 memcpy (m->b[content_type].Key, &header[13], headerlen - 12);
390                                 m->b[content_type].len = striplt (m->b[content_type].Key);
391
392                                 m->b[content_type_name].len = extract_key(m->b[content_type_name].Key, CKEY(m->b[content_type]), HKEY("name"), '=');
393                                 m->b[charset].len           = extract_key(m->b[charset].Key,           CKEY(m->b[content_type]), HKEY("charset"), '=');
394                                 m->b[boundary].len          = extract_key(m->b[boundary].Key,          header,       headerlen,  HKEY("boundary"), '=');
395
396                                 /* Deal with weird headers */
397                                 pch = strchr(m->b[content_type].Key, ' ');
398                                 if (pch != NULL) {
399                                         *pch = '\0';
400                                         m->b[content_type].len = m->b[content_type].Key - pch;
401                                 }
402                                 pch = strchr(m->b[content_type].Key, ';');
403                                 if (pch != NULL) {
404                                         *pch = '\0';
405                                         m->b[content_type].len = m->b[content_type].Key - pch;
406                                 }
407                         }
408                         else if (!strncasecmp(header, "Content-Disposition:", 20)) {
409                                 memcpy (m->b[disposition].Key, &header[20], headerlen - 19);
410                                 m->b[disposition].len = striplt(m->b[disposition].Key);
411
412                                 m->b[content_disposition_name].len = extract_key(m->b[content_disposition_name].Key, CKEY(m->b[disposition]), HKEY("name"), '=');
413                                 m->b[filename].len                 = extract_key(m->b[filename].Key,                 CKEY(m->b[disposition]), HKEY("filename"), '=');
414                                 pch = strchr(m->b[disposition].Key, ';');
415                                 if (pch != NULL) *ptr = '\0';
416                                 m->b[disposition].len = striplt(m->b[disposition].Key);
417                         }
418                         else if (!strncasecmp(header, "Content-ID:", 11)) {
419                                 memcpy(m->b[id].Key, &header[11], headerlen);
420                                 striplt(m->b[id].Key);
421                                 m->b[id].len = stripallbut(m->b[id].Key, '<', '>');
422                         }
423                         else if (!strncasecmp(header, "Content-length: ", 15)) {
424                                 char *clbuf;
425                                 clbuf = &header[15];
426                                 while (isspace(*clbuf))
427                                         clbuf ++;
428                                 m->content_length = (size_t) atol(clbuf);
429                         }
430                         else if (!strncasecmp(header, "Content-transfer-encoding: ", 26)) {
431                                 memcpy(m->b[encoding].Key, &header[26], headerlen - 26);
432                                 m->b[encoding].len = striplt(m->b[encoding].Key);
433                         }
434                         *header = '\0';
435                         headerlen = 0;
436                 }
437                 if ((headerlen + buflen + 2) < SIZ) {
438                         memcpy(&header[headerlen], buf, buflen);
439                         headerlen += buflen;
440                         header[headerlen] = '\0';
441                 }
442                 if (ptr >= content_end) {
443                         return -1;
444                 }
445         } while ((!IsEmptyStr(buf)) && (*ptr != 0));
446
447         m->is_multipart = m->b[boundary].len != 0;
448         *pcontent_start = ptr;
449
450         return 0;
451 }
452
453 /*
454  * Break out the components of a multipart message
455  * (This function expects to be fed HEADERS + CONTENT)
456  * Note: NULL can be supplied as content_end; in this case, the message is
457  * considered to have ended when the parser encounters a 0x00 byte.
458  */
459 void the_mime_parser(char *partnum,
460                      char *content_start, char *content_end,
461                      MimeParserCallBackType CallBack,
462                      MimeParserCallBackType PreMultiPartCallBack,
463                      MimeParserCallBackType PostMultiPartCallBack,
464                      void *userdata,
465                      int dont_decode)
466 {
467
468         char *ptr;
469         char *part_start, *part_end = NULL;
470         char *next_boundary;
471         
472         size_t content_length;
473         int part_seq = 0;
474         size_t length;
475         char nested_partnum[256];
476         int crlf_in_use = 0;
477         char *evaluate_crlf_ptr = NULL;
478         
479         interesting_mime_headers *m;
480         CBufStr *chosen_name;
481
482         content_length = 0;
483
484         m = InitInterestingMimes();
485
486
487         /* If the caller didn't supply an endpointer, generate one by measure */
488         if (content_end == NULL) {
489                 content_end = &content_start[strlen(content_start)];
490         }
491
492
493         if (parse_MimeHeaders(m, &content_start, content_end) != 0)
494                 goto end_parser;
495         
496         /* If this is a multipart message, then recursively process it */
497         ptr = content_start;
498         part_start = NULL;
499         if (m->is_multipart) {
500
501                 /* Tell the client about this message's multipartedness */
502                 if (PreMultiPartCallBack != NULL) {
503                         PreMultiPartCallBack("", 
504                                              "", 
505                                              partnum, 
506                                              "",
507                                              NULL, 
508                                              m->b[content_type].Key, 
509                                              m->b[charset].Key,
510                                              0, 
511                                              m->b[encoding].Key, 
512                                              m->b[id].Key, 
513                                              userdata);
514                 }
515
516                 /* Figure out where the boundaries are */
517                 m->b[startary].len = snprintf(m->b[startary].Key, SIZ, "--%s", m->b[boundary].Key);
518
519                 part_start = NULL;
520                 do {
521                         char tmp;
522
523                         tmp = *content_end;
524                         *content_end = '\0';
525                         
526                         next_boundary = strstr(ptr, m->b[startary].Key);
527                         *content_end = tmp;
528
529                         if ( (part_start != NULL) && (next_boundary != NULL) ) {
530                                 part_end = next_boundary;
531                                 --part_end;             /* omit the trailing LF */
532                                 if (crlf_in_use) {
533                                         --part_end;     /* omit the trailing CR */
534                                 }
535
536                                 if (!IsEmptyStr(partnum)) {
537                                         snprintf(nested_partnum,
538                                                  sizeof nested_partnum,
539                                                  "%s.%d", partnum,
540                                                  ++part_seq);
541                                 }
542                                 else {
543                                         snprintf(nested_partnum,
544                                                  sizeof nested_partnum,
545                                                  "%d", ++part_seq);
546                                 }
547                                 the_mime_parser(nested_partnum,
548                                                 part_start, 
549                                                 part_end,
550                                                 CallBack,
551                                                 PreMultiPartCallBack,
552                                                 PostMultiPartCallBack,
553                                                 userdata,
554                                                 dont_decode);
555                         }
556
557                         if (next_boundary != NULL) {
558                                 /* If we pass out of scope, don't attempt to
559                                  * read past the end boundary. */
560                                 if ((*(next_boundary + m->b[startary].len + 1) == '-') && 
561                                     (*(next_boundary + m->b[startary].len + 2) == '-') ){
562                                         ptr = content_end;
563                                 }
564                                 else {
565                                         /* Set up for the next part. */
566                                         part_start = strstr(next_boundary, "\n");
567                                         
568                                         /* Determine whether newlines are LF or CRLF */
569                                         evaluate_crlf_ptr = part_start;
570                                         --evaluate_crlf_ptr;
571                                         if ((*evaluate_crlf_ptr == '\r') && 
572                                             (*(evaluate_crlf_ptr + 1) == '\n'))
573                                         {
574                                                 crlf_in_use = 1;
575                                         }
576                                         else {
577                                                 crlf_in_use = 0;
578                                         }
579
580                                         /* Advance past the LF ... now we're in the next part */
581                                         ++part_start;
582                                         ptr = part_start;
583                                 }
584                         }
585                         else {
586                                 /* Invalid end of multipart.  Bail out! */
587                                 ptr = content_end;
588                         }
589                 } while ( (ptr < content_end) && (next_boundary != NULL) );
590
591                 if (PostMultiPartCallBack != NULL) {
592                         PostMultiPartCallBack("", 
593                                               "", 
594                                               partnum, 
595                                               "", 
596                                               NULL,
597                                               m->b[content_type].Key, 
598                                               m->b[charset].Key,
599                                               0, 
600                                               m->b[encoding].Key, 
601                                               m->b[id].Key, 
602                                               userdata);
603                 }
604         } /* If it's not a multipart message, then do something with it */
605         else {
606                 part_start = ptr;
607                 length = content_end - part_start;
608                 ptr = part_end = content_end;
609
610
611                 /* The following code will truncate the MIME part to the size
612                  * specified by the Content-length: header.   We have commented it
613                  * out because these headers have a tendency to be wrong.
614                  *
615                  *      if ( (content_length > 0) && (length > content_length) ) {
616                  *              length = content_length;
617                  *      }
618                  */
619
620                 /* Sometimes the "name" field is tacked on to Content-type,
621                  * and sometimes it's tacked on to Content-disposition.  Use
622                  * whichever one we have.
623                  */
624                 if (m->b[content_disposition_name].len > m->b[content_type_name].len) {
625                         chosen_name = &m->b[content_disposition_name];
626                 }
627                 else {
628                         chosen_name = &m->b[content_type_name];
629                 }
630         
631                 /* Ok, we've got a non-multipart part here, so do something with it.
632                  */
633                 mime_decode(partnum,
634                             part_start, 
635                             length,
636                             m->b[content_type].Key, 
637                             m->b[charset].Key,
638                             m->b[encoding].Key, 
639                             m->b[disposition].Key, 
640                             m->b[id].Key, 
641                             chosen_name->Key, 
642                             m->b[filename].Key,
643                             CallBack, 
644                             NULL, NULL,
645                             userdata, 
646                             dont_decode
647                         );
648
649                 /*
650                  * Now if it's an encapsulated message/rfc822 then we have to recurse into it
651                  */
652                 if (!strcasecmp(&m->b[content_type].Key[0], "message/rfc822")) {
653
654                         if (PreMultiPartCallBack != NULL) {
655                                 PreMultiPartCallBack("", 
656                                                      "", 
657                                                      partnum, 
658                                                      "",
659                                                      NULL, 
660                                                      m->b[content_type].Key, 
661                                                      m->b[charset].Key,
662                                                      0, 
663                                                      m->b[encoding].Key, 
664                                                      m->b[id].Key, 
665                                                      userdata);
666                         }
667                         if (CallBack != NULL) {
668                                 if (strlen(partnum) > 0) {
669                                         snprintf(nested_partnum,
670                                                  sizeof nested_partnum,
671                                                  "%s.%d", partnum,
672                                                  ++part_seq);
673                                 }
674                                 else {
675                                         snprintf(nested_partnum,
676                                                  sizeof nested_partnum,
677                                                  "%d", ++part_seq);
678                                 }
679                                 the_mime_parser(nested_partnum,
680                                                 part_start, 
681                                                 part_end,
682                                                 CallBack,
683                                                 PreMultiPartCallBack,
684                                                 PostMultiPartCallBack,
685                                                 userdata,
686                                                 dont_decode
687                                         );
688                         }
689                         if (PostMultiPartCallBack != NULL) {
690                                 PostMultiPartCallBack("", 
691                                                       "", 
692                                                       partnum, 
693                                                       "", 
694                                                       NULL,
695                                                       m->b[content_type].Key, 
696                                                       m->b[charset].Key,
697                                                       0, 
698                                                       m->b[encoding].Key, 
699                                                       m->b[id].Key, 
700                                                       userdata);
701                         }
702
703
704                 }
705
706         }
707
708 end_parser:     /* free the buffers!  end the oppression!! */
709         free(m);
710 }
711
712
713
714 /*
715  * Entry point for the MIME parser.
716  * (This function expects to be fed HEADERS + CONTENT)
717  * Note: NULL can be supplied as content_end; in this case, the message is
718  * considered to have ended when the parser encounters a 0x00 byte.
719  */
720 void mime_parser(char *content_start,
721                  char *content_end,
722                  MimeParserCallBackType CallBack,
723                  MimeParserCallBackType PreMultiPartCallBack,
724                  MimeParserCallBackType PostMultiPartCallBack,
725                  void *userdata,
726                  int dont_decode)
727 {
728
729         the_mime_parser("", content_start, content_end,
730                         CallBack,
731                         PreMultiPartCallBack,
732                         PostMultiPartCallBack,
733                         userdata, dont_decode);
734 }
735
736
737
738
739
740
741 typedef struct _MimeGuess {
742         const char *Pattern;
743         size_t PatternLen;
744         long PatternOffset;
745         const char *MimeString;
746 } MimeGuess;
747
748 MimeGuess MyMimes [] = {
749         {
750                 "GIF",
751                 3,
752                 0,
753                 "image/gif"
754         },
755         {
756                 "\xff\xd8",
757                 2,
758                 0,
759                 "image/jpeg"
760         },
761         {
762                 "\x89PNG",
763                 4,
764                 0,
765                 "image/png"
766         },
767         { // last...
768                 "",
769                 0,
770                 0,
771                 ""
772         }
773 };
774
775
776 const char *GuessMimeType(const char *data, size_t dlen)
777 {
778         int MimeIndex = 0;
779
780         while (MyMimes[MimeIndex].PatternLen != 0)
781         {
782                 if ((MyMimes[MimeIndex].PatternLen + 
783                      MyMimes[MimeIndex].PatternOffset < dlen) &&
784                     strncmp(MyMimes[MimeIndex].Pattern, 
785                             &data[MyMimes[MimeIndex].PatternOffset], 
786                             MyMimes[MimeIndex].PatternLen) == 0)
787                 {
788                         return MyMimes[MimeIndex].MimeString;
789                 }
790                 MimeIndex ++;
791         }
792         /* 
793          * ok, our simple minded algorythm didn't find anything, 
794          * let the big chegger try it, he wil default to application/octet-stream
795          */
796         return (xdg_mime_get_mime_type_for_data(data, dlen));
797 }
798
799
800 const char* GuessMimeByFilename(const char *what, size_t len)
801 {
802         /* we know some hardcoded on our own, try them... */
803         if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
804                 return "image/gif";
805         else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
806                 return  "text/javascript";
807         else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
808                 return "text/plain";
809         else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
810                 return "text/css";
811         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htc", 4))
812                 return "text/x-component";
813         else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
814                 return "image/jpeg";
815         else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
816                 return "image/png";
817         else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
818                 return "image/x-icon";
819         else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
820                 return "text/x-vcard";
821         else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
822                 return "text/html";
823         else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
824                 return "text/html";
825         else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
826                 return "text/vnd.wap.wml";
827         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
828                 return "text/vnd.wap.wmlscript";
829         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
830                 return "application/vnd.wap.wmlc";
831         else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
832                 return "application/vnd.wap.wmlscriptc";
833         else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
834                 return "image/vnd.wap.wbmp";
835         else
836                 /* and let xdgmime do the fallback. */
837                 return xdg_mime_get_mime_type_from_file_name(what);
838 }
839
840 static HashList *IconHash = NULL;
841
842 typedef struct IconName IconName;
843
844 struct IconName {
845         char *FlatName;
846         char *FileName;
847 };
848
849 static void DeleteIcon(void *IconNamePtr)
850 {
851         IconName *Icon = (IconName*) IconNamePtr;
852         free(Icon->FlatName);
853         free(Icon->FileName);
854         free(Icon);
855 }
856
857 /*
858 static const char *PrintFlat(void *IconNamePtr)
859 {
860         IconName *Icon = (IconName*) IconNamePtr;
861         return Icon->FlatName;
862 }
863 static const char *PrintFile(void *IconNamePtr)
864 {
865         IconName *Icon = (IconName*) IconNamePtr;
866         return Icon->FileName;
867 }
868 */
869
870 #define GENSTR "x-generic"
871 #define IGNORE_PREFIX_1 "gnome-mime"
872 int LoadIconDir(const char *DirName)
873 {
874         DIR *filedir = NULL;
875         struct dirent *filedir_entry;
876         int d_namelen;
877         int d_without_ext;
878         IconName *Icon;
879
880         filedir = opendir (DirName);
881         IconHash = NewHash(1, NULL);
882         if (filedir == NULL) {
883                 return 0;
884         }
885
886         while ((filedir_entry = readdir(filedir)))
887         {
888                 char *MinorPtr;
889                 char *PStart;
890 #ifdef _DIRENT_HAVE_D_NAMELEN
891                 d_namelen = filedir_entry->d_namelen;
892 #else
893                 d_namelen = strlen(filedir_entry->d_name);
894 #endif
895                 d_without_ext = d_namelen;
896                 while ((d_without_ext > 0) && (filedir_entry->d_name[d_without_ext] != '.'))
897                         d_without_ext --;
898                 if ((d_without_ext == 0) || (d_namelen < 3))
899                         continue;
900
901                 if ((sizeof(IGNORE_PREFIX_1) < d_namelen) &&
902                     (strncmp(IGNORE_PREFIX_1, 
903                              filedir_entry->d_name, 
904                              sizeof(IGNORE_PREFIX_1) - 1) == 0)) {
905                         PStart = filedir_entry->d_name + sizeof(IGNORE_PREFIX_1);
906                         d_without_ext -= sizeof(IGNORE_PREFIX_1);
907                 }
908                 else {
909                         PStart = filedir_entry->d_name;
910                 }
911                 Icon = malloc(sizeof(IconName));
912
913                 Icon->FileName = malloc(d_namelen + 1);
914                 memcpy(Icon->FileName, filedir_entry->d_name, d_namelen + 1);
915
916                 Icon->FlatName = malloc(d_without_ext + 1);
917                 memcpy(Icon->FlatName, PStart, d_without_ext);
918                 Icon->FlatName[d_without_ext] = '\0';
919                 /* Try to find Minor type in image-jpeg */
920                 MinorPtr = strchr(Icon->FlatName, '-');
921                 if (MinorPtr != NULL) {
922                         size_t MinorLen;
923                         MinorLen = 1 + d_without_ext - (MinorPtr - Icon->FlatName + 1);
924                         if ((MinorLen == sizeof(GENSTR)) && 
925                             (strncmp(MinorPtr + 1, GENSTR, sizeof(GENSTR)) == 0)) {
926                                 /* ok, we found a generic filename. cut the generic. */
927                                 *MinorPtr = '\0';
928                                 d_without_ext = d_without_ext - (MinorPtr - Icon->FlatName);
929                         }
930                         else { /* Map the major / minor separator to / */
931                                 *MinorPtr = '/';
932                         }
933                 }
934
935 //              PrintHash(IconHash, PrintFlat, PrintFile);
936 //              printf("%s - %s\n", Icon->FlatName, Icon->FileName);
937                 Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
938 //              PrintHash(IconHash, PrintFlat, PrintFile);
939         }
940         closedir(filedir);
941         return 1;
942 }
943
944 const char *GetIconFilename(char *MimeType, size_t len)
945 {
946         void *vIcon;
947         IconName *Icon;
948         
949         if(IconHash == NULL)
950                 return NULL;
951
952         GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
953         /* didn't find the exact mimetype? try major only. */
954         if (Icon == NULL) {
955                 char * pMinor;
956                 pMinor = strchr(MimeType, '/');
957                 if (pMinor != NULL) {
958                         *pMinor = '\0';
959                         GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
960                                 Icon = (IconName*) vIcon;
961                 }
962         }
963         if (Icon == NULL) {
964                 return NULL;
965         }
966
967         /*printf("Getting: [%s] == [%s] -> [%s]\n", MimeType, Icon->FlatName, Icon->FileName);*/
968         return Icon->FileName;
969 }
970
971 void ShutDownLibCitadelMime(void)
972 {
973         DeleteHash(&IconHash);
974 }