Added boundary retrieval

Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
tyrolyean 2020-04-29 03:00:12 +02:00
parent 80e3efe078
commit 13b32a48b1
No known key found for this signature in database
GPG Key ID: EDD105663B707C62
5 changed files with 65 additions and 3 deletions

View File

@ -25,7 +25,8 @@
struct email_t mail_from_text(char* message, size_t length);
void redetect_body_head(struct email_t* mail);
void unravel_multipart_mail(struct email_t* mail, char* boundary,
size_t boundary_len);
char* attach_files(char* message, size_t len);

View File

@ -39,4 +39,6 @@ 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);
#define MULTIPART_MIME "multipart/"
#endif /* MAIL_H */

View File

@ -29,4 +29,6 @@ char* search_header_key(struct email_t* mail, const char* key);
char* get_value_from_key(size_t* val_len, size_t key_offset,
struct email_t* mail);
char* get_multipart_boundary(char* content_type, size_t content_len,
size_t* boundary_len);
#endif /* TOOLS_H */

View File

@ -48,8 +48,21 @@ struct email_t mail_from_text(char* message, size_t length){
char * mime_type = get_value_from_key(&value_length,
cont_type - mail.message, &mail);
if(mime_type != NULL){
printf("Found message mime 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
* one. Let's unravel em!
*/
size_t bd_len = 0;
char* bd = get_multipart_boundary(
mime_type, value_length, &bd_len);
if(bd != NULL){
printf("Received boundary: [%.*s]\n",
(int)bd_len, bd);
}
unravel_multipart_mail(&mail, bd, bd_len);
}
}
}
@ -93,6 +106,13 @@ void redetect_body_head(struct email_t* mail){
}
void unravel_multipart_mail(struct email_t* mail, char* boundary,
size_t boundary_len){
return;
}
/* Message is required to be a null terminated string, length is the mail body.
* One may leave something behind the body. len is without the '\0'
* Attempts to replace files inside the email with links to it on a webserver

View File

@ -113,3 +113,40 @@ char* get_value_from_key(size_t* val_len, size_t key_offset,
return val;
}
char* get_multipart_boundary(char* content_type, size_t content_len,
size_t* boundary_len){
if(content_type == NULL || boundary_len == NULL){
return NULL;
}
char* bd = "boundary";
char* boundary_begin = NULL;
size_t bd_len = strlen(bd);
long boundary_offset = -1;
for(size_t i = 0; i < (content_len - bd_len);i++){
if(strncasecmp(&content_type[i], bd, bd_len) == 0){
boundary_offset = i+bd_len;
break;
}
}
if(boundary_offset < 0){
*boundary_len = 0;
return NULL;
}
for(size_t i = boundary_offset; i < content_len;i++){
if(content_type[i] == '"' && boundary_begin == NULL){
boundary_begin = &content_type[i+1];
}else if(content_type[i] == '"'){
*boundary_len = &content_type[i] - boundary_begin ;
return boundary_begin;
}
}
*boundary_len = 0;
return NULL;
}