removed StartLibCitadel()
[citadel.git] / libcitadel / tests / mimeparser_test.c
index 83d5bf0f5547273d52c3c3a97ea7941200bf7d2a..a78fcaf18af5e2b656383412c4bfa5fa1c626116 100644 (file)
@@ -13,7 +13,6 @@
 #include <stddef.h>
 
 
-#include "stringbuf_test.h"
 #include "../lib/libcitadel.h"
 
 /* shamelesly copied from msgbase.h */
@@ -23,6 +22,7 @@ struct ma_info {
                                 * digging through a subsection */
        int did_print;          /* One alternative has been displayed */
        char chosen_part[128];  /* Which part of a m/a did we choose? */
+       const char *printme;
        int chosen_pref;        /* Chosen part preference level (lower is better) */
        int use_fo_hooks;       /* Use fixed output hooks */
        int dont_decode;        /* should we call the decoder or not? */
@@ -32,17 +32,17 @@ struct ma_info {
 /*
  * Callback function for mime parser that simply lists the part
  */
-void list_this_part(char *name, 
-                   char *filename, 
-                   char *partnum, 
-                   char *disp,
-                   void *content, 
-                   char *cbtype, 
-                   char *cbcharset, 
-                   size_t length, 
-                   char *encoding,
-                   char *cbid, 
-                   void *cbuserdata)
+static void list_this_part(char *name, 
+                          char *filename, 
+                          char *partnum, 
+                          char *disp,
+                          void *content, 
+                          char *cbtype, 
+                          char *cbcharset, 
+                          size_t length, 
+                          char *encoding,
+                          char *cbid, 
+                          void *cbuserdata)
 {
        struct ma_info *ma;
        
@@ -63,17 +63,17 @@ void list_this_part(char *name,
 /* 
  * Callback function for multipart prefix
  */
-void list_this_pref(char *name, 
-                   char *filename, 
-                   char *partnum, 
-                   char *disp,
-                   void *content, 
-                   char *cbtype, 
-                   char *cbcharset, 
-                   size_t length, 
-                   char *encoding,
-                   char *cbid, 
-                   void *cbuserdata)
+static void list_this_pref(char *name, 
+                          char *filename, 
+                          char *partnum, 
+                          char *disp,
+                          void *content, 
+                          char *cbtype, 
+                          char *cbcharset, 
+                          size_t length, 
+                          char *encoding,
+                          char *cbid, 
+                          void *cbuserdata)
 {
        struct ma_info *ma;
        
@@ -90,17 +90,17 @@ void list_this_pref(char *name,
 /* 
  * Callback function for multipart sufffix
  */
-void list_this_suff(char *name, 
-                   char *filename, 
-                   char *partnum, 
-                   char *disp,
-                   void *content, 
-                   char *cbtype, 
-                   char *cbcharset, 
-                   size_t length, 
-                   char *encoding,
-                   char *cbid, 
-                   void *cbuserdata)
+static void list_this_suff(char *name, 
+                          char *filename, 
+                          char *partnum, 
+                          char *disp,
+                          void *content, 
+                          char *cbtype, 
+                          char *cbcharset, 
+                          size_t length, 
+                          char *encoding,
+                          char *cbid, 
+                          void *cbuserdata)
 {
        struct ma_info *ma;
        
@@ -116,39 +116,49 @@ void list_this_suff(char *name,
 
 /*
  * Callback function for mime parser that opens a section for downloading
- * /
-void mime_download(char *name, 
-                  char *filename, 
-                  char *partnum, 
-                  char *disp,
-                  void *content, 
-                  char *cbtype, 
-                  char *cbcharset, 
-                  size_t length,
-                  char *encoding, 
-                  char *cbid, 
-                  void *cbuserdata)
+ */
+static void mime_download(char *name, 
+                         char *filename, 
+                         char *partnum, 
+                         char *disp,
+                         void *content, 
+                         char *cbtype, 
+                         char *cbcharset, 
+                         size_t length,
+                         char *encoding, 
+                         char *cbid, 
+                         void *cbuserdata)
 {
-       int rv = 0;
-       FILE *download_fp;
+       int rc = 0;
 
-       /* Silently go away if there's already a download open. * /
+       /* Silently go away if there's already a download open. */
 
-       if (
-               (!IsEmptyStr(partnum) && (!strcasecmp(CC->download_desired_section, partnum)))
-       ||      (!IsEmptyStr(cbid) && (!strcasecmp(CC->download_desired_section, cbid)))
-       ) {
-               download_fp = STDOUT;
-
-       
-               rv = fwrite(content, length, 1, download_fp);
-               fflush(CC->download_fp);
-               rewind(CC->download_fp);
+       struct ma_info *ma;
        
-               OpenCmdResult(filename, cbtype);
+       ma = (struct ma_info *)cbuserdata;
+
+       if ((!IsEmptyStr(partnum) && (!strcasecmp(ma->printme, partnum)))) {
+               char *decoded = NULL;
+               size_t bytes_decoded;
+               rc = mime_decode_now (content, 
+                                     length,
+                                     encoding,
+                                     &decoded,
+                                     &bytes_decoded);
+               if (rc < 0) {
+                       printf("failed to decode content\n");
+                       return;
+               }
+               if (rc == 0){
+                       rc = write(STDOUT_FILENO, content, length);
+               }
+               else {
+                       rc = write(STDOUT_FILENO, decoded, bytes_decoded);
+                       free(decoded);
+               }
        }
 }
-*/
+
 
 
 /*
@@ -203,11 +213,12 @@ int main(int argc, char* argv[])
        char *MimeStr;
        struct ma_info ma;
        int do_proto = 0;
+       int dont_decode = 1;
 
        setvbuf(stdout, NULL, _IONBF, 0);
+       memset(&ma, 0, sizeof(struct ma_info));
 
-
-       while ((a = getopt(argc, argv, "F:f:p")) != EOF)
+       while ((a = getopt(argc, argv, "dpf:P:")) != EOF)
        {
                switch (a) {
                case 'f':
@@ -216,9 +227,13 @@ int main(int argc, char* argv[])
                case 'p':
                        do_proto = 1;
                        break;
+               case 'd':
+                       dont_decode = 0;
+                       break;
+               case 'P':
+                       ma.printme = optarg;
                }
        }
-       StartLibCitadel(8);
 
        if (filename == NULL) {
                printf("Filename requried! -f\n");
@@ -242,14 +257,15 @@ int main(int argc, char* argv[])
        MimeLen = StrLength(MimeBuf);
        MimeStr = SmashStrBuf(&MimeBuf);
 
-       memset(&ma, 0, sizeof(struct ma_info));
-
-       mime_parser(MimeStr, MimeStr + MimeLen,
-                   (do_proto ? *list_this_part : NULL),
-                   (do_proto ? *list_this_pref : NULL),
-                   (do_proto ? *list_this_suff : NULL),
-                   (void *)&ma, 1);
-
+       if (ma.printme == NULL)
+               mime_parser(MimeStr, MimeStr + MimeLen,
+                           (do_proto ? *list_this_part : NULL),
+                           (do_proto ? *list_this_pref : NULL),
+                           (do_proto ? *list_this_suff : NULL),
+                           (void *)&ma, dont_decode);
+       else 
+               mime_parser(MimeStr, MimeStr + MimeLen,
+                           *mime_download, NULL, NULL, (void *)&ma, dont_decode);
 
        free(MimeStr);
        return 0;