summaryrefslogtreecommitdiff
path: root/emailcanary
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2015-10-25 19:42:14 -0700
committerJesse Morgan <jesse@jesterpm.net>2015-10-25 19:42:14 -0700
commit51129fda2179286143921db55a69b3f4dba03c0e (patch)
treef09690727e5000af8e9e7bd9359fe37c81210fcd /emailcanary
parent5319d2f0f013a340cb0fdc396bbe4f0ae8df5142 (diff)
Adding Canary Check and Accounts
Adding the canary check method and tests for the Canary class. Adding accounts to CanaryDB.
Diffstat (limited to 'emailcanary')
-rw-r--r--emailcanary/canary.py47
-rw-r--r--emailcanary/canarydb.py37
-rw-r--r--emailcanary/email-digest-sender.py2
-rw-r--r--emailcanary/emailutils.py16
4 files changed, 78 insertions, 24 deletions
diff --git a/emailcanary/canary.py b/emailcanary/canary.py
index d07e2e9..49972db 100644
--- a/emailcanary/canary.py
+++ b/emailcanary/canary.py
@@ -1,5 +1,5 @@
import uuid, datetime, time
-import email
+import email, emailutils
import re
class Canary:
@@ -8,26 +8,39 @@ class Canary:
self.smtp = smtp
self.fromaddress = fromaddress
- def chirp(self, list, expectedreceipients):
- uuid = uuid.uuid4()
+ def chirp(self, listAddress):
+ chirpUUID = str(uuid.uuid4())
now = datetime.datetime.now()
- self.send(list, now, uuid)
- for dest in expectedreceipients:
- self.db.ping(dest, now, uuid)
-
- def echo(self, receipient, msg):
- uuid = re.match('Canary Email (.+)', msg['Subject']).group(1)
- now = datetime.datetime.now()
-
- self.db.pong(receipient, now, uuid)
-
-
- def send(self, dest, date, uuid):
+ self.send(listAddress, now, chirpUUID)
+ for dest in self.db.get_recipients_for_list(listAddress):
+ self.db.ping(dest, now, chirpUUID)
+
+ def check(self, listAddress):
+ for (listAddress, address, imapserver, password) in self.db.get_accounts(listAddress):
+ mail = emailutils.get_imap(imapserver, address, password)
+ these_subjects = []
+ for uid in emailutils.get_mail_uids(mail):
+ message = emailutils.get_message(mail, uid)
+ if self.processMessage(address, message):
+ emailutils.delete_message(mail, uid)
+ emailutils.close(mail)
+
+ def processMessage(self, receipient, msg):
+ match = re.match('Canary Email (.+)', msg['Subject'])
+ if match:
+ chirpUUID = match.group(1)
+ now = datetime.datetime.now()
+ self.db.pong(receipient, now, chirpUUID)
+ return True
+ return False
+
+
+ def send(self, dest, date, chirpUUID):
msg = email.message.Message()
msg['From'] = self.fromaddress
msg['To'] = dest
- msg['Subject'] = "Canary Email " + str(uuid)
+ msg['Subject'] = "Canary Email " + chirpUUID
msg['Date'] = email.utils.formatdate(time.mktime(date.timetuple()))
- self.smtp.sendmail(self.fromaddress, dest, msg.as_string()) \ No newline at end of file
+ self.smtp.sendmail(self.fromaddress, dest, msg.as_string())
diff --git a/emailcanary/canarydb.py b/emailcanary/canarydb.py
index 112b8f5..59a2ea2 100644
--- a/emailcanary/canarydb.py
+++ b/emailcanary/canarydb.py
@@ -8,10 +8,45 @@ 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 accounts (list, address, imapserver, password)")
def close(self):
self.conn.close()
+ def add_account(self, listAddress, address, imapserver, password):
+ cursor = self.conn.cursor()
+ cursor.execute("INSERT INTO accounts (list, address, imapserver, password) VALUES (?, ?, ?, ?)", \
+ (listAddress, address, imapserver, password))
+ self.conn.commit()
+
+ def remove_account(self, address):
+ cursor = self.conn.cursor()
+ cursor.execute("DELETE FROM accounts WHERE address=?", (address,))
+ self.conn.commit()
+
+ def get_accounts(self, listAddress = None):
+ cursor = self.conn.cursor()
+ if listAddress:
+ cursor.execute("SELECT list, address, imapserver, password FROM accounts WHERE list=?", (listAddress,));
+ else:
+ cursor.execute("SELECT list, address, imapserver, password FROM accounts");
+ results = list()
+ for row in cursor:
+ listAddress = row[0]
+ address = row[1]
+ imapserver = row[2]
+ password = row[3]
+ results.append((listAddress, address, imapserver, password))
+ return results
+
+ def get_recipients_for_list(self, listAddress):
+ cursor = self.conn.cursor()
+ cursor.execute("SELECT address FROM accounts WHERE list=?", (listAddress,));
+ results = list()
+ for row in cursor:
+ results.append(row[0])
+ return results
+
def ping(self, address, time, uuid):
cursor = self.conn.cursor()
cursor.execute("INSERT INTO pings (address, timesent, uuid) VALUES (?, ?, ?)", \
@@ -29,7 +64,6 @@ class CanaryDB:
Each tupl contains (uuid, address, time since send)'''
cursor = self.conn.cursor()
cursor.execute("SELECT uuid, address, timesent FROM pings WHERE timereceived IS NULL");
-
now = datetime.now()
results = list()
for row in cursor:
@@ -37,5 +71,4 @@ class CanaryDB:
address = row[1]
delta = now - row[2]
results.append((uuid, address, delta))
-
return results
diff --git a/emailcanary/email-digest-sender.py b/emailcanary/email-digest-sender.py
index f1583c0..a7ff79f 100644
--- a/emailcanary/email-digest-sender.py
+++ b/emailcanary/email-digest-sender.py
@@ -10,7 +10,7 @@ youve_got_mail = False
all_subjects = {}
for account in ACCOUNTS:
- mail = emailutils.get_imap(account)
+ mail = emailutils.get_imap(account[0], account[1], account[2])
these_subjects = []
for uid in emailutils.get_mail_uids(mail):
message = emailutils.get_message(mail, uid)
diff --git a/emailcanary/emailutils.py b/emailcanary/emailutils.py
index 6646c1e..b87f637 100644
--- a/emailcanary/emailutils.py
+++ b/emailcanary/emailutils.py
@@ -1,9 +1,9 @@
import imaplib, email
-def get_imap(account):
+def get_imap(server, username, password):
'''Connect and login via IMAP'''
- mail = imaplib.IMAP4_SSL(account[0])
- mail.login(account[1], account[2])
+ mail = imaplib.IMAP4_SSL(server)
+ mail.login(username, password)
return mail
def get_mail_uids(mail):
@@ -16,4 +16,12 @@ def get_message(mail, uid):
'''Get a single email message object by UID'''
result, data = mail.uid('fetch', uid, '(RFC822)')
raw_email = data[0][1]
- return email.message_from_string(raw_email) \ No newline at end of file
+ return email.message_from_string(raw_email)
+
+def delete_message(mail, uid):
+ result = mail.uid('store', uid, '+FLAGS', '\\Deleted')
+
+def close(mail):
+ mail.expunge()
+ mail.close()
+ mail.logout()