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 |
|
|
11 |
import sys |
|
12 |
import pynotify |
|
13 |
|
|
14 |
CONFFILE = "mcevent.cfg" |
|
15 |
|
|
16 |
# Default option values |
|
17 |
opt = { |
|
18 |
'use_notify': 0, |
|
19 |
'use_voice': 0, |
|
20 |
'use_sound': 1, |
|
21 |
'short_nick': 1, |
2
|
22 |
'unread_file': '', |
4
|
23 |
'snd_cmd_msg_in': '/usr/bin/play -V0 -v3 sound.wav', |
0
|
24 |
} |
|
25 |
|
|
26 |
NOTIFY_TIMEOUT=4000 |
|
27 |
|
|
28 |
contact_map = { } |
|
29 |
# Nickname used for the notification |
|
30 |
|
|
31 |
online_alerts = { } |
|
32 |
# 0: disabled 1: normal notification 2: permanent notify box |
|
33 |
|
|
34 |
voicemap = { } |
|
35 |
# Specify the name pronounced by espeak |
|
36 |
|
|
37 |
blacklist = { } |
|
38 |
# No notification for these JIDs |
|
39 |
|
|
40 |
CMD_ESPEAK="/usr/bin/espeak" |
|
41 |
|
|
42 |
def init_notify(): |
|
43 |
import locale |
|
44 |
global encoding |
|
45 |
global NOTIFY_LOADED |
|
46 |
pynotify.init('mcnotify') |
|
47 |
encoding = (locale.getdefaultlocale())[1] |
|
48 |
NOTIFY_LOADED = True |
|
49 |
|
|
50 |
def read_conf_from_file(): |
|
51 |
import ConfigParser |
|
52 |
|
4
|
53 |
# TODO: Do not re-read file if it hasn't been touched |
|
54 |
|
0
|
55 |
config = ConfigParser.ConfigParser() |
|
56 |
config.read(CONFFILE) |
|
57 |
|
|
58 |
contact_map.clear() |
|
59 |
online_alerts.clear() |
|
60 |
voicemap.clear() |
|
61 |
blacklist.clear() |
|
62 |
|
|
63 |
if config.has_option("Notifications", "notify"): |
|
64 |
opt['use_notify'] = int(config.get("Notifications", "notify")) |
|
65 |
if config.has_option("Notifications", "voice"): |
|
66 |
opt['use_voice'] = int(config.get("Notifications", "voice")) |
|
67 |
if config.has_option("Notifications", "sound"): |
|
68 |
opt['use_sound'] = int(config.get("Notifications", "sound")) |
|
69 |
if config.has_option("Notifications", "short_nick"): |
|
70 |
opt['short_nick'] = int(config.get("Notifications", "short_nick")) |
2
|
71 |
if config.has_option("Notifications", "unread_file"): |
|
72 |
opt['unread_file'] = config.get("Notifications", "unread_file") |
4
|
73 |
if config.has_option("Notifications", "snd_cmd_msg_in"): |
|
74 |
opt['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in") |
0
|
75 |
|
|
76 |
for id in config.options("Contacts"): |
|
77 |
contact_map[id] = config.get("Contacts", id) |
|
78 |
|
|
79 |
for id in config.options("Alerts"): |
|
80 |
online_alerts[id] = int(config.get("Alerts", id)) |
|
81 |
|
|
82 |
for id in config.options("Voicemap"): |
|
83 |
voicemap[id] = config.get("Voicemap", id) |
|
84 |
|
|
85 |
for id in config.options("Blacklist"): |
|
86 |
blacklist[id] = int(config.get("Blacklist", id)) |
|
87 |
|
|
88 |
if opt['use_notify'] and not NOTIFY_LOADED: |
|
89 |
init_notify() |
|
90 |
|
|
91 |
def say(buddy, text): |
|
92 |
import subprocess |
|
93 |
p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True) |
|
94 |
child_stdin = p.stdin |
|
95 |
child_stdin.write(buddy + " " + text) |
|
96 |
child_stdin.close() |
|
97 |
|
|
98 |
def notify(buddy, msg, timeout): |
|
99 |
msgbox = pynotify.Notification(unicode(buddy, encoding), |
|
100 |
unicode(msg, encoding)) |
|
101 |
msgbox.set_timeout(timeout) |
|
102 |
msgbox.set_urgency(pynotify.URGENCY_LOW) |
|
103 |
msgbox.show() |
|
104 |
|
|
105 |
def get_nick(jid): |
|
106 |
if jid in contact_map: |
|
107 |
buddy = contact_map[jid] |
|
108 |
else: |
|
109 |
buddy = jid |
|
110 |
|
|
111 |
if opt['short_nick'] and '@' in buddy: |
|
112 |
buddy = buddy[0:buddy.index('@')] |
|
113 |
return buddy |
|
114 |
|
|
115 |
def is_blacklisted(jid): |
|
116 |
if jid in blacklist and blacklist[jid]: |
|
117 |
return True |
|
118 |
return False |
|
119 |
|
|
120 |
def process_line(args): |
|
121 |
argn = len(args) |
|
122 |
|
|
123 |
if argn < 2: |
|
124 |
print "Ignoring invalid event line." |
|
125 |
return |
|
126 |
elif argn == 2: |
|
127 |
event,arg1 = args[0:2] |
|
128 |
arg2 = None |
|
129 |
filename = None |
|
130 |
elif argn == 3: |
|
131 |
event,arg1,arg2 = args[0:3] |
|
132 |
filename = None |
|
133 |
else: |
|
134 |
# For now we simply ignore the file name |
|
135 |
event,arg1,arg2 = args[0:3] |
|
136 |
filename = None |
|
137 |
|
|
138 |
if event == 'MSG' and arg1 == 'IN': |
|
139 |
import os |
|
140 |
|
|
141 |
jid = arg2 |
|
142 |
buddy = get_nick(jid) |
|
143 |
msg = 'sent you a message.' |
|
144 |
|
|
145 |
if not is_blacklisted(jid): |
|
146 |
textmsg = None |
|
147 |
|
|
148 |
if filename and os.path.exists(filename): |
|
149 |
f = file(filename) |
|
150 |
textmsg = f.read() |
|
151 |
|
|
152 |
if opt['use_notify']: |
|
153 |
if not textmsg: |
|
154 |
textmsg = msg |
|
155 |
notify(buddy, textmsg, NOTIFY_TIMEOUT) |
|
156 |
|
|
157 |
if opt['use_sound']: |
4
|
158 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0
|
159 |
|
|
160 |
if opt['use_voice'] and not is_blacklisted(jid): |
|
161 |
if jid in voicemap: |
|
162 |
buddy = voicemap[jid] |
|
163 |
say(buddy, msg) |
|
164 |
|
|
165 |
if filename and os.path.exists(filename): |
|
166 |
os.remove(filename) |
|
167 |
|
|
168 |
elif event == 'STATUS': |
|
169 |
jid = arg2 |
|
170 |
if arg1 == 'O' and jid in online_alerts: |
|
171 |
import os |
|
172 |
|
|
173 |
type = online_alerts[jid] |
|
174 |
if type > 0: |
|
175 |
if opt['use_sound']: |
4
|
176 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0
|
177 |
|
|
178 |
buddy = get_nick(jid) |
|
179 |
|
|
180 |
if opt['use_notify']: |
|
181 |
if type == 1: |
|
182 |
timeout = NOTIFY_TIMEOUT |
|
183 |
else: |
|
184 |
timeout = pynotify.EXPIRES_NEVER |
|
185 |
notify(buddy, "is online", timeout) |
|
186 |
|
|
187 |
if filename and os.path.exists(filename): |
|
188 |
os.remove(filename) |
|
189 |
|
|
190 |
if opt['use_voice']: |
|
191 |
if jid in voicemap: |
|
192 |
buddy = voicemap[jid] |
|
193 |
say(buddy, "is online now.") |
|
194 |
|
|
195 |
elif event == 'UNREAD': |
|
196 |
# arg1 is the number of unread buffers |
2
|
197 |
if opt['unread_file'] != '': |
|
198 |
fileHandle = open(opt['unread_file'], 'w') |
|
199 |
fileHandle.write(arg1) |
|
200 |
fileHandle.close() |
0
|
201 |
|
|
202 |
|
|
203 |
##### MAIN ##### |
|
204 |
|
|
205 |
NOTIFY_LOADED = False |
|
206 |
|
|
207 |
# read stdin line by line |
|
208 |
while 1: |
|
209 |
try: |
|
210 |
line = sys.stdin.readline() |
|
211 |
except KeyboardInterrupt: |
|
212 |
print "\nInterrupted!" |
|
213 |
exit(0) |
|
214 |
if not line: |
|
215 |
break |
|
216 |
|
|
217 |
read_conf_from_file() |
|
218 |
lineargs = line.split() |
|
219 |
process_line(lineargs) |
|
220 |
|
|
221 |
if NOTIFY_LOADED: |
|
222 |
pynotify.uninit() |
|
223 |
|
2
|
224 |
# vim:set et sts=4 sw=4: |