Updated the MIME Parser API to include Content-ID in
[citadel.git] / libcitadel / lib / mime_parser.c
index b60b18beb63994b8d2722dc77550f789b76d5a1f..68f09a277514719acd1dfc9e3cc14ffbbc1eb6fa 100644 (file)
@@ -116,6 +116,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 +128,7 @@ void mime_decode(char *partnum,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
                 void (*PreMultiPartCallBack)
                  (char *cbname,
@@ -138,6 +140,7 @@ void mime_decode(char *partnum,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
                 void (*PostMultiPartCallBack)
                  (char *cbname,
@@ -149,6 +152,7 @@ void mime_decode(char *partnum,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
                  void *userdata,
                  int dont_decode
@@ -171,7 +175,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 +207,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 +231,7 @@ void the_mime_parser(char *partnum,
                       char *cbcharset,
                       size_t cblength,
                       char *cbencoding,
+                      char *cbid,
                       void *cbuserdata),
                     void (*PreMultiPartCallBack)
                      (char *cbname,
@@ -238,6 +243,7 @@ void the_mime_parser(char *partnum,
                       char *cbcharset,
                       size_t cblength,
                       char *cbencoding,
+                      char *cbid,
                       void *cbuserdata),
                     void (*PostMultiPartCallBack)
                      (char *cbname,
@@ -249,6 +255,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 +277,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 +328,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 +370,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 +416,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 +494,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 +509,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 +534,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 +548,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 +573,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 +593,7 @@ end_parser: /* free the buffers!  end the oppression!! */
        free(content_disposition_name);
        free(filename);
        free(disposition);
+       free(id);
 }
 
 
@@ -603,6 +617,7 @@ void mime_parser(char *content_start,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
 
                 void (*PreMultiPartCallBack)
@@ -615,6 +630,7 @@ void mime_parser(char *content_start,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
 
                 void (*PostMultiPartCallBack)
@@ -627,6 +643,7 @@ void mime_parser(char *content_start,
                   char *cbcharset,
                   size_t cblength,
                   char *cbencoding,
+                  char *cbid,
                   void *cbuserdata),
 
                  void *userdata,
@@ -681,7 +698,7 @@ MimeGuess MyMimes [] = {
 };
 
 
-const char *GuessMimeType(char *data, size_t dlen)
+const char *GuessMimeType(const char *data, size_t dlen)
 {
        int MimeIndex = 0;
 
@@ -755,8 +772,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 +786,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)
@@ -837,6 +858,7 @@ int LoadIconDir(const char *DirName)
                Put(IconHash, Icon->FlatName, d_without_ext, Icon, DeleteIcon);
 //             PrintHash(IconHash, PrintFlat, PrintFile);
        }
+       closedir(filedir);
        return 1;
 }