Back to index

libcitadel  8.12
Classes | Functions
mimeparser_test.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stddef.h>
#include "../lib/libcitadel.h"

Go to the source code of this file.

Classes

struct  ma_info

Functions

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)
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)
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)
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 main (int argc, char *argv[])

Class Documentation

struct ma_info

Definition at line 19 of file mimeparser_test.c.

Class Members
char chosen_part
int chosen_pref
int did_print
int dont_decode
int freeze
int is_ma
const char * printme
int use_fo_hooks

Function Documentation

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 
) [static]

Definition at line 35 of file mimeparser_test.c.

{
       struct ma_info *ma;
       
       ma = (struct ma_info *)cbuserdata;
       if (ma->is_ma == 0) {
              printf("part=%s|%s|%s|%s|%s|%ld|%s|%s\n",
                     name, 
                     filename, 
                     partnum, 
                     disp, 
                     cbtype, 
                     (long)length, 
                     cbid, 
                     cbcharset);
       }
}

Here is the caller graph for this function:

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 
) [static]

Definition at line 66 of file mimeparser_test.c.

{
       struct ma_info *ma;
       
       ma = (struct ma_info *)cbuserdata;
       if (!strcasecmp(cbtype, "multipart/alternative")) {
              ++ma->is_ma;
       }

       if (ma->is_ma == 0) {
              printf("pref=%s|%s\n", partnum, cbtype);
       }
}

Here is the caller graph for this function:

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 
) [static]

Definition at line 93 of file mimeparser_test.c.

{
       struct ma_info *ma;
       
       ma = (struct ma_info *)cbuserdata;
       if (ma->is_ma == 0) {
              printf("suff=%s|%s\n", partnum, cbtype);
       }
       if (!strcasecmp(cbtype, "multipart/alternative")) {
              --ma->is_ma;
       }
}

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 203 of file mimeparser_test.c.

{
       char a;
       int fd;
       char *filename = NULL;
       struct stat statbuf;
       const char *Err;

       StrBuf *MimeBuf;
       long MimeLen;
       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, "dpf:P:")) != EOF)
       {
              switch (a) {
              case 'f':
                     filename = optarg;
                     break;
              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");
              return 1;
       }
       fd = open(filename, 0);
       if (fd < 0) {
              printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
              return 1;
       }
       if (fstat(fd, &statbuf) == -1) {
              printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
              return 1;
       }
       MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
       if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
              printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
              FreeStrBuf(&MimeBuf);
              return 1;
       }
       MimeLen = StrLength(MimeBuf);
       MimeStr = SmashStrBuf(&MimeBuf);

       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;
}

Here is the call graph for this function:

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 
) [static]

Definition at line 120 of file mimeparser_test.c.

{
       int rc = 0;

       /* Silently go away if there's already a download open. */

       struct ma_info *ma;
       
       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);
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function: