]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/mime_parser.c
* shutdown libcitadel and xdgmime properly
[citadel.git] / libcitadel / lib / mime_parser.c
index 14f292a6def95a637e5fe9275f2804e03a21f88a..026d0555009cd76d66865dd327b484274091f6c5 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "xdgmime/xdgmime.h"
 #include "libcitadel.h"
+#include "libcitadellocal.h"
 
 void extract_key(char *target, char *source, char *key)
 {
@@ -116,6 +117,7 @@ void mime_decode(char *partnum,
                 char *part_start, size_t length,
                 char *content_type, char *charset, char *encoding,
                 char *disposition,
+                char *id,
                 char *name, char *filename,
                 void (*CallBack)
                  (char *cbname,
@@ -127,6 +129,7 @@ void mime_decode(char *partnum,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
                 void (*PreMultiPartCallBack)
                  (char *cbname,
@@ -138,6 +141,7 @@ void mime_decode(char *partnum,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
                 void (*PostMultiPartCallBack)
                  (char *cbname,
@@ -149,6 +153,7 @@ void mime_decode(char *partnum,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
                  void *userdata,
                  int dont_decode
@@ -171,7 +176,7 @@ void mime_decode(char *partnum,
                if (CallBack != NULL) {
                        CallBack(name, filename, fixed_partnum(partnum),
                                disposition, part_start,
-                               content_type, charset, length, encoding, userdata);
+                               content_type, charset, length, encoding, id, userdata);
                        }
                return;
        }
@@ -203,7 +208,7 @@ void mime_decode(char *partnum,
        if (bytes_decoded > 0) if (CallBack != NULL) {
                CallBack(name, filename, fixed_partnum(partnum),
                        disposition, decoded,
-                       content_type, charset, bytes_decoded, "binary", userdata);
+                       content_type, charset, bytes_decoded, "binary", id, userdata);
        }
 
        free(decoded);
@@ -227,6 +232,7 @@ void the_mime_parser(char *partnum,
                       char *cbcharset,
                       size_t cblength,
                       char *cbencoding,
+                      char *cbid,
                       void *cbuserdata),
                     void (*PreMultiPartCallBack)
                      (char *cbname,
@@ -238,6 +244,7 @@ void the_mime_parser(char *partnum,
                       char *cbcharset,
                       size_t cblength,
                       char *cbencoding,
+                      char *cbid,
                       void *cbuserdata),
                     void (*PostMultiPartCallBack)
                      (char *cbname,
@@ -249,6 +256,7 @@ void the_mime_parser(char *partnum,
                       char *cbcharset,
                       size_t cblength,
                       char *cbencoding,
+                      char *cbid,
                       void *cbuserdata),
                      void *userdata,
                      int dont_decode
@@ -270,6 +278,7 @@ void the_mime_parser(char *partnum,
        size_t content_length;
        char *encoding;
        char *disposition;
+       char *id;
        char *name = NULL;
        char *content_type_name;
        char *content_disposition_name;
@@ -320,6 +329,9 @@ void the_mime_parser(char *partnum,
        disposition = malloc(SIZ);
        memset(disposition, 0, SIZ);
 
+       id = malloc(SIZ);
+       memset(id, 0, SIZ);
+
        /* If the caller didn't supply an endpointer, generate one by measure */
        if (content_end == NULL) {
                content_end = &content_start[strlen(content_start)];
@@ -359,6 +371,11 @@ void the_mime_parser(char *partnum,
                                extract_key(content_disposition_name, disposition, "name");
                                extract_key(filename, disposition, "filename");
                        }
+                       if (!strncasecmp(header, "Content-ID:", 11)) {
+                               strcpy(id, &header[11]);
+                               striplt(id);
+                               stripallbut(id, '<', '>');
+                       }
                        if (!strncasecmp(header, "Content-length: ", 15)) {
                                char clbuf[10];
                                safestrncpy(clbuf, &header[15], sizeof clbuf);
@@ -400,7 +417,7 @@ void the_mime_parser(char *partnum,
                if (PreMultiPartCallBack != NULL) {
                        PreMultiPartCallBack("", "", partnum, "",
                                NULL, content_type, charset,
-                               0, encoding, userdata);
+                               0, encoding, id, userdata);
                }
 
                /* Figure out where the boundaries are */
@@ -478,7 +495,7 @@ void the_mime_parser(char *partnum,
 
                if (PostMultiPartCallBack != NULL) {
                        PostMultiPartCallBack("", "", partnum, "", NULL,
-                               content_type, charset, 0, encoding, userdata);
+                               content_type, charset, 0, encoding, id, userdata);
                }
                goto end_parser;
        }
@@ -493,18 +510,15 @@ void the_mime_parser(char *partnum,
                }
                part_end = content_end;
 
-               /******
-                * I thought there was an off-by-one error here, but there isn't.
-                * This probably means that there's an off-by-one error somewhere
-                * else ... or maybe only in certain messages?
-               --part_end;
-               --length;
-               ******/
-               
-               /* Truncate if the header told us to */
-               if ( (content_length > 0) && (length > content_length) ) {
-                       length = content_length;
-               }
+
+               /* The following code will truncate the MIME part to the size
+                * specified by the Content-length: header.   We have commented it
+                * out because these headers have a tendency to be wrong.
+                *
+                *      if ( (content_length > 0) && (length > content_length) ) {
+                *              length = content_length;
+                *      }
+                 */
 
                /* Sometimes the "name" field is tacked on to Content-type,
                 * and sometimes it's tacked on to Content-disposition.  Use
@@ -521,7 +535,7 @@ void the_mime_parser(char *partnum,
                 */
                mime_decode(partnum,
                        part_start, length,
-                       content_type, charset, encoding, disposition,
+                       content_type, charset, encoding, disposition, id,
                        name, filename,
                        CallBack, NULL, NULL,
                        userdata, dont_decode
@@ -535,7 +549,7 @@ void the_mime_parser(char *partnum,
                        if (PreMultiPartCallBack != NULL) {
                                PreMultiPartCallBack("", "", partnum, "",
                                        NULL, content_type, charset,
-                                       0, encoding, userdata);
+                                       0, encoding, id, userdata);
                        }
                        if (CallBack != NULL) {
                                if (strlen(partnum) > 0) {
@@ -560,7 +574,7 @@ void the_mime_parser(char *partnum,
                        }
                        if (PostMultiPartCallBack != NULL) {
                                PostMultiPartCallBack("", "", partnum, "", NULL,
-                                       content_type, charset, 0, encoding, userdata);
+                                       content_type, charset, 0, encoding, id, userdata);
                        }
 
 
@@ -580,6 +594,7 @@ end_parser: /* free the buffers!  end the oppression!! */
        free(content_disposition_name);
        free(filename);
        free(disposition);
+       free(id);
 }
 
 
@@ -603,6 +618,7 @@ void mime_parser(char *content_start,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
 
                 void (*PreMultiPartCallBack)
@@ -615,6 +631,7 @@ void mime_parser(char *content_start,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
 
                 void (*PostMultiPartCallBack)
@@ -627,6 +644,7 @@ void mime_parser(char *content_start,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
 
                  void *userdata,
@@ -681,7 +699,7 @@ MimeGuess MyMimes [] = {
 };
 
 
-const char *GuessMimeType(char *data, size_t dlen)
+const char *GuessMimeType(const char *data, size_t dlen)
 {
        int MimeIndex = 0;
 
@@ -708,33 +726,35 @@ const char *GuessMimeType(char *data, size_t dlen)
 const char* GuessMimeByFilename(const char *what, size_t len)
 {
        /* we know some hardcoded on our own, try them... */
-       if (!strncasecmp(&what[len - 4], ".gif", 4))
+       if ((len > 3) && !strncasecmp(&what[len - 4], ".gif", 4))
                return "image/gif";
-       else if (!strncasecmp(&what[len - 3], ".js", 3))
+       else if ((len > 2) && !strncasecmp(&what[len - 3], ".js", 3))
                return  "text/javascript";
-       else if (!strncasecmp(&what[len - 4], ".txt", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".txt", 4))
                return "text/plain";
-       else if (!strncasecmp(&what[len - 4], ".css", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".css", 4))
                return "text/css";
-       else if (!strncasecmp(&what[len - 4], ".jpg", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".jpg", 4))
                return "image/jpeg";
-       else if (!strncasecmp(&what[len - 4], ".png", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".png", 4))
                return "image/png";
-       else if (!strncasecmp(&what[len - 4], ".ico", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".ico", 4))
                return "image/x-icon";
-       else if (!strncasecmp(&what[len - 5], ".html", 5))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".vcf", 4))
+               return "text/x-vcard";
+       else if ((len > 4) && !strncasecmp(&what[len - 5], ".html", 5))
                return "text/html";
-       else if (!strncasecmp(&what[len - 4], ".htm", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".htm", 4))
                return "text/html";
-       else if (!strncasecmp(&what[len - 4], ".wml", 4))
+       else if ((len > 3) && !strncasecmp(&what[len - 4], ".wml", 4))
                return "text/vnd.wap.wml";
-       else if (!strncasecmp(&what[len - 5], ".wmls", 5))
+       else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmls", 5))
                return "text/vnd.wap.wmlscript";
-       else if (!strncasecmp(&what[len - 5], ".wmlc", 5))
+       else if ((len > 4) && !strncasecmp(&what[len - 5], ".wmlc", 5))
                return "application/vnd.wap.wmlc";
-       else if (!strncasecmp(&what[len - 6], ".wmlsc", 6))
+       else if ((len > 5) && !strncasecmp(&what[len - 6], ".wmlsc", 6))
                return "application/vnd.wap.wmlscriptc";
-       else if (!strncasecmp(&what[len - 5], ".wbmp", 5))
+       else if ((len > 4) && !strncasecmp(&what[len - 5], ".wbmp", 5))
                return "image/vnd.wap.wbmp";
        else
                /* and let xdgmime do the fallback. */
@@ -755,8 +775,10 @@ static void DeleteIcon(void *IconNamePtr)
        IconName *Icon = (IconName*) IconNamePtr;
        free(Icon->FlatName);
        free(Icon->FileName);
+       free(Icon);
 }
 
+/*
 static const char *PrintFlat(void *IconNamePtr)
 {
        IconName *Icon = (IconName*) IconNamePtr;
@@ -767,6 +789,8 @@ static const char *PrintFile(void *IconNamePtr)
        IconName *Icon = (IconName*) IconNamePtr;
        return Icon->FileName;
 }
+*/
+
 #define GENSTR "x-generic"
 #define IGNORE_PREFIX_1 "gnome-mime"
 int LoadIconDir(const char *DirName)
@@ -778,7 +802,7 @@ int LoadIconDir(const char *DirName)
        IconName *Icon;
 
        filedir = opendir (DirName);
-       IconHash = NewHash();
+       IconHash = NewHash(1, NULL);
        if (filedir == NULL) {
                return 0;
        }
@@ -837,24 +861,27 @@ int LoadIconDir(const char *DirName)
                Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
 //             PrintHash(IconHash, PrintFlat, PrintFile);
        }
+       closedir(filedir);
        return 1;
 }
 
 const char *GetIconFilename(char *MimeType, size_t len)
 {
+       void *vIcon;
        IconName *Icon;
        
        if(IconHash == NULL)
                return NULL;
 
-       GetHash(IconHash, MimeType, len, (void**)&Icon);
+       GetHash(IconHash, MimeType, len, &vIcon), Icon = (IconName*) vIcon;
        /* didn't find the exact mimetype? try major only. */
        if (Icon == NULL) {
                char * pMinor;
                pMinor = strchr(MimeType, '/');
                if (pMinor != NULL) {
                        *pMinor = '\0';
-                       GetHash(IconHash, MimeType, pMinor - MimeType, (void**)&Icon);
+                       GetHash(IconHash, MimeType, pMinor - MimeType, &vIcon),
+                               Icon = (IconName*) vIcon;
                }
        }
        if (Icon == NULL) {
@@ -865,7 +892,7 @@ const char *GetIconFilename(char *MimeType, size_t len)
        return Icon->FileName;
 }
 
-void ShutDownLibCitadel(void)
+void ShutDownLibCitadelMime(void)
 {
        DeleteHash(&IconHash);
 }