/* * pilerimport.c, SJ */ #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define SKIPLIST "junk,trash,spam,draft," #define MBOX_ARGS 1024 extern char *optarg; extern int optind; void usage(){ printf("\nusage: pilerimport\n\n"); printf(" [-c ] Config file to use if not the default\n"); printf(" -e EML file to import\n"); printf(" -m Mbox file to import\n"); printf(" -d Directory with EML files to import\n"); printf(" -i IMAP server to connect\n"); printf(" -K POP3 server to connect\n"); printf(" -u Username for imap/pop3 import\n"); printf(" -p Password for imap/pop3 import\n"); printf(" -P Port for imap/pop3 import (default: 143/110\n"); printf(" -t Timeout in sec for imap/pop3 import\n"); printf(" -x Comma separated list of imap folders to skip. Add the trailing comma!\n"); printf(" -f IMAP folder name to import\n"); printf(" -g Move email after import to this IMAP folder\n"); printf(" -F Piler folder name to assign to this import\n"); printf(" -R Assign IMAP folder names as Piler folder names\n"); printf(" -b Import only this many emails\n"); printf(" -s Start importing POP3 emails from this position\n"); printf(" -j Move failed to import emails to this folder\n"); printf(" -a Add recipient to the To:/Cc: list\n"); printf(" -T Update import table at id=\n"); printf(" -D Dry-run, do not import anything\n"); printf(" -y Read pilerexport data from stdin\n"); printf(" -o Only download emails for POP3/IMAP import\n"); printf(" -r Remove imported emails\n"); printf(" -q Quiet mode\n"); exit(0); } int main(int argc, char **argv){ int i, n_mbox=0, read_from_pilerexport=0; char *configfile=CONFIG_FILE, *mbox[MBOX_ARGS], *directory=NULL; char puf[SMALLBUFSIZE], *imapserver=NULL, *pop3server=NULL; struct session_data sdata; struct config cfg; struct data data; struct import import; struct net net; for(i=0; ifilename, 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 arguments: %s\n", optarg); } break; case 'M' : data.import->mboxdir = optarg; break; case 'i' : imapserver = optarg; data.import->server = optarg; break; case 'K' : pop3server = optarg; data.import->server = optarg; if(data.import->port == 143) data.import->port = 110; break; case 'u' : data.import->username = optarg; break; case 'p' : data.import->password = optarg; break; case 'P' : data.import->port = atoi(optarg); break; case 'x' : data.import->skiplist = optarg; break; case 'F' : data.import->folder = optarg; break; case 'f' : data.import->folder_imap = optarg; break; case 'R' : data.recursive_folder_names = 1; break; case 'r' : data.import->remove_after_import = 1; break; case 'g' : data.import->move_folder = optarg; break; case 'j' : data.import->failed_folder = optarg; break; case 'o' : data.import->download_only = 1; data.import->dryrun = 1; 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; case 'a' : snprintf(puf, sizeof(puf)-1, "%s ", optarg); data.import->extra_recipient = puf; break; case 'T' : if(atoi(optarg) < 1){ printf("invalid import table id: %s\n", optarg); return -1; } data.import->table_id = atoi(optarg); break; case 'y' : read_from_pilerexport = 1; break; case 'D' : data.import->dryrun = 1; break; case 'q' : data.quiet = 1; break; case 'h' : case '?' : usage(); break; default : break; } } 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); memset(cfg.security_header, 0, MAXVAL); if((data.recursive_folder_names == 1 || data.import->folder) && cfg.enable_folders == 0){ 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; if(read_key(&cfg)){ printf("%s\n", ERR_READING_KEY); return ERR; } /* enable using the extra email address */ if(data.import->extra_recipient) cfg.process_rcpt_to_addresses = 1; if(open_database(&sdata, &cfg) == ERR) return 0; setlocale(LC_CTYPE, cfg.locale); (void) openlog("pilerimport", LOG_PID, LOG_MAIL); #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); if(data.folder == ERR_FOLDER){ data.folder = add_new_folder(&sdata, data.import->folder, 0); } if(data.folder == ERR_FOLDER){ printf("ERROR: cannot get/add folder '%s'\n", data.import->folder); 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); load_mydomains(&sdata, &data, &cfg); if(data.import->filename[0] != '\0') import_message(&sdata, &data, &cfg); if(mbox[0]){ for(i=0; imboxdir) 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); if(read_from_pilerexport) import_from_pilerexport(&sdata, &data, &cfg); clearrules(data.archiving_rules); clearrules(data.retention_rules); clearrules(data.folder_rules); clearhash(data.mydomains); close_database(&sdata); if(data.quiet == 0) printf("\n"); return 0; }