Skip to content

Instantly share code, notes, and snippets.

@pjlantz
Created July 13, 2010 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjlantz/473696 to your computer and use it in GitHub Desktop.
Save pjlantz/473696 to your computer and use it in GitHub Desktop.
import socket
from twisted.internet import defer, reactor
class IRC(object):
"""
Implementation of a irc client to do irc based
botnet monitoring
"""
def __init__(self, nick):
"""
Constructor sets up configs, threadManager object
and various booleans used for network registration
when connecting to a IRC server
"""
self.nick = nick
self.continueThread = True
self.doJoin = False
self.firstPing = True
def doConnect(self):
"""
Setup socket and connect to irc server
"""
self.irc = socket.socket()
self.irc.connect(('irc.freenode.net', 6667))
self.irc.send('NICK '+ self.nick + '\r\n')
self.irc.send('USER Spybot SpyBot Spybot :SpyBot\r\n')
self.start()
def __doJoin(self):
"""
Join channel specified in the configuration
"""
self.irc.send('JOIN #testasdf kluczbork\r\n')
def doStop(self):
"""
Stop this thread
"""
self.continueThread = False
self.irc.shutdown(socket.SHUT_RDWR)
self.irc.close()
def start(self):
"""
Make connection and define thread loop
"""
d = self.handleProtocol()
if d != None:
d.addCallback(printData)
reactor.callLater(1, self.start)
def handleProtocol(self):
"""
Handle incoming irc protocol and responses
"""
d = defer.Deferred()
if self.doJoin: # after registration
self.__doJoin()
data = self.irc.recv(1024)
if data.find('PING') != -1: # Ping
self.irc.send('PONG ' + data.split()[1] + '\r\n') # Pong
if self.firstPing:
self.doJoin = True # registered and ready to join
self.firstPing = False
return
elif data.find('TOPIC') != -1: # Topic
# Log topic info
#print "IRC: Got new topic set"
pass
elif data.find('332') != -1: # Current topic
# Log current topic info
#print "IRC: Got current channel topic on join"
pass
elif data.find('PRIVMSG') != -1: # privmsg
# Log privmsg
#print "IRC: Got message"
pass
else:
# log unrecognized commands, can also be MOTD and NAMES list
pass
data = self.nick + ": " + data
reactor.callLater(0, d.callback, data)
return d
def printData(data):
pass
IRC('testing1234').doConnect()
IRC('testing5677').doConnect()
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment