X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=libcitadel%2Ftests%2Fmimeparser_test.c;h=a78fcaf18af5e2b656383412c4bfa5fa1c626116;hb=b826c3117bb7ddf1386a4811cb2eb47ea4e1097c;hp=95802ff43eae7f377dd8d22707fa39e2f2b9d9fd;hpb=e08e7a828fa90956816375bcef258c3cffaa41df;p=citadel.git diff --git a/libcitadel/tests/mimeparser_test.c b/libcitadel/tests/mimeparser_test.c index 95802ff43..a78fcaf18 100644 --- a/libcitadel/tests/mimeparser_test.c +++ b/libcitadel/tests/mimeparser_test.c @@ -22,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? */ @@ -31,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; @@ -62,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; @@ -89,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; @@ -115,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); + } } } -*/ + /* @@ -205,9 +216,9 @@ int main(int argc, char* argv[]) int dont_decode = 1; setvbuf(stdout, NULL, _IONBF, 0); + memset(&ma, 0, sizeof(struct ma_info)); - - while ((a = getopt(argc, argv, "dpf:")) != EOF) + while ((a = getopt(argc, argv, "dpf:P:")) != EOF) { switch (a) { case 'f': @@ -219,9 +230,10 @@ int main(int argc, char* argv[]) case 'd': dont_decode = 0; break; + case 'P': + ma.printme = optarg; } } - StartLibCitadel(8); if (filename == NULL) { printf("Filename requried! -f\n"); @@ -245,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, dont_decode); - + 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;