-
Notifications
You must be signed in to change notification settings - Fork 1
/
IrcClient.py
72 lines (61 loc) · 1.78 KB
/
IrcClient.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
import socket, ssl, time
class IRCClient:
tcp_sock = 0
ssl_sock = 0
config = {
"host" : "",
"port" : "",
"server" : "tmi.twitch.tv",
"nick" : "",
"password" : "",
"channel" : ""
}
def __init__(self):
pass
def ssl_send(self, message):
self.ssl_sock.write(message.encode() + b'\r\n')
print(message)
def connect(self, host, port):
self.tcp_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ssl_sock = ssl.wrap_socket(self.tcp_sock)
self.ssl_sock.settimeout(3)
self.ssl_sock.connect((host, port))
self.config["host"] = host
self.config["port"] = port
def disconnect(self):
self.ssl_send('QUIT')
self.ssl_sock.close()
def login(self, nick, password):
self.ssl_send('PASS {}'.format(password))
self.ssl_send('NICK {}'.format(nick))
self.config["nick"] = nick
self.config["password"] = password
def joinChannel(self, channel):
self.ssl_send('JOIN #{}'.format(channel))
self.config["channel"] = channel
def sendMessage(self, message):
self.ssl_send('PRIVMSG #{} :{}'.format(self.config["channel"], message))
def recieveData(self):
try:
data = self.ssl_sock.read().decode('utf-8')
except:
data = ""
if (data.find('PING :' + self.config["server"]) != -1):
self.ssl_send('PONG :' + self.config["server"])
print(data)
return ""
else:
if (data != ""):
print(data)
return data
def parseData(self, data):
data = data.split(' ')
if (data[1] == 'PRIVMSG' and data[2] == '#' + self.config["channel"]):
nick = data[0].split('!')
nick = nick[0][1:]
message = ""
for i in range(3, len(data)):
message += data[i]
message += ' '
return ((nick, message[1:].rstrip()))
return -1