source: svnbot/trunk/svnbot.py @ 17

Last change on this file since 17 was 17, checked in by simon, 18 years ago

Add encoding declaration.

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