-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirclib.py
81 lines (67 loc) · 2.59 KB
/
irclib.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
#!/usr/bin/env python3
# Developed by Rafik Mas'ad (Azag).
# IRCLib 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
# any later version.
#
# IRCLib 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/
import socket
class main():
def __init__(self, HOST, PORT = 6667):
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.irc.connect((HOST, PORT))
self.data_list = []
def identify(self, password, nick, user):
self.irc.send(str.encode('PASS {0}\r\n'.format(password)))
self.irc.send(str.encode('NICK {0}\r\n'.format(nick)))
self.irc.send(str.encode('USER {0} {0} {0} :Python IRC\r\n'.format(user)))
def join(self, channel):
self.irc.send(str.encode('JOIN {0}\r\n'.format(channel)))
def get_data(self):
if not self.data_list:
self.data_list += bytes.decode(self.irc.recv(4096)).split("\r\n")
data = self.data_list[0]
del self.data_list[0]
return data
class commands():
def __init__(self, irc):
self.main = irc
def send(self, receiver, msg):
self.main.irc.send(str.encode("PRIVMSG {0} :{1}\r\n".format(receiver, msg)))
def kick(self, channel, nick):
self.main.irc.send(str.encode("KICK {0} :{1}\r\n".format(channel, nick)))
def get_names(self, channel, bot_nick):
self.main.irc.send(str.encode("NAMES {0} \r\n".format(channel)))
names = []
for data in bytes.decode(self.main.irc.recv(4096)).split("\r\n"):
self.main.data_list += [data]
names += data[data.find("{0} = {1} :".format(bot_nick, channel))+len("{0} = {1} :".format(bot_nick, channel)):].replace("+","").replace("@","").split()
return names
def parse(data):
try:
nick = data[1:data.index("!n=")]
user = data[data.index("!n=")+3:data.index("@")]
ip = data[data.index("@")+1:data.index(" ")]
type = data[data.find(" ")+1:data.find(" ",data.find(" ")+1)+1]
receiver = data[data.find(type)+len(type):data.find(":",2)-1]
msg = data[data.find(":",2)+1:]
return {"nick": nick.strip(),
"user": user.strip(),
"ip": ip.strip(),
"type": type.strip(),
"receiver": receiver.strip(),
"msg": msg}
except ValueError:
return {"nick": "",
"user": "",
"ip": "",
"type": "Other",
"receiver": "",
"msg": data}