mirror of
https://bitbucket.org/jsuto/piler.git
synced 2024-11-08 02:21:59 +01:00
Added support for gui import
Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
parent
96dbde7cba
commit
1d615a1967
@ -107,6 +107,7 @@
|
|||||||
#define SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE "UPDATE " SQL_METADATA_TABLE " SET reference=? WHERE message_id=? AND reference=''"
|
#define SQL_PREPARED_STMT_UPDATE_METADATA_REFERENCE "UPDATE " SQL_METADATA_TABLE " SET reference=? WHERE message_id=? AND reference=''"
|
||||||
#define SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS "SELECT id, type, username, password, server FROM " SQL_IMPORT_TABLE " WHERE started=0 ORDER BY id LIMIT 0,1"
|
#define SQL_PREPARED_STMT_GET_GUI_IMPORT_JOBS "SELECT id, type, username, password, server FROM " SQL_IMPORT_TABLE " WHERE started=0 ORDER BY id LIMIT 0,1"
|
||||||
#define SQL_PREPARED_STMT_INSERT_FOLDER_MESSAGE "INSERT INTO " SQL_FOLDER_MESSAGE_TABLE " (`folder_id`, `id`) VALUES(?,?)"
|
#define SQL_PREPARED_STMT_INSERT_FOLDER_MESSAGE "INSERT INTO " SQL_FOLDER_MESSAGE_TABLE " (`folder_id`, `id`) VALUES(?,?)"
|
||||||
|
#define SQL_PREPARED_STMT_UPDATE_IMPORT_TABLE "UPDATE " SQL_IMPORT_TABLE " SET status=?, imported=? WHERE id=?"
|
||||||
|
|
||||||
/* Error codes */
|
/* Error codes */
|
||||||
|
|
||||||
|
@ -304,6 +304,7 @@ struct import {
|
|||||||
int tot_msgs;
|
int tot_msgs;
|
||||||
int port;
|
int port;
|
||||||
int seq;
|
int seq;
|
||||||
|
int table_id;
|
||||||
char *server;
|
char *server;
|
||||||
char *username;
|
char *username;
|
||||||
char *password;
|
char *password;
|
||||||
|
20
src/import.c
20
src/import.c
@ -144,6 +144,26 @@ int import_message(struct session_data *sdata, struct data *data, struct config
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int update_import_table(struct session_data *sdata, struct data *data) {
|
||||||
|
int ret=ERR, status=2;
|
||||||
|
struct sql sql;
|
||||||
|
|
||||||
|
if(prepare_sql_statement(sdata, &sql, SQL_PREPARED_STMT_UPDATE_IMPORT_TABLE) == ERR) return ret;
|
||||||
|
|
||||||
|
p_bind_init(&sql);
|
||||||
|
|
||||||
|
sql.sql[sql.pos] = (char *)&(status); sql.type[sql.pos] = TYPE_LONG; sql.pos++;
|
||||||
|
sql.sql[sql.pos] = (char *)&(data->import->tot_msgs); sql.type[sql.pos] = TYPE_LONG; sql.pos++;
|
||||||
|
sql.sql[sql.pos] = (char *)&(data->import->table_id); sql.type[sql.pos] = TYPE_LONG; sql.pos++;
|
||||||
|
|
||||||
|
if(p_exec_stmt(sdata, &sql) == OK) ret = OK;
|
||||||
|
|
||||||
|
close_prepared_statement(&sql);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int get_folder_id(struct session_data *sdata, char *foldername, int parent_id){
|
int get_folder_id(struct session_data *sdata, char *foldername, int parent_id){
|
||||||
int id=ERR_FOLDER;
|
int id=ERR_FOLDER;
|
||||||
struct sql sql;
|
struct sql sql;
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
|
|
||||||
int import_message(struct session_data *sdata, struct data *data, struct config *cfg);
|
int import_message(struct session_data *sdata, struct data *data, struct config *cfg);
|
||||||
|
int update_import_table(struct session_data *sdata, struct data *data);
|
||||||
|
|
||||||
int import_from_maildir(struct session_data *sdata, struct data *data, char *directory, struct config *cfg);
|
int import_from_maildir(struct session_data *sdata, struct data *data, char *directory, struct config *cfg);
|
||||||
int import_from_mailbox(char *mailbox, struct session_data *sdata, struct data *data, struct config *cfg);
|
int import_from_mailbox(char *mailbox, struct session_data *sdata, struct data *data, struct config *cfg);
|
||||||
|
@ -103,5 +103,9 @@ int import_from_maildir(struct session_data *sdata, struct data *data, char *dir
|
|||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
|
if(data->import->table_id > 0){
|
||||||
|
update_import_table(sdata, data);
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,7 @@ void usage(){
|
|||||||
printf(" -s <start position> Start importing POP3 emails from this position\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");
|
printf(" -j <failed folder> Move failed to import emails to this folder\n");
|
||||||
printf(" -a <recipient> Add recipient to the To:/Cc: list\n");
|
printf(" -a <recipient> Add recipient to the To:/Cc: list\n");
|
||||||
|
printf(" -T <id> Update import table at id=<id>\n");
|
||||||
printf(" -D Dry-run, do not import anything\n");
|
printf(" -D Dry-run, do not import anything\n");
|
||||||
printf(" -o Only download emails for POP3/IMAP import\n");
|
printf(" -o Only download emails for POP3/IMAP import\n");
|
||||||
printf(" -r Remove imported emails\n");
|
printf(" -r Remove imported emails\n");
|
||||||
@ -96,6 +97,7 @@ int main(int argc, char **argv){
|
|||||||
memset(import.filename, 0, SMALLBUFSIZE);
|
memset(import.filename, 0, SMALLBUFSIZE);
|
||||||
import.mboxdir = NULL;
|
import.mboxdir = NULL;
|
||||||
import.tot_msgs = 0;
|
import.tot_msgs = 0;
|
||||||
|
import.table_id = 0;
|
||||||
import.folder = NULL;
|
import.folder = NULL;
|
||||||
|
|
||||||
data.import = &import;
|
data.import = &import;
|
||||||
@ -132,6 +134,7 @@ int main(int argc, char **argv){
|
|||||||
{"batch-limit", required_argument, 0, 'b' },
|
{"batch-limit", required_argument, 0, 'b' },
|
||||||
{"timeout", required_argument, 0, 't' },
|
{"timeout", required_argument, 0, 't' },
|
||||||
{"start-position",required_argument, 0, 's' },
|
{"start-position",required_argument, 0, 's' },
|
||||||
|
{"table-id", required_argument, 0, 'T' },
|
||||||
{"quiet", no_argument, 0, 'q' },
|
{"quiet", no_argument, 0, 'q' },
|
||||||
{"recursive", required_argument, 0, 'R' },
|
{"recursive", required_argument, 0, 'R' },
|
||||||
{"remove-after-import",no_argument, 0, 'r' },
|
{"remove-after-import",no_argument, 0, 'r' },
|
||||||
@ -145,9 +148,9 @@ int main(int argc, char **argv){
|
|||||||
|
|
||||||
int option_index = 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);
|
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:T:DRroqh?", long_options, &option_index);
|
||||||
#else
|
#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?");
|
int c = getopt(argc, argv, "c:m:M:e:d:i:K:u:p:P:x:F:f:a:b:t:s:g:j:T:DRroqh?");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if(c == -1) break;
|
if(c == -1) break;
|
||||||
@ -257,6 +260,15 @@ int main(int argc, char **argv){
|
|||||||
data.import->extra_recipient = puf;
|
data.import->extra_recipient = puf;
|
||||||
break;
|
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 'D' :
|
case 'D' :
|
||||||
data.import->dryrun = 1;
|
data.import->dryrun = 1;
|
||||||
break;
|
break;
|
||||||
|
@ -6,10 +6,12 @@ import argparse
|
|||||||
import configparser
|
import configparser
|
||||||
import imaplib
|
import imaplib
|
||||||
import re
|
import re
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
opts = {}
|
opts = {}
|
||||||
INBOX = 'INBOX'
|
INBOX = 'INBOX'
|
||||||
|
ST_RUNNING = 1
|
||||||
|
|
||||||
|
|
||||||
def read_options(filename="", opts={}):
|
def read_options(filename="", opts={}):
|
||||||
@ -61,6 +63,12 @@ def process_folder(conn, folder):
|
|||||||
print("Folder {} has {} messages".format(folder, n))
|
print("Folder {} has {} messages".format(folder, n))
|
||||||
|
|
||||||
if n > 0:
|
if n > 0:
|
||||||
|
if opts['id']:
|
||||||
|
cursor = opts['db'].cursor()
|
||||||
|
data = (ST_RUNNING, n, opts['id'])
|
||||||
|
cursor.execute("UPDATE import SET status=%s, total = total + %s WHERE id=%s", data)
|
||||||
|
opts['db'].commit()
|
||||||
|
|
||||||
rc, data = conn.search(None, 'ALL')
|
rc, data = conn.search(None, 'ALL')
|
||||||
for num in data[0].split():
|
for num in data[0].split():
|
||||||
rc, data = conn.fetch(num, '(RFC822)')
|
rc, data = conn.fetch(num, '(RFC822)')
|
||||||
@ -88,7 +96,6 @@ def main():
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print(args)
|
|
||||||
if not bool(args.import_from_table or args.server):
|
if not bool(args.import_from_table or args.server):
|
||||||
print("Please specify either --import-from-table or --server <imap host>")
|
print("Please specify either --import-from-table or --server <imap host>")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -97,23 +104,24 @@ def main():
|
|||||||
opts['verbose'] = args.verbose
|
opts['verbose'] = args.verbose
|
||||||
opts['counter'] = 0
|
opts['counter'] = 0
|
||||||
opts['db'] = None
|
opts['db'] = None
|
||||||
|
opts['id'] = 0
|
||||||
|
|
||||||
server = ''
|
server = ''
|
||||||
user = ''
|
user = ''
|
||||||
password = ''
|
password = ''
|
||||||
|
|
||||||
if args.import_table:
|
if args.import_from_table:
|
||||||
read_options(args.config, opts)
|
read_options(args.config, opts)
|
||||||
try:
|
try:
|
||||||
opts['db'] = dbapi.connect(opts['dbhost'], opts['username'],
|
opts['db'] = dbapi.connect(opts['dbhost'], opts['username'],
|
||||||
opts['password'], opts['database'])
|
opts['password'], opts['database'])
|
||||||
|
|
||||||
cursor = opts['db'].cursor()
|
cursor = opts['db'].cursor()
|
||||||
cursor.execute("SELECT server, username, password FROM import WHERE started=0")
|
cursor.execute("SELECT id, server, username, password FROM import WHERE started=0")
|
||||||
|
|
||||||
row = cursor.fetchone()
|
row = cursor.fetchone()
|
||||||
if row:
|
if row:
|
||||||
(server, user, password) = row
|
(opts['id'], server, user, password) = row
|
||||||
else:
|
else:
|
||||||
print("Nothing to read from import table")
|
print("Nothing to read from import table")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
@ -150,6 +158,7 @@ def main():
|
|||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
if opts['db']:
|
if opts['db']:
|
||||||
|
subprocess.call(["pilerimport", "-d", "/var/piler/imap", "-r"])
|
||||||
opts['db'].close()
|
opts['db'].close()
|
||||||
|
|
||||||
print("Processed {} messages".format(opts['counter']))
|
print("Processed {} messages".format(opts['counter']))
|
||||||
|
Loading…
Reference in New Issue
Block a user