### # Copyright (c) 2005, Ali Afshar # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions, and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions, and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the author of this software nor the name of # contributors to this software may be used to endorse or promote products # derived from this software without specific prior written consent. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. ### import supybot.utils as utils import supybot.world as world from supybot.commands import * import supybot.plugins as plugins import supybot.ircutils as ircutils import supybot.callbacks as callbacks try: import twisted.web.server as server import twisted.web.resource as resource import twisted.python.htmlizer as htmlizer import twisted.internet.reactor as reactor except ImportError: # in case twisted is not installed reactor = None class Webserver(callbacks.Plugin): """Add the help for "@plugin help Webserver" here This should describe *how* to use this plugin.""" pass def __init__(self, irc): callbacks.Plugin.__init__(self, irc) if not reactor: self.irc.error('Twisted is not installed.') self.irc = irc self.site = server.Site(Home(self)) self.children = {} self.listener = None self._startListening() def flush(self): callbacks.Plugin.flush(self) self._stopListening() self._startListening() def _startListening(self, port=None): """Start the server listening.""" if not port: port = self.registryValue('port') self.listener = reactor.listenTCP(port, self.site) def _stopListening(self): """Stop the server listening.""" if self.listener: self.listener.stopListening() def addSite(self, name, resource): self.children[name] = resource def removeSite(self, name): if name in self.children: del self.children[name] def die(self): self._stopListening() class SBPage(resource.Resource): isLeaf = True def __init__(self, cb): self.cbPlugin = cb resource.Resource.__init__(self) def renderAll(self, request, opts={}): opts['title'] = self.renderTitle(request) opts['content'] = self.renderContent(request) opts['heading'] = self.renderHeader(request) opts['tbar-fg'] = 'white' opts['tbar-bg'] = '#000099' opts['homeaddr'] = self.cbPlugin.registryValue('homeURL') return HTML_PAGE % opts def renderContent(self, request): return 'empty page' def renderHeader(self, request): return '%s' % self.__class__ def renderTitle(self, request): return "My Supybot's Home Page" def renderNavbar(self, request): return 'no nav bar' def renderRecent(self, request): return 'recent' def render_GET(self, request): return self.renderAll(request) def render_POST(self, request): return self.renderAll(request) class Home(SBPage): isLeaf = False def renderContent(self, request): chlist = ['