Add link insert

Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
tyrolyean 2020-05-01 01:11:44 +02:00
parent 3614bf3078
commit 5f898f4e49
No known key found for this signature in database
GPG key ID: EDD105663B707C62
3 changed files with 90 additions and 3 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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));