author | Mikael Berthe <mikael@lilotux.net> |
Sat, 21 Jun 2008 21:15:00 +0200 | |
changeset 10 | cf31fa2e0bbc |
parent 9 | 91f9fe1adf0c |
child 11 | 5ee5101decd0 |
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 |
||
10 | 27 |
NOTIFY_TIMEOUT = 4000 |
0 | 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 |
||
10 | 44 |
CMD_ESPEAK = "/usr/bin/espeak" |
45 |
||
46 |
ENCODING = '' |
|
0 | 47 |
|
48 |
def init_notify(): |
|
49 |
import locale |
|
10 | 50 |
global ENCODING, NOTIFY_LOADED |
0 | 51 |
pynotify.init('mcnotify') |
10 | 52 |
ENCODING = (locale.getdefaultlocale())[1] |
0 | 53 |
NOTIFY_LOADED = True |
54 |
||
55 |
def read_conf_from_file(): |
|
56 |
import ConfigParser |
|
57 |
||
58 |
config = ConfigParser.ConfigParser() |
|
59 |
config.read(CONFFILE) |
|
60 |
||
61 |
contact_map.clear() |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
62 |
contact_custom_msg.clear() |
0 | 63 |
online_alerts.clear() |
64 |
voicemap.clear() |
|
65 |
blacklist.clear() |
|
66 |
||
67 |
if config.has_option("Notifications", "notify"): |
|
68 |
opt['use_notify'] = int(config.get("Notifications", "notify")) |
|
69 |
if config.has_option("Notifications", "voice"): |
|
70 |
opt['use_voice'] = int(config.get("Notifications", "voice")) |
|
71 |
if config.has_option("Notifications", "sound"): |
|
72 |
opt['use_sound'] = int(config.get("Notifications", "sound")) |
|
73 |
if config.has_option("Notifications", "short_nick"): |
|
74 |
opt['short_nick'] = int(config.get("Notifications", "short_nick")) |
|
2 | 75 |
if config.has_option("Notifications", "unread_file"): |
76 |
opt['unread_file'] = config.get("Notifications", "unread_file") |
|
4 | 77 |
if config.has_option("Notifications", "snd_cmd_msg_in"): |
78 |
opt['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in") |
|
0 | 79 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
80 |
if config.has_section("Contacts"): |
10 | 81 |
for cid in config.options("Contacts"): |
82 |
contact_map[cid] = config.get("Contacts", cid) |
|
0 | 83 |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
84 |
if config.has_section("Contact_Customized_Messages"): |
10 | 85 |
for cid in config.options("Contact_Customized_Messages"): |
86 |
contact_custom_msg[cid] = config.get("Contact_Customized_Messages", |
|
87 |
cid) |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
88 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
89 |
if config.has_section("Alerts"): |
10 | 90 |
for cid in config.options("Alerts"): |
91 |
online_alerts[cid] = int(config.get("Alerts", cid)) |
|
0 | 92 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
93 |
if config.has_section("Voicemap"): |
10 | 94 |
for cid in config.options("Voicemap"): |
95 |
voicemap[cid] = config.get("Voicemap", cid) |
|
0 | 96 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
97 |
if config.has_section("Blacklist"): |
10 | 98 |
for cid in config.options("Blacklist"): |
99 |
blacklist[cid] = int(config.get("Blacklist", cid)) |
|
0 | 100 |
|
101 |
if opt['use_notify'] and not NOTIFY_LOADED: |
|
102 |
init_notify() |
|
103 |
||
104 |
def say(buddy, text): |
|
105 |
import subprocess |
|
106 |
p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True) |
|
107 |
child_stdin = p.stdin |
|
108 |
child_stdin.write(buddy + " " + text) |
|
109 |
child_stdin.close() |
|
110 |
||
111 |
def notify(buddy, msg, timeout): |
|
10 | 112 |
msgbox = pynotify.Notification(unicode(buddy, ENCODING), |
113 |
unicode(msg, ENCODING)) |
|
0 | 114 |
msgbox.set_timeout(timeout) |
115 |
msgbox.set_urgency(pynotify.URGENCY_LOW) |
|
116 |
msgbox.show() |
|
117 |
||
118 |
def get_nick(jid): |
|
119 |
if jid in contact_map: |
|
120 |
buddy = contact_map[jid] |
|
121 |
else: |
|
122 |
buddy = jid |
|
123 |
||
124 |
if opt['short_nick'] and '@' in buddy: |
|
125 |
buddy = buddy[0:buddy.index('@')] |
|
126 |
return buddy |
|
127 |
||
128 |
def is_blacklisted(jid): |
|
129 |
if jid in blacklist and blacklist[jid]: |
|
130 |
return True |
|
131 |
return False |
|
132 |
||
133 |
def process_line(args): |
|
134 |
argn = len(args) |
|
135 |
||
136 |
if argn < 2: |
|
137 |
print "Ignoring invalid event line." |
|
138 |
return |
|
139 |
elif argn == 2: |
|
10 | 140 |
event, arg1 = args[0:2] |
141 |
arg2 = None |
|
142 |
filename = None |
|
0 | 143 |
elif argn == 3: |
10 | 144 |
event, arg1, arg2 = args[0:3] |
145 |
filename = None |
|
0 | 146 |
else: |
147 |
# For now we simply ignore the file name |
|
10 | 148 |
event, arg1, arg2 = args[0:3] |
149 |
filename = None |
|
0 | 150 |
|
151 |
if event == 'MSG' and arg1 == 'IN': |
|
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: |
|
10 | 191 |
alert_type = online_alerts[jid] |
192 |
if alert_type > 0: |
|
0 | 193 |
if opt['use_sound']: |
4 | 194 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0 | 195 |
|
196 |
buddy = get_nick(jid) |
|
197 |
||
198 |
if opt['use_notify']: |
|
10 | 199 |
if alert_type == 1: |
0 | 200 |
timeout = NOTIFY_TIMEOUT |
201 |
else: |
|
202 |
timeout = pynotify.EXPIRES_NEVER |
|
203 |
notify(buddy, "is online", timeout) |
|
204 |
||
205 |
if filename and os.path.exists(filename): |
|
206 |
os.remove(filename) |
|
207 |
||
208 |
if opt['use_voice']: |
|
209 |
if jid in voicemap: |
|
210 |
buddy = voicemap[jid] |
|
211 |
say(buddy, "is online now.") |
|
212 |
||
213 |
elif event == 'UNREAD': |
|
214 |
# arg1 is the number of unread buffers |
|
2 | 215 |
if opt['unread_file'] != '': |
216 |
fileHandle = open(opt['unread_file'], 'w') |
|
217 |
fileHandle.write(arg1) |
|
218 |
fileHandle.close() |
|
0 | 219 |
|
220 |
||
221 |
##### MAIN ##### |
|
222 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
223 |
try: |
10 | 224 |
opts, cargs = getopt.getopt(sys.argv[1:], "c:", ["help", "output="]) |
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
225 |
except getopt.GetoptError, err: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
226 |
print str(err) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
227 |
sys.exit(2) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
228 |
|
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
229 |
for o, a in opts: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
230 |
if o == "-c": |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
231 |
CONFFILE = a |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
232 |
|
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
233 |
try: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
234 |
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
|
235 |
except OSError: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
236 |
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
|
237 |
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
|
238 |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
239 |
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
|
240 |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
241 |
read_conf_from_file() |
0 | 242 |
|
10 | 243 |
# Read stdin line by line, and process the commands |
0 | 244 |
while 1: |
245 |
try: |
|
246 |
line = sys.stdin.readline() |
|
247 |
except KeyboardInterrupt: |
|
248 |
print "\nInterrupted!" |
|
249 |
exit(0) |
|
250 |
if not line: |
|
251 |
break |
|
252 |
||
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
253 |
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
|
254 |
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
|
255 |
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
|
256 |
last_conf_read = last_conf_change |
0 | 257 |
lineargs = line.split() |
258 |
process_line(lineargs) |
|
259 |
||
260 |
if NOTIFY_LOADED: |
|
261 |
pynotify.uninit() |
|
262 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
263 |
sys.exit(0) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
264 |
|
10 | 265 |
# vim:set si et sts=4 sw=4: |