author | Mikael Berthe <mikael@lilotux.net> |
Sat, 21 Jun 2008 19:35:23 +0200 | |
changeset 6 | 72c249247e18 |
parent 5 | 1400a0fa01d4 |
child 7 | 727c0c584faa |
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 |
0 | 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 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
contact_map[id] = config.get("Contacts", id) |
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("Alerts"): |
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
81 |
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
|
82 |
online_alerts[id] = int(config.get("Alerts", id)) |
0 | 83 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
voicemap[id] = config.get("Voicemap", id) |
0 | 87 |
|
6
72c249247e18
Do not error out if a section does not exist
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
blacklist[id] = int(config.get("Blacklist", id)) |
0 | 91 |
|
92 |
if opt['use_notify'] and not NOTIFY_LOADED: |
|
93 |
init_notify() |
|
94 |
||
95 |
def say(buddy, text): |
|
96 |
import subprocess |
|
97 |
p = subprocess.Popen(CMD_ESPEAK, stdin=subprocess.PIPE, close_fds=True) |
|
98 |
child_stdin = p.stdin |
|
99 |
child_stdin.write(buddy + " " + text) |
|
100 |
child_stdin.close() |
|
101 |
||
102 |
def notify(buddy, msg, timeout): |
|
103 |
msgbox = pynotify.Notification(unicode(buddy, encoding), |
|
104 |
unicode(msg, encoding)) |
|
105 |
msgbox.set_timeout(timeout) |
|
106 |
msgbox.set_urgency(pynotify.URGENCY_LOW) |
|
107 |
msgbox.show() |
|
108 |
||
109 |
def get_nick(jid): |
|
110 |
if jid in contact_map: |
|
111 |
buddy = contact_map[jid] |
|
112 |
else: |
|
113 |
buddy = jid |
|
114 |
||
115 |
if opt['short_nick'] and '@' in buddy: |
|
116 |
buddy = buddy[0:buddy.index('@')] |
|
117 |
return buddy |
|
118 |
||
119 |
def is_blacklisted(jid): |
|
120 |
if jid in blacklist and blacklist[jid]: |
|
121 |
return True |
|
122 |
return False |
|
123 |
||
124 |
def process_line(args): |
|
125 |
argn = len(args) |
|
126 |
||
127 |
if argn < 2: |
|
128 |
print "Ignoring invalid event line." |
|
129 |
return |
|
130 |
elif argn == 2: |
|
131 |
event,arg1 = args[0:2] |
|
132 |
arg2 = None |
|
133 |
filename = None |
|
134 |
elif argn == 3: |
|
135 |
event,arg1,arg2 = args[0:3] |
|
136 |
filename = None |
|
137 |
else: |
|
138 |
# For now we simply ignore the file name |
|
139 |
event,arg1,arg2 = args[0:3] |
|
140 |
filename = None |
|
141 |
||
142 |
if event == 'MSG' and arg1 == 'IN': |
|
143 |
import os |
|
144 |
||
145 |
jid = arg2 |
|
146 |
buddy = get_nick(jid) |
|
147 |
msg = 'sent you a message.' |
|
148 |
||
149 |
if not is_blacklisted(jid): |
|
150 |
textmsg = None |
|
151 |
||
152 |
if filename and os.path.exists(filename): |
|
153 |
f = file(filename) |
|
154 |
textmsg = f.read() |
|
155 |
||
156 |
if opt['use_notify']: |
|
157 |
if not textmsg: |
|
158 |
textmsg = msg |
|
159 |
notify(buddy, textmsg, NOTIFY_TIMEOUT) |
|
160 |
||
161 |
if opt['use_sound']: |
|
4 | 162 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0 | 163 |
|
164 |
if opt['use_voice'] and not is_blacklisted(jid): |
|
165 |
if jid in voicemap: |
|
166 |
buddy = voicemap[jid] |
|
167 |
say(buddy, msg) |
|
168 |
||
169 |
if filename and os.path.exists(filename): |
|
170 |
os.remove(filename) |
|
171 |
||
172 |
elif event == 'STATUS': |
|
173 |
jid = arg2 |
|
174 |
if arg1 == 'O' and jid in online_alerts: |
|
175 |
import os |
|
176 |
||
177 |
type = online_alerts[jid] |
|
178 |
if type > 0: |
|
179 |
if opt['use_sound']: |
|
4 | 180 |
os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1') |
0 | 181 |
|
182 |
buddy = get_nick(jid) |
|
183 |
||
184 |
if opt['use_notify']: |
|
185 |
if type == 1: |
|
186 |
timeout = NOTIFY_TIMEOUT |
|
187 |
else: |
|
188 |
timeout = pynotify.EXPIRES_NEVER |
|
189 |
notify(buddy, "is online", timeout) |
|
190 |
||
191 |
if filename and os.path.exists(filename): |
|
192 |
os.remove(filename) |
|
193 |
||
194 |
if opt['use_voice']: |
|
195 |
if jid in voicemap: |
|
196 |
buddy = voicemap[jid] |
|
197 |
say(buddy, "is online now.") |
|
198 |
||
199 |
elif event == 'UNREAD': |
|
200 |
# arg1 is the number of unread buffers |
|
2 | 201 |
if opt['unread_file'] != '': |
202 |
fileHandle = open(opt['unread_file'], 'w') |
|
203 |
fileHandle.write(arg1) |
|
204 |
fileHandle.close() |
|
0 | 205 |
|
206 |
||
207 |
##### MAIN ##### |
|
208 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
209 |
try: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
210 |
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
|
211 |
except getopt.GetoptError, err: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
212 |
print str(err) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
213 |
sys.exit(2) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
214 |
|
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
215 |
for o, a in opts: |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
216 |
if o == "-c": |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
217 |
CONFFILE = a |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
218 |
|
0 | 219 |
NOTIFY_LOADED = False |
220 |
||
221 |
# read stdin line by line |
|
222 |
while 1: |
|
223 |
try: |
|
224 |
line = sys.stdin.readline() |
|
225 |
except KeyboardInterrupt: |
|
226 |
print "\nInterrupted!" |
|
227 |
exit(0) |
|
228 |
if not line: |
|
229 |
break |
|
230 |
||
231 |
read_conf_from_file() |
|
232 |
lineargs = line.split() |
|
233 |
process_line(lineargs) |
|
234 |
||
235 |
if NOTIFY_LOADED: |
|
236 |
pynotify.uninit() |
|
237 |
||
5
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
238 |
sys.exit(0) |
1400a0fa01d4
Add command line option -c
Mikael Berthe <mikael@lilotux.net>
parents:
4
diff
changeset
|
239 |
|
2 | 240 |
# vim:set et sts=4 sw=4: |