diff --git a/include/mail.h b/include/mail.h index 44272fc..c0ea07b 100644 --- a/include/mail.h +++ b/include/mail.h @@ -20,6 +20,7 @@ #include #include +#include struct email_t{ char* message; @@ -29,6 +30,10 @@ struct email_t{ * there was no clear distinction between header and body... */ size_t header_len, body_offset, message_length; + bool is_multipart; + char* boundary; + size_t submes_cnt; + struct email_t** submes; }; int append_header(struct email_t* mail, const char* key, const char* value); diff --git a/include/tools.h b/include/tools.h index 0f571f7..75c2f4e 100644 --- a/include/tools.h +++ b/include/tools.h @@ -18,10 +18,15 @@ #ifndef TOOLS_H #define TOOLS_H +#include "mail.h" #include char* insert_string(char * destination, const char* source, size_t dest_orig_len, size_t offset); +char* search_header_key(struct email_t* mail, const char* key); + +char* get_value_from_key(size_t* val_len, size_t key_offset, + struct email_t* mail); #endif /* TOOLS_H */ diff --git a/src/attach.c b/src/attach.c index ec40265..58f890d 100644 --- a/src/attach.c +++ b/src/attach.c @@ -20,6 +20,7 @@ #include "detect.h" #include "config.h" #include "mail.h" +#include "tools.h" #include #include @@ -33,8 +34,24 @@ struct email_t mail_from_text(char* message, size_t length){ memset(&mail, 0, sizeof(mail)); mail.message = message; mail.message_length = length; + mail.submes_cnt = 0; + mail.submes = NULL; redetect_body_head(&mail); + char* cont_type = search_header_key(&mail, "Content-Type"); + if(cont_type == NULL){ + /* Halleluja, I've got nothing to do! WOO */ + mail.is_multipart = false; + return mail; + }else{ + size_t value_length = 0; + char * mime_type = get_value_from_key(&value_length, + cont_type - mail.message, &mail); + if(mime_type != NULL){ + printf("Found message mime type: %.*s\n", + (int)value_length, mime_type); + } + } return mail; } diff --git a/src/tools.c b/src/tools.c index 879f753..b0784bd 100644 --- a/src/tools.c +++ b/src/tools.c @@ -16,10 +16,12 @@ */ #include "tools.h" +#include "mail.h" #include #include #include +#include /* Takes a string destination and inserts at the given point offset the string * in source. Really more like a stringcat with shifting the end. @@ -47,3 +49,66 @@ char* insert_string(char * destination, const char* source, } +/* Searches the given header for the key provided and, if found, returns the + * pointer to that key NOT the value + */ +char* search_header_key(struct email_t* mail, const char* key){ + + if(mail == NULL || key == NULL){ + return NULL; + } + size_t keylen = strlen(key); + + for(size_t i = 0; i < (mail->header_len - keylen); i++){ + if(mail->message[i] == '\n'){ + if(strncasecmp(&mail->message[i+1], key, keylen) == 0){ + return &mail->message[i+1]; + } + } + } + + return NULL; +} + +/* If the key doesn't have a value, or nothing can be found, NULL is returned, + * otherwise a pointer to the first character of the value is returned. + */ +char* get_value_from_key(size_t* val_len, size_t key_offset, + struct email_t* mail){ + + if(val_len == NULL || mail == NULL){ + + return NULL; + } + + size_t colon = 0; + for(size_t i = key_offset; i < mail->header_len; i++){ + if(mail->message[i] == ':'){ + colon = i; + } + } + + colon++; + char* val = NULL; + for(size_t i = colon; i < (mail->header_len-1); i++){ + if(isalnum(mail->message[i])){ + val = &mail->message[i]; + } + + if(mail->message[i] == '\n' && !isblank(mail->message[i+1])){ + if(val != NULL){ + *val_len = &mail->message[i] - 1 - val; + }else{ + *val_len = 0; + } + return val; + } + } + if(val != NULL){ + *val_len = &mail->message[mail->header_len] - val; + }else{ + *val_len = 0; + } + + return val; +}