1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#!/usr/local/bin/python3.4
import os
import re
import sys
import yaml
import pushover
import email.header
import email.parser
try:
# Use argv[1] as path to config file if specified
cfg = sys.argv[1]
except IndexError:
# Fall back to push.cfg in the same direcetory
cfg = os.path.dirname(os.path.realpath(__file__)) + '/push.cfg'
with open(cfg, 'r') as fh:
maps = yaml.load(fh)
try:
sender_map = maps['senders']
except KeyError:
print("Found no senders.", file=sys.stderr)
sys.exit(1)
try:
recipient_map = maps['recipients']
except KeyError:
print("Found no recipients.", file=sys.stderr)
sys.exit(1)
try:
recipient = os.environ['RECIPIENT']
except KeyError:
print("Recipient missing.", file=sys.stderr)
sys.exit(1)
try:
sender = os.environ['SENDER']
except KeyError:
print("Sender missing.", file=sys.stderr)
sys.exit(1)
# Select api key from map based on sender
for s in sender_map:
try:
if re.match(s['re'], sender):
try:
api_token = s['key']
except KeyError:
print("Found no key for sender.", file=sys.stderr)
sys.exit(1)
else:
break
except KeyError:
next
else:
print("Found no matching sender.", file=sys.stderr)
sys.exit(1)
# Select user key(s) from map based on sender
for r in recipient_map:
try:
if re.match(r['re'], recipient):
try:
user_tokens = r['keys']
except KeyError:
print("Found no key(s) for recipient.", file=sys.stderr)
sys.exit(1)
else:
break
except KeyError:
next
else:
print("Found no matching receiver.", file=sys.stderr)
sys.exit(1)
# Read an email from stdin
parser = email.parser.Parser()
mail = parser.parse(sys.stdin)
try:
# Try to get the subject from the email and recode to utf-8 if possible
title, encoding = email.header.decode_header(mail.get('subject'))[0]
except TypeError:
title = None
else:
try:
title = bytes(title).decode(encoding).encode('utf-8')
except (NameError, TypeError):
pass
# Lets at least try to handle mime multipart
if mail.is_multipart():
# Try to find something plain text
for current in mail.walk():
if current.get_content_type() == 'text/plain':
payload = current.get_payload(decode=True)
encoding = current.get_charsets()[0]
break
else:
# A realy stupid fallback
payload = mail.get_payload(0)
encoding = mail.get_charsets()[0]
else:
# Hurray no multipart
payload = mail.get_payload(decode=True)
encoding = mail.get_charsets()[0]
# Recode payload to utf-8 if possible
try:
payload = bytes(payload).decode(encoding).encode('utf-8')
except (NameError, TypeError):
pass
msg = payload.strip()
# Send notification to each user key
for user_token in user_tokens:
device = None
try:
k = list(user_token.keys())[0]
device = user_token[k]
except AttributeError:
pass
except (IndexError, KeyError):
next # We should not get here (at least not from a KeyError)
else:
user_token = k
p = pushover.Client(user_token, api_token=api_token)
p.send_message(msg, title=title, device=device)
|