author | Mikael Berthe <mikael@lilotux.net> |
Sat, 21 Jun 2008 19:52:15 +0200 | |
changeset 7 | 727c0c584faa |
parent 6 | 72c249247e18 |
child 8 | 14dd3f4fd4dc |
permissions | -rwxr-xr-x |
0 | 1 |
#! /usr/bin/env python |
2 |
# -*- coding: iso-8859-15 -*- |
|
3 |
# |
|
4 |
# Copyright (C) 2007 Adam Wolk "Mulander" <netprobe@gmail.com> |
|
5 |
# Copyright (C) 2007, 2008 Mikael Berthe "McKael" <mikael@lilotux.net> |
|
6 |
# |
|
7 |
# This script is provided under the terms of the GNU General Public License, |
|
8 |
# see the file COPYING in the root mcabber source directory. |
|
9 |
# |
|
10 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
11 |
import sys, getopt |
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
12 |
import os |
0 | 13 |
import pynotify |
14 |
||
15 |
CONFFILE = "mcevent.cfg" |
|
16 |
||
17 |
# Default option values |
|
18 |
opt = { |
|
19 |
'use_notify': 0, |
|
20 |
'use_voice': 0, |
|
21 |
'use_sound': 1, |
|
22 |
'short_nick': 1, |
|
2 | 23 |
'unread_file': '', |
4 | 24 |
'snd_cmd_msg_in': '/usr/bin/play -V0 -v3 sound.wav', |
0 | 25 |
} |
26 |
||
27 |
NOTIFY_TIMEOUT=4000 |
|
28 |
||
29 |
contact_map = { } |
|
30 |
# Nickname used for the notification |
|
31 |
||
32 |
online_alerts = { } |
|
33 |
# 0: disabled 1: normal notification 2: permanent notify box |
|
34 |
||
35 |
voicemap = { } |
|
36 |
# Specify the name pronounced by espeak |
|
37 |
||
38 |
blacklist = { } |
|
39 |
# No notification for these JIDs |
|
40 |
||
41 |
CMD_ESPEAK="/usr/bin/espeak" |
|
42 |
||
43 |
def init_notify(): |
|
44 |
import locale |
|
45 |
global encoding |
|
46 |
global NOTIFY_LOADED |
|
47 |
pynotify.init('mcnotify') |
|
48 |
encoding = (locale.getdefaultlocale())[1] |
|
49 |
NOTIFY_LOADED = True |
|
50 |
||
51 |
def read_conf_from_file(): |
|
52 |
import ConfigParser |
|
53 |
||
54 |
config = ConfigParser.ConfigParser() |
|
55 |
config.read(CONFFILE) |
|
56 |
||
57 |
contact_map.clear() |
|
58 |
online_alerts.clear() |
|
59 |
voicemap.clear() |
|
60 |
blacklist.clear() |
|
61 |
||
62 |
if config.has_option("Notifications", "notify"): |
|
63 |
opt['use_notify'] = int(config.get("Notifications", "notify")) |
|
64 |
if config.has_option("Notifications", "voice"): |
|
65 |
opt['use_voice'] = int(config.get("Notifications", "voice")) |
|
66 |
if config.has_option("Notifications", "sound"): |
|
67 |
opt['use_sound'] = int(config.get("Notifications", "sound")) |
|
68 |
if config.has_option("Notifications", "short_nick"): |
|
69 |
opt['short_nick'] = int(config.get("Notifications", "short_nick")) |
|
2 | 70 |
if config.has_option("Notifications", "unread_file"): |
71 |
opt['unread_file'] = config.get("Notifications", "unread_file") |
|
4 | 72 |
if config.has_option("Notifications", "snd_cmd_msg_in"): |
73 |
opt['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in") |
|
0 | 74 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
75 |
if config.has_section("Contacts"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
76 |
for id in config.options("Contacts"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
77 |
contact_map[id] = config.get("Contacts", id) |
0 | 78 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
79 |
if config.has_section("Alerts"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
80 |
for id in config.options("Alerts"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
81 |
online_alerts[id] = int(config.get("Alerts", id)) |
0 | 82 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
83 |
if config.has_section("Voicemap"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
84 |
for id in config.options("Voicemap"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
85 |
voicemap[id] = config.get("Voicemap", id) |
0 | 86 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
87 |
if config.has_section("Blacklist"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
88 |
for id in config.options("Blacklist"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
89 |
blacklist[id] = int(config.get("Blacklist", id)) |
0 | 90 |
|
91 |
if opt['use_notify'] and not NOTIFY_LOADED: |
|
92 |
init_notify() |
|
93 |
||
94 |
def say(buddy, text): |
|
95 |
import subprocess |
|
96 |
p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True) |
|
97 |
child_stdin = p.stdin |
|
98 |
child_stdin.write(buddy + " " + text) |
|
99 |
child_stdin.close() |
|
100 |
||
101 |
def notify(buddy, msg, timeout): |
|
102 |
msgbox = pynotify.Notification(unicode(buddy, encoding), |
|
103 |
unicode(msg, encoding)) |
|
104 |
msgbox.set_timeout(timeout) |
|
105 |
msgbox.set_urgency(pynotify.URGENCY_LOW) |
|
106 |
msgbox.show() |
|
107 |
||
108 |
def get_nick(jid): |
|
109 |
if jid in contact_map: |
|
110 |
buddy = contact_map[jid] |
|
111 |
else: |
|
112 |
buddy = jid |
|
113 |
||
114 |
if opt['short_nick'] and '@' in buddy: |
|
115 |
buddy = buddy[0:buddy.index('@')] |
|
116 |
return buddy |
|
117 |
||
118 |
def is_blacklisted(jid): |
|
119 |
if jid in blacklist and blacklist[jid]: |
|
120 |
return True |
|
121 |
return False |
|
122 |
||
123 |
def process_line(args): |
|
124 |
argn = len(args) |
|
125 |
||
126 |
if argn < 2: |
|
127 |
print "Ignoring invalid event line." |
|
128 |
return |
|
129 |
elif argn == 2: |
|
130 |
event,arg1 = args[0:2] |
|
131 |
arg2 = None |
|
132 |
filename = None |
|
133 |
elif argn == 3: |
|
134 |
event,arg1,arg2 = args[0:3] |
|
135 |
filename = None |
|
136 |
else: |
|
137 |
# For now we simply ignore the file name |
|
138 |
event,arg1,arg2 = args[0:3] |
|
139 |
filename = None |
|
140 |
||
141 |
if event == 'MSG' and arg1 == 'IN': |
|
142 |
import os |
|
143 |
||
144 |
jid = arg2 |
|
145 |
buddy = get_nick(jid) |
|
146 |
msg = 'sent you a message.' |
|
147 |
||
148 |
if not is_blacklisted(jid): |
|
149 |
textmsg = None |
|
150 |
||
151 |
if filename and os.path.exists(filename): |
|
152 |
f = file(filename) |
|
153 |
textmsg = f.read() |
|
154 |
||
155 |
if opt['use_notify']: |
|
156 |
if not textmsg: |
|
157 |
textmsg = msg |
|
158 |
notify(buddy, textmsg, NOTIFY_TIMEOUT) |
|
159 |
||
160 |
if opt['use_sound']: |
|
4 | 161 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0 | 162 |
|
163 |
if opt['use_voice'] and not is_blacklisted(jid): |
|
164 |
if jid in voicemap: |
|
165 |
buddy = voicemap[jid] |
|
166 |
say(buddy, msg) |
|
167 |
||
168 |
if filename and os.path.exists(filename): |
|
169 |
os.remove(filename) |
|
170 |
||
171 |
elif event == 'STATUS': |
|
172 |
jid = arg2 |
|
173 |
if arg1 == 'O' and jid in online_alerts: |
|
174 |
import os |
|
175 |
||
176 |
type = online_alerts[jid] |
|
177 |
if type > 0: |
|
178 |
if opt['use_sound']: |
|
4 | 179 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0 | 180 |
|
181 |
buddy = get_nick(jid) |
|
182 |
||
183 |
if opt['use_notify']: |
|
184 |
if type == 1: |
|
185 |
timeout = NOTIFY_TIMEOUT |
|
186 |
else: |
|
187 |
timeout = pynotify.EXPIRES_NEVER |
|
188 |
notify(buddy, "is online", timeout) |
|
189 |
||
190 |
if filename and os.path.exists(filename): |
|
191 |
os.remove(filename) |
|
192 |
||
193 |
if opt['use_voice']: |
|
194 |
if jid in voicemap: |
|
195 |
buddy = voicemap[jid] |
|
196 |
say(buddy, "is online now.") |
|
197 |
||
198 |
elif event == 'UNREAD': |
|
199 |
# arg1 is the number of unread buffers |
|
2 | 200 |
if opt['unread_file'] != '': |
201 |
fileHandle = open(opt['unread_file'], 'w') |
|
202 |
fileHandle.write(arg1) |
|
203 |
fileHandle.close() |
|
0 | 204 |
|
205 |
||
206 |
##### MAIN ##### |
|
207 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
208 |
try: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
209 |
opts, args = getopt.getopt(sys.argv[1:], "c:", ["help", "output="]) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
210 |
except getopt.GetoptError, err: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
211 |
print str(err) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
212 |
sys.exit(2) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
213 |
|
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
214 |
for o, a in opts: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
215 |
if o == "-c": |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
216 |
CONFFILE = a |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
217 |
|
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
218 |
try: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
219 |
last_conf_read = os.stat(CONFFILE).st_ctime |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
220 |
except OSError: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
221 |
sys.stderr.write("Cannot read config file!\n") |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
222 |
sys.exit(3) |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
223 |
|
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
224 |
read_conf_from_file() |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
225 |
|
0 | 226 |
NOTIFY_LOADED = False |
227 |
||
228 |
# read stdin line by line |
|
229 |
while 1: |
|
230 |
try: |
|
231 |
line = sys.stdin.readline() |
|
232 |
except KeyboardInterrupt: |
|
233 |
print "\nInterrupted!" |
|
234 |
exit(0) |
|
235 |
if not line: |
|
236 |
break |
|
237 |
||
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
238 |
last_conf_change = os.stat(CONFFILE).st_ctime |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
239 |
if last_conf_change > last_conf_read: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
240 |
read_conf_from_file() |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
241 |
last_conf_read = last_conf_change |
0 | 242 |
lineargs = line.split() |
243 |
process_line(lineargs) |
|
244 |
||
245 |
if NOTIFY_LOADED: |
|
246 |
pynotify.uninit() |
|
247 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
248 |
sys.exit(0) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
249 |
|
2 | 250 |
# vim:set et sts=4 sw=4: |