I have tidied up my initial experimentations into a class named ‘Bot’ and added the ability to define ‘commands’ on the fly. Although a long way from being a useful bot, an amusing example of what python can do quickly.
The code follows:
#!/usr/bin/env python
import sys,socket,string,time
class Bot:
responses = {
'WHOAREYOU': 'I am the bot!',
'HELLO': 'Hello, how are you?'
}
def __init__(self, host, port, nick, ident):
self.host = host
self.port = port
self.nick = nick
self.ident = ident
self.socket = socket.socket()
def connect(self):
self.socket.connect((self.host, self.port))
time.sleep(0.2)
self.socket.send("NICK %s\r\n" % self.nick)
time.sleep(0.2)
self.socket.send("USER %s %s bla :%s\r\n" % (
self.ident, self.host,
"the bot called %s" % self.nick
))
def disconnect(self):
print "Quitting..."
self.socket.send ("QUIT\r\n")
def join(self, channel):
print "==> Joining %s" % channel
self.socket.send("JOIN %s\r\n" % channel)
def leave(self, channel, message = 'Bye Bye'):
self.socket.send("PART %s :%s\r\n" % (channel, message))
def pong(self, data):
self.socket.send("PONG %s\r\n" % data)
def handleMessage(self, channel, nick, message):
print "%s on %s: %s" % (nick, channel, message)
if(message[0] == '!'):
print "command=>"
dat = message.split()
self.handleCommand(channel, nick, dat[0], dat[1:])
def addResponse(self, command, response):
self.responses[command] = ' '.join(response)
def handleCommand(self, channel, nick, command, arguments):
command = command[1:]
if command == 'BYE':
self.disconnect()
exit()
elif command == 'JOIN':
self.join(arguments[0])
elif command == 'LEAVE':
self.leave(channel)
elif command == 'IDENTIFY':
self.reply('NickServ', '', "IDENTIFY %s" % arguments[0])
elif command == 'DEFINE':
self.addResponse(arguments[0], arguments[1:])
elif command in self.responses:
self.reply(channel, nick, self.responses[command])
def reply(self, channel, nick, message):
self.socket.send("PRIVMSG %s %s: %s\r\n" % (channel, nick, message))
def flush(self):
print self.socket.recv(4096)
def mainloop(self):
readbuffer = ''
while 1:
data = self.socket.recv(1024)
if data:
print data
readbuffer=readbuffer+data
temp=string.split(readbuffer, "\n")
readbuffer=temp.pop()
for line in temp:
line = line.rstrip()
line = line.split()
#handle possibilities
print "line ##'%s'##" % line
if line[0] == "PING":
self.pong(line[1])
elif line[1] == "PRIVMSG":
channel = line[2]
nick = line[0].split('!')[0]
message = ' '.join(line[3:])[1:]
self.handleMessage(channel, nick, message)
if __name__ == '__main__':
bot = Bot('irc.freenode.net', 6667, 'BavBot', 'bavbot')
try:
bot.connect()
bot.flush()
time.sleep(0.2)
#bot.join('##bavardage')
bot.mainloop()
except:
print "interrupt"
print sys.exc_info()[0]
bot.disconnect()
raise
Commands:
- !IDENTIFY password – identifies the nickname
- !JOIN channel – joins the specified channel
- !LEAVE channel – leaves the specified channel
- !QUIT – disconnects from the IRC server
- !DEFINE command response – sets up a response to the specified command (give the command without the preceding ‘!’)

No comments yet
Comments feed for this article