FUNCTIONALITY!

This commit is contained in:
Steve Dougherty 2012-03-02 13:09:00 -05:00
parent 92b3f7a45b
commit 6e76c6cb4a
1 changed files with 39 additions and 28 deletions

View File

@ -10,30 +10,42 @@ log.startLogging(sys.stdout)
__version__ = "0.2"
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:
log.msg("Not relaying to self")
continue
instance = self.protocolInstances[identifier]
log.msg(identifier, instance)
instance.twoWaySay(message)
#Global scope: all protocol instances will need this.
communicator = Communicator()
class IRCRelayer(irc.IRCClient):
realname = "Relay P. Botternson"
username = "RelayBot"
def __init__(self, name, network, channel):
def __init__(self, name, network, channel, identifier):
self.network = network
self.channel = channel
self.nickname = name
self.relayUsable = False
self.otherBot = None
self.identifier = identifier
self.privMsgResponse = "This is a relay bot between I2P #flip-bridge and FLIP #i2p-bridge. operhiem1 <Freemail: operhiem1@oblda5d6jfleur3uomyws52uljrvo4l2jbuwcwsuk54tcn3qi5ehqwlsojvdaytcjnseslbnki3fozckj5ztaqkblb3gw3dwmreeg6dhk5te2ncyj55hgmkmkq4xoytworgdkrdpgvvsyqkrifbucqkf.freemail I2P-bote: operhiem1@QcTYSRYota-9WDSgfoUfaOkeSiPc7cyBuHqbgJ28YmilVk66-n1U1Zf1sCwTS2eDxlk4iwMZuufRmATsPJdkipw4EuRfaHLXKktwtkSTXNhciDsTMgJn7Ka14ayVuuPiF2tKzyaCTV4H2vc7sUkOKLsH9lyccVnFdYOnL~bkZiCGDI>"
log.msg("IRC Relay created. Name: %s | Network: %s "%(name, network))
def setRelay(otherBot):
self.otherBot = otherBot
def relay(self, message):
log.msg("Checking whether to relay \"%s\""%message)
if not self.otherBot:
log.msg("Otherbot is NoneType.")
return
if self.relayUsable:
log.msg("Saying.")
self.otherBot.twoWaySay(message)
communicator.relay(self, message)
def signedOn(self):
log.msg("[%s] Connected to network"%self.network)
@ -41,7 +53,7 @@ class IRCRelayer(irc.IRCClient):
def connectionLost(self, reason):
log.msg("[%s] Connection lost, unregistering"%self.network)
self.relayUsable = False
communicator.unregister(self)
def twoWaySay(self, message, args=None):
log.msg("[TwoWay] Saying %s into channel %s"%(message, self.channel))
@ -49,11 +61,7 @@ class IRCRelayer(irc.IRCClient):
def joined(self, channel):
log.msg("I joined channel %s, registering"%channel)
self.relayUsable = True
def left(self, channel):
log.msg("I left channel %s"%channel)
self.relayUsable = False
communicator.register(self)
def privmsg(self, user, channel, message):
user = user.split("!")[0]
@ -64,7 +72,8 @@ class IRCRelayer(irc.IRCClient):
self.msg(user, self.privMsgResponse)
def kickedFrom(self, channel, kicker, message):
log.msg("%s kicked me! Rude. Message = %s"%(kicker, message))
log.msg("Kicked by %s. Message \"%s\""%(kicker, message))
communicator.unregister(self)
def nickChanged(self, nick):
log.msg("My nick is now %s"%nick)
@ -84,8 +93,7 @@ class IRCRelayer(irc.IRCClient):
def userRenamed(self, oldname, newname):
self.relay("User %s changed nick to %s"%(oldname, newname))
class BaseFactory(protocol.ClientFactory):
noisy = False
def clientConnectionLost(self,connector,reason):
@ -107,19 +115,22 @@ class BaseFactory(protocol.ClientFactory):
class RelayFactory(BaseFactory):
protocol = IRCRelayer
def __init__(self, network, channel, name = "RelayBot"):
def __init__(self, network, channel, port=6667, name = "RelayBot"):
self.network = network
self.channel = channel
self.name = name
self.port = port
self.terminated = False
def buildProtocol(self, addr):
x = self.protocol(self.name, self.network, self.channel)
identifier = (self.network, self.channel, self.port)
x = self.protocol(self.name, self.network, self.channel, identifier)
x.factory = self
return x
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
#TODO: reconnecting factory thing
connector.connect()
def handler(signum, frame):
@ -131,13 +142,13 @@ timeout = 120
def clientString(hostname, port):
return "tcp:host={0}:port={1}".format(hostname, port)
botOneF = RelayFactory(hostname, "#i2p-bridge")
botOneF = RelayFactory(hostname, "#i2p-bridge", 6667)
connectionOne = clientFromString(reactor, clientString(hostname, 6667))
connectionOne.connect(botOneF)
botOneDeferred = connectionOne.connect(botOneF)
botTwoF = RelayFactory(hostname, "#test-lol")
botTwoF = RelayFactory(hostname, "#test-lol", 6669)
connectionTwo = clientFromString(reactor, clientString(hostname, 6669))
connectionTwo.connect(botTwoF)
botTwoDeferred = connectionTwo.connect(botTwoF)
reactor.callWhenRunning(signal, SIGINT, handler)
reactor.run()