Added header search functions
Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
parent
71a4208434
commit
646517cc97
4 changed files with 92 additions and 0 deletions
|
@ -20,6 +20,7 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct email_t{
|
||||
char* message;
|
||||
|
@ -29,6 +30,10 @@ struct email_t{
|
|||
* there was no clear distinction between header and body...
|
||||
*/
|
||||
size_t header_len, body_offset, message_length;
|
||||
bool is_multipart;
|
||||
char* boundary;
|
||||
size_t submes_cnt;
|
||||
struct email_t** submes;
|
||||
};
|
||||
|
||||
int append_header(struct email_t* mail, const char* key, const char* value);
|
||||
|
|
|
@ -18,10 +18,15 @@
|
|||
#ifndef TOOLS_H
|
||||
#define TOOLS_H
|
||||
|
||||
#include "mail.h"
|
||||
#include <stddef.h>
|
||||
|
||||
char* insert_string(char * destination, const char* source,
|
||||
size_t dest_orig_len, size_t offset);
|
||||
|
||||
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);
|
||||
|
||||
#endif /* TOOLS_H */
|
||||
|
|
17
src/attach.c
17
src/attach.c
|
@ -20,6 +20,7 @@
|
|||
#include "detect.h"
|
||||
#include "config.h"
|
||||
#include "mail.h"
|
||||
#include "tools.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
@ -33,8 +34,24 @@ struct email_t mail_from_text(char* message, size_t length){
|
|||
memset(&mail, 0, sizeof(mail));
|
||||
mail.message = message;
|
||||
mail.message_length = length;
|
||||
mail.submes_cnt = 0;
|
||||
mail.submes = NULL;
|
||||
|
||||
redetect_body_head(&mail);
|
||||
char* cont_type = search_header_key(&mail, "Content-Type");
|
||||
if(cont_type == NULL){
|
||||
/* Halleluja, I've got nothing to do! WOO */
|
||||
mail.is_multipart = false;
|
||||
return mail;
|
||||
}else{
|
||||
size_t value_length = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
return mail;
|
||||
}
|
||||
|
|
65
src/tools.c
65
src/tools.c
|
@ -16,10 +16,12 @@
|
|||
*/
|
||||
|
||||
#include "tools.h"
|
||||
#include "mail.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* Takes a string destination and inserts at the given point offset the string
|
||||
* in source. Really more like a stringcat with shifting the end.
|
||||
|
@ -47,3 +49,66 @@ char* insert_string(char * destination, const char* source,
|
|||
|
||||
}
|
||||
|
||||
/* Searches the given header for the key provided and, if found, returns the
|
||||
* pointer to that key NOT the value
|
||||
*/
|
||||
char* search_header_key(struct email_t* mail, const char* key){
|
||||
|
||||
if(mail == NULL || key == NULL){
|
||||
return NULL;
|
||||
}
|
||||
size_t keylen = strlen(key);
|
||||
|
||||
for(size_t i = 0; i < (mail->header_len - keylen); i++){
|
||||
if(mail->message[i] == '\n'){
|
||||
if(strncasecmp(&mail->message[i+1], key, keylen) == 0){
|
||||
return &mail->message[i+1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* If the key doesn't have a value, or nothing can be found, NULL is returned,
|
||||
* otherwise a pointer to the first character of the value is returned.
|
||||
*/
|
||||
char* get_value_from_key(size_t* val_len, size_t key_offset,
|
||||
struct email_t* mail){
|
||||
|
||||
if(val_len == NULL || mail == NULL){
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t colon = 0;
|
||||
for(size_t i = key_offset; i < mail->header_len; i++){
|
||||
if(mail->message[i] == ':'){
|
||||
colon = i;
|
||||
}
|
||||
}
|
||||
|
||||
colon++;
|
||||
char* val = NULL;
|
||||
for(size_t i = colon; i < (mail->header_len-1); i++){
|
||||
if(isalnum(mail->message[i])){
|
||||
val = &mail->message[i];
|
||||
}
|
||||
|
||||
if(mail->message[i] == '\n' && !isblank(mail->message[i+1])){
|
||||
if(val != NULL){
|
||||
*val_len = &mail->message[i] - 1 - val;
|
||||
}else{
|
||||
*val_len = 0;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
if(val != NULL){
|
||||
*val_len = &mail->message[mail->header_len] - val;
|
||||
}else{
|
||||
*val_len = 0;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue