2012-03-02 04:31:57 +00:00
|
|
|
from twisted.words.protocols import irc
|
|
|
|
from twisted.internet import reactor, protocol
|
|
|
|
from twisted.python import log
|
2012-03-02 17:24:25 +00:00
|
|
|
from twisted.internet.endpoints import clientFromString
|
|
|
|
from signal import signal, SIGINT
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
log.startLogging(sys.stdout)
|
|
|
|
|
|
|
|
__version__ = "0.2"
|
|
|
|
|
2012-03-02 18:09:00 +00:00
|
|
|
class Communicator:
|
|
|
|
def __init__(self):
|
|
|
|
self.protocolInstances = {}
|
|
|
|
|
|
|
|
def register(self, protocol):
|
|
|
|
self.protocolInstances[protocol.identifier] = protocol
|
|
|
|
|
|
|
|
def unregister(self, protocol):
|
|
|
|
del self.protocolInstances[protocol.identifier]
|
|
|
|
|
|
|
|
def relay(self, protocol, message):
|
|
|
|
for identifier in self.protocolInstances.keys():
|
|
|
|
if identifier == protocol.identifier:
|
|
|
|
continue
|
|
|
|
instance = self.protocolInstances[identifier]
|
|
|
|
instance.twoWaySay(message)
|
|
|
|
|
|
|
|
#Global scope: all protocol instances will need this.
|
|
|
|
communicator = Communicator()
|
|
|
|
|
2012-03-02 04:31:57 +00:00
|
|
|
class IRCRelayer(irc.IRCClient):
|
2012-03-02 17:24:25 +00:00
|
|
|
realname = "Relay P. Botternson"
|
|
|
|
username = "RelayBot"
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 20:05:09 +00:00
|
|
|
def __init__(self, name, network, channel, identifier, privMsgResponse):
|
2012-03-02 04:31:57 +00:00
|
|
|
self.network = network
|
2012-03-02 17:24:25 +00:00
|
|
|
self.channel = channel
|
2012-03-02 04:31:57 +00:00
|
|
|
self.nickname = name
|
2012-03-02 18:09:00 +00:00
|
|
|
self.identifier = identifier
|
2012-03-02 20:05:09 +00:00
|
|
|
self.privMsgResponse = privMsgResponse
|
|
|
|
log.msg("IRC Relay created. Name: %s | Host: %s | Channel: %s"%(name, network, channel))
|
2012-03-02 17:24:25 +00:00
|
|
|
|
2012-03-02 20:59:26 +00:00
|
|
|
#TODO: Possible override by FLIP bot to remove _## suffix.
|
|
|
|
def formatUsername(self, username):
|
|
|
|
return "[%s]"%username
|
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
def relay(self, message):
|
2012-03-02 18:09:00 +00:00
|
|
|
communicator.relay(self, message)
|
2012-03-02 14:31:43 +00:00
|
|
|
|
2012-03-02 04:31:57 +00:00
|
|
|
def signedOn(self):
|
2012-03-02 20:59:26 +00:00
|
|
|
log.msg("[%s] Connected to network."%self.network)
|
2012-03-02 17:24:25 +00:00
|
|
|
self.join(self.channel, "")
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def connectionLost(self, reason):
|
2012-03-02 20:59:26 +00:00
|
|
|
log.msg("[%s] Connection lost, unregistering."%self.network)
|
2012-03-02 18:09:00 +00:00
|
|
|
communicator.unregister(self)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
def twoWaySay(self, message, args=None):
|
|
|
|
self.say(self.channel, message)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def joined(self, channel):
|
2012-03-02 20:59:26 +00:00
|
|
|
log.msg("Joined channel %s, registering."%channel)
|
2012-03-02 18:09:00 +00:00
|
|
|
communicator.register(self)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def privmsg(self, user, channel, message):
|
|
|
|
user = user.split("!")[0]
|
2012-03-02 20:59:26 +00:00
|
|
|
#If someone addresses the bot directly, respond in the same way.
|
|
|
|
if channel == self.nickname:
|
|
|
|
log.msg("Recieved privmsg from %s."%user)
|
2012-03-02 17:24:25 +00:00
|
|
|
self.msg(user, self.privMsgResponse)
|
2012-03-02 20:59:26 +00:00
|
|
|
else:
|
|
|
|
self.relay("%s %s"%(self.formatUsername(user), message))
|
|
|
|
if message.startswith(self.nickname + ':'):
|
|
|
|
self.say(self.channel, self.privMsgResponse)
|
|
|
|
#For consistancy, if anyone responds to the bot's response:
|
|
|
|
self.relay("%s %s"%(self.formatUsername(self.nickname), self.privMsgResponse))
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def kickedFrom(self, channel, kicker, message):
|
2012-03-02 18:09:00 +00:00
|
|
|
log.msg("Kicked by %s. Message \"%s\""%(kicker, message))
|
|
|
|
communicator.unregister(self)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def userJoined(self, user, channel):
|
2012-03-02 20:59:26 +00:00
|
|
|
self.relay("%s joined."%user)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def userLeft(self, user, channel):
|
2012-03-02 20:59:26 +00:00
|
|
|
self.relay("%s left."%user)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def userQuit(self, user, quitMessage):
|
2012-03-02 20:59:26 +00:00
|
|
|
user.relay("%s quit. (%s)"%(user, quitMessage))
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def action(self, user, channel, data):
|
2012-03-02 17:24:25 +00:00
|
|
|
self.relay("* %s %s"%(user, data))
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def userRenamed(self, oldname, newname):
|
2012-03-02 20:59:26 +00:00
|
|
|
self.relay("%s is now known as %s."%(oldname, newname))
|
2012-03-02 17:24:25 +00:00
|
|
|
|
2012-03-02 18:09:00 +00:00
|
|
|
|
2012-03-02 04:31:57 +00:00
|
|
|
class BaseFactory(protocol.ClientFactory):
|
|
|
|
noisy = False
|
|
|
|
def clientConnectionLost(self,connector,reason):
|
|
|
|
if hasattr(self, "terminated"):
|
|
|
|
if self.terminated:
|
|
|
|
connector.disconnect()
|
|
|
|
return log.msg("ClientFactory %s closing down"%self)
|
|
|
|
log.msg('Disconnect. Reason: %s'%reason.getErrorMessage())
|
2012-03-02 17:24:25 +00:00
|
|
|
reactor.callLater(5, connector.connect)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
def clientConnectionFailed(self,connector,reason):
|
|
|
|
if hasattr(self, "terminated"):
|
|
|
|
if self.terminated:
|
|
|
|
connector.disconnect()
|
|
|
|
return log.msg("ClientFactory %s closing down"%self)
|
|
|
|
log.msg('Disconnect. Reason: %s'%reason.getErrorMessage())
|
2012-03-02 17:24:25 +00:00
|
|
|
reactor.callLater(5, connector.connect)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
|
|
|
class RelayFactory(BaseFactory):
|
|
|
|
protocol = IRCRelayer
|
|
|
|
|
2012-03-02 20:05:09 +00:00
|
|
|
def __init__(self, network, channel, privMsgResponse = "I am a bot", port=6667, name = "RelayBot"):
|
2012-03-02 04:31:57 +00:00
|
|
|
self.network = network
|
2012-03-02 17:24:25 +00:00
|
|
|
self.channel = channel
|
2012-03-02 04:31:57 +00:00
|
|
|
self.name = name
|
2012-03-02 18:09:00 +00:00
|
|
|
self.port = port
|
2012-03-02 20:05:09 +00:00
|
|
|
self.privMsgResponse = privMsgResponse
|
2012-03-02 04:31:57 +00:00
|
|
|
self.terminated = False
|
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
2012-03-02 18:09:00 +00:00
|
|
|
identifier = (self.network, self.channel, self.port)
|
2012-03-02 20:05:09 +00:00
|
|
|
x = self.protocol(self.name, self.network, self.channel, identifier, self.privMsgResponse)
|
2012-03-02 04:31:57 +00:00
|
|
|
x.factory = self
|
|
|
|
return x
|
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
|
|
"""If we get disconnected, reconnect to server."""
|
2012-03-02 18:09:00 +00:00
|
|
|
#TODO: reconnecting factory thing
|
2012-03-02 17:24:25 +00:00
|
|
|
connector.connect()
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
def handler(signum, frame):
|
|
|
|
reactor.stop()
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
hostname = "localhost"
|
|
|
|
timeout = 120
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
def clientString(hostname, port):
|
|
|
|
return "tcp:host={0}:port={1}".format(hostname, port)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 20:05:09 +00:00
|
|
|
preamble = "This is a bot which relays traffic between #i2p-bridge on FLIP and #flip-bridge on I2Prc. "
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 20:05:09 +00:00
|
|
|
contact = "Freemail: operhiem1@oblda5d6jfleur3uomyws52uljrvo4l2jbuwcwsuk54tcn3qi5ehqwlsojvdaytcjnseslbnki3fozckj5ztaqkblb3gw3dwmreeg6dhk5te2ncyj55hgmkmkq4xoytworgdkrdpgvvsyqkrifbucqkf.freemail"
|
|
|
|
|
|
|
|
port = 6667
|
|
|
|
botOneF = RelayFactory(hostname, "#i2p-bridge", preamble + contact, port)
|
|
|
|
connectionOne = clientFromString(reactor, clientString(hostname, port))
|
|
|
|
connectionOne.connect(botOneF)
|
|
|
|
|
|
|
|
contact = "I2P-bote: operhiem1@QcTYSRYota-9WDSgfoUfaOkeSiPc7cyBuHqbgJ28YmilVk66-n1U1Zf1sCwTS2eDxlk4iwMZuufRmATsPJdkipw4EuRfaHLXKktwtkSTXNhciDsTMgJn7Ka14ayVuuPiF2tKzyaCTV4H2vc7sUkOKLsH9lyccVnFdYOnL~bkZiCGDI"
|
|
|
|
|
|
|
|
port = 6669
|
|
|
|
botTwoF = RelayFactory(hostname, "#test-lol", preamble + contact, port)
|
|
|
|
connectionTwo = clientFromString(reactor, clientString(hostname, port))
|
|
|
|
connectionTwo.connect(botTwoF)
|
2012-03-02 04:31:57 +00:00
|
|
|
|
2012-03-02 17:24:25 +00:00
|
|
|
reactor.callWhenRunning(signal, SIGINT, handler)
|
|
|
|
reactor.run()
|