Added ability to announce presence

Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
tyrolyean 2020-04-29 00:32:25 +02:00
parent 0524358cef
commit fdc2ddf53b
No known key found for this signature in database
GPG key ID: EDD105663B707C62
7 changed files with 124 additions and 12 deletions

View file

@ -21,15 +21,7 @@
#include <stdint.h>
#include <stddef.h>
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;
};
#include "mail.h"
struct email_t mail_from_text(char* message, size_t length);
void redetect_body_head(struct email_t* mail);

37
include/mail.h Normal file
View file

@ -0,0 +1,37 @@
/*
* mail.h - Mail editing toolset
* 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 MAIL_H
#define MAIL_H
#include <stdint.h>
#include <stddef.h>
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;
};
int append_header(struct email_t* mail, const char* key, const char* value);
int append_to_header(struct email_t* mail, const char* pair);
#endif /* MAIL_H */

View file

@ -20,7 +20,7 @@
#include <stddef.h>
const char* insert_string(char * destination, const char* source,
char* insert_string(char * destination, const char* source,
size_t dest_orig_len, size_t offset);

View file

@ -19,6 +19,7 @@
#include "attach.h"
#include "detect.h"
#include "config.h"
#include "mail.h"
#include <string.h>
#include <stdio.h>
@ -94,6 +95,10 @@ char* attach_files(char* message, size_t len){
return email.message;
}
/* Now we can start the real work! */
/* Announce our presence via header */
append_header(&email,"X-Mailattached", instance_id);
return email.message;
}

77
src/mail.c Normal file
View file

@ -0,0 +1,77 @@
/*
* mail.c - Mail editing toolset
* 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 "mail.h"
#include "tools.h"
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
int append_header(struct email_t* mail, const char* key, const char* value){
if(mail == NULL || key == NULL || value == NULL){
return -1;
}
const char* in_between = ": ";
size_t len = strlen(key) + strlen(value) + strlen(in_between) + 1;
char* buffer = malloc(len);
if(buffer == NULL){
return -1;
}
memset(buffer, 0, len);
strcat(buffer, key);
strcat(buffer, in_between);
strcat(buffer, value);
if(append_to_header(mail, buffer) < 0){
free(buffer);
return -1;
}
free(buffer);
return 0;
}
int append_to_header(struct email_t* mail, const char* pair){
if(pair == NULL || mail == NULL){
return -1;
}
char *buffer = malloc(strlen(pair) + 3);
if(buffer == NULL){
return -1;
}
memset(buffer, 0, strlen(pair) + 3);
buffer[0] = '\r';
buffer[1] = '\n';
strcat(buffer, pair);
mail->message=insert_string(mail->message, buffer, strlen(buffer),
mail->header_len);
free(buffer);
return 0;
}

View file

@ -248,7 +248,8 @@ int init_net(){
serveraddr.sin_port = htons((unsigned short)listen_port);
if (bind(parentfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0) {
sizeof(serveraddr)) < 0) {
perror("ERROR on binding");
return -1;
}

View file

@ -24,7 +24,7 @@
/* 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,
char* insert_string(char * destination, const char* source,
size_t dest_orig_len, size_t offset){
if(source == NULL|| dest_orig_len < offset){