Skip to content

Instantly share code, notes, and snippets.

@shashi
Created August 2, 2009 13:47
Show Gist options
  • Save shashi/160088 to your computer and use it in GitHub Desktop.
Save shashi/160088 to your computer and use it in GitHub Desktop.
A Neat IRC Logger.
#!/usr/bin/python
# IRC Logger.
# By Shashi Gowda <http://identi.ca/shashi>.
import sys
import socket
import time
import os
HOST="irc.freenode.net"
PORT=6667
NICK="logBot"
IDENT="logBot"
REALNAME="IRC Logger bot by Shashi Gowda"
CHANNELS=["archlinux", "archlinux-offtopic"]
LOGDIR="/var/log/irc"
CONSOLEOUTPUT=False # Print/not
flag=False
readbuffer=""
t=time.time()
t=str(t-int(t))
NICK+=t[-4:]
s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
for c in CHANNELS:
s.send("JOIN #"+c+"\r\n")
def log(line):
logline=""
if(line[0]!=':'):
return True
line=line.split(':',2)
if line[1].find("PRIVMSG")>0:
logline=line[1].split('!')[0] +":\t"+ line[2]
logline=time.asctime().split()[3]+" | " + logline
channel=line[1].split("PRIVMSG",1)[1].strip().strip('#')
if CONSOLEOUTPUT:
print channel +" >>> "+ logline
if not (os.path.exists(LOGDIR+"/"+channel)):
os.makedirs(LOGDIR+"/"+channel)
f=open(LOGDIR+"/"+channel+"/"+time.strftime("%Y-%m-%d")+".txt","a")
f.write(logline+"\n")
f.close()
while 1:
readbuffer=readbuffer+s.recv(1024)
temp=readbuffer.split("\n")
readbuffer=temp.pop( )
for line in temp:
line=line.rstrip( )
log(line)
if(line.find("PING")==0):
s.send("PONG %s\r\n" % line[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment