Added mime type detection

Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
Tyrolyean 2020-04-29 22:18:08 +02:00
parent b1ef07c103
commit 5e12445ac7
No known key found for this signature in database
GPG key ID: 81EC9BAC5E9667C6
3 changed files with 54 additions and 5 deletions

View file

@ -23,6 +23,11 @@
#include <stdbool.h>
#include <sys/types.h>
struct type_file_info_t{
char* name;
char* mime_type;
};
struct email_t{
char* message;
/* From the below values you can say pretty much anything about the
@ -58,8 +63,11 @@ struct email_t{
struct email_t** submes;
struct email_t* parent;
bool base64_encoded;
struct type_file_info_t file_info;
};
int append_header(struct email_t* mail, const char* key, const char* value);
int append_to_header(struct email_t* mail, const char* pair);
@ -69,8 +77,7 @@ void propagate_insert_delete(struct email_t* mail, char* change_p,
ssize_t change);
void propagate_root_pointer(struct email_t* mail, char* change_p, char* old_p);
struct type_file_info_t{
};
struct type_file_info_t get_mime_file_info(struct email_t* mail);
#define MULTIPART_MIME "multipart/"
#define BASE64_ENC "base64"

View file

@ -98,6 +98,8 @@ struct email_t* mail_from_text(char* message, size_t length,
}
}
mail->base64_encoded = detect_base64(mail);
mail->file_info = get_mime_file_info(mail);
return mail;
}
@ -237,9 +239,18 @@ void print_mail_structure(struct email_t *email, unsigned int level){
print_mail_structure(email->submes[i], level+1);
}
}else{
printf("final message with length %lu and type [%.*s] \n",
email->message_length, (int)email->ct_len,
email->content_type);
if( email->file_info.mime_type == NULL){
printf("final message with length %lu and type "
"[%.*s] \n",
email->message_length, (int)email->ct_len,
email->content_type);
}else{
printf("final message with length %lu and type [%s] \n",
email->message_length,
email->file_info.mime_type);
}
}
return;

View file

@ -168,5 +168,36 @@ void propagate_root_pointer(struct email_t* mail, char* change_p, char* old_p){
return;
}
struct type_file_info_t get_mime_file_info(struct email_t* mail){
struct type_file_info_t fileinfo;
fileinfo.name = NULL;
fileinfo.mime_type = NULL;
if(mail->content_type == NULL){
return fileinfo;
}
char* semic = strchr(mail->content_type, ';');
size_t mime_len = 0;
if(semic > (mail->content_type + mail->ct_len)){
mime_len = mail->ct_len;
}else{
mime_len = semic - mail->content_type;
}
fileinfo.mime_type = malloc(mime_len + 1);
memset(fileinfo.mime_type, 0, mime_len + 1);
memcpy(fileinfo.mime_type, mail->content_type, mime_len);
size_t filename_len = 0;
char* filename = get_value_equals(mail->content_type, mail->ct_len,
&filename_len, "name");
if(filename != NULL){
fileinfo.name = malloc(filename_len + 1);
memset(fileinfo.name, 0, filename_len + 1);
memcpy(fileinfo.name, filename, filename_len);
}
return fileinfo;
}