]> git.pld-linux.org Git - packages/ekg2-script-pynotif.git/blob - pynotif.py
- define file encoding
[packages/ekg2-script-pynotif.git] / pynotif.py
1 # vim:fileencoding=utf-8
2
3 #   This program is free software: you can redistribute it and/or modify
4 #   it under the terms of the GNU General Public License as published by
5 #   the Free Software Foundation, either version 3 of the License, or
6 #   (at your option) any later version.
7 #
8 #   This program is distributed in the hope that it will be useful,
9 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #   GNU General Public License for more details.
12 #
13 #   You should have received a copy of the GNU General Public License
14 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 #   Copyright (c) 2009 by PaweÅ‚ Tomak <satherot (at) gmail (dot) com>
17
18 import ekg
19 import time
20 import pynotify
21 import re
22 import sys
23
24 TIMEOUT_STATUS=3500
25 TIMEOUT_MSG=3500
26
27 def removeHTML(text):
28     reg = re.compile("&")
29     text = reg.sub("&#38;", text)
30     reg = re.compile("<")
31     text = reg.sub("&#60;", text)
32     reg = re.compile(">")
33     text = reg.sub("&#62;", text)
34     return text
35
36 def transStatus(status):
37     return {
38             'avail': 'dostepny',
39             'away': 'zaraz wracam',
40             'blocking': 'BLOKUJE',
41             'error': 'BLAD STATUSU!!',
42             'ffc': 'chetny do rozmowy',
43             'chat': 'chetny do rozmowy',
44             'dnd': 'nie przeszkadzac',
45             'xa': 'bardzo zajety',
46             'notavail': 'niedostepny',
47             }[status]
48
49 def displayNotify(title, text, timeout, type):
50     if not pynotify.init("EkgNotif"):
51         ekg.echo("you don't seem to have pynotify installed")
52         return 0
53     n = pynotify.Notification(title, text, type)
54     n.set_timeout(timeout)
55     n.show()
56     return 1
57
58 def notifyStatus(session, uid, status, descr):
59     regexp = re.compile('irc:*')
60     regexp = regexp.findall(session)
61     if len(regexp):
62         return 1
63     regexp = re.compile('.*' + session + '.*')
64     regexp = regexp.findall(uid)
65     if len(regexp):
66         ekg.echo("Zmienil sie status sesji: %s. Nie zostal on zmieniony przez ten program. Sprawdz to, jesli nie zmieniales statusu jakims innym programem" % session)
67         return 1
68     sesja = ekg.session_get(session)
69     regexp = re.compile('([a-z]{2,4}:[^/]+)')
70     regexp = regexp.match(uid)
71     regexp = regexp.group()
72     try:
73         user = sesja.user_get(regexp)
74     except KeyError:
75         ekg.echo("Nie znalazlem uzytkownika %s." % uid)
76         return 1
77     status = transStatus(status)
78     nick = user.nickname or user.uid or "Empty"
79     s = status or "Empty"
80     s = removeHTML(s)
81     text = "<b>" + nick + "</b> zmienil status na <b>" + s + "</b>"
82     if descr:
83         descr = removeHTML(descr)
84         text = text + ":\n" + descr + "\n"
85     return displayNotify(session, text, TIMEOUT_STATUS, ekg.config["notify:icon_status"])
86
87 def notifyMessage(session, uid, type, text, stime, ignore_level):
88     regexp = re.compile('irc:*')
89     regexp = regexp.findall(session)
90     if len(regexp):
91         return 1
92     text = removeHTML(text)
93     sesja = ekg.session_get(session)
94     try:
95         user = sesja.user_get(uid)
96     except KeyError:
97         ekg.echo("Nie znalazlem uzytkownika %s." % uid)
98         return 1
99     t = time.strftime("%H:%M:%S", time.gmtime(stime))
100     title = t + " " + user.nickname
101     if len(text) > 200:
102         text = text[0:199] + "... >>>\n\n"
103     return displayNotify(title, text, TIMEOUT_MSG, ekg.config["notify:icon_msg"])
104
105 def timeCheck(name, args):
106     global TIMEOUT_MSG
107     global TIMEOUT_STATUS
108     rexp = re.compile('^[0-9]{4,4}')
109     rexp = rexp.findall(args)
110     if len(rexp) == 1:
111         if name == "notify:message_timeout":
112             TIMEOUT_MSG = int(rexp[0])
113             return 1
114         if name == "notify:status_timeout":
115             TIMEOUT_STATUS = int(rexp[0])
116             return 1
117
118     if name == "notify:message_timeout":
119         ekg.echo("Zmienna %s bedzie pomijana do czasu, az zostanie ustawiona wartosc z zakresu od 1000ms do 9999ms. Jej obecna wartosc to: %i" % (name,TIMEOUT_MSG))
120     elif name == "notify:status_timeout":
121         ekg.echo("Zmienna %s bedzie pomijana do czasu, az zostanie ustawiona wartosc z zakresu od 1000ms do 9999ms. Jej obecna wartosc to: %i" % (name,TIMEOUT_STATUS))
122     return 0
123
124 ekg.handler_bind('protocol-status', notifyStatus)
125 ekg.handler_bind("protocol-message-received", notifyMessage)
126 ekg.variable_add("notify:icon_status", "dialog-warning")
127 ekg.variable_add("notify:icon_msg", "dialog-warning")
128 ekg.variable_add("notify:message_timeout", "3500", timeCheck)
129 ekg.variable_add("notify:status_timeout", "3500", timeCheck)
130
131 if int(ekg.config["notify:message_timeout"]) < 1000 or int(ekg.config["notify:message_timeout"]) > 9999:
132     timeCheck("notify:message_timeout", ekg.config["notify:message_timeout"])
133 if int(ekg.config["notify:status_timeout"]) < 1000 or int(ekg.config["notify:status_timeout"]) > 9999:
134     timeCheck("notify:status_timeout", ekg.config["notify:status_timeout"])
135
This page took 0.109289 seconds and 3 git commands to generate.