--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/fixbot/module.py Sat Feb 20 22:31:17 2010 +0200
@@ -0,0 +1,118 @@
+from twisted.internet import protocol
+from twisted.application import service, internet
+from twisted.python import log
+
+import datetime
+
+class ModuleInfo (object) :
+ """
+ Nexus-side handle on a Module
+ """
+
+ # module's name
+ name = None
+
+ def __str__ (self) :
+ return "Module %s:" % (self.name)
+
+ def __repr__ (self) :
+ return "<module %s>" % (self.name, )
+
+
+class Event (object) :
+ """
+ An Event, sent by a Module to the Nexus, to be distributed further
+ """
+
+ # the ModuleInfo object
+ module = None
+
+ # the event type as a string
+ type = None
+
+ # event message as a string (up to 64k, although that won't fit onto IRC..)
+ msg = None
+
+ # timestamp as a datetime.datetime
+ when = None
+
+ def __init__ (self, module, type, msg) :
+ self.module = module
+ self.type = type
+ self.msg = msg
+
+ self.when = datetime.datetime.now()
+
+ def __str__ (self) :
+ return "[%s] %s" % (self.type, self.msg)
+
+ def __repr__ (self) :
+ return "%s @ %s" % (self.type, self.when)
+
+
+class Module (ModuleInfo, protocol.ClientFactory) :
+ """
+ Module core, handles the API connection state and processes all messages
+ """
+
+ # our API connection to the Nexus
+ connection = None
+
+ def __init__ (self, config, protocol) :
+ """
+ config - configuration for connecting to nexus
+ protocol - API client protocol to use for this factory
+ """
+
+ self.protocol = protocol
+
+ self.connection = None
+
+ # XXX: legacy: self.secret = config['api-secret']
+
+ def _onRegistered (self, connection) :
+ """
+ Connected to nexus and registered
+ """
+
+ log.msg("Connected and registered")
+
+ self.connection = connection
+
+ self.handleConnect()
+
+ def disconnect (self) :
+ """
+ Disconnect from Nexus
+ """
+
+ self.connection.transport.loseConnection()
+
+ def sendEvent (self, type, msg) :
+ """
+ Send event to nexus
+ """
+
+ self.connection.sendEvent(Event(self, type, msg))
+
+ def handleConnect (self) :
+ """
+ Do something once we are connected to nexus and registered
+ """
+
+ pass
+
+def makeService (module_class, config, protocol) :
+ s = service.MultiService()
+
+ # build factory
+ factory = module_class(config, protocol)
+
+ # the API client
+ log.msg("Connecting to API server on [%s:%d]" % (config['api-server'], config['api-port']))
+ api_client = internet.TCPClient(config['api-server'], config['api-port'], factory)
+
+ api_client.setServiceParent(s)
+
+ return s
+