piler/src/pilerimport.c

363 lines
11 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;
2013-08-23 13:02:51 +02:00
void usage(){
printf("\nusage: pilerimport\n\n");
printf(" [-c <config file>] Config file to use if not the default\n");
printf(" -e <eml file> EML file to import\n");
printf(" -m <mailbox file> Mbox file to import\n");
printf(" -d <dir> Directory with EML files to import\n");
printf(" -i <imap server> IMAP server to connect\n");
printf(" -K <pop3 server> POP3 server to connect\n");
printf(" -u <username> Username for imap/pop3 import\n");
printf(" -p <password> Password for imap/pop3 import\n");
printf(" -P <port> Port for imap/pop3 import (default: 143/110\n");
printf(" -t <timeout> Timeout in sec for imap/pop3 import\n");
printf(" -x <folder1,folder2,....folderN,> Comma separated list of imap folders to skip. Add the trailing comma!\n");
printf(" -f <imap folder> IMAP folder name to import\n");
printf(" -g <imap folder> Move email after import to this IMAP folder\n");
printf(" -F <folder> Piler folder name to assign to this import\n");
2015-12-05 11:56:01 +01:00
printf(" -R Assign IMAP folder names as Piler folder names\n");
printf(" -b <batch limit> Import only this many emails\n");
printf(" -s <start position> Start importing POP3 emails from this position\n");
printf(" -j <failed folder> Move failed to import emails to this folder\n");
2016-04-05 21:10:09 +02:00
printf(" -a <recipient> Add recipient to the To:/Cc: list\n");
printf(" -D Dry-run, do not import anything\n");
printf(" -o Only download emails for POP3/IMAP import\n");
printf(" -r Remove imported emails\n");
printf(" -q Quiet mode\n");
exit(0);
2011-12-30 15:52:59 +01:00
}
int main(int argc, char **argv){
int i, n_mbox=0;
char *configfile=CONFIG_FILE, *mbox[MBOX_ARGS], *directory=NULL;
char puf[SMALLBUFSIZE], *imapserver=NULL, *pop3server=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;
struct net net;
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
2013-09-11 09:19:29 +02:00
srand(getpid());
data.folder = 0;
data.recursive_folder_names = 0;
2013-09-27 14:33:15 +02:00
data.quiet = 0;
2013-08-23 13:02:51 +02:00
import.import_job_id = import.total_messages = import.total_size = import.processed_messages = import.batch_processing_limit = 0;
import.started = import.updated = import.finished = import.remove_after_import = 0;
import.extra_recipient = import.move_folder = import.failed_folder = NULL;
import.start_position = 1;
2014-10-15 23:22:36 +02:00
import.download_only = 0;
import.dryrun = 0;
import.port = 143;
import.server = NULL;
import.username = NULL;
import.password = NULL;
import.database = NULL;
import.skiplist = SKIPLIST;
import.folder_imap = NULL;
memset(import.filename, 0, SMALLBUFSIZE);
import.mboxdir = NULL;
import.tot_msgs = 0;
import.folder = NULL;
2013-08-23 13:02:51 +02:00
data.import = &import;
net.socket = -1;
net.timeout = 30;
data.net = &net;
inithash(data.mydomains);
initrules(data.archiving_rules);
initrules(data.retention_rules);
initrules(data.folder_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' },
{"folder_imap", required_argument, 0, 'f' },
2014-06-04 22:20:10 +02:00
{"add-recipient",required_argument, 0, 'a' },
{"batch-limit", required_argument, 0, 'b' },
{"timeout", required_argument, 0, 't' },
{"start-position",required_argument, 0, 's' },
2014-06-04 22:20:10 +02:00
{"quiet", no_argument, 0, 'q' },
{"recursive", required_argument, 0, 'R' },
2014-06-04 22:20:10 +02:00
{"remove-after-import",no_argument, 0, 'r' },
{"failed-folder", required_argument, 0, 'j' },
{"move-folder", required_argument, 0, 'g' },
2014-10-15 23:22:36 +02:00
{"only-download",no_argument, 0, 'o' },
2014-06-04 22:20:10 +02:00
{"dry-run", no_argument, 0, 'D' },
2012-08-21 21:57:39 +02:00
{"help", no_argument, 0, 'h' },
{0,0,0,0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:DRroqh?", long_options, &option_index);
2012-08-21 21:57:39 +02:00
#else
int c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:DRroqh?");
2012-08-21 21:57:39 +02:00
#endif
if(c == -1) break;
switch(c){
case 'c' :
configfile = optarg;
break;
case 'e' :
snprintf(data.import->filename, SMALLBUFSIZE-1, "%s", 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' :
data.import->mboxdir = optarg;
2012-08-21 21:57:39 +02:00
break;
case 'i' :
imapserver = optarg;
data.import->server = optarg;
break;
2012-11-24 23:02:08 +01:00
case 'K' :
pop3server = optarg;
data.import->server = optarg;
if(data.import->port == 143) data.import->port = 110;
2012-11-24 23:02:08 +01:00
break;
case 'u' :
data.import->username = optarg;
break;
case 'p' :
data.import->password = optarg;
break;
2012-10-27 14:40:25 +02:00
case 'P' :
data.import->port = atoi(optarg);
2012-10-27 14:40:25 +02:00
break;
case 'x' :
data.import->skiplist = optarg;
break;
case 'F' :
data.import->folder = optarg;
2012-08-21 21:57:39 +02:00
break;
case 'f' :
data.import->folder_imap = optarg;
break;
case 'R' :
data.recursive_folder_names = 1;
break;
2012-09-28 14:15:45 +02:00
case 'r' :
data.import->remove_after_import = 1;
2012-09-28 14:15:45 +02:00
break;
case 'g' :
data.import->move_folder = optarg;
break;
case 'j' :
data.import->failed_folder = optarg;
break;
2014-10-15 23:22:36 +02:00
case 'o' :
data.import->download_only = 1;
data.import->dryrun = 1;
2014-10-15 23:22:36 +02:00
break;
case 'b' :
data.import->batch_processing_limit = atoi(optarg);
break;
case 't' :
data.import->timeout = atoi(optarg);
break;
case 's' :
if(atoi(optarg) < 1){
printf("invalid start position: %s\n", optarg);
return -1;
}
data.import->start_position = atoi(optarg);
break;
2014-06-04 22:20:10 +02:00
case 'a' :
snprintf(puf, sizeof(puf)-1, "%s ", optarg);
data.import->extra_recipient = puf;
2014-06-04 22:20:10 +02:00
break;
2013-08-23 13:02:51 +02:00
case 'D' :
data.import->dryrun = 1;
2013-08-23 13:02:51 +02:00
break;
2012-09-28 14:15:45 +02:00
case 'q' :
2013-09-27 14:33:15 +02:00
data.quiet = 1;
2012-09-28 14:15:45 +02:00
break;
case 'h' :
case '?' :
usage();
break;
default :
break;
}
2011-12-30 15:52:59 +01:00
}
if(!mbox[0] && !data.import->mboxdir && !data.import->filename && !directory && !imapserver && !pop3server) usage();
if(data.import->failed_folder && !can_i_write_directory(data.import->failed_folder)){
printf("cannot write failed directory '%s'\n", data.import->failed_folder);
return ERR;
}
if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!");
cfg = read_config(configfile);
2011-12-30 15:52:59 +01:00
if((data.recursive_folder_names == 1 || data.import->folder) && cfg.enable_folders == 0){
2015-12-05 11:56:01 +01:00
printf("please set enable_folders=1 in piler.conf to use the folder options\n");
return ERR;
}
/* make sure we don't discard messages without a valid Message-Id when importing manually */
cfg.archive_emails_not_having_message_id = 1;
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
}
/* enable using the extra email address */
if(data.import->extra_recipient) cfg.process_rcpt_to_addresses = 1;
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(data.import->folder){
data.folder = get_folder_id(&sdata, data.import->folder, 0);
2013-06-29 15:22:07 +02:00
if(data.folder == ERR_FOLDER){
data.folder = add_new_folder(&sdata, data.import->folder, 0);
}
2013-06-29 15:22:07 +02:00
if(data.folder == ERR_FOLDER){
printf("ERROR: cannot get/add folder '%s'\n", data.import->folder);
2013-04-28 14:18:09 +02:00
close_database(&sdata);
return 0;
}
}
load_rules(&sdata, data.archiving_rules, SQL_ARCHIVING_RULE_TABLE);
load_rules(&sdata, data.retention_rules, SQL_RETENTION_RULE_TABLE);
load_rules(&sdata, data.folder_rules, SQL_FOLDER_RULE_TABLE);
2011-12-30 15:52:59 +01:00
2013-01-06 22:16:21 +01:00
load_mydomains(&sdata, &data, &cfg);
if(data.import->filename[0] != '\0') import_message(&sdata, &data, &cfg);
if(mbox[0]){
for(i=0; i<n_mbox; i++){
import_from_mailbox(mbox[i], &sdata, &data, &cfg);
}
}
if(data.import->mboxdir) import_mbox_from_dir(data.import->mboxdir, &sdata, &data, &cfg);
if(directory) import_from_maildir(&sdata, &data, directory, &cfg);
if(imapserver) import_from_imap_server(&sdata, &data, &cfg);
if(pop3server) import_from_pop3_server(&sdata, &data, &cfg);
2011-12-30 15:52:59 +01:00
clearrules(data.archiving_rules);
clearrules(data.retention_rules);
clearrules(data.folder_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
2013-09-27 14:33:15 +02:00
if(data.quiet == 0) printf("\n");
2012-08-21 21:57:39 +02:00
return 0;
2011-12-30 15:52:59 +01:00
}