From 6677a104753cda9cc1f96a4a93bb4c4b80502d17 Mon Sep 17 00:00:00 2001 From: Giulio Date: Mon, 21 May 2018 15:47:49 +0200 Subject: [PATCH] First import --- app.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ attachments/.gitkeep | 1 + schema.sql | 3 +++ 3 files changed, 64 insertions(+) create mode 100644 app.py create mode 100644 attachments/.gitkeep create mode 100644 schema.sql diff --git a/app.py b/app.py new file mode 100644 index 0000000..01ebb3e --- /dev/null +++ b/app.py @@ -0,0 +1,60 @@ +import imaplib +import email +import email.header +import sqlite3 +import hashlib +import time + +conn = sqlite3.connect("mailboxes.db") +conn.row_factory = sqlite3.Row +sql = conn.cursor() + +def connect(server, username, password, folder): + c = imaplib.IMAP4_SSL(server) + c.login(username, password) + c.select(folder, readonly=True) + return c + +def mailbox(c, id): + + r, data = c.search(None, "ALL") + for num in data[0].split(): + r, data = c.fetch(num, '(RFC822)') + msg = email.message_from_string(data[0][1]) + + subject = str(email.header.decode_header(msg['Subject'])[0][0]) + mailid = hashlib.sha1(email.header.decode_header(msg['Message-ID'])[0][0]).hexdigest() + sender = email.header.decode_header(msg['From'])[0][0] + + print(subject) + + + sql.execute("SELECT * FROM mails WHERE mailid = ?", (mailid, )) + if sql.fetchone() != None: + continue + + sql.execute("INSERT INTO mails (mailid, `date`, `from`, `to`, subject, body, account_id) VALUES (?, ?, ?, ?, ?, ?, ?)", (mailid, 0, sender.decode('utf-8', 'ignore'), 'idk', subject.decode('utf-8', 'ignore'), data[0][1].decode('utf-8', 'ignore'), id)) + + conn.commit() + + # Save attachments + if msg.get_content_maintype() == 'multipart': + for part in msg.walk(): + if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None and part.get_filename() is not None: + filename = str(time.time()) + "_" + part.get_filename() + open('attachments/' + filename, 'wb').write(part.get_payload(decode=True)) + sql.execute("UPDATE mails SET has_attachments = 1 WHERE mailid = ?", (mailid, )) + sql.execute("INSERT INTO attachments (mailid, filename) VALUES (?, ?)", (mailid, filename, )) + + + + +def main(): + sql.execute("SELECT * FROM accounts") + accounts = sql.fetchall() + for account in accounts: + mailbox(connect(account["server"], account["username"], account["password"], account["folder"]), account["id"]) + +main() + +conn.commit() \ No newline at end of file diff --git a/attachments/.gitkeep b/attachments/.gitkeep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/attachments/.gitkeep @@ -0,0 +1 @@ + diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..ac30c09 --- /dev/null +++ b/schema.sql @@ -0,0 +1,3 @@ +CREATE TABLE "accounts" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `server` TEXT, `username` TEXT, `password` TEXT, `folder` TEXT, `last_fetch` INTEGER ); +CREATE TABLE "attachments" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `mailid` INTEGER, `filename` TEXT ); +CREATE TABLE "mails" ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `mailid` TEXT, `date` INTEGER, `from` TEXT, `to` TEXT, `subject` TEXT, `body` BLOB, `has_attachments` INTEGER, `account_id` INTEGER );