Skip to content

Instantly share code, notes, and snippets.

@psykzz
Created January 15, 2015 00:13
Show Gist options
  • Save psykzz/836632fb12f336fcb159 to your computer and use it in GitHub Desktop.
Save psykzz/836632fb12f336fcb159 to your computer and use it in GitHub Desktop.
IRC bot
import socket
import sys
class IRC:
socket = socket.socket()
read_buffer = ''
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def raw(self, *message):
_message = ' '.join(message)
print _message
self.socket.send(_message+'\r\n')
def send(self, chan, msg):
self.raw("PRIVMSG", chan, msg)
def connect(self, server, channel, nickname):
server, port = server.split(':', 1)
# Lets check we got a port
port = int(port) or 6667
#defines the socket
print "connecting to:", server, port
self.socket.connect((server, port)) #connects to the server
self.raw("PASS", nickname)
self.raw("NICK", nickname)
self.raw("USER", nickname, server, "blah :blahblah") #user authentication
self.raw("JOIN", channel) #join the chan
def process(self):
# Deal with the buffer
self.read_buffer = self.read_buffer + self.socket.recv(1024) #receive the text
temp = self.read_buffer.split('\n')
self.read_buffer = temp.pop()
for line in temp:
line = self.process_line(line)
if line:
yield line
def process_line(self, line):
line = line.rstrip()
line = line.split()
if line[0] == "PING":
self.raw('PONG', line[1])
else:
return ' '.join(line)
if __name__ == '__main__':
channel = "#psykzz"
server = "irc.netgamers.org:6667"
nickname = "ThatBot"
irc = IRC()
irc.connect(server, channel, nickname)
text = irc.process()
for message in text:
print message
if "PRIVMSG" in message and channel in message and "hello" in message:
irc.send(channel, "Hello!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment