fixbot/nexus.py
changeset 21 aa6df8f9c44a
child 35 5b6043ce9686
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fixbot/nexus.py	Mon Sep 15 00:27:05 2008 +0300
@@ -0,0 +1,70 @@
+from twisted.application import internet, service
+from twisted.internet import reactor, protocol
+from twisted.python import log
+import sys
+
+import irc, api
+
+class Nexus (object) :
+    def __init__ (self) :
+        """
+            Must set .irc/.api attrs to irc.Factory/api.ServerFactory instances
+        """
+
+        self.modules = dict()
+
+
+    def registerModule (self, module, transport) :
+        self.modules[module.name] = (module, transport)
+
+        self.irc.connection.moduleConnected(module, transport.transport.getPeer())
+
+    def unregisterModule (self, module, reason) :
+        del self.modules[module.name]
+
+        self.irc.connection.moduleDisconnected(module, reason)
+    
+    def handleEvent (self, event) :
+        self.irc.connection.sendEvent(event)
+
+    def getModules (self) :
+        return (module for (module, transport) in self.modules.itervalues())
+
+    def getModuleInfo (self, name) :
+        module, connection = self.modules[name]
+
+        return module, connection.transport.getPeer()
+ 
+def makeService (config) :
+    n = Nexus()
+    s = service.MultiService()
+    
+    # the IRC side
+    n.irc = irc.Factory(n, 
+        config['irc-nickname'], 
+        config['irc-username'],
+        config['irc-channel']
+    )
+    
+    log.msg("Connecting to IRC server on [%s:%d]", config['irc-hostname'], config['irc-port'])
+    irc_client = internet.TCPClient(config['irc-hostname'], config['irc-port'], n.irc)
+
+    irc_client.setServiceParent(s)
+
+    # the API side
+    n.api = api.ServerFactory(n)
+    
+    log.msg("Starting API server on [%s:%d]", config['api-port'], config['api-listen'])
+    api_server = internet.TCPServer(config['api-port'], n.api, interface=config['api-listen'])
+
+    api_server.setServiceParent(s)
+
+    # return the service collection
+    return s
+       
+if __name__ == '__main__' :
+    log.startLogging(sys.stderr)
+
+    nexus = Nexus()
+    reactor.run()
+