-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.py
341 lines (277 loc) · 11 KB
/
irc.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import socket
import ssl
import sys
from conf import conf
import events
import urk
import ui
import windows
DISCONNECTED = 0
CONNECTING = 1
INITIALIZING = 2
CONNECTED = 3
def parse_irc(msg, server):
msg = msg.split(' ')
# if our very first character is :
# then this is the source,
# otherwise insert the server as the source
if msg and msg[0].startswith(':'):
msg[0] = msg[0][1:]
else:
msg.insert(0, server)
# loop through the msg until we find
# something beginning with :
for i, token in enumerate(msg):
if token.startswith(':'):
# remove the :
msg[i] = msg[i][1:]
# join up the rest
msg[i:] = [' '.join(msg[i:])]
break
# filter out the empty pre-":" tokens and add on the text to the end
return [m for m in msg[:-1] if m] + msg[-1:]
# note: this sucks and makes very little sense, but it matches the BNF
# as far as we've tested, which seems to be the goal
def default_nicks():
try:
nicks = [conf.get('nick')] + conf.get('altnicks',[])
if not nicks[0]:
import getpass
nicks = [getpass.getuser()]
except:
nicks = ["mrurk"]
return nicks
class Network(object):
socket = None
def __init__(self, server="irc.default.org", port=6667, nicks=[],
username="", fullname="", name=None, ssl=None, **kwargs):
self.server = server
self.port = port
self.name = name or server
self.nicks = nicks or default_nicks()
self.me = self.nicks[0]
self.username = username or "urk"
self.fullname = fullname or conf.get("fullname", self.username)
self.password = ''
self.isupport = {
'NETWORK': server,
'PREFIX': '(ohv)@%+',
'CHANMODES': 'b,k,l,imnpstr',
}
self.prefixes = {'o':'@', 'h':'%', 'v':'+', '@':'o', '%':'h', '+':'v'}
self.status = DISCONNECTED
self.failedhosts = [] #hosts we've tried and failed to connect to
self.channel_prefixes = '&#+$' # from rfc2812
self.on_channels = set()
self.requested_joins = set()
self.requested_parts = set()
self.ssl = ssl
self.buffer = ''
def create_socket(self, family, type, proto):
sock = socket.socket(family, type, proto)
if self.ssl is not None:
if self.ssl is True:
context = ssl.create_default_context()
else:
context = self.ssl
sock = context.wrap_socket(sock, server_hostname=self.server)
return sock
#called when we get a result from the dns lookup
def on_dns(self, result, error):
if error:
self.disconnect(error=error[1])
else:
#import os
#import random
#random.seed()
#random.shuffle(result)
if socket.has_ipv6: #prefer ipv6
result = [(f, t, p, c, a) for (f, t, p, c, a) in result if f == socket.AF_INET6]+result
elif hasattr(socket,"AF_INET6"): #ignore ipv6
result = [(f, t, p, c, a) for (f, t, p, c, a) in result if f != socket.AF_INET6]
self.failedlasthost = False
for f, t, p, c, a in result:
if (f, t, p, c, a) not in self.failedhosts:
try:
self.socket = self.create_socket(f, t, p)
except:
__import__('traceback').print_exc()
continue
self.source = ui.fork(self.on_connect, self.socket.connect, a)
self.failedhosts.append((f, t, p, c, a))
if set(self.failedhosts) >= set(result):
self.failedlasthost = True
break
else:
self.failedlasthost = True
if len(result):
self.failedhosts[:] = (f, t, p, c, a),
f, t, p, c, a = result[0]
try:
self.socket = self.create_socket(f, t, p)
self.source = ui.fork(self.on_connect, self.socket.connect, a)
except:
self.disconnect(error="Couldn't find a host we can connect to")
else:
self.disconnect(error="Couldn't find a host we can connect to")
#called when socket.open() returns
def on_connect(self, result, error):
if error:
self.disconnect(error=error[1])
#we should immediately retry if we failed to open the socket and there are hosts left
if self.status == DISCONNECTED and not self.failedlasthost:
windows.get_default(self).write("* Retrying with next available host")
self.connect()
else:
self.source = source = ui.Source()
self.status = INITIALIZING
self.failedhosts[:] = ()
events.trigger('SocketConnect', network=self)
if source.enabled:
self.source = ui.fork(self.on_read, self.socket.recv, 8192)
#called when we read data or failed to read data
def on_read(self, result, error):
if error:
self.disconnect(error=error[1])
elif not result:
self.disconnect(error="Connection closed by remote host")
else:
self.source = source = ui.Source()
self.buffer = (self.buffer + result).split("\r\n")
for line in self.buffer[:-1]:
self.got_msg(line)
if self.buffer:
self.buffer = self.buffer[-1]
else:
self.buffer = ''
if source.enabled:
self.source = ui.fork(self.on_read, self.socket.recv, 8192)
def raw(self, msg):
events.trigger("OwnRaw", network=self, raw=msg)
if self.status >= INITIALIZING:
self.socket.send(msg + "\r\n")
def got_msg(self, msg):
pmsg = parse_irc(msg, self.server)
e_data = events.data(
raw=msg,
msg=pmsg,
text=pmsg[-1],
network=self,
window=windows.get_default(self)
)
if "!" in pmsg[0]:
e_data.source, e_data.address = pmsg[0].split('!',1)
else:
e_data.source, e_data.address = pmsg[0], ''
if len(pmsg) > 2:
e_data.target = pmsg[2]
else:
e_data.target = pmsg[-1]
events.trigger('Raw', e_data)
def connect(self):
if not self.status:
self.status = CONNECTING
self.source = ui.fork(self.on_dns, socket.getaddrinfo, self.server, self.port, 0, socket.SOCK_STREAM)
events.trigger('Connecting', network=self)
def disconnect(self, error=None):
if self.socket:
self.socket.close()
if self.source:
self.source.unregister()
self.source = None
self.socket = None
self.status = DISCONNECTED
#note: connecting from onDisconnect is probably a Bad Thing
events.trigger('Disconnect', network=self, error=error)
#trigger a nick change if the nick we want is different from the one we
# had.
if self.me != self.nicks[0]:
events.trigger(
'Nick', network=self, window=windows.get_default(self),
source=self.me, target=self.nicks[0], address='',
text=self.nicks[0]
)
self.me = self.nicks[0]
def norm_case(self, string):
return string.lower()
def quit(self, msg=None):
if self.status:
try:
if msg == None:
msg = conf.get('quitmsg', "%s - %s" % (urk.long_version, urk.website))
self.raw("QUIT :%s" % msg)
except:
pass
self.disconnect()
def join(self, target, key='', requested=True):
if key:
key = ' '+key
self.raw("JOIN %s%s" % (target,key))
if requested:
for chan in target.split(' ',1)[0].split(','):
if chan == '0':
self.requested_parts.update(self.on_channels)
else:
self.requested_joins.add(self.norm_case(chan))
def part(self, target, msg="", requested=True):
if msg:
msg = " :" + msg
self.raw("PART %s%s" % (target, msg))
if requested:
for chan in target.split(' ',1)[0].split(','):
self.requested_parts.add(self.norm_case(target))
def msg(self, target, msg):
self.raw("PRIVMSG %s :%s" % (target, msg))
events.trigger(
'OwnText', source=self.me, target=str(target), text=msg,
network=self, window=windows.get_default(self)
)
def notice(self, target, msg):
self.raw("NOTICE %s :%s" % (target, msg))
events.trigger(
'OwnNotice', source=self.me, target=str(target), text=msg,
network=self, window=windows.get_default(self)
)
#a Network that is never connected
class DummyNetwork(Network):
def __nonzero__(self):
return False
def __init__(self):
Network.__init__(self)
self.name = self.server = self.isupport['NETWORK'] = "None"
def connect(self):
raise NotImplementedError, "Cannot connect dummy network."
def raw(self, msg):
raise NotImplementedError, "Cannot send %s over the dummy network." % repr(msg)
dummy_network = DummyNetwork()
#this was ported from srvx's tools.c
def match_glob(text, glob, t=0, g=0):
while g < len(glob):
if glob[g] in '?*':
star_p = q_cnt = 0
while g < len(glob):
if glob[g] == '*':
star_p = True
elif glob[g] == '?':
q_cnt += 1
else:
break
g += 1
t += q_cnt
if t > len(text):
return False
if star_p:
if g == len(glob):
return True
for i in xrange(t, len(text)):
if text[i] == glob[g] and match_glob(text, glob, i+1, g+1):
return True
return False
else:
if t == len(text) and g == len(glob):
return True
if t == len(text) or g == len(glob) or text[t] != glob[g]:
return False
t += 1
g += 1
return t == len(text)