2020-01-05 16:42:47 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
from email.message import EmailMessage
|
|
|
|
|
|
|
|
try:
|
|
|
|
import smime
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("Please install missing package: python3 -m pip install smime")
|
|
|
|
|
2021-04-25 16:14:28 +02:00
|
|
|
SMTP_BIN = "/usr/bin/msmtp"
|
2020-01-05 16:42:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(description="Send a notification")
|
|
|
|
parser.add_argument("-V", "--version", action="version",
|
|
|
|
help="print version", version="0.1")
|
|
|
|
|
|
|
|
parser.add_argument("-d", "--debug", action="store_true",
|
|
|
|
help="print debug output")
|
|
|
|
|
|
|
|
subparsers = parser.add_subparsers(dest='subparser')
|
|
|
|
|
|
|
|
# ext
|
|
|
|
parser_ext = subparsers.add_parser("ext", help="Notify by external command")
|
|
|
|
parser_ext.add_argument("cmd", type=str,
|
|
|
|
help="Path to external command")
|
|
|
|
|
|
|
|
parser_ext.add_argument("message", type=str,
|
|
|
|
help="Message to send")
|
|
|
|
|
|
|
|
# e-Mail
|
|
|
|
parser_mail = subparsers.add_parser("mail", help="Notify by e-Mail")
|
|
|
|
|
|
|
|
parser_mail.add_argument("recipient", type=str,
|
|
|
|
help="E-Mail address of recipient")
|
|
|
|
|
|
|
|
parser_mail.add_argument("message", type=str,
|
|
|
|
help="Message to send")
|
|
|
|
|
|
|
|
parser_mail.add_argument("-s", "--subject", type=str, default="RB Notification",
|
|
|
|
help="Subject for message")
|
|
|
|
|
|
|
|
parser_mail.add_argument("-c", "--cert", type=str, default="pub.pem",
|
|
|
|
help="Path to public x509 certificate of recipient")
|
|
|
|
|
|
|
|
parser_mail.add_argument("-e", "--encrypt", action="store_true",
|
|
|
|
help="S/MIME encrypt body")
|
|
|
|
|
|
|
|
parser_mail.add_argument("--from-name", type=str, default="From-Name",
|
|
|
|
help="Sender name")
|
|
|
|
|
|
|
|
parser_mail.add_argument("--from-address", type=str, default="from-address@example.com",
|
|
|
|
help="Sender e-Mail address")
|
|
|
|
|
|
|
|
# slack
|
|
|
|
parser_slack = subparsers.add_parser("slack", help="Notify by Slack")
|
|
|
|
parser_slack.add_argument("message", type=str,
|
|
|
|
help="Message to send")
|
|
|
|
|
|
|
|
# parse args and run selected subparser
|
|
|
|
kwargs = vars(parser.parse_args())
|
|
|
|
try:
|
|
|
|
globals()[kwargs.pop('subparser')](**kwargs)
|
|
|
|
except KeyError:
|
|
|
|
parser.print_help()
|
|
|
|
|
|
|
|
|
|
|
|
def ext(cmd=None, message=None, debug=False):
|
|
|
|
if debug:
|
|
|
|
print("calling: {}".format(cmd))
|
|
|
|
print("with msg: {}".format(message))
|
|
|
|
|
|
|
|
if not os.path.exists(cmd):
|
|
|
|
raise Exception("File not found: {}".format(cmd))
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.run([cmd, message], stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print("Running shell command \"{}\" caused "
|
|
|
|
"error: {} (RC: {}".format(err.cmd, err.output, err.returncode))
|
|
|
|
raise Exception(err)
|
|
|
|
|
|
|
|
|
|
|
|
# e-Mail
|
2020-08-02 14:41:27 +02:00
|
|
|
def mail(recipient: str = None, message: str = None, subject: str = None, cert: str = None,
|
|
|
|
encrypt: bool = False, from_name: str = None, from_address: str = None, debug: bool = False):
|
2020-01-05 16:42:47 +01:00
|
|
|
if debug:
|
|
|
|
print("send mail")
|
|
|
|
print("msg: {}".format(message))
|
|
|
|
print("to: {}".format(recipient))
|
2020-06-27 22:14:36 +02:00
|
|
|
print("from: {} <{}>".format(from_name, from_address))
|
2020-01-05 16:42:47 +01:00
|
|
|
print("subject: {}".format(subject))
|
|
|
|
print("cert: {}".format(cert))
|
|
|
|
print("encrypt: {}".format(encrypt))
|
|
|
|
|
|
|
|
if encrypt:
|
|
|
|
if not os.path.exists(cert):
|
|
|
|
raise Exception("File not found: {}".format(cert))
|
|
|
|
|
|
|
|
msg_content = [
|
|
|
|
"To: {}".format(recipient),
|
2020-06-27 22:14:36 +02:00
|
|
|
'From: {} <{}>'.format(from_name, from_address),
|
2020-01-05 16:42:47 +01:00
|
|
|
"Subject: {}".format(subject),
|
|
|
|
"",
|
2021-04-25 16:14:28 +02:00
|
|
|
"{}".format(message)
|
2020-01-05 16:42:47 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
with open(cert, 'rb') as pem:
|
|
|
|
msg = smime.encrypt('\n'.join(msg_content), pem.read())
|
|
|
|
|
|
|
|
msg_to_send = msg.encode()
|
|
|
|
|
|
|
|
else:
|
|
|
|
msg = EmailMessage()
|
|
|
|
|
|
|
|
msg['Subject'] = "{}".format(subject)
|
2020-06-27 22:14:36 +02:00
|
|
|
msg['From'] = '{} <{}>'.format(from_name, from_address),
|
2020-01-05 16:42:47 +01:00
|
|
|
msg['To'] = recipient
|
|
|
|
|
2020-09-15 13:54:29 +02:00
|
|
|
msg.set_payload(message.encode('utf8'))
|
2020-01-05 16:42:47 +01:00
|
|
|
msg_to_send = msg.as_bytes()
|
|
|
|
|
|
|
|
# send message via e-Mail
|
2021-04-25 16:14:28 +02:00
|
|
|
if not os.path.exists(SMTP_BIN):
|
|
|
|
raise Exception("File not found: {}".format(SMTP_BIN))
|
2020-01-05 16:42:47 +01:00
|
|
|
|
|
|
|
try:
|
2021-04-25 16:14:28 +02:00
|
|
|
cmd = [SMTP_BIN, recipient]
|
2020-01-05 16:42:47 +01:00
|
|
|
subprocess.run(cmd, input=msg_to_send, stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
except subprocess.CalledProcessError as err:
|
|
|
|
print("Running shell command \"{}\" caused "
|
|
|
|
"error: {} (RC: {}".format(err.cmd, err.output, err.returncode))
|
|
|
|
raise Exception(err)
|
|
|
|
|
|
|
|
|
2020-08-02 14:41:27 +02:00
|
|
|
def slack(message: str = None, debug: bool = False):
|
2020-01-05 16:42:47 +01:00
|
|
|
if debug:
|
|
|
|
print("send slack")
|
|
|
|
print("msg: {}".format(message))
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|