aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xpush.py54
1 files changed, 37 insertions, 17 deletions
diff --git a/push.py b/push.py
index 5bd05d9..1a12f27 100755
--- a/push.py
+++ b/push.py
@@ -21,31 +21,51 @@ with open(cfg, 'r') as fh:
sender_map = maps.get('senders', [])
recipient_map = maps.get('recipients', [])
-recipient = os.environ.get('RECIPIENT', '')
-sender = os.environ.get('SENDER', '')
-api_token = None
-user_tokens = None
+try:
+ recipient = os.environ['RECIPIENT']
+except KeyError:
+ print >>sys.stderr, "Recipient missing."
+ sys.exit(1)
-# Make sure we got a sender and a recipient
-if len(recipient) == 0 or len(sender) == 0:
- print >>sys.stderr, "recipient or sender missing."
+try:
+ sender = os.environ['SENDER']
+except KeyError:
+ print >>sys.stderr, "Sender missing."
sys.exit(1)
+api_token = None
+user_tokens = None
+
# Select api key from map based on sender
for s in sender_map:
- if re.match(s.get('re', r'^$'), sender):
- api_token = s.get('key', None)
- break
+ try:
+ if re.match(s['re'], sender):
+ try:
+ api_token = s['key']
+ break
+ except KeyError:
+ print >>sys.stderr, "Found no key for sender."
+ sys.exit(1)
+ except KeyError:
+ next
+else:
+ print >>sys.stderr, "Found no matching sender."
+ sys.exit(1)
# Select user key(s) from map based on sender
for r in recipient_map:
- if re.match(r.get('re', r'^$'), recipient):
- user_tokens = r.get('keys', None)
- break
-
-# Make sure we have both user and api keys
-if api_token is None or user_tokens is None:
- print >>sys.stderr, "found no matching keys for sender or recipient."
+ try:
+ if re.match(r['re'], recipient):
+ try:
+ user_tokens = r['keys']
+ break
+ except KeyError:
+ print >>sys.stderr, "Found no key(s) for recipient."
+ sys.exit(1)
+ except KeyError:
+ next
+else:
+ print >>sys.stderr, "Found no matching receiver."
sys.exit(1)
# Read an email from stdin