forked from andrew103/irc_bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc.py
41 lines (30 loc) · 1.13 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
import socket
class IRC:
irc = socket.socket()
def __init__(self):
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def send(self, chan, msg):
self.irc.send("PRIVMSG " + chan + " :" + msg + "\n")
def quit(self, msg):
self.irc.send("QUIT " + msg + "\n")
self.irc.close()
def change_nick(self, nick):
self.irc.send("NICK " + nick + "\n")
def connect(self, server, channel, botnick):
# Defines the socket
print("Connecting to: "+server)
# Connects to the server
self.irc.connect((server, 6667))
# User authentication
# (botnick + " ")*3 has been used for code optimization
# as requested in the Pull Request #1
self.irc.send("USER " + (botnick + " ")*3 + ":This is a fun bot!\n")
self.irc.send("NICK " + botnick + "\n")
# Join the channel
self.irc.send("JOIN " + channel + "\n")
def get_text(self):
# Receive the text
text = self.irc.recv(2040)
if text.find('PING') != -1:
self.irc.send('PONG ' + text.split()[1] + '\r\n')
return text