secret
anonymous / ircbot.py
Created

  • Download Gist
ircbot.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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,)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.