Added insert tool and mail parsing
Signed-off-by: Tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
parent
76c4529999
commit
ec6ad62d57
|
@ -19,8 +19,22 @@
|
||||||
#define ATTACH_H
|
#define ATTACH_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
char* attach_files(FILE* buffer);
|
struct email_t{
|
||||||
|
char* message;
|
||||||
|
/* From the below values you can say pretty much anything about the
|
||||||
|
* message. The line delimiting body and header is left out. The header
|
||||||
|
* len includes the last \r\n of the header. If header len is zero,
|
||||||
|
* there was no clear distinction between header and body...
|
||||||
|
*/
|
||||||
|
size_t header_len, body_offset, message_length;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct email_t mail_from_text(char* message, size_t length);
|
||||||
|
void redetect_body_head(struct email_t* mail);
|
||||||
|
|
||||||
|
|
||||||
|
char* attach_files(char* message, size_t len);
|
||||||
|
|
||||||
#endif /* ATTACH_H */
|
#endif /* ATTACH_H */
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* tools.h - Utility functions
|
||||||
|
* The author licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TOOLS_H
|
||||||
|
#define TOOLS_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
const char* insert_string(char * destination, const char* source,
|
||||||
|
size_t dest_orig_len, size_t offset);
|
||||||
|
|
||||||
|
#endif /* TOOLS_H */
|
78
src/attach.c
78
src/attach.c
|
@ -18,23 +18,71 @@
|
||||||
|
|
||||||
#include "attach.h"
|
#include "attach.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
char* attach_files(FILE* buffer){
|
struct email_t mail_from_text(char* message, size_t length){
|
||||||
fseek(buffer, 0, SEEK_END);
|
|
||||||
size_t len = ftell(buffer);
|
struct email_t mail;
|
||||||
|
memset(&mail, 0, sizeof(mail));
|
||||||
|
mail.message = message;
|
||||||
|
mail.message_length = length;
|
||||||
|
|
||||||
|
redetect_body_head(&mail);
|
||||||
|
|
||||||
char* new_body = malloc(len + 1);
|
return mail;
|
||||||
if(new_body == NULL){
|
}
|
||||||
return NULL;
|
|
||||||
}
|
void redetect_body_head(struct email_t* mail){
|
||||||
memset(new_body,0, len + 1);
|
|
||||||
fseek(buffer, 0, SEEK_SET);
|
char* last_lf = NULL;
|
||||||
if(fread(new_body, 1, len, buffer) <= 0){
|
char* body_start = NULL;
|
||||||
perror("Failed to read file buffer");
|
bool found_alnum = false;
|
||||||
free(new_body);
|
for(size_t i = 0; i < mail->message_length; i++){
|
||||||
}
|
if(mail->message[i] == '\n' && !found_alnum){
|
||||||
|
body_start = &(mail->message[i+1]);
|
||||||
return new_body;
|
break;
|
||||||
|
|
||||||
|
}else if(mail->message[i] == '\n'){
|
||||||
|
last_lf = &(mail->message[i]);
|
||||||
|
found_alnum = false;
|
||||||
|
}else if(isalnum(mail->message[i])){
|
||||||
|
found_alnum = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* In that case we only have a body and no header, or something along
|
||||||
|
* those lines... In any case, if postfix delivers that to us, something
|
||||||
|
* is probably wrong!
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(body_start == NULL) {
|
||||||
|
fprintf(stderr, "Received message without header!");
|
||||||
|
mail->header_len = 0;
|
||||||
|
mail->body_offset = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mail->body_offset = body_start - mail->message;
|
||||||
|
mail->header_len = last_lf - mail->message;
|
||||||
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
char* attach_files(char* message, size_t len){
|
||||||
|
|
||||||
|
struct email_t email = mail_from_text(message,len);
|
||||||
|
|
||||||
|
printf("Received message header: [%.*s]\n", email.header_len,
|
||||||
|
email.message);
|
||||||
|
printf("Received message body: [%s]\n",
|
||||||
|
email.message + email.body_offset);
|
||||||
|
|
||||||
|
/* Now we have a null terminated body which we can edit! */
|
||||||
|
|
||||||
|
return email.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
35
src/main.c
35
src/main.c
|
@ -65,7 +65,7 @@ void* client_handle_async(void* params){
|
||||||
}
|
}
|
||||||
|
|
||||||
int n; /* message byte size */
|
int n; /* message byte size */
|
||||||
char buf[10];
|
char buf[1000];
|
||||||
struct pollfd fds[2];
|
struct pollfd fds[2];
|
||||||
memset(fds, 0 , sizeof(fds));
|
memset(fds, 0 , sizeof(fds));
|
||||||
fds[0].fd = cli->fd;
|
fds[0].fd = cli->fd;
|
||||||
|
@ -84,17 +84,17 @@ void* client_handle_async(void* params){
|
||||||
|
|
||||||
while(poll(fds, 2, timeout) > 0){
|
while(poll(fds, 2, timeout) > 0){
|
||||||
for(size_t i = 0; i < 2; i++){
|
for(size_t i = 0; i < 2; i++){
|
||||||
|
|
||||||
if(!(fds[i].revents & POLLIN)){
|
if(!(fds[i].revents & POLLIN)){
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bzero(buf, sizeof(buf));
|
bzero(buf, sizeof(buf));
|
||||||
n = read(fds[i].fd, buf, sizeof(buf)-1);
|
n = read(fds[i].fd, buf, sizeof(buf)-1);
|
||||||
if (n <= 0) {
|
if (n <= 0) {
|
||||||
goto closeup;
|
goto closeup;
|
||||||
}
|
}
|
||||||
printf("%lu:[%s]", i, buf);
|
|
||||||
|
|
||||||
|
|
||||||
if(i==0 && !in_body){
|
if(i==0 && !in_body){
|
||||||
/* As long as we are outside the real mail body
|
/* As long as we are outside the real mail body
|
||||||
|
@ -112,7 +112,7 @@ void* client_handle_async(void* params){
|
||||||
write((fds[!i].fd),
|
write((fds[!i].fd),
|
||||||
input_buffer+(in_len-n),
|
input_buffer+(in_len-n),
|
||||||
body_offs-(in_len-n));
|
body_offs-(in_len-n));
|
||||||
printf("Beginning of body found! "
|
printf("Beginning of message found! "
|
||||||
"Awaiting end...\n");
|
"Awaiting end...\n");
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
|
@ -124,22 +124,35 @@ void* client_handle_async(void* params){
|
||||||
*/
|
*/
|
||||||
in_len += n;
|
in_len += n;
|
||||||
input_buffer = realloc(input_buffer, in_len);
|
input_buffer = realloc(input_buffer, in_len);
|
||||||
strncat(input_buffer,buf, n);
|
strncat(input_buffer, buf, n);
|
||||||
body_p = detect_end_of_body(input_buffer+
|
body_p = detect_end_of_body(input_buffer+
|
||||||
body_offs);
|
body_offs);
|
||||||
|
|
||||||
if(body_p != NULL){
|
if(body_p != NULL){
|
||||||
|
|
||||||
printf("We found the body:\n%.*s\n",
|
printf("Data found, interpreting...\n");
|
||||||
body_p-(input_buffer+body_offs),
|
|
||||||
input_buffer+body_offs);
|
|
||||||
|
|
||||||
size_t body_len = body_p-
|
size_t body_len = body_p-
|
||||||
(input_buffer+body_offs);
|
(input_buffer+body_offs);
|
||||||
|
|
||||||
write((fds[!i].fd),
|
char* new_body = attach_files(
|
||||||
input_buffer+body_offs,
|
input_buffer+body_offs,
|
||||||
body_len);
|
body_len);
|
||||||
|
|
||||||
|
if(new_body != NULL){
|
||||||
|
/* Write the replacement */
|
||||||
|
write((fds[!i].fd), new_body,
|
||||||
|
strlen(new_body));
|
||||||
|
free(new_body);
|
||||||
|
}else{
|
||||||
|
/* Write the original */
|
||||||
|
write((fds[!i].fd),
|
||||||
|
input_buffer+body_offs,
|
||||||
|
body_len);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Rest of conversation after message */
|
||||||
|
|
||||||
write((fds[!i].fd),
|
write((fds[!i].fd),
|
||||||
input_buffer+body_offs+body_len,
|
input_buffer+body_offs+body_len,
|
||||||
in_len-(body_offs+body_len));
|
in_len-(body_offs+body_len));
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* tools.c - Utility functions
|
||||||
|
* The author licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "tools.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <strings.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.
|
||||||
|
* dest_orig_len is without '\0'*/
|
||||||
|
const char* insert_string(char * destination, const char* source,
|
||||||
|
size_t dest_orig_len, size_t offset){
|
||||||
|
|
||||||
|
if(source == NULL|| dest_orig_len < offset){
|
||||||
|
return destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t src_len = strlen(source);
|
||||||
|
size_t new_len = dest_orig_len + src_len + 1;
|
||||||
|
|
||||||
|
char* result = realloc(destination, new_len);
|
||||||
|
if(result == NULL){
|
||||||
|
/* Out of memory... Failed me.. */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memmove(result+offset+src_len, result+offset, dest_orig_len - offset);
|
||||||
|
memcpy(result+offset, source, src_len);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue