-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirc.py
268 lines (209 loc) · 8.32 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
#!/usr/bin/python
# vim: set fileencoding=utf-8 :
import socket
import string
import sys
import time
IRCVERSION = u'python Irc'
DEBUG_IRC = False
class Irc:
def __init__(self, server, port, nick, ident, realname):
self.server = server
self.port = port
self.nick = nick
self.ident = ident
self.realname = realname
self.onRawHandlers = []
self.onConnectedHandlers = []
self.onTextHandlers = []
self.onCtcpHandlers = []
self.onJoinHandlers = []
self.onPartHandlers = []
self.onQuitHandlers = []
self.connected = False
self.reconnectWait = 10
self.wantNick = nick
def connect(self):
self.__connect_to_server(self.server, self.port, self.nick,
self.ident, self.realname)
self.state = self.stateConnect
print 'connected'
def __connect_to_server(self, server, port, nick, ident, realname):
self.server = server
self.port = port
self.nick = nick
self.ident = ident
self.realname = realname
def add_on_raw(self, obj):
self.add_on_raw_handler(obj)
return obj
def add_on_raw_handler(self, obj):
self.onRawHandlers.append(obj)
def add_on_connected(self, obj):
self.add_on_connected_handler(obj)
return obj
def add_on_connected_handler(self, obj):
self.onConnectedHandlers.append(obj)
def add_on_text(self, obj):
self.add_on_text_handler(obj)
return obj
def add_on_text_handler(self, obj):
self.onTextHandlers.append(obj)
def add_on_ctcp_handler(self, obj):
self.onCtcpHandlers.append(obj)
def add_on_join_handler(self, obj):
self.onJoinHandlers.append(obj)
def add_on_part_handler(self, obj):
self.onPartHandlers.append(obj)
def __call_on_raw_handlers(self, text):
for obj in self.onRawHandlers:
obj(self, text)
def __call_on_connected_handlers(self, server):
for obj in self.onConnectedHandlers:
obj(self, server)
def __call_on_text_handlers(self, msgfrom, target, text):
for obj in self.onTextHandlers:
obj(self, msgfrom, target, text)
def __call_on_ctcp_handlers(self, msgfrom, target, text):
for obj in self.onCtcpHandlers:
obj(self, msgfrom, target, text)
def __call_on_join_handlers(self, who, channel):
for obj in self.onJoinHandlers:
obj(self, who, channel)
def __call_on_part_handlers(self, who, channel):
for obj in self.onPartHandlers:
obj(self, who, channel)
def __call_on_quit_handlers(self, who, message):
for obj in self.onQuitHandlers:
obj(self, who, message)
def __on_raw(self, text):
if (DEBUG_IRC):
print(text)
self.__call_on_raw_handlers(text)
def __on_connected(self, server):
self.__call_on_connected_handlers(server)
def __on_ctcp(self, msgfrom, target, text):
#can't rely on handlers to implement this, it's
#required or some servers might boot us off
if (text == chr(1) + 'VERSION' + chr(1)):
self.send_ctcp_msg(msgfrom, 'VERSION ' + IRCVERSION)
self.__call_on_ctcp_handlers(msgfrom, target, text)
def __on_text(self, msgfrom, target, text):
if (self.__is_ctcp_msg(text)):
self.__on_ctcp(msgfrom, target, text)
else:
self.__call_on_text_handlers(msgfrom, target, text)
def __on_join(self, who, channel):
self.__call_on_join_handlers(who, channel)
def __on_part(self, who, channel):
self.__call_on_part_handlers(who, channel)
def __on_quit(self, who, message):
if who == self.wantNick:
self.send(u'NICK ' + self.wantNick)
self.__call_on_quit_handlers(who, message)
def __is_ctcp_msg(self, msg):
if (len(msg) > 0):
if (msg[0] == chr(1) and msg.endswith(chr(1))):
return True
return False
def join(self, channel):
self.send(u'JOIN ' + channel)
def part(self, channel):
self.send(u'PART ' + channel)
def send_msg(self, target, text):
self.send(u'PRIVMSG ' + target + u' :' + text)
def send_ctcp_msg(self, target, command):
reply = u'NOTICE ' + target + u' :' + chr(1) + command + chr(1)
self.send(reply)
def send(self, text):
if (DEBUG_IRC):
print(u'-> ' + text)
self.s.send(text + u'\r\n')
def stateConnect(self):
try:
print(u'Connecting to %s' % self.server)
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((self.server, self.port))
self.state = self.stateConnecting
except socket.error:
print(u'Unable to connect to %s' % self.server)
self.s.close()
self.state = self.stateReconnect
def stateConnecting(self):
self.send(u'NICK %s' % self.nick)
self.send(u'USER %s %s %s :%s' % (self.ident, self.server,
self.nick, self.realname))
self.readbuf = u''
self.connected = False
nickTemp = 0;
try:
while not self.connected:
self.readbuf = (self.readbuf +
unicode(self.s.recv(2048), u'ISO-8859-1'))
temp = string.split(self.readbuf, u'\n');
self.readbuf = temp.pop();
for line in temp:
line = string.rstrip(line)
fullline = line
line = string.split(line)
if fullline.find(u'Nickname is already in use') != -1:
self.nick = self.wantNick + str(nickTemp)
nickTemp += 1
self.send(u'NICK %s' % self.nick)
self.send(u'USER %s %s %s :%s' % (self.ident,
self.server, self.nick, self.realname))
if (line[1] == u'001'):
self.connected = True
self.__on_connected(self.server)
self.state = self.stateConnected
except socket.error:
print(u'Lost connection to %s' % self.server)
self.s.close()
self.state = self.stateReconnect
def stateConnected(self):
try:
#this next 3 lines of apparent madness are to
#ensure we only parse full lines. We have no
#guarantee that each chunk of data coming in
#will be in any way complete...
self.readbuf = (self.readbuf +
unicode(self.s.recv(2048), u'ISO-8859-1'))
temp = unicode.split(self.readbuf, u'\n');
self.readbuf = temp.pop();
for line in temp:
line = unicode.rstrip(line)
fullline = line
line = unicode.split(line)
if (line[0] == u'PING'):
self.send(u'PONG %s' % line[1])
self.__on_raw(fullline)
msgfrom = line[0].split(u'!')[0].strip(u':')
if (line[1] == u'PRIVMSG'):
target = line[2]
if (target == self.nick):
target = u''
msg = u' '.join(line[3:])[1:]
self.__on_text(msgfrom, target, msg)
if (line[1] == u'JOIN'):
channel = line[2][1:]
self.__on_join(msgfrom, channel)
if (line[1] == u'PART'):
channel = line[2]
self.__on_part(msgfrom, channel)
if (line[1] == u'QUIT'):
msg = u' '.join(line[2:])[1:]
self.__on_quit(msgfrom, msg)
except socket.error:
print(u'Lost connection to %s' % self.server)
self.s.close()
self.state = self.stateReconnect
def stateDisconnected(self):
print(u'Disconnected from %s' % self.server)
self.state = self.stateReconnect
def stateReconnect(self):
time.sleep(self.reconnectWait)
print(u'Reconnecting to %s' % self.server)
self.state = self.stateConnect
def process(self):
while 1:
self.state()