From 6259efbc44be3e21e42b29246941cd300e79200f Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 24 Nov 2015 11:12:49 -0800 Subject: Adding emailcanary script Adding the main launch script. Adding the listAddress to the pings table. Fixing the Makefile --- emailcanary/canary.py | 18 +++++++++++++----- emailcanary/canarydb.py | 30 +++++++++++++++++------------- emailcanary/emailutils.py | 2 +- 3 files changed, 31 insertions(+), 19 deletions(-) (limited to 'emailcanary') diff --git a/emailcanary/canary.py b/emailcanary/canary.py index 49972db..e020349 100644 --- a/emailcanary/canary.py +++ b/emailcanary/canary.py @@ -1,5 +1,5 @@ import uuid, datetime, time -import email, emailutils +import email.message, emailutils import re class Canary: @@ -12,12 +12,20 @@ class Canary: chirpUUID = str(uuid.uuid4()) now = datetime.datetime.now() + receipients = self.db.get_recipients_for_list(listAddress) + if len(receipients) == 0: + raise Exception("No receipients for listAddress '%s'", (listAddress,)) + self.send(listAddress, now, chirpUUID) - for dest in self.db.get_recipients_for_list(listAddress): - self.db.ping(dest, now, chirpUUID) + for dest in receipients: + self.db.ping(listAddress, dest, now, chirpUUID) def check(self, listAddress): - for (listAddress, address, imapserver, password) in self.db.get_accounts(listAddress): + '''Check for messages from listAddress and return a list of missing chirps''' + accounts = self.db.get_accounts(listAddress) + if len(accounts) == 0: + raise Exception("No receipients for listAddress '%s'", (listAddress,)) + for (listAddress, address, imapserver, password) in accounts: mail = emailutils.get_imap(imapserver, address, password) these_subjects = [] for uid in emailutils.get_mail_uids(mail): @@ -25,6 +33,7 @@ class Canary: if self.processMessage(address, message): emailutils.delete_message(mail, uid) emailutils.close(mail) + return self.db.get_missing_pongs(listAddress) def processMessage(self, receipient, msg): match = re.match('Canary Email (.+)', msg['Subject']) @@ -35,7 +44,6 @@ class Canary: return True return False - def send(self, dest, date, chirpUUID): msg = email.message.Message() msg['From'] = self.fromaddress diff --git a/emailcanary/canarydb.py b/emailcanary/canarydb.py index 59a2ea2..980431b 100644 --- a/emailcanary/canarydb.py +++ b/emailcanary/canarydb.py @@ -7,7 +7,7 @@ class CanaryDB: # Create Tables if necessary cursor = self.conn.cursor() - cursor.execute("CREATE TABLE IF NOT EXISTS pings (address, uuid, timesent timestamp, timereceived timestamp)") + cursor.execute("CREATE TABLE IF NOT EXISTS pings (listAddress, address, uuid, timesent timestamp, timereceived timestamp)") cursor.execute("CREATE TABLE IF NOT EXISTS accounts (list, address, imapserver, password)") def close(self): @@ -19,9 +19,9 @@ class CanaryDB: (listAddress, address, imapserver, password)) self.conn.commit() - def remove_account(self, address): + def remove_account(self, listAddress, address): cursor = self.conn.cursor() - cursor.execute("DELETE FROM accounts WHERE address=?", (address,)) + cursor.execute("DELETE FROM accounts WHERE list=? AND address=?", (listAddress, address)) self.conn.commit() def get_accounts(self, listAddress = None): @@ -47,10 +47,10 @@ class CanaryDB: results.append(row[0]) return results - def ping(self, address, time, uuid): + def ping(self, listAddress, address, time, uuid): cursor = self.conn.cursor() - cursor.execute("INSERT INTO pings (address, timesent, uuid) VALUES (?, ?, ?)", \ - (address, time, uuid)) + cursor.execute("INSERT INTO pings (listAddress, address, timesent, uuid) VALUES (?, ?, ?, ?)", \ + (listAddress, address, time, uuid)) self.conn.commit() def pong(self, address, time, uuid): @@ -59,16 +59,20 @@ class CanaryDB: (time, address, uuid)) self.conn.commit() - def get_missing_pongs(self): + def get_missing_pongs(self, listAddress = None): '''Return a list of tupls of missing pongs. - Each tupl contains (uuid, address, time since send)''' + Each tupl contains (listAddress, uuid, address, time since send)''' cursor = self.conn.cursor() - cursor.execute("SELECT uuid, address, timesent FROM pings WHERE timereceived IS NULL"); + if listAddress: + cursor.execute("SELECT listAddress, uuid, address, timesent FROM pings WHERE timereceived IS NULL AND listAddress = ?", (listAddress,)); + else: + cursor.execute("SELECT listAddress, uuid, address, timesent FROM pings WHERE timereceived IS NULL"); now = datetime.now() results = list() for row in cursor: - uuid = row[0] - address = row[1] - delta = now - row[2] - results.append((uuid, address, delta)) + listAddress = row[0] + uuid = row[1] + address = row[2] + delta = now - row[3] + results.append((listAddress, uuid, address, delta)) return results diff --git a/emailcanary/emailutils.py b/emailcanary/emailutils.py index b87f637..83c2f46 100644 --- a/emailcanary/emailutils.py +++ b/emailcanary/emailutils.py @@ -19,7 +19,7 @@ def get_message(mail, uid): return email.message_from_string(raw_email) def delete_message(mail, uid): - result = mail.uid('store', uid, '+FLAGS', '\\Deleted') + result = mail.uid('store', uid, '+FLAGS', '(\Deleted)') def close(mail): mail.expunge() -- cgit v1.2.3