### # 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.world as world import supybot.dbi as dbi import supybot.conf as conf import supybot.utils as utils from supybot.commands import * import supybot.ircmsgs as ircmsgs import supybot.plugins as plugins import supybot.ircutils as ircutils import supybot.callbacks as callbacks import supybot.log as log import os import cgi import time import datetime import cStringIO as StringIO try: PageModule = world.ircs[0].getCallback('Webserver').classModule PageClass = PageModule.plugin.PluginPage except Exception, e: log.error('Webserver plugin must be loaded') class Pastebin(callbacks.Plugin): """A pastebin including web server.""" def __init__(self, irc): callbacks.Plugin.__init__(self, irc) self.db = PastebinDB() self.mostrecent = [] self.serverPlugin = irc.getCallback('Webserver') if not self.serverPlugin: irc.error('Webserver plugin must be running') else: PastebinHome.cbPastebin = self self.serverPlugin.addSite('PasteBin', PastebinHome) def doPaste(self, irc, cname, nick, text, ip): date = time.time() pid = self.db.addPaste(poster=nick, post=text, ip=ip, date=date) self.mostrecent.append(pid) if len(self.mostrecent) > self.registryValue('recentPasteCount'): self.mostrecent.pop(0) if self.registryValue('announce'): url = '%s/%s/%s/PasteBin?view=%s' % \ (conf.supybot.plugins.Webserver.rootURL().rstrip('/'), irc.network, cname[1:], pid) mess = 'Pastebin: New paste by %s at %s' % \ (nick, format('%u', url)) m = ircmsgs.notice(cname, mess) irc.sendMsg(m) return pid def mostRecent(self): for pid in self.mostrecent: yield self.db.get(pid) def die(self): self.serverPlugin.removeSite('paste') class PastebinDB(object): def __init__(self): basedir = conf.supybot.directories.data.dirize('Pastebin') if not os.path.exists(basedir): os.mkdir(basedir) dbpath = os.path.join(basedir, 'pastes.db') self.db = dbi.DB(dbpath, Record=PastebinRecord, Mapping='cdb') def getPaste(self, pid): return self.db.get(pid) def addPaste(self, **kw): newPaste = PastebinRecord(**kw) pid = self.db.add(newPaste) return pid class PastebinHome(PageClass): isLeaf = True def renderContent(self, request): self.cbPlugin.log.critical('%s %s', dir(request), request.method) segments = [] pastetext = '' prenick = '' if request.method == 'GET': if 'view' in request.args and len(request.args): pid = request.args['view'].pop() try: goodid = int(pid) except ValueError: goodid = None if goodid: record = self.cbPastebin.db.getPaste(goodid) segments.append(self.renderView(request, record)) pastetext = record.post else: segments.append(self.renderHome(request)) else: segments.append(self.renderHome(request)) else: #post (we know this since no other requests ever get here) segments.append(self.renderPost(request)) segments.append(self.renderForm(prenick, pastetext)) return ''.join(segments) def renderView(self, request, record): date = time.asctime(time.localtime(record.date)) lines = [] for i, line in enumerate(record.post.splitlines()): lines.append(XHTML_LINE % (i, cgi.escape(line).replace(' ', ' '))) return XHTML_VIEW % (record.poster, date, ''.join(lines)) def renderHome(self, request): T = """