From 51129fda2179286143921db55a69b3f4dba03c0e Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 25 Oct 2015 19:42:14 -0700 Subject: Adding Canary Check and Accounts Adding the canary check method and tests for the Canary class. Adding accounts to CanaryDB. --- emailcanary/canary.py | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) (limited to 'emailcanary/canary.py') 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()) -- cgit v1.2.3