0
|
1 |
from twisted.internet import reactor, protocol
|
|
2 |
from twisted.python import log
|
4
|
3 |
import sys
|
0
|
4 |
|
4
|
5 |
import irc, api
|
0
|
6 |
|
|
7 |
class IRCFactory (protocol.ClientFactory) :
|
4
|
8 |
protocol = irc.BotProtocol
|
0
|
9 |
|
4
|
10 |
def __init__ (self, nexus) :
|
|
11 |
self.nexus = nexus
|
|
12 |
|
|
13 |
log.msg("Connection to IRC at %s:%d" % (irc.HOSTNAME, irc.PORT))
|
|
14 |
reactor.connectTCP(irc.HOSTNAME, irc.PORT, self)
|
|
15 |
|
|
16 |
self.connection = None
|
|
17 |
|
|
18 |
def connected (self, connection) :
|
|
19 |
self.connection = connection
|
|
20 |
|
|
21 |
class APIFactory (protocol.ServerFactory) :
|
|
22 |
protocol = api.ServerProtocol
|
|
23 |
|
|
24 |
def __init__ (self, nexus) :
|
|
25 |
self.nexus = nexus
|
|
26 |
|
|
27 |
log.msg("API listening on %s:%d" % (api.SERVER_HOST, api.PORT))
|
|
28 |
reactor.listenTCP(api.PORT, self, interface=api.SERVER_HOST)
|
|
29 |
|
|
30 |
class Nexus (object) :
|
0
|
31 |
def __init__ (self) :
|
4
|
32 |
self.modules = dict()
|
0
|
33 |
|
4
|
34 |
self.irc = IRCFactory(self)
|
|
35 |
self.api = APIFactory(self)
|
|
36 |
|
|
37 |
def registerModule (self, module, transport) :
|
|
38 |
self.modules[module.name] = (module, transport)
|
|
39 |
|
|
40 |
self.irc.connection.moduleConnected(module, transport.transport.getPeer())
|
|
41 |
|
|
42 |
def unregisterModule (self, module, reason) :
|
|
43 |
del self.modules[module.name]
|
|
44 |
|
|
45 |
self.irc.connection.moduleDisconnected(module, reason)
|
|
46 |
|
|
47 |
def handleEvent (self, event) :
|
|
48 |
self.irc.connection.sendEvent(event)
|
|
49 |
|
|
50 |
if __name__ == '__main__' :
|
|
51 |
log.startLogging(sys.stderr)
|
|
52 |
|
|
53 |
nexus = Nexus()
|
|
54 |
reactor.run()
|
|
55 |
|