/
notclient.py
executable file
·169 lines (136 loc) · 4.99 KB
/
notclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/python
import optparse, os, time, sys
import indicate
#python-indicate, the reason for this example
from twisted.internet import gtk2reactor
gtk2reactor.install()
from twisted.internet import reactor
from twisted.internet.protocol import ClientFactory, Protocol
from twisted.internet import defer
import os.path, pynotify, gtk, pygtk
notifications = {}
notificationsecs = 5
def parse_args():
usage = """usage: (--port=<port number>) <host>
--port will default to 2346
"""
parser = optparse.OptionParser(usage)
help = "The port to listen on. Default to a random available port."
parser.add_option('--port', type='int', help=help)
options, args = parser.parse_args()
if len(args) != 1:
parser.error('You must specify the host!')
hostname = args[0]
return options, hostname
class WatcherClientProtocol(Protocol):
pinged = False
connected = False
started = True
lastmessage = 0
messageHistory = ""
def dataReceived(self, data):
data = data.replace("\n","")
if (data.find("PONG") != -1):
self.pinged = True
self.started = False
elif not self.started:
print data
where = data[:data.find(' ')]
message = data[data.find(' ')+1:]
addNotification(where)
if where[0] != '#':
message = "Private message"
if ((time.time() - self.lastmessage) < notificationsecs):
self.messageHistory = self.messageHistory + "\n" + message
self.n.update(where, self.messageHistory)
#self.n.update(where, "UPDATED")
self.n.show()
else:
self.messageHistory = message
self.n = pynotify.Notification(where, message)
self.n.set_urgency(pynotify.URGENCY_NORMAL)
self.n.set_timeout(notificationsecs * 1000)
self.n.set_category("device")
helper = gtk.Button()
icon = helper.render_icon(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
self.n.set_icon_from_pixbuf(icon)
self.n.show()
self.lastmessage = time.time()
def connectionMade(self):
print "Client connected"
self.connected = True
self.transport.write("terg\r\n")
self.transport.write("terg\r\n")
from twisted.internet import reactor
reactor.callInThread(self.pingerThread)
def connectionLost(self, reason):
print "Connection lost"
self.connected = False
@defer.inlineCallbacks
def pingerThread(self):
while(self.connected):
self.pinged = False
self.transport.write("PING\r\n")
yield wait(5)
if not self.pinged:
print "Disconnected!"
self.transport.loseConnection()
class AggressiveClientFactory(ClientFactory):
protocol = WatcherClientProtocol
#def __init__(self):
#nothing
@defer.inlineCallbacks
def clientConnectionFailed(self, connector, reason):
print "Client failed, reconnecting"
yield wait(5)
connector.connect()
@defer.inlineCallbacks
def clientConnectionLost(self, connector, reason):
print "Client lost, reconnecting"
yield wait(5)
connector.connect()
def connect(host, port):
from twisted.internet import reactor
def wait(seconds, result=None):
d = defer.Deferred()
from twisted.internet import reactor
reactor.callLater(seconds, d.callback, result)
return d
def main():
options, hostname = parse_args()
pynotify.init("Timekpr notification")
configureIndicate()
from twisted.internet import reactor
factory = AggressiveClientFactory()
reactor.connectTCP(hostname, options.port or 2346, factory)
reactor.run()
def clearNotifications():
nots = dict(notifications)
for item in nots:
notifications[item].set_property("draw-attention","false")
del notifications[item]
del(nots)
def serverClick(*args):
clearNotifications()
def labelClick(indicator, time):
clearNotifications()
def addNotification(who):
if not who in notifications:
notifications[who] = indicate.Indicator()
notifications[who].set_property("name", who)
notifications[who].set_property("count", "1")
notifications[who].label = who
notifications[who].connect("user-display", labelClick)
notifications[who].set_property("draw-attention","true")
notifications[who].show()
else:
amt = int(notifications[who].get_property("count")) + 1
notifications[who].set_property("count", str(amt))
notifications[who].set_property("draw-attention","true")
def configureIndicate():
server = indicate.indicate_server_ref_default()
server.set_type("message.mail")
server.set_desktop_file("/usr/share/applications/notclient.desktop")
server.connect("server-display", serverClick)
if __name__ == '__main__':
main()