piler/src/pilerimport.c

253 lines
5.4 KiB
C
Raw Normal View History

2011-12-30 15:52:59 +01:00
/*
* pilerimport.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
2011-12-30 15:52:59 +01:00
#include <unistd.h>
#include <time.h>
#include <locale.h>
#include <syslog.h>
#include <piler.h>
extern char *optarg;
extern int optind;
2011-12-30 15:52:59 +01:00
int import_message(char *filename, struct session_data *sdata, struct __data *data, struct __config *cfg){
int rc=ERR;
2011-12-30 15:52:59 +01:00
char *rule;
struct stat st;
struct _state state;
2012-02-10 13:57:04 +01:00
struct __counters counters;
2011-12-30 15:52:59 +01:00
if(stat(filename, &st) != 0){
printf("cannot read: %s\n", filename);
return rc;
}
if(S_ISREG(st.st_mode) == 0){
printf("%s is not a file\n", filename);
return rc;
}
2011-12-30 15:52:59 +01:00
init_session_data(sdata);
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
2011-12-30 15:52:59 +01:00
sdata->sent = 0;
sdata->tot_len = st.st_size;
state = parse_message(sdata, cfg);
post_parse(sdata, &state, cfg);
if(sdata->sent > sdata->now) sdata->sent = sdata->now;
2012-02-02 15:29:07 +01:00
if(sdata->sent == -1) sdata->sent = 0;
2011-12-30 15:52:59 +01:00
rule = check_againt_ruleset(data->rules, &state, st.st_size);
if(rule){
printf("discarding %s by archiving policy: %s\n", filename, rule);
rc = OK;
goto ENDE;
}
make_digests(sdata, cfg);
2011-12-30 15:52:59 +01:00
rc = process_message(sdata, &state, data, cfg);
2011-12-30 15:52:59 +01:00
ENDE:
unlink(sdata->tmpframe);
switch(rc) {
case OK:
printf("imported: %s\n", filename);
2012-02-10 13:57:04 +01:00
bzero(&counters, sizeof(counters));
counters.c_size += sdata->tot_len;
update_counters(sdata, data, &counters, cfg);
break;
2011-12-30 15:52:59 +01:00
case ERR_EXISTS:
printf("discarding duplicate message: %s\n", filename);
break;
2011-12-30 15:52:59 +01:00
default:
printf("failed to import: %s\n", filename);
break;
}
2011-12-30 15:52:59 +01:00
return rc;
}
int import_from_mailbox(char *mailbox, struct session_data *sdata, struct __data *data, struct __config *cfg){
FILE *F, *f=NULL;
int rc=ERR, tot_msgs=0;
char buf[MAXBUFSIZE], fname[SMALLBUFSIZE];
time_t t;
F = fopen(mailbox, "r");
if(!F){
printf("cannot open mailbox: %s\n", mailbox);
return rc;
2011-12-30 15:52:59 +01:00
}
t = time(NULL);
2011-12-30 15:52:59 +01:00
while(fgets(buf, sizeof(buf)-1, F)){
2011-12-30 15:52:59 +01:00
if(buf[0] == 'F' && buf[1] == 'r' && buf[2] == 'o' && buf[3] == 'm' && buf[4] == ' '){
tot_msgs++;
if(f){
fclose(f);
rc = import_message(fname, sdata, data, cfg);
unlink(fname);
}
2011-12-30 15:52:59 +01:00
snprintf(fname, sizeof(fname)-1, "%ld-%d", t, tot_msgs);
f = fopen(fname, "w+");
continue;
}
2011-12-30 15:52:59 +01:00
if(f) fprintf(f, "%s", buf);
}
2011-12-30 15:52:59 +01:00
if(f){
fclose(f);
rc = import_message(fname, sdata, data, cfg);
unlink(fname);
}
2011-12-30 15:52:59 +01:00
fclose(F);
return rc;
}
int import_from_maildir(char *directory, struct session_data *sdata, struct __data *data, struct __config *cfg){
DIR *dir;
struct dirent *de;
int rc=ERR, tot_msgs=0;
char fname[SMALLBUFSIZE];
dir = opendir(directory);
if(!dir){
printf("cannot open directory: %s\n", directory);
return rc;
}
while((de = readdir(dir))){
if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
snprintf(fname, sizeof(fname)-1, "%s/%s", directory, de->d_name);
rc = import_message(fname, sdata, data, cfg);
if(rc != ERR) tot_msgs++;
}
closedir(dir);
return rc;
}
void usage(){
2012-01-08 16:16:00 +01:00
printf("usage: pilerimport [-c <config file>] -e <eml file> | -m <mailbox file> | -d <directory>\n");
exit(0);
2011-12-30 15:52:59 +01:00
}
int main(int argc, char **argv){
int i, rc;
char *configfile=CONFIG_FILE, *mailbox=NULL, *emlfile=NULL, *directory=NULL;
2011-12-30 15:52:59 +01:00
struct session_data sdata;
struct __config cfg;
struct __data data;
while((i = getopt(argc, argv, "c:m:e:d:h?")) > 0){
switch(i){
case 'c' :
configfile = optarg;
break;
case 'e' :
emlfile = optarg;
break;
case 'd' :
directory = optarg;
break;
case 'm' :
mailbox = optarg;
break;
case 'h' :
case '?' :
usage();
break;
default :
break;
}
2011-12-30 15:52:59 +01:00
}
if(!mailbox && !emlfile && !directory) usage();
cfg = read_config(configfile);
2011-12-30 15:52:59 +01:00
if(read_key(&cfg)){
printf("%s\n", ERR_READING_KEY);
return 1;
}
mysql_init(&(sdata.mysql));
mysql_options(&(sdata.mysql), MYSQL_OPT_CONNECT_TIMEOUT, (const char*)&cfg.mysql_connect_timeout);
if(mysql_real_connect(&(sdata.mysql), cfg.mysqlhost, cfg.mysqluser, cfg.mysqlpwd, cfg.mysqldb, cfg.mysqlport, cfg.mysqlsocket, 0) == 0){
printf("cant connect to mysql server\n");
return 0;
}
mysql_real_query(&(sdata.mysql), "SET NAMES utf8", strlen("SET NAMES utf8"));
mysql_real_query(&(sdata.mysql), "SET CHARACTER SET utf8", strlen("SET CHARACTER SET utf8"));
setlocale(LC_CTYPE, cfg.locale);
data.rules = NULL;
load_archiving_rules(&sdata, &(data.rules));
if(emlfile) rc = import_message(emlfile, &sdata, &data, &cfg);
if(mailbox) rc = import_from_mailbox(mailbox, &sdata, &data, &cfg);
if(directory) rc = import_from_maildir(directory, &sdata, &data, &cfg);
2011-12-30 15:52:59 +01:00
free_rule(data.rules);
mysql_close(&(sdata.mysql));
return 0;
}