author | Tero Marttila <terom@paivola.fi> |
Fri, 21 Mar 2008 02:02:52 +0200 | |
changeset 8 | 3d648daf9538 |
parent 6 | 614161f85d9b |
child 9 | 75cc996c6ba1 |
permissions | -rw-r--r-- |
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) |
|
6
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
49 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
50 |
def getModules (self) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
51 |
return (module for (module, transport) in self.modules.itervalues()) |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
52 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
53 |
def getModuleInfo (self, name) : |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
54 |
module, connection = self.modules[name] |
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
55 |
|
614161f85d9b
some cleanup, bugfixes, commands for the irc bot, shared-secret for the API
Tero Marttila <terom@paivola.fi>
parents:
4
diff
changeset
|
56 |
return module, connection.transport.getPeer() |
4 | 57 |
|
58 |
if __name__ == '__main__' : |
|
59 |
log.startLogging(sys.stderr) |
|
60 |
||
61 |
nexus = Nexus() |
|
62 |
reactor.run() |
|
63 |