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