This is a simple IRC chats logging bot. ALl it does is to connect to an IRC Channel, and log the conversations in real time, in a raw manner. The code presented here is just a proof of concept. For actual code, please visit http://code.google.com/p/pikudotbot/.
#!/usr/bin/env python
"""
# A simple IRC Bot, that logs conversation in an IRC Channel
#
# (C) 2008, Pranav Prakash
#
# Email: pranny@gmail.com
#
#
# This program 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
# (at your option) any later version.
#
# This program 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 .
"""
import sys, string, socket
class Bot(object):
"A Simple Bot class"
"""This is the bot class, and the actual bots will be instances of
this class"""
def __init__(self, HOST, PORT, NICK, REALNAME, IDENTITY):
self.host = HOST
self.port = PORT
self.nick = NICK
self.realname = REALNAME
self.identity = IDENTITY
self.connected = 0 # disconnected by default
self.sock = socket.socket()
self.join = 0
def connect(self, HOST = 'irc.freenode.net', PORT = 6667):
loop = 1
if self.connected == 1:
print "Disconnecting from %s:%s" %(self.host, self.port)
self.connected = 0
self.host = HOST
self.port = PORT
self.sock.close()
else:
print "Connecting to %s:%s" %(self.host, self.port)
self.sock.connect((self.host, self.port))
self.sock.send('NICK ' + self.nick + '\n')
self.sock.send('USER ' + self.identity + ' ' + self.host + ' blah :' + self.realname+'\n')
while loop == 1:
t = self.sock.recv(512)
print t
if t.find('PRIVMSG') != -1:
self.join = 1 # ready to Join a channel
return
if len(t) < 1:
loop = 0
self.join = 0 # not ready to join a channel
def joinChannel(self, CHANNEL):
self.channel = CHANNEL
loop = 1
print self.join
if self.join == 1:
self.sock.send('JOIN '+self.channel+'\n')
while loop ==1:
t = self.sock.recv(512)
print t
if t.find('privmsg') != -1 or t.find('PRIVMSG') != -1:
logIt(t)
else:
print "Not ready to join"
def disconnect(self):
if self.connected == 1:
self.sock.close()
def logIt(text):
f = open('logfile','a')
f.write(text)
f.close()
if __name__ == '__main__':
piku = Bot('irc.freenode.net', 6667, 'piku_b02', 'piku_b02', 'piku_b02')
piku.connect()
piku.joinChannel('#orkut_linux')