Create a gist now

Instantly share code, notes, and snippets.

anonymous /ircbot.py Secret
Created Feb 21, 2013

What would you like to do?
from twisted.words.protocols import irc
from twisted.internet import protocol, reactor
from twisted.protocols.basic import LineReceiver
from twisted.internet.endpoints import clientFromString, serverFromString
from twisted.protocols import basic
messages = []
class Echo(protocol.Protocol):
def dataReceived(self, data):
messages.append(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
class IRCBot(irc.IRCClient):
def dataReceived(self, data):
global messages
try:
self.say(messages.pop())
except:
pass
basic.LineReceiver.dataReceived(self, data.replace('\r', ''))
@property
def nickname(self):
return self.factory.nickname
def signedOn(self):
self.join(self.factory.channel)
print "Signed on as %s." % (self.nickname,)
def joined(self, channel):
print "Joined %s." % (channel,)
def privmsg(self, user, channel, msg):
"""This function is called when a message is sent to the bot or to a
channel the bot is in"""
print user, msg
def connectionLost(self, reason):
reactor.stop()
def say(self, msg):
self.msg(self.factory.channel, msg)
class IRCBotFactory(protocol.ClientFactory):
protocol = IRCBot
def __init__(self, channel, nickname):
self.channel = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s). Reconnecting" % (reason,)
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment