From 0b380f56fe1b05cd4ae1acc6692dce3227fa2497 Mon Sep 17 00:00:00 2001 From: Daniel Berlin Date: Wed, 22 Oct 2003 22:05:34 +0000 Subject: [PATCH] Add --id option to geekquote, make it always allow multiline quotes --- plugins/Http.py | 25 +++++++++++++++---------- test/test_Http.py | 3 ++- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/plugins/Http.py b/plugins/Http.py index 6bea689a8..151c4ab3d 100644 --- a/plugins/Http.py +++ b/plugins/Http.py @@ -38,6 +38,7 @@ import plugins import re import sets import urllib2 +import getopt import utils import debug @@ -259,27 +260,31 @@ class Http(callbacks.Privmsg): debug.recoverableException() irc.error(msg, debug.exnToString(e)) - _geekquotere = re.compile('

(.*?)

') _mlgeekquotere = re.compile('

(.*?)

', re.M | re.DOTALL) def geekquote(self, irc, msg, args): - """[] + """[--id=] Returns a random geek quote from bash.org; the optional argument - specifies whether multi-line quotes (which are longer - than other quotes, generally) are to be allowed. + --id specifies which quote to retrieve. """ - multiline = privmsgs.getArgs(args, needed=0, optional=1) + (optlist, rest) = getopt.getopt(args, '', ['id=']) + id = 'random1' + for (option, arg) in optlist: + if option == '--id': + try: + id = int(arg) + except ValueError, e: + irc.error(msg, 'Invalid id: %s' % e) + return + try: - fd = urllib2.urlopen('http://bash.org/?random1') + fd = urllib2.urlopen('http://bash.org/?%s' % id) except urllib2.URLError: irc.error(msg, 'Error connecting to geekquote server.') return html = fd.read() fd.close() - if multiline: - m = self._mlgeekquotere.search(html) - else: - m = self._geekquotere.search(html) + m = self._mlgeekquotere.search(html) if m is None: irc.error(msg, 'No quote found.') return diff --git a/test/test_Http.py b/test/test_Http.py index 46b2e4e9f..2b5b84126 100644 --- a/test/test_Http.py +++ b/test/test_Http.py @@ -52,7 +52,8 @@ class HttpTest(PluginTestCase, PluginDocumentation): def testGeekquote(self): self.assertNotError('geekquote') - self.assertNotError('geekquote multiline') + self.assertNotError('geekquote --id=4848') + self.assertError('geekquote --id=48a') def testAcronym(self): self.assertNotError('acronym PERL')