add notify

This commit is contained in:
Daniel Langbein 2020-12-26 15:15:54 +01:00
parent 1197cf1f87
commit d32db6c1f0
4 changed files with 62 additions and 30 deletions

View File

@ -7,15 +7,18 @@ clean: remove
install: permissions install: permissions
permissions: copy permissions: copy
chmod 755 /usr/local/bin/notify
chmod 755 /usr/local/bin/execNotify chmod 755 /usr/local/bin/execNotify
find /usr/local/bin/execNotifyDir \( -type d -exec chmod 755 {} + \) -o \( -type f -exec chmod 644 {} + \) find /usr/local/bin/execNotifyDir \( -type d -exec chmod 755 {} + \) -o \( -type f -exec chmod 644 {} + \)
chown root:root /usr/local/bin/execNotify chown root:root /usr/local/bin/execNotify
chown -R root:root /usr/local/bin/execNotifyDir chown -R root:root /usr/local/bin/execNotifyDir
copy: copy:
cp notify /usr/local/bin/notify
cp execNotify /usr/local/bin/execNotify cp execNotify /usr/local/bin/execNotify
cp -r execNotifyDir/ /usr/local/bin/ cp -r execNotifyDir/ /usr/local/bin/
remove: remove:
rm /usr/local/bin/notify
rm /usr/local/bin/execNotify rm /usr/local/bin/execNotify
rm -r /usr/local/bin/execNotifyDir rm -r /usr/local/bin/execNotifyDir

View File

@ -7,20 +7,10 @@ from execNotifyDir import exec, config, mail
def main(): def main():
prevMailError = checkPrevError()
if len(argv) >= 2: if len(argv) >= 2:
success = executeCommand(argv[1:]) if not executeCommand(argv[1:]):
else: exit(1)
success = True exit(0)
if prevMailError:
tryToInform()
if not success:
exit(1)
else:
exit(0)
def executeCommand(command: List) -> bool: def executeCommand(command: List) -> bool:
@ -44,22 +34,7 @@ def executeCommand(command: List) -> bool:
return True 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__': if __name__ == '__main__':
main() main()
if mail.prevMailNotSent():
mail.informAboutOldMail()

View File

@ -1,6 +1,7 @@
import sys import sys
import datetime import datetime
import smtplib, ssl import smtplib, ssl
import socket
from email.mime.text import MIMEText from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
@ -66,3 +67,20 @@ def saveMail(SUBJECT: str, BODY: str):
except Exception as e: except Exception as e:
print('Error: Could not write to file!', file=sys.stderr) print('Error: Could not write to file!', file=sys.stderr)
print(e, 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)

36
notify Executable file
View File

@ -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 <body> | ./notify
echo <body> | ./notify <subject>
./notify <subject> <body_1> <body_2> ... <body_n>
"""
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()