-
Notifications
You must be signed in to change notification settings - Fork 0
/
pirc_gtk.py
executable file
·230 lines (180 loc) · 7.61 KB
/
pirc_gtk.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import gobject
from libpirc.connection import Connection
from libpirc.world import World
from test import Test
class ChatWidget(gtk.VBox):
def __init__(self):
super(ChatWidget, self).__init__(False)
self.logbox = gtk.TextView()
self.logbox.set_editable(False)
self.logbox.set_cursor_visible(False)
self.logbox.set_wrap_mode(gtk.WRAP_WORD)
self.logbox.show()
self.logscrollbox = gtk.ScrolledWindow()
self.logscrollbox.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
self.logscrollbox.add(self.logbox)
self.logscrollbox.show()
self.nickbox = gtk.TreeView(gtk.ListStore(gobject.TYPE_STRING))
self.nickbox.set_headers_visible(False)
nick_col = gtk.TreeViewColumn('Nick')
# TODO: figure out what the shit is going on here
cell_renderer = gtk.CellRendererText()
nick_col.pack_start(cell_renderer)
nick_col.set_attributes(cell_renderer, text=0)
self.nickbox.append_column(nick_col)
self.nickbox.show_all()
self.entry = gtk.Entry()
self.entry.show()
def _writeline(self, str):
vadj = self.logscrollbox.get_vadjustment()
print 'vadj upper/lower/page_size/value is {0}/{1}/{2}/{3}'.format(vadj.upper,vadj.lower,vadj.page_size,vadj.value)
scrollbar_was_at_bottom = (vadj.value + vadj.page_size >= vadj.upper)
print scrollbar_was_at_bottom
str = str.rstrip('\r\n')
buf = self.logbox.get_buffer()
iter = buf.get_end_iter()
buf.insert(iter, str + '\r\n')
if scrollbar_was_at_bottom:
vadj.set_value(vadj.upper)
class ServerWidget(ChatWidget):
def __init__(self, connection):
super(ServerWidget, self).__init__()
self.entry.connect("activate", self.handle_text_entered)
self.connection = connection
# and also subscribe to connection's events here!
self.connection.received += self.handle_received
self.connection.sent += self.handle_sent
self.pack_start(self.logscrollbox)
self.pack_start(self.entry, False)
def handle_text_entered(self, widget, data=None):
words = self.entry.get_text().split(' ')
if words[0] == '/server':
self.connection.connect(words[1], 6667, 'pi') #hack
else:
self.connection.writeline(self.entry.get_text())
self.entry.set_text('')
def handle_received(self, msg):
gobject.idle_add(self._writeline, (' >> ' + msg))
def handle_sent(self, msg):
gobject.idle_add(self._writeline, ('<< ' + msg))
class ChannelWidget(ChatWidget):
def __init__(self, channel):
super(ChannelWidget, self).__init__()
self.channel = channel
# and also subscribe to channel's events here!
self.channel.msg_received += self.handle_msg_received
self.channel.nick_added += self.handle_nick_added
self.channel.nick_removed += self.handle_nick_removed
self.channel.nick_changed += self.handle_nick_changed
# self.channel.someone_kicked += self.handle_someone_kicked
# self.channel.someone_joined += self.handle_someone_joined
# self.channel.someone_parted += self.handle_someone_parted
# self.channel.someone_quit += self.handle_someone_quit
self.log_and_nicks_box = gtk.HPaned()
self.log_and_nicks_box.add1(self.logscrollbox)
self.log_and_nicks_box.add2(self.nickbox)
self.log_and_nicks_box.set_position(300) # hack
self.log_and_nicks_box.show()
self.pack_start(self.log_and_nicks_box)
self.pack_start(self.entry, False)
def handle_msg_received(self, channel, user, msg_text):
gtk.gdk.threads_enter()
try:
self._writeline('<{0}> {1}'.format(user.nick, msg_text))
finally:
gtk.gdk.threads_leave()
def handle_nick_added(self, channel, nick):
gtk.gdk.threads_enter()
try:
self._add_nick_to_nickbox(nick)
finally:
gtk.gdk.threads_leave()
def handle_nick_removed(self, channel, nick):
gtk.gdk.threads_enter()
try:
self._remove_nick_from_nickbox(nick)
finally:
gtk.gdk.threads_leave()
def handle_nick_changed(self, channel, old_nick, new_nick):
gtk.gdk.threads_enter()
try:
self._change_nick_in_nickbox(old_nick, new_nick)
self._writeline('{0} is now known as {1}'.format(old_nick, new_nick))
finally:
gtk.gdk.threads_leave()
def _add_nick_to_nickbox(self, nick):
print 'adding nick to box'
self.nickbox.get_model().append([nick])
print 'added nick to box'
def _remove_nick_from_nickbox(self, nick):
matching_iters = []
self.nickbox.get_model().foreach(self.iter_matches_nick, (matching_iters, nick))
self.nickbox.get_model().remove(matching_iters[0])
def _change_nick_in_nickbox(self, old_nick, new_nick):
matching_iters = []
self.nickbox.get_model().foreach(self.iter_matches_nick, (matching_iters, old_nick))
self.nickbox.get_model().set(matching_iters[0], 0, new_nick)
def iter_matches_nick(self, model, path, iter, (matching_iters, nick_sought)):
nick = self.nickbox.get_model().get_value(iter, 0)
if nick == nick_sought:
print 'found matching nick'
matching_iters.append(iter)
return False
class PircGtk:
def delete_event(self, widget, event, data=None):
print "delete event occurred"
return False
def destroy(self, widget, data=None):
print "destroy signal occurred"
self.world.shut_it_down()
gtk.main_quit()
def handle_connection_added(self, world, new_connection):
gobject.idle_add(self.make_server_tab, new_connection)
def handle_channel_opened(self, world, new_channel):
#print 'handling channel opened by idle_adding make_channel_tab'
#gobject.idle_add(self.make_channel_tab, new_channel)
gtk.gdk.threads_enter()
try:
self.make_channel_tab(new_channel)
finally:
gtk.gdk.threads_leave()
def make_channel_tab(self, channel):
channel_widget = ChannelWidget(channel)
channel_widget.show()
self.notebook.append_page(channel_widget, gtk.Label(channel.name))
#TODO: insert it immediately after the right server tab
self.notebook.set_current_page(self.notebook.page_num(channel_widget))
def make_server_tab(self, connection):
chat_widget = ServerWidget(connection)
chat_widget.show()
self.notebook.append_page(chat_widget, gtk.Label('a server'))
def __init__(self, world):
self.world = world
self.world.connection_added += self.handle_connection_added
self.world.channel_opened += self.handle_channel_opened
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.delete_event)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.window.set_default_size(640, 480)
self.notebook = gtk.Notebook()
self.notebook.show()
self.window.add(self.notebook)
self.window.show()
def main(self):
gtk.gdk.threads_init()
gtk.main()
if __name__ == "__main__":
world = World()
pirc_gtk = PircGtk(world)
conn = world.new_connection()
conn.connect('irc.whatnet.org', 6667, 'pi')
try:
pirc_gtk.main()
except KeyboardInterrupt:
pirc_gtk.world.shut_it_down()
raise