mailnag-notify.py 902 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env python3
  2. import sys
  3. import subprocess
  4. def notify_send(subj, data):
  5. result = subprocess.run(["notify-send", "-t", "0", subj, data])
  6. if result.returncode == -127:
  7. print("error: notify-send program not found in PATH", file=sys.stderr)
  8. exit(1)
  9. elif result.returncode != 0:
  10. print("error: notify-send failed", file=sys.stderr)
  11. exit(1)
  12. senders = {}
  13. for i in range(2, len(sys.argv), 3):
  14. sender = sys.argv[i + 1]
  15. subj = sys.argv[i + 2]
  16. print(sender)
  17. print(subj)
  18. if sender not in senders:
  19. senders[sender] = subj
  20. elif isinstance(subj, str):
  21. senders[sender] = 2
  22. else:
  23. senders[sender] += 1
  24. for sender, data in senders.items():
  25. if isinstance(data, str):
  26. notify_send("New mail from " + sender, data)
  27. else:
  28. notify_send("New mail from " + sender, str(data) + " unread messages")