From d32db6c1f02ea3699fcdc9c2d17c75d1df95b5c7 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 26 Dec 2020 15:15:54 +0100 Subject: [PATCH] add notify --- Makefile | 3 +++ execNotify | 35 +++++------------------------------ execNotifyDir/mail.py | 18 ++++++++++++++++++ notify | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 30 deletions(-) create mode 100755 notify diff --git a/Makefile b/Makefile index 48fe44f..e85d574 100644 --- a/Makefile +++ b/Makefile @@ -7,15 +7,18 @@ clean: remove install: permissions permissions: copy + chmod 755 /usr/local/bin/notify chmod 755 /usr/local/bin/execNotify find /usr/local/bin/execNotifyDir \( -type d -exec chmod 755 {} + \) -o \( -type f -exec chmod 644 {} + \) chown root:root /usr/local/bin/execNotify chown -R root:root /usr/local/bin/execNotifyDir copy: + cp notify /usr/local/bin/notify cp execNotify /usr/local/bin/execNotify cp -r execNotifyDir/ /usr/local/bin/ remove: + rm /usr/local/bin/notify rm /usr/local/bin/execNotify rm -r /usr/local/bin/execNotifyDir diff --git a/execNotify b/execNotify index 19683ec..8a1a0b6 100755 --- a/execNotify +++ b/execNotify @@ -7,20 +7,10 @@ from execNotifyDir import exec, config, mail def main(): - prevMailError = checkPrevError() - if len(argv) >= 2: - success = executeCommand(argv[1:]) - else: - success = True - - if prevMailError: - tryToInform() - - if not success: - exit(1) - else: - exit(0) + if not executeCommand(argv[1:]): + exit(1) + exit(0) def executeCommand(command: List) -> bool: @@ -44,22 +34,7 @@ def executeCommand(command: List) -> bool: return True -def checkPrevError(): - return config.getErrorFile().exists() - - -def tryToInform(): - """ - Try to inform user via mail about previous error(s) that could not be sent to him before. - Maybe this time sending of an email works ;) - """ - - SUBJECT = '{} | Some mails not sent!'.format(socket.gethostname()) - BODY = 'Please check the file {} for mails which could previously not be sent to you!\n' \ - 'Note: You may delete the file after reading it ;)'.format( - config.getErrorFile()) - mail.sendMail(SUBJECT=SUBJECT, BODY=BODY) - - if __name__ == '__main__': main() + if mail.prevMailNotSent(): + mail.informAboutOldMail() diff --git a/execNotifyDir/mail.py b/execNotifyDir/mail.py index 295b482..29c56a8 100644 --- a/execNotifyDir/mail.py +++ b/execNotifyDir/mail.py @@ -1,6 +1,7 @@ import sys import datetime import smtplib, ssl +import socket from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart @@ -66,3 +67,20 @@ def saveMail(SUBJECT: str, BODY: str): except Exception as e: print('Error: Could not write to file!', file=sys.stderr) print(e, file=sys.stderr) + + +def prevMailNotSent(): + return config.getErrorFile().exists() + + +def informAboutOldMail(): + """ + Try to inform user via mail about previous error(s) that could not be sent to him before. + Maybe this time sending of an email works ;) + """ + + SUBJECT = '{} | Some mails not sent!'.format(socket.gethostname()) + BODY = 'Please check the file {} for mails which could previously not be sent to you!\n' \ + 'Note: You may delete the file after reading it ;)' \ + .format(config.getErrorFile()) + sendMail(SUBJECT=SUBJECT, BODY=BODY) diff --git a/notify b/notify new file mode 100755 index 0000000..5f8c40c --- /dev/null +++ b/notify @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +from sys import argv, stderr, stdin +import socket +from execNotifyDir import exec, config, mail + + +def main(): + """ + echo | ./notify + echo | ./notify + ./notify ... + """ + + BODY = None + subj = None + hostname = socket.gethostname() + + if len(argv) >= 2: + subj = argv[1] + if len(argv) >= 3: + BODY = str(argv[2:]) + if subj is None: + subj = "notify" + if BODY is None: + BODY = "=== stdin ===\n" + stdin.read() + + SUBJECT = "{} | {}".format(hostname, subj) + print(BODY) + + mail.sendMailOrWriteToFile(SUBJECT=SUBJECT, BODY=BODY) + + +if __name__ == '__main__': + main() + if mail.prevMailNotSent(): + mail.informAboutOldMail()