imapfetch.py can read imap connection from import table

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2020-10-26 23:08:03 +01:00
parent b2358e6307
commit 68f72d0784

View File

@ -1,14 +1,32 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import MySQLdb as dbapi
import argparse import argparse
import configparser
import imaplib import imaplib
import re import re
import sys
opts = {} opts = {}
INBOX = 'INBOX' INBOX = 'INBOX'
def read_options(filename="", opts={}):
s = "[piler]\n" + open(filename, 'r').read()
config = configparser.ConfigParser()
config.read_string(s)
if config.has_option('piler', 'mysqlhost'):
opts['dbhost'] = config.get('piler', 'mysqlhost')
else:
opts['dbhost'] = 'localhost'
opts['username'] = config.get('piler', 'mysqluser')
opts['password'] = config.get('piler', 'mysqlpwd')
opts['database'] = config.get('piler', 'mysqldb')
def read_folder_list(conn): def read_folder_list(conn):
result = [] result = []
@ -34,11 +52,13 @@ def read_folder_list(conn):
def process_folder(conn, folder): def process_folder(conn, folder):
print("Processing {}".format(folder)) if opts['verbose']:
print("Processing {}".format(folder))
rc, data = conn.select(folder) rc, data = conn.select(folder)
n = int(data[0]) n = int(data[0])
print("Folder {} has {} messages".format(folder, n)) if opts['verbose']:
print("Folder {} has {} messages".format(folder, n))
if n > 0: if n > 0:
rc, data = conn.search(None, 'ALL') rc, data = conn.search(None, 'ALL')
@ -46,39 +66,71 @@ def process_folder(conn, folder):
rc, data = conn.fetch(num, '(RFC822)') rc, data = conn.fetch(num, '(RFC822)')
if opts['verbose']: if opts['verbose']:
print(rc, num) print(rc, num)
opts['counter'] = opts['counter'] + 1 opts['counter'] += 1
with open("{}.eml".format(opts['counter']), "wb") as f: with open("{}.eml".format(opts['counter']), "wb") as f:
f.write(data[0][1]) f.write(data[0][1])
def main(): def main():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", type=str, help="imap server", required=True) parser.add_argument("-c", "--config", type=str, help="piler.conf path",
default="/etc/piler/piler.conf")
parser.add_argument("-s", "--server", type=str, help="imap server")
parser.add_argument("-P", "--port", type=int, help="port number", default=143) parser.add_argument("-P", "--port", type=int, help="port number", default=143)
parser.add_argument("-u", "--user", type=str, help="imap user", required=True) parser.add_argument("-u", "--user", type=str, help="imap user")
parser.add_argument("-p", "--password", type=str, help="imap password", parser.add_argument("-p", "--password", type=str, help="imap password")
required=True)
parser.add_argument("-x", "--skip-list", type=str, help="IMAP folders to skip", parser.add_argument("-x", "--skip-list", type=str, help="IMAP folders to skip",
default="junk,trash,spam,draft") default="junk,trash,spam,draft")
parser.add_argument("-f", "--folders", type=str, parser.add_argument("-f", "--folders", type=str,
help="Comma separated list of IMAP folders to download") help="Comma separated list of IMAP folders to download")
parser.add_argument("-i", "--import-from-table", help="Read imap conn data from import table", action='store_true')
parser.add_argument("-v", "--verbose", help="verbose mode", action='store_true') parser.add_argument("-v", "--verbose", help="verbose mode", action='store_true')
args = parser.parse_args() 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)
opts['skip_folders'] = args.skip_list.split(',') opts['skip_folders'] = args.skip_list.split(',')
opts['verbose'] = args.verbose opts['verbose'] = args.verbose
opts['counter'] = 0 opts['counter'] = 0
opts['db'] = None
server = ''
user = ''
password = ''
if args.import_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")
row = cursor.fetchone()
if row:
(server, user, password) = row
except dbapi.DatabaseError as e:
print("Error %s" % e)
else:
server = args.server
user = args.user
password = args.password
if opts['verbose']: if opts['verbose']:
print("Skipped folder list: {}".format(opts['skip_folders'])) print("Skipped folder list: {}".format(opts['skip_folders']))
if args.port == 993: if args.port == 993:
conn = imaplib.IMAP4_SSL(args.server) conn = imaplib.IMAP4_SSL(server)
else: else:
conn = imaplib.IMAP4(args.server) conn = imaplib.IMAP4(server)
conn.login(args.user, args.password) conn.login(user, password)
conn.select() conn.select()
if args.folders: if args.folders:
@ -94,6 +146,10 @@ def main():
conn.close() conn.close()
if opts['db']:
opts['db'].close()
print("Processed {} messages".format(opts['counter']))
if __name__ == "__main__": if __name__ == "__main__":
main() main()