random-scripts/old/mail/mailnag-notify.py

35 lines
902 B
Python
Raw Permalink Normal View History

2022-12-25 16:48:14 -08:00
#!/usr/bin/env python3
import sys
import subprocess
def notify_send(subj, data):
result = subprocess.run(["notify-send", "-t", "0", subj, data])
2022-12-25 16:48:14 -08:00
if result.returncode == -127:
print("error: notify-send program not found in PATH", file=sys.stderr)
exit(1)
elif result.returncode != 0:
print("error: notify-send failed", file=sys.stderr)
exit(1)
senders = {}
for i in range(2, len(sys.argv), 3):
sender = sys.argv[i + 1]
subj = sys.argv[i + 2]
print(sender)
print(subj)
if sender not in senders:
senders[sender] = subj
elif isinstance(subj, str):
senders[sender] = 2
else:
senders[sender] += 1
for sender, data in senders.items():
if isinstance(data, str):
notify_send("New mail from " + sender, data)
else:
notify_send("New mail from " + sender, str(data) + " unread messages")