summaryrefslogtreecommitdiff
path: root/bin/emailcanary
diff options
context:
space:
mode:
Diffstat (limited to 'bin/emailcanary')
-rwxr-xr-xbin/emailcanary105
1 files changed, 105 insertions, 0 deletions
diff --git a/bin/emailcanary b/bin/emailcanary
new file mode 100755
index 0000000..2c3a630
--- /dev/null
+++ b/bin/emailcanary
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+
+import argparse
+import sys
+import smtplib
+from emailcanary import canarydb
+from emailcanary import canary
+
+SUCCESS=0
+FAILURE=1
+
+def parse_args():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-d', '--database', default='/etc/emailcanary.db', help='Specify the database to use.')
+ parser.add_argument('-s', '--smtp', default='localhost:25', help='SMTP Server to send chirps to.')
+ parser.add_argument('-f', '--from', dest='fromaddress', help='Specify the email address to send the ping from.')
+
+ group = parser.add_mutually_exclusive_group(required=True)
+ group.add_argument('--chirp', metavar='listAddress', help='Send an email to the given canary list address.')
+ group.add_argument('--check', metavar='listAddress', help='Check the recepient addresses for the list address.')
+ group.add_argument('--add', nargs=4, metavar=('listAddress', 'address', 'imapserver', 'password'), help='Add a new recepient and list.')
+ group.add_argument('--remove', nargs='+', metavar=('listAddress', 'address'), help='Remove recepients from the list.')
+ group.add_argument('--list', action='store_true', help='List all configured addresses.')
+
+ args = parser.parse_args()
+ return args
+
+def get_smtp(address):
+ index = address.find(':')
+ if index == -1:
+ server = address
+ port = 465
+ else:
+ (server, port) = address.split(':')
+ return smtplib.SMTP_SSL(server, port)
+
+def list(db):
+ accounts = db.get_accounts()
+ if len(accounts) == 0:
+ print('No accounts configured.')
+ return SUCCESS
+ print "%-25s %-25s %-25s" % ('List Address', 'Recepient', 'IMAP Server')
+ print "-" * 80
+ for account in accounts:
+ print "%-25s %-25s %-25s" % (account[0], account[1], account[2])
+ return SUCCESS
+
+def add(db, listAddress, recepient, imapserver, password):
+ db.add_account(listAddress, recepient, imapserver, password)
+ return SUCCESS
+
+def remove(db, listAddress, recepients):
+ if len(recepients) == 0:
+ recepients = db.get_accounts(listAddress)
+ for address in recepients:
+ db.remove_account(listAddress, address)
+ return SUCCESS
+
+def check(db, birdie, listAddress):
+ missing = birdie.check(listAddress)
+ if len(missing) == 0:
+ return SUCCESS
+ print "list recepient uuid time"
+ for chirp in missing:
+ print "%s %s %s %d" % (chirp[0], chirp[1], chirp[2], chirp[3].total_seconds())
+ return FAILURE
+
+def main():
+ args = parse_args()
+ if not args:
+ return
+
+ smtp = None
+ db = None
+ try:
+ db = canarydb.CanaryDB(args.database)
+
+ if args.list:
+ return list(db)
+ elif args.add:
+ return add(db, args.add[0], args.add[1], args.add[2], args.add[3])
+ elif args.remove:
+ return remove(db, args.remove[0], args.remove[1:])
+ else:
+ smtp = get_smtp(args.smtp)
+ birdie = canary.Canary(db, smtp, args.fromaddress)
+ if args.chirp:
+ birdie.chirp(args.chirp)
+ return SUCCESS
+ elif args.check:
+ return check(db, birdie, args.check)
+ else:
+ raise Exception('Unknown action')
+
+ except Exception, e:
+ sys.stderr.write("Error: %s\n" % (str(e)))
+ return FAILURE
+ finally:
+ if smtp:
+ smtp.quit()
+ if db:
+ db.close()
+
+if __name__ == "__main__":
+ sys.exit(main())