Compare commits
No commits in common. "f280420c2f71805b6de3accb8a24005b6592de02" and "ec6ad62d57b0ddc66f3a98c24dbcdc2eebb167bf" have entirely different histories.
f280420c2f
...
ec6ad62d57
2 changed files with 65 additions and 83 deletions
|
@ -78,8 +78,7 @@ char* attach_files(char* message, size_t len){
|
||||||
|
|
||||||
printf("Received message header: [%.*s]\n", email.header_len,
|
printf("Received message header: [%.*s]\n", email.header_len,
|
||||||
email.message);
|
email.message);
|
||||||
printf("Received message body: [%.*s]\n",
|
printf("Received message body: [%s]\n",
|
||||||
email.message_length-email.body_offset
|
|
||||||
email.message + email.body_offset);
|
email.message + email.body_offset);
|
||||||
|
|
||||||
/* Now we have a null terminated body which we can edit! */
|
/* Now we have a null terminated body which we can edit! */
|
||||||
|
|
145
src/main.c
145
src/main.c
|
@ -40,82 +40,6 @@ struct client_info {
|
||||||
int fd;
|
int fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
void receive_mail(char** input_buffer, size_t *in_len, bool *in_body,
|
|
||||||
bool *after_body, char** body_p, size_t *body_offs, struct pollfd fds[2],
|
|
||||||
char *buf, int n){
|
|
||||||
|
|
||||||
if(!*in_body){
|
|
||||||
/* As long as we are outside the real mail body
|
|
||||||
* we can basically passthrough the commands
|
|
||||||
*/
|
|
||||||
*in_len += n;
|
|
||||||
*input_buffer = realloc(*input_buffer, *in_len);
|
|
||||||
strncat(*input_buffer,buf, n);
|
|
||||||
*body_p = detect_start_of_body(*input_buffer);
|
|
||||||
if(*body_p != NULL){
|
|
||||||
/* We reached the beginning of the body
|
|
||||||
* now! */
|
|
||||||
*body_offs = body_p - input_buffer;
|
|
||||||
*in_body = true;
|
|
||||||
write((fds[1].fd),
|
|
||||||
*input_buffer+(*in_len-n),
|
|
||||||
*body_offs-(*in_len-n));
|
|
||||||
printf("Beginning of message found! "
|
|
||||||
"Awaiting end...\n");
|
|
||||||
|
|
||||||
}else{
|
|
||||||
write((fds[1].fd), buf, n);
|
|
||||||
|
|
||||||
}
|
|
||||||
} else if(!*after_body){
|
|
||||||
/* We keep the body until we have it completely
|
|
||||||
*/
|
|
||||||
*in_len += n;
|
|
||||||
*input_buffer = realloc(*input_buffer, *in_len);
|
|
||||||
strncat(*input_buffer, buf, n);
|
|
||||||
*body_p = detect_end_of_body(*input_buffer+
|
|
||||||
*body_offs);
|
|
||||||
|
|
||||||
if(*body_p != NULL){
|
|
||||||
|
|
||||||
printf("Data found, interpreting...\n");
|
|
||||||
size_t body_len = *body_p-
|
|
||||||
(*input_buffer+*body_offs);
|
|
||||||
|
|
||||||
char* new_body = attach_files(
|
|
||||||
*input_buffer+*body_offs,
|
|
||||||
body_len);
|
|
||||||
|
|
||||||
if(new_body != NULL){
|
|
||||||
/* Write the replacement */
|
|
||||||
write((fds[1].fd), new_body,
|
|
||||||
strlen(new_body));
|
|
||||||
free(new_body);
|
|
||||||
}else{
|
|
||||||
/* Write the original */
|
|
||||||
write((fds[1].fd),
|
|
||||||
*input_buffer+*body_offs,
|
|
||||||
body_len);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Rest of conversation after message */
|
|
||||||
|
|
||||||
write((fds[1].fd),
|
|
||||||
*input_buffer+*body_offs+body_len,
|
|
||||||
*in_len-(*body_offs+body_len));
|
|
||||||
|
|
||||||
*after_body = true;
|
|
||||||
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
write((fds[1].fd), buf, n);
|
|
||||||
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void* client_handle_async(void* params){
|
void* client_handle_async(void* params){
|
||||||
|
|
||||||
struct client_info * cli = params;
|
struct client_info * cli = params;
|
||||||
|
@ -172,11 +96,70 @@ void* client_handle_async(void* params){
|
||||||
goto closeup;
|
goto closeup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(i == 0){
|
if(i==0 && !in_body){
|
||||||
receive_mail(&input_buffer,
|
/* As long as we are outside the real mail body
|
||||||
&in_len, &in_body, &after_body,
|
* we can basically passthrough the commands
|
||||||
&body_p, &body_offs,
|
*/
|
||||||
fds, buf, n);
|
in_len += n;
|
||||||
|
input_buffer = realloc(input_buffer, in_len);
|
||||||
|
strncat(input_buffer,buf, n);
|
||||||
|
body_p = detect_start_of_body(input_buffer);
|
||||||
|
if(body_p != NULL){
|
||||||
|
/* We reached the beginning of the body
|
||||||
|
* now! */
|
||||||
|
body_offs = body_p - input_buffer;
|
||||||
|
in_body = true;
|
||||||
|
write((fds[!i].fd),
|
||||||
|
input_buffer+(in_len-n),
|
||||||
|
body_offs-(in_len-n));
|
||||||
|
printf("Beginning of message found! "
|
||||||
|
"Awaiting end...\n");
|
||||||
|
|
||||||
|
}else{
|
||||||
|
write((fds[!i].fd), buf, n);
|
||||||
|
|
||||||
|
}
|
||||||
|
} else if(i==0 && !after_body){
|
||||||
|
/* We keep the body until we have it completely
|
||||||
|
*/
|
||||||
|
in_len += n;
|
||||||
|
input_buffer = realloc(input_buffer, in_len);
|
||||||
|
strncat(input_buffer, buf, n);
|
||||||
|
body_p = detect_end_of_body(input_buffer+
|
||||||
|
body_offs);
|
||||||
|
|
||||||
|
if(body_p != NULL){
|
||||||
|
|
||||||
|
printf("Data found, interpreting...\n");
|
||||||
|
size_t body_len = body_p-
|
||||||
|
(input_buffer+body_offs);
|
||||||
|
|
||||||
|
char* new_body = attach_files(
|
||||||
|
input_buffer+body_offs,
|
||||||
|
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),
|
||||||
|
input_buffer+body_offs+body_len,
|
||||||
|
in_len-(body_offs+body_len));
|
||||||
|
|
||||||
|
after_body = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
write((fds[!i].fd), buf, n);
|
write((fds[!i].fd), buf, n);
|
||||||
|
|
Loading…
Reference in a new issue