Tuesday 8 April 2008

Simple IRC Logging Bot in Python

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')

6 comments:

  1. How do i make it respond to its name ?
    If someone mentions its name , it could respond as " Hi' my master (name) is away, he'll be back here at XX hrs !"
    something like that

    ReplyDelete
  2. For responding, i have added another module, which is available at here

    ReplyDelete
  3. Pranny,
    I dont have this parser module for python.
    I searched for it everymhere, but couldnt find it.

    ReplyDelete
  4. You will get the "parser.py" in the source code only

    ReplyDelete

  5. Connect to any IRC chat server @ http://www.ChatPhobia.com (no software to install, web based applet that will work on any web browser with JAVA).

    ReplyDelete
  6. Perfect, thank you. It's much easier for me to learn with simple examples of actual stuff that I actually want to do.

    ReplyDelete