From 5f898f4e492d5df78cd0014ce8c03acd3a7eac83 Mon Sep 17 00:00:00 2001 From: tyrolyean Date: Fri, 1 May 2020 01:11:44 +0200 Subject: [PATCH] Add link insert Signed-off-by: tyrolyean --- include/mail.h | 2 +- src/attach.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/mail.c | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/include/mail.h b/include/mail.h index d47facc..d362ef5 100644 --- a/include/mail.h +++ b/include/mail.h @@ -71,7 +71,7 @@ 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); - +int append_to_body(struct email_t* mail, const char* text); int remove_mail(struct email_t* mail); struct email_t* get_root_mail(struct email_t* mail); diff --git a/src/attach.c b/src/attach.c index 02a7114..0ca51fb 100644 --- a/src/attach.c +++ b/src/attach.c @@ -319,6 +319,40 @@ finish: return mess; } +/* This function returns the first message where it makes sense to append the + * attachment message to. To differentiate between text and html the mime type + * is sent along + */ +struct email_t* get_appendable_mail(struct email_t* mail, + const char* mime_type){ + + if(mail->is_multipart){ + struct email_t* ret = NULL; + for(size_t i = 0; i < mail->submes_cnt; i++){ + struct email_t* ret2 = + get_appendable_mail(mail->submes[i], mime_type); + if(ret2 != NULL){ + ret = ret2; + } + } + return ret; + + } + + if(mail->base64_encoded || mail->file_info.mime_type == NULL || + mail->file_info.name != NULL){ + return NULL; + } + + if(strcasecmp(mail->file_info.mime_type, mime_type) == 0){ + return mail; + }else{ + return NULL; + } + + +} + /* This function RECURSIVELY replaces ALL base 64 encoded messages above the * threshold set in the config file with links to that file in the HTML format. * Call this function with the mail ROOT object if you want to replace all @@ -358,6 +392,15 @@ int replace_files(struct email_t* mail, const char* dirname, bool* created){ } } + struct email_t* root = get_root_mail(mail); + if(root == NULL){ + return -1; + } + struct email_t* txt_app = get_appendable_mail(root, "text/plain"); + struct email_t* html_app = get_appendable_mail(root, "text/html"); + if(txt_app == NULL && html_app == NULL){ + return -1; + } char* chosen_filename = NULL; if(mail->base64_encoded){ if(base64_decode_file(dirname, mail, &chosen_filename) < 0){ @@ -424,8 +467,14 @@ int replace_files(struct email_t* mail, const char* dirname, bool* created){ mail->file_info.mime_type, url_base, chosen_filename+directory_len); - printf(html_buffer); - printf(text_buffer); + + if(txt_app != NULL){ + append_to_body(txt_app, text_buffer); + } + if(html_app != NULL){ + append_to_body(html_app, html_buffer); + + } free(html_buffer); free(text_buffer); diff --git a/src/mail.c b/src/mail.c index ad9ea74..96cdfe1 100644 --- a/src/mail.c +++ b/src/mail.c @@ -79,6 +79,44 @@ int append_to_header(struct email_t* mail, const char* pair){ } propagate_size_change(mail, strlen(buffer)); + propagate_root_pointer(root, new_root, old_root); + propagate_insert_delete(root, root->message+root_offset, + strlen(buffer)); + + mail->header_len += strlen(buffer); + mail->body_offset += strlen(buffer); + + free(buffer); + redetect_body_head(mail); + + return 0; + +} + +/* Append the string text DIRECTLY to the end of the body of the message mail*/ +int append_to_body(struct email_t* mail, const char* text){ + + if(text == NULL || mail == NULL){ + return -1; + } + + char *buffer = malloc(strlen(text) + 3); + memset(buffer, 0, strlen(text) + 3); + buffer[0] = '\r'; + buffer[1] = '\n'; + strcat(buffer, text); + struct email_t* root = get_root_mail(mail); + size_t root_offset = mail->message_length + + (mail->message - root->message); + + char* old_root = root->message; + char * new_root = insert_string(root->message, buffer, + root->message_length, root_offset); + if(new_root == NULL){ + return -1; + } + propagate_size_change(mail, strlen(buffer)); + propagate_root_pointer(root, new_root, old_root); propagate_insert_delete(root, root->message+root_offset, strlen(buffer));