-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsmtpq_notify.py
executable file
·105 lines (86 loc) · 3.23 KB
/
msmtpq_notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
# vim: set fileencoding=utf-8 :
""" msmtpq_notify.py
Notifies desktop user if msmtpq has actually sent or enqueued mail"""
#
# Copyright (C) 2011-2018 Georg Lutz <georg AT NOSPAM georglutz DOT de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import subprocess
import sys
def debug_print(text):
'''Print out debug messages when called as msmtpq_notify_debug.py (e.g. per symlink)'''
if os.path.basename(sys.argv[0]) == "msmtpq_notify_debug.py":
print(text)
def get_nr_of_entries_in_queue():
'''Returns the nr of entries in queue as an result of an call to "msmptq -d".
-1 means error in call of msmtpq.'''
result = -1
try:
proc = subprocess.Popen(
args=" -d", executable="msmtpq", stderr=subprocess.PIPE, stdout=subprocess.PIPE)
except:
return result
os.waitpid(proc.pid, 0)
result = 0
for line in proc.stdout:
if b" mail id = [ " in line:
result += 1
return result
def notify(text):
'''Sends text message to desktop'''
debug_print("notify: " + "\"" + text + "\"")
proc = subprocess.Popen(
"notify-send msmtpq_notify \"" + text + "\"", stderr=subprocess.PIPE,
stdout=subprocess.PIPE, shell=True)
for line in proc.stderr:
line = line.strip(os.linesep)
debug_print("Error notify-send: " + line)
def call_msmtpq():
'''Calls msmtpQ and returns its exit code.
Note that a call to msmtpQ also runs the queue automatically.'''
result = -1
args = ["msmtpQ"]
if len(sys.argv) > 1:
args += sys.argv[1:]
try:
proc = subprocess.Popen(
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except:
return result
for line in sys.stdin:
proc.stdin.write(str.encode(line))
proc.communicate()
result = proc.returncode
return result
########### MAIN PROGRAM #############
if __name__ == "__main__":
NR_OF_ENTRIES_BEFORE = get_nr_of_entries_in_queue()
if NR_OF_ENTRIES_BEFORE < 0:
notify("Error in calling msmtpq. Is it installed?")
sys.exit(2)
RETURN_CODE = call_msmtpq()
if RETURN_CODE < 0:
notify("Error in calling msmtpQ. Is it installed?")
sys.exit(2)
NR_OF_ENTRIES_AFTER = get_nr_of_entries_in_queue()
if NR_OF_ENTRIES_AFTER < 0:
notify("Error in calling msmtpq. Check installation!")
sys.exit(2)
if NR_OF_ENTRIES_BEFORE == 0 and NR_OF_ENTRIES_AFTER == 0:
notify("Sucessfully send message.")
else:
notify("Messages in queue: %d." % (NR_OF_ENTRIES_AFTER))
sys.exit(RETURN_CODE)