-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyic.py
411 lines (354 loc) · 12.7 KB
/
pyic.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# -*- encoding: utf-8 -*-
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from helpers import multiple_line_end, multiple_line, codes
from dcc import ntop, int2uint4, clean_msg, decompose_dcc_offer, dcc_download
import socket
import select
VERSION = "0.3"
def clean_usr(usr):
if usr.startswith("@"):
return usr[1:]
elif usr.startswith("+"):
return usr[1:]
else:
return usr
def getline(sock, timeout=None):
"""Retrieve a single line from the socket"""
l = ""
while True:
i, o, e = select.select([sock], [], [], timeout)
if timeout is not None and i == []:
l = None
break
else:
r = sock.recv(1)
if (len(r) < 1):
raise Exception("Connection closed")
if (r == "\n"):
break
elif (r != "\r"):
l += r
return l
class split_msg(object):
"""Irc message type containing the basic information"""
by = "" # Sender nickname
origin = "" # Sender info: user@host
to = "" # Receiver (you... or the channel)
private = False # Is client to client
type = "" # Type of message
msg = ""
multiline = False
multiline_end = False
ctcp = False
ctcp_msg = ""
raw = ""
# Constructor
def __init__(self, s):
#":by!origin type to:msg"
if not s:
return None
self.raw = s
block = s.split(' ', 2)
block[0] = block[0][1:]
if ("!" in block[0]):
first = block[0].split('!')
self.by = first[0]
self.origin = first[1]
else:
self.by = block[0]
if block[1].endswith(":"):
clean = block[1][:-1].upper()
else:
clean = block[1].upper()
self.type = clean
if (self.type in multiple_line):
multiline = True
if (self.type in multiple_line_end):
multiline_end = True
if (len(block[2]) > 0):
parts = block[2].split(' ', 1)
self.to = parts[0]
if len(parts) == 2 and parts[1].startswith(":"):
self.msg = parts[1][1:]
elif len(parts) == 2:
self.msg = parts[1]
s = self.msg
i = -1
if (chr(1) in s):
i = s.index(chr(1))
if (i >= 0) and (i <= len(s)):
self.ctcp = True
self.ctcp_msg = s[i + 1:]
if (chr(1) in self.ctcp_msg):
i = self.ctcp_msg.index(chr(1))
else:
return
if (i >= 0) and (i <= len(self.ctcp_msg)):
self.ctcp_msg = self.ctcp_msg[:i]
dcc = decompose_dcc_offer(self.ctcp_msg)
if (dcc is not False):
self.ip = dcc[0]
self.port = dcc[1]
self.file = ""
self.turbo = dcc[3]
self.size = dcc[4]
try:
self.file += dcc[2]
except:
pass
self.type = codes["DCC_SEND_OFFER"]
return
class irc_client(object):
"""Irc Client main class"""
motd = ""
msgBuff = []
def send(self, deliver):
"""Send a message through the socket"""
self.sock.send(deliver + "\r\n")
def get_motd(self):
"""Show the Message Of The Day"""
return self.motd
def away(self, status=None):
if status is None:
self.send("AWAY")
else:
self.send("AWAY " + status)
def set_chanmode(self, channel, mode, data=None):
"""Set channel mode"""
if data is None:
self.send("MODE " + channel + " " + mode)
else:
self.send("MODE " + channel + " " + mode + " " + str(data))
def set_mode(self, mode):
"""Set own user mode"""
self.send("MODE " + self.nick + " " + mode)
def ping(self, to):
"""Send a ping to a server or user"""
self.send("PING " + to)
def collect_who_data(self):
"""Collect WHOIS/WHOWAS data"""
m = self.getmsg(True)
data = {"channels": []}
while (m.type != codes["RPL_ENDOFWHOWAS"]) and \
(m.type != codes["RPL_ENDOFWHOIS"]) and \
(m.type != codes["ERR_NOSUCHNICK"]):
if (m.type == codes["RPL_WHOISUSER"]):
d = m.msg.split(":")
data["real_name"] = d[1]
d = d[0].split(" ")
data["nick"] = d[0]
data["user"] = d[1]
data["host"] = d[2]
elif (m.type == codes["RPL_WHOWASUSER"]):
d = m.msg.split(":")
data["real_name"] = d[1]
d = d[0].split(" ")
data["nick"] = d[0]
data["user"] = d[1]
data["host"] = d[2]
elif (m.type == codes["RPL_WHOISSERVER"]):
d = m.msg.split(":")
data["server_info"] = d[1]
d = d[0].split(" ")
data["server"] = d[1]
elif (m.type == codes["RPL_WHOISOPERATOR"]):
data["isOp"] = True
elif (m.type == codes["RPL_WHOISIDLE"]):
d = m.msg.split(":")
data["idle"] = d[1]
d = d[0].split(" ")
data["time"] = d[1]
elif (m.type == codes["RPL_WHOISCHANNELS"]):
d = m.msg.split(":")
data["channels"] += d[1].strip().split(" ")
else:
self.msgBuff.append(m)
m = self.getmsg(True)
return data
def send_whois(self, user):
"""Ask for user information"""
self.send("WHOIS " + user)
def whois(self, user):
"""Read for user information"""
self.send_whois(user)
data = self.collect_who_data()
return data
def send_whowas(self, user):
"""Ask for user (that no longer exists) information"""
self.send("WHOWAS " + user)
def whowas(self, user):
""" Read for former user information"""
self.send_whowas(user)
data = self.collect_who_data()
return data
def set_topic(self, channel, topic):
"""Set a channel topic"""
self.send("TOPIC " + channel + " :" + topic)
def retr_topic(self, channel):
"""Retrieves a channel topic (has to check messages then)"""
self.send("TOPIC " + channel)
def get_topic(self, channel):
""" Reads the current topic on a channel"""
self.retr_topic(channel)
while (True):
m = self.getmsg(True)
if (m.type == codes["RPL_TOPIC"]):
return m.msg.split(":")[-1]
elif (m.type == codes["RPL_NOTOPIC"]):
return False
self.msgBuff.append(m)
def retr_names(self, channel):
"""Retrieves a nick list"""
self.send("NAMES " + channel)
def get_users(self, channel):
""" Reads the nick list"""
self.retr_names(channel)
data = []
m = self.getmsg(True)
while (m.type != codes["RPL_ENDOFNAMES"]):
if (m.type == codes["RPL_NAMEREPLY"]):
data += m.msg.split(":")[-1].strip().split(" ")
else:
self.msgBuff.append(m)
m = self.getmsg(True)
return data
def retr_channels(self):
"""Retrieves a channel list"""
self.send("LIST")
def get_channels(self):
"""Read the channel list"""
self.retr_channels()
channels = []
m = self.getmsg(True)
while (m.type != codes["RPL_LISTEND"]):
if (m.type == codes["RPL_LISTSTART"]):
pass
elif (m.type == codes["RPL_LIST"]):
if (":" in m.msg):
i = m.msg.index(":")
topic = m.msg[i + 1:]
d = m.msg[: i].strip().split(" ")
if (len(d) > 1):
channels.append((d[0], d[1], topic))
else:
self.msgBuff.append(m)
m = self.getmsg(True)
return channels
def invite(self, nick, channel):
"""Invite someone to a channel"""
self.send("INVITE " + nick + " " + channel)
def kick(self, channel, nick, comment=None):
"""Remove a user from a channel"""
if comment is None:
self.send("KICK " + channel + " " + nick)
else:
self.send("KICK " + channel + " " + nick + " : " + str(comment))
def leave_channel(self, channel):
"""Quit the channel"""
self.send("PART " + channel)
def quit(self, msg="Client quit"):
""" Quit the server"""
self.send("QUIT :" + str(msg))
self.sock.close()
def join(self, channel, passwd=None):
"""Join a channel"""
if channel.startswith("#") or channel.startswith("&"):
if passwd is None:
self.send("JOIN " + channel)
else:
self.send("JOIN " + channel + " " + passwd)
else:
print "Error " + channel + " " + passwd
def change_nick(self, nick):
"""Changes the nickname"""
self.nick = nick
self.send("NICK " + nick)
def refresh_motd(self):
"""Ask for the MOTD"""
self.send("MOTD")
def getmsg(self, fresh=False, timeout=None):
"""Receives a message (and transparently answers to server ping)"""
if (not fresh) and (len(self.msgBuff) > 0):
return self.msgBuff.pop(0)
while True:
s = getline(self.sock, timeout)
if s is None:
return None
if (len(s) > 3):
if (s.lower()[0:4] == "ping"):
self.pong(s)
continue
m = split_msg(s)
if m is None:
return None
else:
if ("version" in m.ctcp_msg.lower()):
self.sendVer(m.by)
continue
elif (m.type == codes["RPL_MOTD"]):
self.motd += m.msg + "\n"
continue
elif (m.type == codes["RPL_MOTDSTART"]):
self.motd = ""
continue
return m
def sendmsg(self, to, msg):
"""Send a message (msg) to a receiver (to), which can
be a channel or a user"""
self.send("PRIVMSG " + to + " :" + msg)
def notice(self, to, msg):
"""Same as sendmsg, but MUSTN'T be answered"""
self.send("NOTICE " + to + " :" + msg)
def pong(self, ping):
"""Answer a ping"""
self.send("PONG" + ping[4:])
def sendVer(self, by):
"""Answer a version request"""
self.notice(by, chr(1) + VERSION + chr(1))
def connect_to_server(self, serverpwd, pwd, use_ssl):
"""Connecting to the server and login"""
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if use_ssl:
import ssl
self.sock = ssl.wrap_socket(self.sock)
self.sock.connect((self.server, self.port))
if (serverpwd is not None):
self.send("PASS " + serverpwd)
self.change_nick(self.nick)
m = self.getmsg()
self.send("USER " + self.username + " " + self.username2 +
" " + self.server + ": " + self.fullname)
while (m.type not in (codes["RPL_ENDOFMOTD"], codes["ERR_NOMOTD"])):
m = self.getmsg()
if (pwd is not None):
self.sendmsg("NickServ", "IDENTIFY " + pwd)
def __init__(self,
nick,
server,
port=6667,
ssl=False,
username="user",
username2="user",
fullname="user Name",
serverpwd=None,
pwd=None):
"""Object constructor"""
self.nick = nick
self.server = server
self.port = port
self.ssl = ssl
self.username = username
self.username2 = username2
self.fullname = fullname
self.connect_to_server(pwd, serverpwd, ssl)