From 5814135512ffb612ac874fad0b089fdda3338236 Mon Sep 17 00:00:00 2001 From: Tyrolyean Date: Wed, 29 Apr 2020 16:29:36 +0200 Subject: [PATCH] Added change of header propagation Signed-off-by: Tyrolyean --- include/mail.h | 6 ++++++ src/attach.c | 3 +++ src/mail.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/include/mail.h b/include/mail.h index 62e290f..ac7a1ce 100644 --- a/include/mail.h +++ b/include/mail.h @@ -21,6 +21,7 @@ #include #include #include +#include struct email_t{ char* message; @@ -56,6 +57,11 @@ struct email_t{ int append_header(struct email_t* mail, const char* key, const char* value); int append_to_header(struct email_t* mail, const char* pair); +struct email_t* get_root_mail(struct email_t* mail); + +void propagate_insert_delete(struct email_t* mail, char* change_p, + ssize_t change); + #define MULTIPART_MIME "multipart/" #endif /* MAIL_H */ diff --git a/src/attach.c b/src/attach.c index a3d96e6..a04b36c 100644 --- a/src/attach.c +++ b/src/attach.c @@ -64,10 +64,12 @@ struct email_t* mail_from_text(char* message, size_t length, if(mime_type != NULL){ mail->ct_len = value_length; mail->content_type = mime_type; + if(verbose){ printf("Found content type: %.*s\n", (int)value_length, mime_type); } + if(strncasecmp(mime_type, MULTIPART_MIME, strlen(MULTIPART_MIME)) == 0){ /* We have multiple messages cramped inside this @@ -88,6 +90,7 @@ struct email_t* mail_from_text(char* message, size_t length, (int)bd_len, bd); } } + /* Expands the mail message to multiple ones */ unravel_multipart_mail(mail); } diff --git a/src/mail.c b/src/mail.c index e34cd01..f711cf7 100644 --- a/src/mail.c +++ b/src/mail.c @@ -67,9 +67,15 @@ int append_to_header(struct email_t* mail, const char* pair){ buffer[0] = '\r'; buffer[1] = '\n'; strcat(buffer, pair); + struct email_t* root = get_root_mail(mail); + size_t root_offset = mail->header_len + (mail->message - root->message); mail->message=insert_string(mail->message, buffer, mail->message_length, mail->header_len); + + propagate_insert_delete(root, root->message+root_offset, + strlen(buffer)); + free(buffer); redetect_body_head(mail); @@ -77,3 +83,43 @@ int append_to_header(struct email_t* mail, const char* pair){ } +struct email_t* get_root_mail(struct email_t* mail){ + + if(mail == NULL){ + return NULL; + } + + if(mail->parent == NULL){ + return mail; + }else{ + return get_root_mail(mail->parent); + } + +} + +/* This propagates a size change inside the root email down to all submails + * Call this function with the root, changes will propagate down via recursion! + * The change pointer should point to the last character IN FRONT of the change. + * an insert has a positive amount of chane, a delete a negative one. + */ +void propagate_insert_delete(struct email_t* mail, char* change_p, + ssize_t change){ + + if(mail == NULL || change_p == NULL || change == 0){ + /* MISSION ABORT!! */ + return; + } + + if(mail->message >= change_p){ + mail->message += change; + } + + if(mail->is_multipart){ + for(size_t i = 0; i < mail->submes_cnt; i++){ + propagate_insert_delete(mail->submes[i], change_p, + change); + } + } + + return; +}