piler/src/pilerimport.c

656 lines
18 KiB
C
Raw Normal View History

2011-12-30 15:52:59 +01:00
/*
* pilerimport.c, SJ
*/
#define _FILE_OFFSET_BITS 64
2011-12-30 15:52:59 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <dirent.h>
2011-12-30 15:52:59 +01:00
#include <unistd.h>
#include <time.h>
#include <locale.h>
2012-08-21 21:57:39 +02:00
#include <getopt.h>
2011-12-30 15:52:59 +01:00
#include <syslog.h>
#include <piler.h>
2013-01-10 22:30:30 +01:00
#define SKIPLIST "junk,trash,spam,draft,"
#define MBOX_ARGS 1024
extern char *optarg;
extern int optind;
int quiet=0;
2013-08-23 13:02:51 +02:00
int dryrun=0;
2012-09-28 14:15:45 +02:00
int remove_after_successful_import = 0;
2013-08-23 13:02:51 +02:00
int import_from_gui=0;
2012-09-28 14:15:45 +02:00
int connect_to_imap_server(int sd, int *seq, char *username, char *password, int port, struct __data *data, int use_ssl);
2012-10-27 14:40:25 +02:00
int list_folders(int sd, int *seq, char *folders, int foldersize, int use_ssl, struct __data *data);
2013-08-23 13:02:51 +02:00
int process_imap_folder(int sd, int *seq, char *folder, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg);
int connect_to_pop3_server(int sd, char *username, char *password, int port, struct __data *data, int use_ssl);
2013-08-23 13:02:51 +02:00
int process_pop3_emails(int sd, struct session_data *sdata, struct __data *data, int use_ssl, int dryrun, struct __config *cfg);
2012-11-24 23:02:08 +01:00
void close_connection(int sd, struct __data *data, int use_ssl);
2013-08-23 13:02:51 +02:00
void update_import_job_stat(struct __data *data);
int import_from_mailbox(char *mailbox, struct session_data *sdata, struct __data *data, struct __config *cfg){
FILE *F, *f=NULL;
2012-07-09 15:18:21 +02:00
int rc=ERR, tot_msgs=0, ret=OK;
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);
2012-12-03 13:27:14 +01:00
f = NULL;
rc = import_message(fname, sdata, data, cfg);
if(rc == ERR){
printf("error importing: '%s'\n", fname);
ret = ERR;
}
else unlink(fname);
if(quiet == 0) printf("processed: %7d\r", tot_msgs); fflush(stdout);
}
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);
if(rc == ERR){
printf("error importing: '%s'\n", fname);
ret = ERR;
}
else unlink(fname);
2012-12-03 13:27:14 +01:00
if(quiet == 0) printf("processed: %7d\r", tot_msgs); fflush(stdout);
}
2011-12-30 15:52:59 +01:00
fclose(F);
2012-08-21 21:57:39 +02:00
return ret;
}
int import_mbox_from_dir(char *directory, struct session_data *sdata, struct __data *data, int *tot_msgs, struct __config *cfg){
DIR *dir;
struct dirent *de;
int rc=ERR, ret=OK, i=0;
2013-06-29 15:22:07 +02:00
int folder;
2012-08-21 21:57:39 +02:00
char fname[SMALLBUFSIZE];
struct stat st;
dir = opendir(directory);
if(!dir){
printf("cannot open directory: %s\n", directory);
return ERR;
}
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);
if(stat(fname, &st) == 0){
if(S_ISDIR(st.st_mode)){
folder = data->folder;
2012-08-21 21:57:39 +02:00
rc = import_mbox_from_dir(fname, sdata, data, tot_msgs, cfg);
data->folder = folder;
2012-08-21 21:57:39 +02:00
if(rc == ERR) ret = ERR;
}
else {
if(S_ISREG(st.st_mode)){
if(i == 0 && data->recursive_folder_names == 1){
2013-01-28 13:40:44 +01:00
folder = get_folder_id(sdata, data, fname, data->folder);
2013-06-29 15:22:07 +02:00
if(folder == ERR_FOLDER){
2013-01-28 13:40:44 +01:00
folder = add_new_folder(sdata, data, fname, data->folder);
2013-06-29 15:22:07 +02:00
if(folder == ERR_FOLDER){
printf("error: cannot get/add folder '%s' to parent id: %d\n", fname, data->folder);
return ERR;
}
else {
data->folder = folder;
}
}
}
2012-08-21 21:57:39 +02:00
rc = import_from_mailbox(fname, sdata, data, cfg);
if(rc == OK) (*tot_msgs)++;
else ret = ERR;
i++;
2012-08-21 21:57:39 +02:00
}
else {
printf("%s is not a file\n", fname);
}
}
}
else {
printf("cannot stat() %s\n", fname);
}
}
closedir(dir);
2012-07-09 15:18:21 +02:00
return ret;
}
int import_from_maildir(char *directory, struct session_data *sdata, struct __data *data, int *tot_msgs, struct __config *cfg){
DIR *dir;
struct dirent *de;
int rc=ERR, ret=OK, i=0;
2013-06-29 15:22:07 +02:00
int folder;
char *p, fname[SMALLBUFSIZE];
struct stat st;
dir = opendir(directory);
if(!dir){
printf("cannot open directory: %s\n", directory);
2012-07-09 15:18:21 +02:00
return ERR;
}
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);
if(stat(fname, &st) == 0){
if(S_ISDIR(st.st_mode)){
folder = data->folder;
rc = import_from_maildir(fname, sdata, data, tot_msgs, cfg);
data->folder = folder;
2012-07-09 15:18:21 +02:00
if(rc == ERR) ret = ERR;
}
else {
if(S_ISREG(st.st_mode)){
if(i == 0 && data->recursive_folder_names == 1){
p = strrchr(directory, '/');
if(p) p++;
else {
printf("invalid directory name: '%s'\n", directory);
return ERR;
}
2013-01-28 13:40:44 +01:00
folder = get_folder_id(sdata, data, p, data->folder);
2013-06-29 15:22:07 +02:00
if(folder == ERR_FOLDER){
2013-01-28 13:40:44 +01:00
folder = add_new_folder(sdata, data, p, data->folder);
2013-06-29 15:22:07 +02:00
if(folder == ERR_FOLDER){
printf("error: cannot get/add folder '%s' to parent id: %d\n", p, data->folder);
return ERR;
}
else {
data->folder = folder;
}
}
}
rc = import_message(fname, sdata, data, cfg);
if(rc == OK) (*tot_msgs)++;
else {
printf("error importing: '%s'\n", fname);
ret = ERR;
}
2012-09-28 14:15:45 +02:00
if(remove_after_successful_import == 1 && ret != ERR) unlink(fname);
i++;
if(quiet == 0) printf("processed: %7d\r", *tot_msgs); fflush(stdout);
}
else {
printf("%s is not a file\n", fname);
}
}
}
else {
printf("cannot stat() %s\n", fname);
}
}
closedir(dir);
2012-07-09 15:18:21 +02:00
return ret;
}
2013-08-23 13:02:51 +02:00
int import_from_imap_server(char *server, char *username, char *password, int port, struct session_data *sdata, struct __data *data, char *skiplist, int dryrun, struct __config *cfg){
2012-10-27 14:40:25 +02:00
int rc=ERR, ret=OK, sd, seq=1, skipmatch, use_ssl=0;
char *p, puf[SMALLBUFSIZE];
2013-01-10 22:30:30 +01:00
char muf[SMALLBUFSIZE];
char folders[MAXBUFSIZE];
char port_string[6];
struct addrinfo hints, *res;
snprintf(port_string, sizeof(port_string)-1, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((rc = getaddrinfo(server, port_string, &hints, &res)) != 0){
printf("getaddrinfo for '%s': %s\n", server, gai_strerror(rc));
return ERR;
}
2012-10-27 14:40:25 +02:00
if(port == 993) use_ssl = 1;
if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
printf("cannot create socket\n");
ret = ERR;
goto ENDE_IMAP;
}
if(connect(sd, res->ai_addr, res->ai_addrlen) == -1){
printf("connect()\n");
ret = ERR;
goto ENDE_IMAP;
}
if(connect_to_imap_server(sd, &seq, username, password, port, data, use_ssl) == ERR){
close(sd);
ret = ERR;
goto ENDE_IMAP;
}
2012-10-27 14:40:25 +02:00
list_folders(sd, &seq, &folders[0], sizeof(folders), use_ssl, data);
p = &folders[0];
do {
memset(puf, 0, sizeof(puf));
p = split(p, '\n', puf, sizeof(puf)-1);
if(strlen(puf) < 1) continue;
skipmatch = 0;
if(skiplist && strlen(skiplist) > 0){
2013-01-10 22:30:30 +01:00
snprintf(muf, sizeof(muf)-1, "%s,", puf);
if(strstr(skiplist, muf)) skipmatch = 1;
}
if(skipmatch == 1){
if(quiet == 0) printf("SKIPPING FOLDER: %s\n", puf);
continue;
}
if(quiet == 0) printf("processing folder: %s... ", puf);
2013-08-23 13:02:51 +02:00
if(process_imap_folder(sd, &seq, puf, sdata, data, use_ssl, dryrun, cfg) == ERR) ret = ERR;
} while(p);
2012-10-27 14:40:25 +02:00
close_connection(sd, data, use_ssl);
ENDE_IMAP:
freeaddrinfo(res);
2012-07-09 15:18:21 +02:00
return ret;
}
2013-08-23 13:02:51 +02:00
int import_from_pop3_server(char *server, char *username, char *password, int port, struct session_data *sdata, struct __data *data, int dryrun, struct __config *cfg){
int rc, ret=OK, sd, use_ssl=0;
char port_string[6];
struct addrinfo hints, *res;
snprintf(port_string, sizeof(port_string)-1, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((rc = getaddrinfo(server, port_string, &hints, &res)) != 0){
printf("getaddrinfo for '%s': %s\n", server, gai_strerror(rc));
return ERR;
}
2012-11-24 23:02:08 +01:00
if(port == 995) use_ssl = 1;
if((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1){
2012-11-24 23:02:08 +01:00
printf("cannot create socket\n");
ret = ERR;
goto ENDE_POP3;
2012-11-24 23:02:08 +01:00
}
if(connect(sd, res->ai_addr, res->ai_addrlen) == -1){
printf("connect()\n");
ret = ERR;
goto ENDE_POP3;
}
if(connect_to_pop3_server(sd, username, password, port, data, use_ssl) == ERR){
2012-11-24 23:02:08 +01:00
close(sd);
ret = ERR;
goto ENDE_POP3;
2012-11-24 23:02:08 +01:00
}
2013-08-23 13:02:51 +02:00
if(process_pop3_emails(sd, sdata, data, use_ssl, dryrun, cfg) == ERR) ret = ERR;
2012-11-24 23:02:08 +01:00
close_connection(sd, data, use_ssl);
ENDE_POP3:
freeaddrinfo(res);
2012-11-24 23:02:08 +01:00
return ret;
}
2013-08-23 13:02:51 +02:00
int read_gui_import_data(struct session_data *sdata, struct __data *data, int dryrun, struct __config *cfg){
int rc=ERR;
char s_type[SMALLBUFSIZE], s_username[SMALLBUFSIZE], s_password[SMALLBUFSIZE], s_server[SMALLBUFSIZE];
memset(s_type, 0, sizeof(s_type));
memset(s_username, 0, sizeof(s_username));
memset(s_password, 0, sizeof(s_password));
memset(s_server, 0, sizeof(s_server));
if(prepare_sql_statement(sdata, &(data->stmt_generic), SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS) == ERR) return ERR;
p_bind_init(data);
if(p_exec_query(sdata, data->stmt_generic, data) == ERR) goto ENDE;
p_bind_init(data);
data->sql[data->pos] = (char *)&(data->import->import_job_id); data->type[data->pos] = TYPE_LONG; data->len[data->pos] = sizeof(int); data->pos++;
data->sql[data->pos] = &s_type[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s_type)-2; data->pos++;
data->sql[data->pos] = &s_username[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s_username)-2; data->pos++;
data->sql[data->pos] = &s_password[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s_password)-2; data->pos++;
data->sql[data->pos] = &s_server[0]; data->type[data->pos] = TYPE_STRING; data->len[data->pos] = sizeof(s_server)-2; data->pos++;
p_store_results(sdata, data->stmt_generic, data);
if(p_fetch_results(data->stmt_generic) == OK) rc = OK;
p_free_results(data->stmt_generic);
ENDE:
close_prepared_statement(data->stmt_generic);
time(&(data->import->started));
data->import->status = 1;
update_import_job_stat(data);
if(strcmp(s_type, "pop3") == 0){
import_from_pop3_server(s_server, s_username, s_password, 110, sdata, data, dryrun, cfg);
}
return rc;
}
void usage(){
2012-11-24 23:02:08 +01:00
printf("usage: pilerimport [-c <config file>] -e <eml file> | -m <mailbox file> | -d <directory> | -i <imap server> | -K <pop3 server> | -u <imap username> -p <imap password> -P <imap port> [-F <foldername>] [-R] [-r] [-q]\n");
exit(0);
2011-12-30 15:52:59 +01:00
}
int main(int argc, char **argv){
2012-10-27 14:40:25 +02:00
int i, c, rc=0, n_mbox=0, tot_msgs=0, port=143;
2012-08-21 21:57:39 +02:00
char *configfile=CONFIG_FILE, *emlfile=NULL, *mboxdir=NULL, *mbox[MBOX_ARGS], *directory=NULL;
2012-11-24 23:02:08 +01:00
char *imapserver=NULL, *pop3server=NULL, *username=NULL, *password=NULL, *skiplist=SKIPLIST, *folder=NULL;
2011-12-30 15:52:59 +01:00
struct session_data sdata;
struct __config cfg;
struct __data data;
2013-08-23 13:02:51 +02:00
struct import import;
2011-12-30 15:52:59 +01:00
for(i=0; i<MBOX_ARGS; i++) mbox[i] = NULL;
2011-12-30 15:52:59 +01:00
data.folder = 0;
data.recursive_folder_names = 0;
2013-08-23 13:02:51 +02:00
import.import_job_id = import.total_messages = import.processed_messages = 0;
import.started = import.updated = import.finished = 0;
data.import = &import;
inithash(data.mydomains);
initrules(data.archiving_rules);
initrules(data.retention_rules);
2012-08-21 21:57:39 +02:00
while(1){
#ifdef _GNU_SOURCE
static struct option long_options[] =
{
{"config", required_argument, 0, 'c' },
{"eml", required_argument, 0, 'e' },
{"dir", required_argument, 0, 'd' },
{"mbox", required_argument, 0, 'm' },
{"mboxdir", required_argument, 0, 'M' },
{"imapserver", required_argument, 0, 'i' },
2012-11-24 23:02:08 +01:00
{"pop3server", required_argument, 0, 'K' },
2012-08-21 21:57:39 +02:00
{"username", required_argument, 0, 'u' },
{"password", required_argument, 0, 'p' },
2012-10-27 14:40:25 +02:00
{"port", required_argument, 0, 'P' },
2012-08-21 21:57:39 +02:00
{"skiplist", required_argument, 0, 'x' },
{"folder", required_argument, 0, 'F' },
2012-09-28 14:15:45 +02:00
{"quiet", required_argument, 0, 'q' },
{"recursive", required_argument, 0, 'R' },
2012-09-28 14:15:45 +02:00
{"remove-after-import", required_argument, 0, 'r' },
2013-08-23 13:02:51 +02:00
{"gui-import", required_argument, 0, 'G' },
{"dry-run", required_argument, 0, 'D' },
2012-08-21 21:57:39 +02:00
{"help", no_argument, 0, 'h' },
{0,0,0,0}
};
int option_index = 0;
2013-08-23 13:02:51 +02:00
c = getopt_long(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:GDRrqh?", long_options, &option_index);
2012-08-21 21:57:39 +02:00
#else
2013-08-23 13:02:51 +02:00
c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:GDRrqh?");
2012-08-21 21:57:39 +02:00
#endif
if(c == -1) break;
switch(c){
case 'c' :
configfile = optarg;
break;
case 'e' :
emlfile = optarg;
break;
case 'd' :
directory = optarg;
break;
case 'm' :
if(n_mbox < MBOX_ARGS){
mbox[n_mbox++] = optarg;
} else {
printf("too many -m <mailbox> arguments: %s\n", optarg);
}
break;
2012-08-21 21:57:39 +02:00
case 'M' :
mboxdir = optarg;
break;
case 'i' :
imapserver = optarg;
break;
2012-11-24 23:02:08 +01:00
case 'K' :
pop3server = optarg;
if(port == 143) port = 110;
break;
case 'u' :
username = optarg;
break;
case 'p' :
password = optarg;
break;
2012-10-27 14:40:25 +02:00
case 'P' :
port = atoi(optarg);
break;
case 'x' :
skiplist = optarg;
break;
case 'F' :
folder = optarg;
2012-08-21 21:57:39 +02:00
break;
case 'R' :
data.recursive_folder_names = 1;
break;
2012-09-28 14:15:45 +02:00
case 'r' :
remove_after_successful_import = 1;
break;
2013-08-23 13:02:51 +02:00
case 'G' :
import_from_gui = 1;
break;
case 'D' :
dryrun = 1;
break;
2012-09-28 14:15:45 +02:00
case 'q' :
quiet = 1;
break;
case 'h' :
case '?' :
usage();
break;
default :
break;
}
2011-12-30 15:52:59 +01:00
}
2013-08-23 13:02:51 +02:00
if(!mbox[0] && !mboxdir && !emlfile && !directory && !imapserver && !pop3server && import_from_gui == 0) usage();
cfg = read_config(configfile);
2011-12-30 15:52:59 +01:00
if(read_key(&cfg)){
printf("%s\n", ERR_READING_KEY);
2012-07-09 15:18:21 +02:00
return ERR;
2011-12-30 15:52:59 +01:00
}
2013-04-28 14:18:09 +02:00
if(open_database(&sdata, &cfg) == ERR) return 0;
2011-12-30 15:52:59 +01:00
setlocale(LC_CTYPE, cfg.locale);
2012-03-20 10:35:22 +01:00
(void) openlog("pilerimport", LOG_PID, LOG_MAIL);
2013-04-22 22:28:01 +02:00
#ifdef HAVE_MEMCACHED
memcached_init(&(data.memc), cfg.memcached_servers, 11211);
#endif
if(folder){
2013-01-28 13:40:44 +01:00
data.folder = get_folder_id(&sdata, &data, folder, 0);
2013-06-29 15:22:07 +02:00
if(data.folder == ERR_FOLDER){
2013-01-28 13:40:44 +01:00
data.folder = add_new_folder(&sdata, &data, folder, 0);
}
2013-06-29 15:22:07 +02:00
if(data.folder == ERR_FOLDER){
printf("error: cannot get/add folder '%s'\n", folder);
2013-04-28 14:18:09 +02:00
close_database(&sdata);
return 0;
}
}
load_rules(&sdata, &data, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, &data, data.retention_rules, SQL_RETENTION_RULE_TABLE);
2011-12-30 15:52:59 +01:00
2013-01-06 22:16:21 +01:00
load_mydomains(&sdata, &data, &cfg);
if(emlfile) rc = import_message(emlfile, &sdata, &data, &cfg);
if(mbox[0]){
for(i=0; i<n_mbox; i++){
rc = import_from_mailbox(mbox[i], &sdata, &data, &cfg);
}
}
2012-08-21 21:57:39 +02:00
if(mboxdir) rc = import_mbox_from_dir(mboxdir, &sdata, &data, &tot_msgs, &cfg);
if(directory) rc = import_from_maildir(directory, &sdata, &data, &tot_msgs, &cfg);
2013-08-23 13:02:51 +02:00
if(imapserver && username && password) rc = import_from_imap_server(imapserver, username, password, port, &sdata, &data, skiplist, dryrun, &cfg);
if(pop3server && username && password) rc = import_from_pop3_server(pop3server, username, password, port, &sdata, &data, dryrun, &cfg);
if(import_from_gui == 1) rc = read_gui_import_data(&sdata, &data, dryrun, &cfg);
2011-12-30 15:52:59 +01:00
clearrules(data.archiving_rules);
clearrules(data.retention_rules);
2011-12-30 15:52:59 +01:00
clearhash(data.mydomains);
2013-07-12 22:54:45 +02:00
2013-04-28 14:18:09 +02:00
close_database(&sdata);
2011-12-30 15:52:59 +01:00
2012-08-21 21:57:39 +02:00
if(quiet == 0) printf("\n");
2012-07-09 15:18:21 +02:00
return rc;
2011-12-30 15:52:59 +01:00
}