Helper functions

mailmime_transfer_encoding_get

#include <libetpan/libetpan.h>

int mailmime_transfer_encoding_get(struct mailmime_fields * fields);
        

mailmime_transfer_encoding_get() will return the standard MIME encoding mechanism.

Example 4-46. extracting MIME encoding mechanism

#include <libetpan/libetpan.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main(int argc, char ** argv)
{
  int fd;
  int r;
  
  status = EXIT_FAILURE;
  
  fd = open("message.rfc2822", O_RDONLY);
  if (fd >= 0) {
    void * mem;
    struct stat stat_info;
    
    r = fstat(fd, &stat_info);
    if (r >= 0) {
      mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
      if (mem != MAP_FAILED) {
        struct mailimf_fields * f;
	size_t current_index;
	
	current_index = 0;
	r = mailimf_fields_parse(mem, stat_info.st_size,
          &current_index, &f);
	if (r == MAILIMF_NO_ERROR) {
          struct mailmime_fields * mime_fields;

	  r = mailmime_fields_parse(f, &mime_fields);
          if (r == MAILIMF_NO_ERROR) {
            int encoding;
            
            encoding = mailmime_transfer_encoding_get(mime_fields);
            
            /* do the things */
            
	    mailmime_fields_free(mime_fields);
	    status = EXIT_SUCCESS;
          }

	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailmime_content_charset_get and mailmime_content_param_get

#include <libetpan/libetpan.h>

char * mailmime_content_charset_get(struct mailmime_content * content);

char * mailmime_content_param_get(struct mailmime_content * content,
				  char * name);

char * mailmime_extract_boundary(struct mailmime_content * content_type);
        

mailmime_content_charset_get() will return the charset parameter of MIME content type.

mailmime_content_param_get() will return the value of a given parameter of MIME content type.

mailmime_extract_boundary() will return the charset parameter of MIME content type.

Example 4-47. extracting information from MIME content type

#include <libetpan/libetpan.h>
#include <sys/stat.h>
#include <sys/mman.h>

int main(int argc, char ** argv)
{
  int fd;
  int r;
  
  status = EXIT_FAILURE;
  
  fd = open("message.rfc2822", O_RDONLY);
  if (fd >= 0) {
    void * mem;
    struct stat stat_info;
    
    r = fstat(fd, &stat_info);
    if (r >= 0) {
      mem = mmap(NULL, stat_info.st_size, PROT_READ, MAP_PRIVATE);
      if (mem != MAP_FAILED) {
        struct mailimf_fields * f;
	size_t current_index;
	
	current_index = 0;
	r = mailimf_fields_parse(mem, stat_info.st_size,
          &current_index, &f);
	if (r == MAILIMF_NO_ERROR) {
          clistiter * cur;
        
          for(cur = clist_begin(f->fld_list) ; cur != NULL ; cur =
            clist_next(cur)) {
            struct mailmime_field * mime_field;
            struct mailimf_field * field;
            
            field = clist_content(cur);
            
            if (field->fld_type == MAILIMF_FIELD_OPTIONAL_FIELD) {
              if (strcasecmp(field->fld_data.fld_optional_field->fld_name,
                "Content-Type") == 0) {
                struct mailmime_content * content_type;
                size_t current_index;

                current_index = 0;
	        r = mailmime_content_parse(field->fld_data.fld_optional_field->fld_value,
                  strlen(field->fld_data.fld_optional_field->fld_value),
                  &current_index, &content_type);
	        if (r == MAILIMF_NO_ERROR) {
                  char * charset;
                  char * name;
                  char * boundary;                  

                  charset = mailmime_content_charset_get(content_type);
                  name = mailmime_content_param_get(content_type, "name");
                  boundary = mailmime_extract_boundary(content_type);
                  
	          /* do the things */
                  
                  free(boundary);
                  
	          status = EXIT_SUCCESS;
	          mailmime_content_free(content_type);
	        }
              }
            }
          }
	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}