Parser functions

mailimf_address_list_parse

int
mailimf_address_list_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_address_list ** result);
        

mailimf_address_list_parse() parse a list of addresses in RFC 2822 form.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-33. parsing a list of addresses

#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_address_list * addr_list;
	size_t current_index;        
	
	current_index = 0;
	r = mailimf_address_list_parse(mem, stat_info.st_size,
          &current_index, &addr_list);
	if (r == MAILIMF_NO_ERROR) {
	    display_address_list(addr_list);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_address_list_free(addr_list);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_address_parse

#include <libetpan/libetpan.h>

int
mailimf_address_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_address ** result);
        

mailimf_address_parse() parse an address in RFC 2822 form.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-34. parsing an address

#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_address * addr;
	size_t current_index;        
	
	current_index = 0;
	r = mailimf_address_parse(mem, stat_info.st_size,
          &current_index, &addr);
	if (r == MAILIMF_NO_ERROR) {
	    display_address(addr);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_address_free(addr);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_body_parse

#include <libetpan/libetpan.h>

int mailimf_body_parse(char * message, size_t length,
		       size_t * index,
		       struct mailimf_body ** result);
        

mailimf_body_parse() parse text body of a message.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-35. parsing a message body

#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_body * b;
        struct mailimf_fields * f;
	size_t current_index;        
	size_t size;
	
	size = stat_info.st_size;
	current_index = 0;
	r = mailimf_fields_parse(mem, size, &current_index, &f);
	if (r == MAILIMF_NO_ERROR) {	
	  r = mailimf_crlf_parse(mem, size, &current_index);
	  /* ignore parse error of crlf */
	  
	  r = mailimf_body_parse(mem, size, &current_index, &b);
	  if (r == MAILIMF_NO_ERROR) {
	    
	    display_body(b);
	    /* do the things */
	    status = EXIT_SUCCESS;
	    mailimf_body_free(b);
	  }
	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_envelope_and_optional_fields_parse

#include <libetpan/libetpan.h>

int
mailimf_envelope_and_optional_fields_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_fields ** result);
        

mailimf_envelope_and_optional_fields_parse() returns a list of most useful headers (parsed). The other headers will be placed in the list in a non-parsed form.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-36. parsing commonly used fields and return other fields in a non-parsed form

#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_envelope_and_optional_fields_parse(mem, stat_info.st_size,
            &current_index, &f);
	if (r == MAILIMF_NO_ERROR) {
	    display_fields(m);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_envelope_fields_parse

#include <libetpan/libetpan.h>

int mailimf_envelope_fields_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_fields ** result);
        

mailimf_envelope_fields_parse() return a list of most useful headers (parsed).

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-37. parsing commonly used fields

#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_envelope_fields_parse(mem, stat_info.st_size,
            &current_index, &f);
	if (r == MAILIMF_NO_ERROR) {
	    display_fields(m);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_optional_fields_parse

#include <libetpan/libetpan.h>

int
mailimf_optional_fields_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_fields ** result);
        

mailimf_optional_fields_parse return a list of non-parsed headers.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-38. parsing optional fields

#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_optional_fields_parse(mem, stat_info.st_size,
            &current_index, &f);
	if (r == MAILIMF_NO_ERROR) {
	    display_fields(m);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_fields_parse

#include <libetpan/libetpan.h>

int mailimf_fields_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_fields ** result);
        

mailimf_fields_parse() parse headers of a message.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-39. parsing header fields

#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) {
	    display_fields(f);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_fields_free(f);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
            

mailimf_ignore_field_parse

#include <libetpan/libetpan.h>

int mailimf_ignore_field_parse(char * message, size_t length,
    size_t * index);
        

mailimf_ignore_field_parse() skip the next header.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-40. skipping fields

#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) {
	size_t current_index;        
	
	current_index = 0;
	r = mailimf_ignore_field_parse(mem, stat_info.st_size,
            &current_index);
	if (r == MAILIMF_NO_ERROR) {
	  /* do the things */
	  status = EXIT_SUCCESS;
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_mailbox_list_parse

#include <libetpan/libetpan.h>

int
mailimf_mailbox_list_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_mailbox_list ** result);
        

mailimf_mailbox_list_parse() parse a list of mailboxes in RFC 2822 form.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-41. parsing a list of mailboxes

#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_mailbox_list * mb_list;
	size_t current_index;        
	
	current_index = 0;
	r = mailimf_mailbox_list_parse(mem, stat_info.st_size,
          &current_index, &mb_list);
	if (r == MAILIMF_NO_ERROR) {
	    display_mailbox_list(mb_list);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_mailbox_list_free(mb_list);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_mailbox_parse

        
#include <libetpan/libetpan.h>

int mailimf_mailbox_parse(char * message, size_t length,
    size_t * index,
    struct mailimf_mailbox ** result);
        

mailimf_mailbox_parse parse a mailbox in RFC 2822 form.

return MAILIMF_NO_ERROR on success, MAILIMF_ERROR_XXX on error.

Example 3-42. parsing a mailbox

#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_mailbox_list * mb_list;
	size_t current_index;        
	
	current_index = 0;
	r = mailimf_mailbox_parse(mem, stat_info.st_size,
          &current_index, &mb_list);
	if (r == MAILIMF_NO_ERROR) {
	    display_mailbox_list(mb_list);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_mailbox_free(mb_list);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}
          

mailimf_message_parse

#include <libetpan/libetpan.h>

int mailimf_message_parse(char * message, size_t length,
			  size_t * index,
			  struct mailimf_message ** result);
        

mailimf_message_parse parse message (headers and body).

Example 3-43. parsing a message

#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_message * m;
	size_t current_index;        
	
	current_index = 0;
	r = mailimf_message_parse(mem, stat_info.st_size,
          &current_index, &m);
	if (r == MAILIMF_NO_ERROR) {
	    display_message(m);
	  /* do the things */
	  status = EXIT_SUCCESS;
	  mailimf_message_free(m);
	}
      }
      munmap(mem, stat_info.st_size);
    }
    
    close(fd);
  }
  
  exit(status);
}