Added support for gui import

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2020-10-27 21:53:16 +01:00
parent 96dbde7cba
commit 1d615a1967
7 changed files with 54 additions and 6 deletions

View File

@ -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 */

View File

@ -304,6 +304,7 @@ struct import {
int tot_msgs;
int port;
int seq;
int table_id;
char *server;
char *username;
char *password;

View File

@ -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;

View File

@ -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);

View File

@ -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;
}

View File

@ -52,6 +52,7 @@ void usage(){
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(" -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(" -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;

View File

@ -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 <imap host>")
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']))