From 1d615a196792610dc15c63388ffbe041bafb5282 Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Tue, 27 Oct 2020 21:53:16 +0100 Subject: [PATCH] Added support for gui import Signed-off-by: Janos SUTO --- src/config.h | 1 + src/defs.h | 1 + src/import.c | 20 ++++++++++++++++++++ src/import.h | 1 + src/import_maildir.c | 4 ++++ src/pilerimport.c | 16 ++++++++++++++-- util/imapfetch.py | 17 +++++++++++++---- 7 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/config.h b/src/config.h index 117f3f8a..e1542ad5 100644 --- a/src/config.h +++ b/src/config.h @@ -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_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_UPDATE_IMPORT_TABLE "UPDATE " SQL_IMPORT_TABLE " SET status=?, imported=? WHERE id=?" /* Error codes */ diff --git a/src/defs.h b/src/defs.h index 50be06f0..f7477a65 100644 --- a/src/defs.h +++ b/src/defs.h @@ -304,6 +304,7 @@ struct import { int tot_msgs; int port; int seq; + int table_id; char *server; char *username; char *password; diff --git a/src/import.c b/src/import.c index eb16f8f8..601c9b84 100644 --- a/src/import.c +++ b/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 id=ERR_FOLDER; struct sql sql; diff --git a/src/import.h b/src/import.h index 0fa2a211..0979acab 100644 --- a/src/import.h +++ b/src/import.h @@ -7,6 +7,7 @@ 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_mailbox(char *mailbox, struct session_data *sdata, struct data *data, struct config *cfg); diff --git a/src/import_maildir.c b/src/import_maildir.c index d5cae635..aac3c6a3 100644 --- a/src/import_maildir.c +++ b/src/import_maildir.c @@ -103,5 +103,9 @@ int import_from_maildir(struct session_data *sdata, struct data *data, char *dir } closedir(dir); + if(data->import->table_id > 0){ + update_import_table(sdata, data); + } + return ret; } diff --git a/src/pilerimport.c b/src/pilerimport.c index 6b34d6f0..0f07d1e9 100644 --- a/src/pilerimport.c +++ b/src/pilerimport.c @@ -52,6 +52,7 @@ void usage(){ 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(" -o Only download emails for POP3/IMAP import\n"); printf(" -r Remove imported emails\n"); @@ -96,6 +97,7 @@ int main(int argc, char **argv){ memset(import.filename, 0, SMALLBUFSIZE); import.mboxdir = NULL; import.tot_msgs = 0; + import.table_id = 0; import.folder = NULL; data.import = &import; @@ -132,6 +134,7 @@ int main(int argc, char **argv){ {"batch-limit", required_argument, 0, 'b' }, {"timeout", required_argument, 0, 't' }, {"start-position",required_argument, 0, 's' }, + {"table-id", required_argument, 0, 'T' }, {"quiet", no_argument, 0, 'q' }, {"recursive", required_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 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 - 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 if(c == -1) break; @@ -257,6 +260,15 @@ int main(int argc, char **argv){ 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 'D' : data.import->dryrun = 1; break; diff --git a/util/imapfetch.py b/util/imapfetch.py index 08b6649f..df9d570d 100755 --- a/util/imapfetch.py +++ b/util/imapfetch.py @@ -6,10 +6,12 @@ import argparse import configparser import imaplib import re +import subprocess import sys opts = {} INBOX = 'INBOX' +ST_RUNNING = 1 def read_options(filename="", opts={}): @@ -61,6 +63,12 @@ def process_folder(conn, folder): print("Folder {} has {} messages".format(folder, n)) 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') for num in data[0].split(): rc, data = conn.fetch(num, '(RFC822)') @@ -88,7 +96,6 @@ def main(): args = parser.parse_args() - print(args) if not bool(args.import_from_table or args.server): print("Please specify either --import-from-table or --server ") sys.exit(1) @@ -97,23 +104,24 @@ def main(): opts['verbose'] = args.verbose opts['counter'] = 0 opts['db'] = None + opts['id'] = 0 server = '' user = '' password = '' - if args.import_table: + if args.import_from_table: read_options(args.config, opts) try: opts['db'] = dbapi.connect(opts['dbhost'], opts['username'], opts['password'], opts['database']) 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() if row: - (server, user, password) = row + (opts['id'], server, user, password) = row else: print("Nothing to read from import table") sys.exit(0) @@ -150,6 +158,7 @@ def main(): conn.close() if opts['db']: + subprocess.call(["pilerimport", "-d", "/var/piler/imap", "-r"]) opts['db'].close() print("Processed {} messages".format(opts['counter']))