2012-05-26 15:18:22 +02:00
|
|
|
/*
|
|
|
|
* import.c, SJ
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-10 13:04:54 +02:00
|
|
|
#include <strings.h>
|
2012-05-26 15:18:22 +02:00
|
|
|
#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(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg){
|
|
|
|
int rc=ERR, fd;
|
|
|
|
char *rule;
|
|
|
|
struct stat st;
|
|
|
|
struct _state state;
|
|
|
|
struct __counters counters;
|
|
|
|
|
|
|
|
|
2013-01-06 21:04:58 +01:00
|
|
|
init_session_data(sdata, cfg->server_id);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
2012-09-21 11:04:20 +02:00
|
|
|
if(cfg->verbosity > 1) printf("processing: %s\n", filename);
|
2012-05-26 15:18:22 +02:00
|
|
|
|
|
|
|
if(strcmp(filename, "-") == 0){
|
|
|
|
|
|
|
|
if(read_from_stdin(sdata) == ERR){
|
|
|
|
printf("error reading from stdin\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", sdata->ttmpfile);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if(stat(filename, &st) != 0){
|
|
|
|
printf("cannot stat() %s\n", filename);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(S_ISREG(st.st_mode) == 0){
|
|
|
|
printf("%s is not a file\n", filename);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if(fd == -1){
|
|
|
|
printf("cannot open %s\n", filename);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
|
|
|
|
|
|
|
|
sdata->tot_len = st.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sdata->sent = 0;
|
2012-12-03 14:21:55 +01:00
|
|
|
sdata->delivered = 0;
|
2012-05-26 15:18:22 +02:00
|
|
|
|
2013-01-06 22:16:21 +01:00
|
|
|
state = parse_message(sdata, 1, data, cfg);
|
2012-05-26 15:18:22 +02:00
|
|
|
post_parse(sdata, &state, cfg);
|
|
|
|
|
2012-12-03 14:21:55 +01:00
|
|
|
if(sdata->sent <= 0 && sdata->delivered > 0) sdata->sent = sdata->delivered;
|
|
|
|
|
2012-05-26 15:18:22 +02:00
|
|
|
if(sdata->sent > sdata->now) sdata->sent = sdata->now;
|
|
|
|
if(sdata->sent == -1) sdata->sent = 0;
|
|
|
|
|
|
|
|
/* fat chances that you won't import emails before 1990.01.01 */
|
|
|
|
|
|
|
|
if(sdata->sent > 631148400) sdata->retained = sdata->sent;
|
|
|
|
|
|
|
|
rule = check_againt_ruleset(data->archiving_rules, &state, sdata->tot_len, sdata->spam_message);
|
|
|
|
|
|
|
|
if(rule){
|
|
|
|
printf("discarding %s by archiving policy: %s\n", filename, rule);
|
|
|
|
rc = OK;
|
|
|
|
goto ENDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
make_digests(sdata, cfg);
|
|
|
|
|
|
|
|
rc = process_message(sdata, &state, data, cfg);
|
|
|
|
|
|
|
|
ENDE:
|
|
|
|
unlink(sdata->tmpframe);
|
|
|
|
|
|
|
|
if(strcmp(filename, "-") == 0) unlink(sdata->ttmpfile);
|
|
|
|
|
|
|
|
|
|
|
|
switch(rc) {
|
|
|
|
case OK:
|
|
|
|
bzero(&counters, sizeof(counters));
|
|
|
|
counters.c_size += sdata->tot_len;
|
|
|
|
update_counters(sdata, data, &counters, cfg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ERR_EXISTS:
|
2012-07-09 15:18:21 +02:00
|
|
|
rc = OK;
|
2012-05-26 15:18:22 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("failed to import: %s (id: %s)\n", filename, sdata->ttmpfile);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
unsigned long get_folder_id(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){
|
2012-08-23 10:23:58 +02:00
|
|
|
unsigned long id=0;
|
2012-09-03 17:12:02 +02:00
|
|
|
MYSQL_BIND bind[2];
|
|
|
|
unsigned long len[2];
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(prepare_a_mysql_statement(sdata, &(data->stmt_get_folder_id), SQL_PREPARED_STMT_GET_FOLDER_ID) == ERR) goto ENDE;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
memset(bind, 0, sizeof(bind));
|
|
|
|
|
|
|
|
bind[0].buffer_type = MYSQL_TYPE_STRING;
|
|
|
|
bind[0].buffer = foldername;
|
|
|
|
bind[0].is_null = 0;
|
|
|
|
len[0] = strlen(foldername); bind[0].length = &len[0];
|
|
|
|
|
2012-09-03 17:12:02 +02:00
|
|
|
bind[1].buffer_type = MYSQL_TYPE_LONG;
|
|
|
|
bind[1].buffer = (char *)&parent_id;
|
|
|
|
bind[1].is_null = 0;
|
|
|
|
bind[1].length = 0;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(mysql_stmt_bind_param(data->stmt_get_folder_id, bind)){
|
2012-08-23 10:23:58 +02:00
|
|
|
goto CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(mysql_stmt_execute(data->stmt_get_folder_id)){
|
2012-08-23 10:23:58 +02:00
|
|
|
goto CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(bind, 0, sizeof(bind));
|
|
|
|
|
|
|
|
bind[0].buffer_type = MYSQL_TYPE_LONG;
|
|
|
|
bind[0].buffer = (char *)&id;
|
|
|
|
bind[0].is_null = 0;
|
|
|
|
bind[0].length = 0;
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(mysql_stmt_bind_result(data->stmt_get_folder_id, bind)){
|
2012-08-23 10:23:58 +02:00
|
|
|
goto CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(mysql_stmt_store_result(data->stmt_get_folder_id)){
|
2012-08-23 10:23:58 +02:00
|
|
|
goto CLOSE;
|
|
|
|
}
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
mysql_stmt_fetch(data->stmt_get_folder_id);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
CLOSE:
|
2013-01-28 13:40:44 +01:00
|
|
|
mysql_stmt_close(data->stmt_get_folder_id);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
ENDE:
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
unsigned long add_new_folder(struct session_data *sdata, struct __data *data, char *foldername, int parent_id){
|
2012-08-23 10:23:58 +02:00
|
|
|
unsigned long id=0;
|
|
|
|
MYSQL_BIND bind[2];
|
|
|
|
unsigned long len[2];
|
|
|
|
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(prepare_a_mysql_statement(sdata, &(data->stmt_insert_into_folder_table), SQL_PREPARED_STMT_INSERT_INTO_FOLDER_TABLE) == ERR) goto ENDE;
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
memset(bind, 0, sizeof(bind));
|
|
|
|
|
|
|
|
bind[0].buffer_type = MYSQL_TYPE_STRING;
|
|
|
|
bind[0].buffer = foldername;
|
|
|
|
bind[0].is_null = 0;
|
|
|
|
len[0] = strlen(foldername); bind[0].length = &len[0];
|
|
|
|
|
2012-09-03 17:12:02 +02:00
|
|
|
bind[1].buffer_type = MYSQL_TYPE_LONG;
|
|
|
|
bind[1].buffer = (char *)&parent_id;
|
|
|
|
bind[1].is_null = 0;
|
|
|
|
bind[1].length = 0;
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(mysql_stmt_bind_param(data->stmt_insert_into_folder_table, bind)){
|
|
|
|
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_FOLDER_TABLE, mysql_stmt_error(data->stmt_insert_into_folder_table));
|
2012-08-23 10:23:58 +02:00
|
|
|
goto CLOSE;
|
|
|
|
}
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
if(mysql_stmt_execute(data->stmt_insert_into_folder_table)){
|
2012-08-23 10:23:58 +02:00
|
|
|
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_execute error: *%s*", sdata->ttmpfile, SQL_RECIPIENT_TABLE, mysql_error(&(sdata->mysql)));
|
|
|
|
goto CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-28 13:40:44 +01:00
|
|
|
id = mysql_stmt_insert_id(data->stmt_insert_into_folder_table);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
CLOSE:
|
2013-01-28 13:40:44 +01:00
|
|
|
mysql_stmt_close(data->stmt_insert_into_folder_table);
|
2012-08-23 10:23:58 +02:00
|
|
|
|
|
|
|
ENDE:
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|