author | Mikael Berthe <mikael@lilotux.net> |
Sun, 28 Mar 2010 23:04:18 +0200 | |
changeset 14 | 02d532e70dfb |
parent 13 | 4db51cfb9e15 |
child 15 | d4c85df8d0b8 |
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, |
|
11 | 8 |
# see the file COPYING in this directory. |
0 | 9 |
# |
11 | 10 |
""" |
11 |
This script reads mcabber event lines from the standard input |
|
12 |
and creates events accordingly. Examples: |
|
13 |
STATUS O user2@domain.org |
|
14 |
MSG IN user@domain.org |
|
13
4db51cfb9e15
Use new UNREAD parameters (mcabber 0.10.0)
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
15 |
UNREAD 1 0 1 0 |
11 | 16 |
""" |
0 | 17 |
|
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
18 |
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
|
19 |
import os |
0 | 20 |
import pynotify |
21 |
||
22 |
CONFFILE = "mcevent.cfg" |
|
23 |
||
24 |
# Default option values |
|
11 | 25 |
OPT = { |
0 | 26 |
'use_notify': 0, |
27 |
'use_voice': 0, |
|
28 |
'use_sound': 1, |
|
29 |
'short_nick': 1, |
|
2 | 30 |
'unread_file': '', |
4 | 31 |
'snd_cmd_msg_in': '/usr/bin/play -V0 -v3 sound.wav', |
0 | 32 |
} |
33 |
||
10 | 34 |
NOTIFY_TIMEOUT = 4000 |
0 | 35 |
|
11 | 36 |
CONTACT_MAP = { } |
0 | 37 |
# Nickname used for the notification |
38 |
||
11 | 39 |
CONTACT_CUSTOM_MSG = { } |
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
40 |
# Message used for the notification |
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
41 |
|
11 | 42 |
ONLINE_ALERTS = { } |
0 | 43 |
# 0: disabled 1: normal notification 2: permanent notify box |
44 |
||
11 | 45 |
VOICEMAP = { } |
0 | 46 |
# Specify the name pronounced by espeak |
47 |
||
11 | 48 |
BLACKLIST = { } |
0 | 49 |
# No notification for these JIDs |
50 |
||
10 | 51 |
CMD_ESPEAK = "/usr/bin/espeak" |
52 |
||
53 |
ENCODING = '' |
|
0 | 54 |
|
55 |
def init_notify(): |
|
11 | 56 |
""" |
57 |
Initialize the pynotify subsystem. |
|
58 |
""" |
|
0 | 59 |
import locale |
10 | 60 |
global ENCODING, NOTIFY_LOADED |
0 | 61 |
pynotify.init('mcnotify') |
10 | 62 |
ENCODING = (locale.getdefaultlocale())[1] |
0 | 63 |
NOTIFY_LOADED = True |
64 |
||
65 |
def read_conf_from_file(): |
|
11 | 66 |
""" |
67 |
Read the configuration file. |
|
68 |
""" |
|
0 | 69 |
import ConfigParser |
70 |
||
71 |
config = ConfigParser.ConfigParser() |
|
72 |
config.read(CONFFILE) |
|
73 |
||
11 | 74 |
CONTACT_MAP.clear() |
75 |
CONTACT_CUSTOM_MSG.clear() |
|
76 |
ONLINE_ALERTS.clear() |
|
77 |
VOICEMAP.clear() |
|
78 |
BLACKLIST.clear() |
|
0 | 79 |
|
80 |
if config.has_option("Notifications", "notify"): |
|
11 | 81 |
OPT['use_notify'] = int(config.get("Notifications", "notify")) |
0 | 82 |
if config.has_option("Notifications", "voice"): |
11 | 83 |
OPT['use_voice'] = int(config.get("Notifications", "voice")) |
0 | 84 |
if config.has_option("Notifications", "sound"): |
11 | 85 |
OPT['use_sound'] = int(config.get("Notifications", "sound")) |
0 | 86 |
if config.has_option("Notifications", "short_nick"): |
11 | 87 |
OPT['short_nick'] = int(config.get("Notifications", "short_nick")) |
2 | 88 |
if config.has_option("Notifications", "unread_file"): |
11 | 89 |
OPT['unread_file'] = config.get("Notifications", "unread_file") |
4 | 90 |
if config.has_option("Notifications", "snd_cmd_msg_in"): |
11 | 91 |
OPT['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in") |
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("Contacts"): |
10 | 94 |
for cid in config.options("Contacts"): |
11 | 95 |
CONTACT_MAP[cid] = config.get("Contacts", cid) |
0 | 96 |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
97 |
if config.has_section("Contact_Customized_Messages"): |
10 | 98 |
for cid in config.options("Contact_Customized_Messages"): |
11 | 99 |
CONTACT_CUSTOM_MSG[cid] = config.get("Contact_Customized_Messages", |
10 | 100 |
cid) |
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
101 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
102 |
if config.has_section("Alerts"): |
10 | 103 |
for cid in config.options("Alerts"): |
11 | 104 |
ONLINE_ALERTS[cid] = int(config.get("Alerts", cid)) |
0 | 105 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
106 |
if config.has_section("Voicemap"): |
10 | 107 |
for cid in config.options("Voicemap"): |
11 | 108 |
VOICEMAP[cid] = config.get("Voicemap", cid) |
0 | 109 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
110 |
if config.has_section("Blacklist"): |
10 | 111 |
for cid in config.options("Blacklist"): |
11 | 112 |
BLACKLIST[cid] = int(config.get("Blacklist", cid)) |
0 | 113 |
|
11 | 114 |
if OPT['use_notify'] and not NOTIFY_LOADED: |
0 | 115 |
init_notify() |
116 |
||
117 |
def say(buddy, text): |
|
11 | 118 |
""" |
119 |
Create a subprocess and run a speech synthesizer. |
|
120 |
""" |
|
0 | 121 |
import subprocess |
122 |
p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True) |
|
123 |
child_stdin = p.stdin |
|
124 |
child_stdin.write(buddy + " " + text) |
|
125 |
child_stdin.close() |
|
126 |
||
127 |
def notify(buddy, msg, timeout): |
|
11 | 128 |
""" |
129 |
Create a pynotify popup. |
|
130 |
""" |
|
10 | 131 |
msgbox = pynotify.Notification(unicode(buddy, ENCODING), |
132 |
unicode(msg, ENCODING)) |
|
0 | 133 |
msgbox.set_timeout(timeout) |
134 |
msgbox.set_urgency(pynotify.URGENCY_LOW) |
|
135 |
msgbox.show() |
|
136 |
||
137 |
def get_nick(jid): |
|
11 | 138 |
""" |
139 |
Return the nick of the given contact (JID). |
|
140 |
""" |
|
141 |
if jid in CONTACT_MAP: |
|
142 |
buddy = CONTACT_MAP[jid] |
|
0 | 143 |
else: |
144 |
buddy = jid |
|
145 |
||
11 | 146 |
if OPT['short_nick'] and '@' in buddy: |
0 | 147 |
buddy = buddy[0:buddy.index('@')] |
148 |
return buddy |
|
149 |
||
150 |
def is_blacklisted(jid): |
|
11 | 151 |
""" |
152 |
Return True if the given contcat (JID) is blacklisted. |
|
153 |
""" |
|
154 |
if jid in BLACKLIST and BLACKLIST[jid]: |
|
0 | 155 |
return True |
156 |
return False |
|
157 |
||
158 |
def process_line(args): |
|
11 | 159 |
""" |
160 |
Parse the provided line and run events. |
|
161 |
""" |
|
0 | 162 |
argn = len(args) |
163 |
||
164 |
if argn < 2: |
|
165 |
print "Ignoring invalid event line." |
|
166 |
return |
|
167 |
elif argn == 2: |
|
10 | 168 |
event, arg1 = args[0:2] |
169 |
arg2 = None |
|
170 |
filename = None |
|
0 | 171 |
elif argn == 3: |
10 | 172 |
event, arg1, arg2 = args[0:3] |
173 |
filename = None |
|
0 | 174 |
else: |
175 |
# For now we simply ignore the file name |
|
10 | 176 |
event, arg1, arg2 = args[0:3] |
177 |
filename = None |
|
0 | 178 |
|
179 |
if event == 'MSG' and arg1 == 'IN': |
|
180 |
jid = arg2 |
|
9 | 181 |
if not jid: |
182 |
print "Ignoring invalid MSG event line." |
|
183 |
return |
|
184 |
||
0 | 185 |
buddy = get_nick(jid) |
11 | 186 |
if jid in CONTACT_CUSTOM_MSG: |
187 |
msg = CONTACT_CUSTOM_MSG[jid] |
|
188 |
elif 'default' in CONTACT_CUSTOM_MSG: |
|
189 |
msg = CONTACT_CUSTOM_MSG['default'] |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
190 |
else: |
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
191 |
msg = 'sent you a message.' |
0 | 192 |
|
193 |
if not is_blacklisted(jid): |
|
194 |
textmsg = None |
|
195 |
||
196 |
if filename and os.path.exists(filename): |
|
11 | 197 |
fileh = file(filename) |
198 |
textmsg = fileh.read() |
|
0 | 199 |
|
11 | 200 |
if OPT['use_notify']: |
0 | 201 |
if not textmsg: |
202 |
textmsg = msg |
|
203 |
notify(buddy, textmsg, NOTIFY_TIMEOUT) |
|
204 |
||
11 | 205 |
if OPT['use_sound']: |
206 |
os.system(OPT['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
|
0 | 207 |
|
11 | 208 |
if OPT['use_voice'] and not is_blacklisted(jid): |
209 |
if jid in VOICEMAP: |
|
210 |
buddy = VOICEMAP[jid] |
|
0 | 211 |
say(buddy, msg) |
212 |
||
213 |
if filename and os.path.exists(filename): |
|
214 |
os.remove(filename) |
|
215 |
||
216 |
elif event == 'STATUS': |
|
217 |
jid = arg2 |
|
11 | 218 |
if arg1 == 'O' and jid in ONLINE_ALERTS: |
219 |
alert_type = ONLINE_ALERTS[jid] |
|
10 | 220 |
if alert_type > 0: |
11 | 221 |
if OPT['use_sound']: |
222 |
os.system(OPT['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
|
0 | 223 |
|
224 |
buddy = get_nick(jid) |
|
225 |
||
11 | 226 |
if OPT['use_notify']: |
10 | 227 |
if alert_type == 1: |
0 | 228 |
timeout = NOTIFY_TIMEOUT |
229 |
else: |
|
230 |
timeout = pynotify.EXPIRES_NEVER |
|
231 |
notify(buddy, "is online", timeout) |
|
232 |
||
233 |
if filename and os.path.exists(filename): |
|
234 |
os.remove(filename) |
|
235 |
||
11 | 236 |
if OPT['use_voice']: |
237 |
if jid in VOICEMAP: |
|
238 |
buddy = VOICEMAP[jid] |
|
0 | 239 |
say(buddy, "is online now.") |
240 |
||
241 |
elif event == 'UNREAD': |
|
14 | 242 |
unread_all, unread_attn = map(int, args[1:3]) |
243 |
unread_muc, unread_muc_attn = map(int, args[3:5]) |
|
244 |
unread = unread_all - (unread_muc - unread_muc_attn) |
|
245 |
if unread == unread_all: |
|
246 |
unread_str = str(unread) |
|
13
4db51cfb9e15
Use new UNREAD parameters (mcabber 0.10.0)
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
247 |
else: |
14 | 248 |
unread_str = "%d/%d" % (unread, unread_all) |
11 | 249 |
if OPT['unread_file'] != '': |
250 |
fileh = open(OPT['unread_file'], 'w') |
|
13
4db51cfb9e15
Use new UNREAD parameters (mcabber 0.10.0)
Mikael Berthe <mikael@lilotux.net>
parents:
12
diff
changeset
|
251 |
fileh.write(unread_str) |
11 | 252 |
fileh.close() |
0 | 253 |
|
254 |
||
255 |
##### MAIN ##### |
|
256 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
257 |
try: |
10 | 258 |
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
|
259 |
except getopt.GetoptError, err: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
260 |
print str(err) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
261 |
sys.exit(2) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
262 |
|
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
263 |
for o, a in opts: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
264 |
if o == "-c": |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
265 |
CONFFILE = a |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
266 |
|
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
267 |
try: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
268 |
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
|
269 |
except OSError: |
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
270 |
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
|
271 |
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
|
272 |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
273 |
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
|
274 |
|
8
14dd3f4fd4dc
New section "Contact_Customized_Messages"
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
275 |
read_conf_from_file() |
0 | 276 |
|
10 | 277 |
# Read stdin line by line, and process the commands |
0 | 278 |
while 1: |
279 |
try: |
|
280 |
line = sys.stdin.readline() |
|
281 |
except KeyboardInterrupt: |
|
282 |
print "\nInterrupted!" |
|
12 | 283 |
sys.exit(0) |
0 | 284 |
if not line: |
285 |
break |
|
286 |
||
7
727c0c584faa
Do not re-read the config. file if it hasn't changed
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
287 |
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
|
288 |
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
|
289 |
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
|
290 |
last_conf_read = last_conf_change |
0 | 291 |
lineargs = line.split() |
292 |
process_line(lineargs) |
|
293 |
||
294 |
if NOTIFY_LOADED: |
|
295 |
pynotify.uninit() |
|
296 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
297 |
sys.exit(0) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
298 |
|
10 | 299 |
# vim:set si et sts=4 sw=4: |