piler/src/import.c

198 lines
5.0 KiB
C
Raw Normal View History

/*
* import.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include <syslog.h>
#include <piler.h>
int import_message(struct session_data *sdata, struct data *data, struct config *cfg){
2016-02-28 21:19:19 +01:00
int rc=ERR;
char *p, *rule, newpath[SMALLBUFSIZE];
struct stat st;
2015-11-21 23:06:47 +01:00
struct parser_state state;
struct counters counters;
init_session_data(sdata, cfg);
2014-07-01 11:43:36 +02:00
if(data->import->extra_recipient){
snprintf(sdata->rcptto[0], SMALLBUFSIZE-1, "%s", data->import->extra_recipient);
sdata->num_of_rcpt_to = 1;
}
if(cfg->verbosity > 1) printf("processing: %s\n", data->import->filename);
if(strcmp(data->import->filename, "-") == 0){
if(read_from_stdin(sdata) == ERR){
printf("ERROR: error reading from stdin\n");
return rc;
}
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", sdata->ttmpfile);
}
else {
if(stat(data->import->filename, &st) != 0){
printf("ERROR: cannot stat() %s\n", data->import->filename);
return rc;
}
if(S_ISREG(st.st_mode) == 0){
printf("ERROR: %s is not a file\n", data->import->filename);
return rc;
}
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", data->import->filename);
sdata->tot_len = st.st_size;
2016-02-28 21:19:19 +01:00
}
2014-07-16 12:39:54 +02:00
if(sdata->tot_len < cfg->min_message_size){
2016-02-28 21:19:19 +01:00
printf("%s is too short: %d bytes\n", sdata->filename, sdata->tot_len);
return rc;
}
2016-02-28 21:19:19 +01:00
data->import->total_size += sdata->tot_len;
sdata->delivered = 0;
2014-01-13 13:06:10 +01:00
sdata->import = 1;
2013-01-06 22:16:21 +01:00
state = parse_message(sdata, 1, data, cfg);
post_parse(sdata, &state, cfg);
rule = check_againt_ruleset(data->archiving_rules, &state, sdata->tot_len, sdata->spam_message);
if(rule){
if(data->quiet == 0) printf("discarding %s by archiving policy: %s\n", data->import->filename, rule);
rc = OK;
}
else {
make_digests(sdata, cfg);
2013-06-29 15:43:16 +02:00
if(sdata->hdr_len < 10){
printf("%s: invalid message, hdr_len: %d\n", data->import->filename, sdata->hdr_len);
rc = ERR;
}
else {
rc = process_message(sdata, &state, data, cfg);
unlink(state.message_id_hash);
2013-06-29 15:43:16 +02:00
}
}
unlink(sdata->tmpframe);
remove_stripped_attachments(&state);
if(strcmp(data->import->filename, "-") == 0) unlink(sdata->ttmpfile);
switch(rc) {
case OK:
bzero(&counters, sizeof(counters));
counters.c_rcvd = 1;
counters.c_size += sdata->tot_len;
counters.c_stored_size = sdata->stored_len;
update_counters(sdata, &counters, cfg);
break;
case ERR_EXISTS:
2012-07-09 15:18:21 +02:00
rc = OK;
2013-05-16 12:24:46 +02:00
bzero(&counters, sizeof(counters));
counters.c_duplicate = 1;
update_counters(sdata, &counters, cfg);
2013-05-16 12:24:46 +02:00
if(data->quiet == 0) printf("duplicate: %s (duplicate id: %llu)\n", data->import->filename, sdata->duplicate_id);
break;
default:
printf("failed to import: %s (id: %s)\n", data->import->filename, sdata->ttmpfile);
break;
}
if(rc != OK && data->import->failed_folder){
p = strrchr(data->import->filename, '/');
if(p)
p++;
else
p = data->import->filename;
snprintf(newpath, sizeof(newpath)-2, "%s/%s", data->import->failed_folder, p);
if(rename(data->import->filename, newpath))
printf("cannot move %s to %s\n", data->import->filename, newpath);
}
return rc;
}
int get_folder_id(struct session_data *sdata, char *foldername, int parent_id){
2013-06-29 15:22:07 +02:00
int id=ERR_FOLDER;
struct sql sql;
if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) return id;
p_bind_init(&sql);
sql.sql[sql.pos] = foldername; sql.type[sql.pos] = TYPE_STRING; sql.pos++;
sql.sql[sql.pos] = (char *)&parent_id; sql.type[sql.pos] = TYPE_LONG; sql.pos++;
if(p_exec_stmt(sdata, &sql) == OK){
p_bind_init(&sql);
sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONG; sql.len[sql.pos] = sizeof(unsigned long); sql.pos++;
p_store_results(&sql);
p_fetch_results(&sql);
p_free_results(&sql);
}
close_prepared_statement(&sql);
return id;
}
int add_new_folder(struct session_data *sdata, char *foldername, int parent_id){
2013-06-29 15:22:07 +02:00
int id=ERR_FOLDER;
struct sql sql;
2013-06-29 15:22:07 +02:00
if(foldername == NULL) return id;
if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) return id;
p_bind_init(&sql);
sql.sql[sql.pos] = foldername; sql.type[sql.pos] = TYPE_STRING; sql.pos++;
sql.sql[sql.pos] = (char *)&parent_id; sql.type[sql.pos] = TYPE_LONG; sql.pos++;
if(p_exec_stmt(sdata, &sql) == OK){
id = p_get_insert_id(&sql);
}
close_prepared_statement(&sql);
return id;
}