1 | import sys |
---|
2 | from twisted.application import service, internet |
---|
3 | from twisted.protocols import irc |
---|
4 | from twisted.internet import protocol |
---|
5 | from twisted.python import log |
---|
6 | from twisted.web import server, xmlrpc |
---|
7 | |
---|
8 | |
---|
9 | class SvnBotXmlrpc(xmlrpc.XMLRPC): |
---|
10 | |
---|
11 | def __init__(self, bot, channel, tracurl=None): |
---|
12 | self.bot = bot |
---|
13 | self.channel = channel |
---|
14 | self.tracurl = tracurl |
---|
15 | xmlrpc.XMLRPC.__init__(self) |
---|
16 | |
---|
17 | def xmlrpc_say(self, msg): |
---|
18 | self.bot.say(self.channel, msg) |
---|
19 | return 0 |
---|
20 | |
---|
21 | def xmlrpc_showCommitSummary(self, rev, author): |
---|
22 | self.bot.say(self.channel, |
---|
23 | "Revision %s committed by %s: %s" % ( |
---|
24 | rev, author, self.tracurl+'changeset/'+rev)) |
---|
25 | return 0 |
---|
26 | |
---|
27 | |
---|
28 | class SvnBot(irc.IRCClient): |
---|
29 | """A bot that retrieves subversion repository metadata.""" |
---|
30 | xmlrpcService = None |
---|
31 | pingerService = None |
---|
32 | |
---|
33 | def __init__(self): |
---|
34 | self.nickname = "svnbot" |
---|
35 | self.realname = "Subversion Bot" |
---|
36 | |
---|
37 | #def sendLine(self, line): |
---|
38 | # irc.IRCClient.sendLine(self, line) |
---|
39 | |
---|
40 | #def join(self, channel, key=None): |
---|
41 | # irc.IRCClient.join(self, channel, key) |
---|
42 | |
---|
43 | #def mode(self, chan, set, modes, limit = None, user = None, mask = None): |
---|
44 | # irc.IRCClient.mode(self, chan, set, modes, limit, user, mask) |
---|
45 | |
---|
46 | #def say(self, channel, message, length = None): |
---|
47 | # irc.IRCClient.say(self, channel, message, length) |
---|
48 | |
---|
49 | #def ping(self, user, text=None): |
---|
50 | # irc.IRCClient.ping(self, user, text) |
---|
51 | |
---|
52 | #def pong(self, user, secs): |
---|
53 | # irc.IRCClient.pong(self, user, secs) |
---|
54 | |
---|
55 | def connectionMade(self): |
---|
56 | irc.IRCClient.connectionMade(self) |
---|
57 | x = SvnBotXmlrpc(self, self.factory.channel, |
---|
58 | tracurl=self.factory.tracurl) |
---|
59 | if self.xmlrpcService is None: |
---|
60 | self.xmlrpcService = internet.TCPServer(7080, server.Site(x), |
---|
61 | interface='localhost') |
---|
62 | self.xmlrpcService.setServiceParent(serviceCollection) |
---|
63 | else: |
---|
64 | self.xmlrpcService.startService() |
---|
65 | #if self.pingerService is None: |
---|
66 | # self.pingerService = internet.TimerService(120, |
---|
67 | # self.ping, self.nickname, text=None) |
---|
68 | # self.pingerService.setServiceParent(serviceCollection) |
---|
69 | #else: |
---|
70 | # self.pingerService.startService() |
---|
71 | |
---|
72 | def connectionLost(self, reason): |
---|
73 | irc.IRCClient.connectionLost(self, reason) |
---|
74 | self.xmlrpcService.stopService() |
---|
75 | #self.pingerService.stopService() |
---|
76 | |
---|
77 | def signedOn(self): |
---|
78 | """Called when bot has succesfully signed on to server.""" |
---|
79 | irc.IRCClient.signedOn(self) |
---|
80 | self.mode(self.nickname, 1, 'B') |
---|
81 | self.join(self.factory.channel) |
---|
82 | |
---|
83 | #def lineReceived(self, line): |
---|
84 | # irc.IRCClient.dataReceived(self, line) |
---|
85 | |
---|
86 | #def joined(self, channel): |
---|
87 | # irc.IRCClient.joined(self, channel) |
---|
88 | |
---|
89 | #def left(self, channel): |
---|
90 | # irc.IRCClient.left(self, channel) |
---|
91 | |
---|
92 | #def irc_PING(self, prefix, params): |
---|
93 | # irc.IRCClient.irc_PING(self, prefix, params) |
---|
94 | |
---|
95 | #def irc_unknown(self, prefix, command, params): |
---|
96 | # irc.IRCClient.irc_unknown(self, prefix, command, params) |
---|
97 | |
---|
98 | |
---|
99 | class SvnBotFactory(protocol.ReconnectingClientFactory): |
---|
100 | """A factory for SvnBots. |
---|
101 | |
---|
102 | A new protocol instance will be created each time we connect to the server. |
---|
103 | """ |
---|
104 | |
---|
105 | # The class of the protocol to build when new connection is made |
---|
106 | protocol = SvnBot |
---|
107 | |
---|
108 | def __init__(self, channel, tracurl=None): |
---|
109 | self.channel = channel |
---|
110 | self.tracurl = tracurl |
---|
111 | |
---|
112 | #def clientConnectionFailed(self, connector, reason): |
---|
113 | # """Called when a connection has failed to connect.""" |
---|
114 | # protocol.ReconnectingClientFactory.clientConnectionFailed( |
---|
115 | # self, connector, reason) |
---|
116 | |
---|
117 | #def clientConnectionLost(self, connector, unused_reason): |
---|
118 | # protocol.ReconnectingClientFactory.clientConnectionLost( |
---|
119 | # self, connector, unused_reason) |
---|
120 | |
---|
121 | #def resetDelay(self): |
---|
122 | # """Call me after a successful connection to reset.""" |
---|
123 | # protocol.ReconnectingClientFactory.resetDelay(self) |
---|
124 | |
---|
125 | #def retry(self, connector=None): |
---|
126 | # """Have this connector connect again, after a suitable delay.""" |
---|
127 | # protocol.ReconnectingClientFactory.retry(self, connector) |
---|
128 | |
---|
129 | #def stopTrying(self): |
---|
130 | # """I put a stop to any attempt to reconnect in progress.""" |
---|
131 | # protocol.ReconnectingClientFactory.stopTrying(self) |
---|
132 | |
---|
133 | #def startedConnecting(self, connector): |
---|
134 | # """Called when a connection has been started.""" |
---|
135 | # protocol.ReconnectingClientFactory.startedConnecting(self, connector) |
---|
136 | |
---|
137 | def buildProtocol(self, addr): |
---|
138 | """Create an instance of a subclass of Protocol.""" |
---|
139 | self.resetDelay() |
---|
140 | return protocol.ReconnectingClientFactory.buildProtocol(self, addr) |
---|
141 | |
---|
142 | #def doStart(self): |
---|
143 | # """Make sure startFactory is called.""" |
---|
144 | # protocol.ReconnectingClientFactory.doStart(self) |
---|
145 | |
---|
146 | #def doStop(self): |
---|
147 | # """Make sure stopFactory is called.""" |
---|
148 | # protocol.ReconnectingClientFactory.doStop(self) |
---|
149 | |
---|
150 | #def startFactory(self): |
---|
151 | # """This will be called before I begin listening on a Port or |
---|
152 | # Connector.""" |
---|
153 | # protocol.ReconnectingClientFactory.startFactory(self) |
---|
154 | |
---|
155 | #def stopFactory(self): |
---|
156 | # """This will be called before I stop listening on all |
---|
157 | # Ports/Connectors.""" |
---|
158 | # protocol.ReconnectingClientFactory.stopFactory(self) |
---|
159 | |
---|
160 | |
---|
161 | application = service.Application('svnbot') |
---|
162 | serviceCollection = service.IServiceCollection(application) |
---|
163 | svnBot = SvnBotFactory(channel='#test') |
---|
164 | tcpClient = internet.TCPClient('irc.example.com', 6667, svnBot) |
---|
165 | tcpClient.setServiceParent(serviceCollection) |
---|
166 | |
---|