diff options
-rw-r--r-- | LICENSE.md | 21 | ||||
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | push.cfg | 14 | ||||
-rw-r--r-- | push.py | 61 |
4 files changed, 107 insertions, 0 deletions
diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9c84c31 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Marius Halden + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..25e47c1 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# mail2pushover + +Forward emails to pushover based on email sender and recipient address. + +## Usage +Add something similar in /etc/aliases +``` +address: |/path/to/push.py /path/to/push.cfg +``` +The path to push.cfg is only needed if it's not in the same diectory as +push.py diff --git a/push.cfg b/push.cfg new file mode 100644 index 0000000..13040f3 --- /dev/null +++ b/push.cfg @@ -0,0 +1,14 @@ +senders: + - re: 'sender1\@example\.com' + key: 'api token' + - re: 'sender2\@example\.com' + key: 'api token' + +recipients: + - re: 'recipient1\@example\.com' + keys: + - 'user token 1' + - 'user token 2' + - re: 'recipient2\@example\.com' + keys: + - 'user token' @@ -0,0 +1,61 @@ +#!/usr/local/bin/python + +import os +import re +import sys +import yaml +import pushover +import email.header +import email.parser + +try: + cfg = sys.argv[1] +except IndexError: + cfg = os.path.dirname(os.path.realpath(__file__)) + '/push.cfg' + +with open(cfg) as fh: + maps = yaml.load(fh) + +sender_map = maps.get('senders', None) +recipient_map = maps.get('recipients', None) + +recipient = os.environ.get('RECIPIENT', '') +sender = os.environ.get('SENDER', '') +api_token = None +user_tokens = None + +if len(recipient) == 0 or len(sender) == 0: + print >>sys.stderr, "recipient or sender missing." + sys.exit(1) + +for s in sender_map: + if re.match(s.get('re', r'^$'), sender): + api_token = s.get('key', None) + break + +for r in recipient_map: + if re.match(r.get('re', r'^$'), recipient): + user_tokens = r.get('keys', None) + break + +if not api_token or not user_tokens: + print >>sys.stderr, "found no matching keys for sender or recipient." + sys.exit(1) + +parser = email.parser.Parser() +mail = parser.parse(sys.stdin) + +title, encoding = email.header.decode_header(mail.get('subject'))[0] +if title != None and encoding != None: + title = title.decode(encoding).encode('utf-8') + +payload = mail.get_payload(decode=True) +encoding = mail.get_charsets()[0] +if payload != None and encoding != None: + payload = payload.decode(encoding).encode('utf-8') + +msg = payload.strip() + +for user_token in user_tokens: + p = pushover.Client(user_token, api_token=api_token) + p.send_message(msg, title=title) |