Added file storage for other message attachments

Signed-off-by: tyrolyean <tyrolyean@tyrolyean.net>
This commit is contained in:
tyrolyean 2020-04-30 02:08:33 +02:00
parent 090c55dd7b
commit a71a10a0b5
No known key found for this signature in database
GPG key ID: EDD105663B707C62
3 changed files with 39 additions and 17 deletions

View file

@ -25,6 +25,8 @@
char* generate_safe_dirname();
int base64_decode_file(const char* directory, const struct email_t* mail);
int decode_file(const char* directory, const char * message, size_t len,
struct type_file_info_t finf);
bool file_exists(const char* filename);

View file

@ -336,14 +336,22 @@ int replace_base64_files(struct email_t* mail, const char* dirname){
return 0;
}
if(!mail->base64_encoded){
return 0;
if(mail->base64_encoded){
if(base64_decode_file(dirname, mail) < 0){
fprintf(stderr, "Failed to decode base64 file\n!");
return -1;
}
}else{
if(decode_file(dirname, (mail->message+mail->body_offset),
(mail->message_length - mail->body_offset),
mail->file_info) < 0){
fprintf(stderr, "Failed to decode base64 file\n!");
return -1;
}
}
if(base64_decode_file(dirname, mail) < 0){
fprintf(stderr, "Failed to decode base64 file\n!");
return -1;
}
/* Replace the mail message with some html text TODO */
return 0;

View file

@ -100,37 +100,51 @@ int base64_decode_file(const char* directory, const struct email_t* mail){
fprintf(stderr, "Failed to decode base64 message\n");
return -1;
}
int n = decode_file(directory,(char*) decoded, dec_len, mail->file_info);
free(decoded);
return n;
}
size_t fn_len = strlen(mail->file_info.name) + strlen(directory) + 1;
int decode_file(const char* directory, const char * message, size_t len,
struct type_file_info_t finf){
if(directory == NULL || message == NULL){
/* I don't know how I should call that file! */
return 0;
}
size_t fn_len = strlen(finf.name) + strlen(directory) + 1;
char* filename = malloc(fn_len);
if(filename == NULL){
free(decoded);
return -1;
}
memset(filename, 0, fn_len);
strcat(filename, directory);
strcat(filename, mail->file_info.name);
strcat(filename, finf.name);
bool exists = false;
for(size_t i = 0; i < 10; i++){
exists = file_exists(filename);
if(exists){
free(filename);
fn_len = strlen(mail->file_info.name) +
fn_len = strlen(finf.name) +
strlen(directory) + 20;
filename = malloc(fn_len);
snprintf(filename, fn_len, "%s%i-%s",directory,
rand(), mail->file_info.name);
rand(), finf.name);
}
}
if(exists){
/* What?*/
fprintf(stderr,"Failed to create unique file name!\n");
free(filename);
free(decoded);
return -1;
}
@ -138,18 +152,16 @@ int base64_decode_file(const char* directory, const struct email_t* mail){
if(outfile == NULL){
perror("Failed to open base64 out file");
free(filename);
free(decoded);
return -1;
}
if(verbose){
printf("Storing base64 file len %lu to [%s]\n",
dec_len, filename);
printf("Storing file len %lu to [%s]\n",
len, filename);
}
fwrite(decoded, dec_len, 1, outfile);
fwrite(message, len, 1, outfile);
fclose(outfile);
free(filename);
free(decoded);
return 0;
}