Added change of header propagation

Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
Tyrolyean 2020-04-29 16:29:36 +02:00
parent 0fc1108c73
commit 5814135512
No known key found for this signature in database
GPG key ID: 81EC9BAC5E9667C6
3 changed files with 55 additions and 0 deletions

View file

@ -21,6 +21,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <sys/types.h>
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 */

View file

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

View file

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