refactor and rewrite for asyncio
This commit is contained in:
+12
-20
@@ -1,4 +1,5 @@
|
||||
"""Notification helpers: email, pushover, mattermost, signal and dispatcher."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
import http.client
|
||||
import urllib.parse
|
||||
@@ -11,6 +12,7 @@ DEFAULT_PUSHPROVIDERS = ["all", "pushover", "mattermost", "signal"]
|
||||
|
||||
# module-level configuration set via setup()
|
||||
_config = {}
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup(cfg: dict):
|
||||
@@ -27,8 +29,7 @@ def send_email(aemail, smtpserver, sender, subject, body, debug=0):
|
||||
server.set_debuglevel(1)
|
||||
server.sendmail(sender, aemail, body)
|
||||
except Exception as e:
|
||||
if debug:
|
||||
print("email send failed:", e)
|
||||
logger.warning("email send failed: %s", e)
|
||||
try:
|
||||
server.quit()
|
||||
except Exception:
|
||||
@@ -72,12 +73,10 @@ def pushover(token: str, user: str, msg: str, debug: int = 0) -> bool:
|
||||
{"Content-type": "application/x-www-form-urlencoded"},
|
||||
)
|
||||
r = conn.getresponse()
|
||||
if debug:
|
||||
print("pushover response:", r.status, r.reason)
|
||||
logger.debug("pushover response: %s %s", r.status, r.reason)
|
||||
return r.status == 200
|
||||
except Exception as e:
|
||||
if debug:
|
||||
print("pushover error:", e)
|
||||
logger.error("pushover error: %s", e)
|
||||
return False
|
||||
|
||||
|
||||
@@ -98,12 +97,10 @@ def pushmattermost(host: str, token: str, channel: str, msg: str, username: str
|
||||
payload["icon_url"] = icon
|
||||
try:
|
||||
rc = mm.webhooks.call_webhook(token, payload)
|
||||
if debug:
|
||||
print("mattermost rc:", rc)
|
||||
logger.debug("mattermost rc: %s", rc)
|
||||
return bool(rc is None or rc == "")
|
||||
except Exception as e:
|
||||
if debug:
|
||||
print("mattermost error:", e)
|
||||
logger.error("mattermost error: %s", e)
|
||||
return False
|
||||
|
||||
|
||||
@@ -113,20 +110,16 @@ def pushsignal(signal_cli_bin: str, user: str, recipient: str, msg: str, debug:
|
||||
Uses subprocess to call signal-cli. Returns True if the command succeeded.
|
||||
"""
|
||||
CLI = [signal_cli_bin, "-u", user, "send", "-m", msg, recipient]
|
||||
if debug:
|
||||
print("signal cli: ", CLI)
|
||||
logger.debug("signal cli: %s", CLI)
|
||||
try:
|
||||
res = subprocess.run(CLI, capture_output=True)
|
||||
if res.returncode != 0:
|
||||
if debug:
|
||||
print("signal failed:", res.stderr.decode())
|
||||
logger.error("signal failed: %s". res.stderr.decode())
|
||||
return False
|
||||
if debug:
|
||||
print("signal sent:", res.stdout.decode())
|
||||
logger.debug("signal sent: %s", res.stdout.decode())
|
||||
return True
|
||||
except Exception as e:
|
||||
if debug:
|
||||
print("signal exception:", e)
|
||||
logger.exception("signal exception: %s", e)
|
||||
return False
|
||||
|
||||
|
||||
@@ -152,8 +145,7 @@ def pushmsg(cfg: dict, msg: str, debug: int = 0):
|
||||
if p in ("all", "signal"):
|
||||
ok = pushsignal(cfg.get("signal_cli", "/usr/local/bin/signal-cli"), cfg.get("signal_user", ""), cfg.get("signal_recipient", ""), msg, debug=debug)
|
||||
results["signal"] = ok
|
||||
if debug:
|
||||
print("push results:", results)
|
||||
logger.debug("push results: %s", results)
|
||||
return results
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user