Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created December 9, 2011 22:54
Show Gist options
  • Select an option

  • Save enjalot/1453687 to your computer and use it in GitHub Desktop.

Select an option

Save enjalot/1453687 to your computer and use it in GitHub Desktop.
simple irc logging script
#!/usr/bin/env python
"""
# A simple IRC Bot, that logs conversation in an IRC Channel
#
# (C) 2008, Pranav Prakash
# Email: [email protected]
#
#
#
# Modified by Ian 'enjalot' Johnson
# Email: [email protected]
#
#
#
# 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 <http://www.gnu.org/licenses/>.
"""
import string, socket, re
from datetime import datetime
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
#self.p = pars.Parser() # Parser
#self.a = act.Action() # Action to be taken
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
self.joined = 0
if self.join == 1:
self.sock.send('JOIN '+self.channel+'\n')
while loop ==1:
t = self.sock.recv(512)
self.process(t)
if self.joined == 0:
self.joined = 1
else:
print "Not ready to join"
def disconnect(self):
if self.connected == 1:
self.sock.close()
def command(self, text):
msg = text.split(":")[-1].strip()
if msg == "names":
cmd = 'NAMES %s\n' % (self.channel.strip())
self.sock.send(cmd)
return True
if msg == "join":
cmd = 'JOIN #d3test1\n'
print "cmd: ", cmd
self.sock.send(cmd)
return True
return False
def process(self, text):
#Case: PING
if text.find("PING :") != -1:
"""Just recieved a PING request from the sever
Hence, bot responds back by PONG message"""
self.sock.send("PONG :" + re.split(":", text)[1]+"\n")
return False
#Case: Join
#We send a NAMES command everytime someone joins or leaves. better safe than sorry!
if text.find("JOIN") != -1:
cmd = 'NAMES %s\n' % (self.channel.strip())
self.sock.send(cmd)
#Case: Quit
if text.find("QUIT") != -1:
cmd = 'NAMES %s\n' % (self.channel.strip())
self.sock.send(cmd)
#Case: Mention
#Case: command
if self.command(text):
print "COMMAND ME"
self.save(text)
def save(self, text):
t = datetime.utcnow()
#Filename by day
filename = "d3.js_%d-%.2d-%.2d" % (t.year, t.month, t.day)
#print "filename", filename
log = open(filename, 'a')
#Add timestamp
text = "%s|: %s" % (t, text)
print text
log.write(text)
if __name__ == '__main__':
bot = Bot('irc.freenode.net', 6667, 'visbot', 'visbot', 'visbot')
bot.connect()
bot.joinChannel('#d3.js')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment