diff --git a/bot.py b/bot.py index 9244bb2..c1f33b1 100755 --- a/bot.py +++ b/bot.py @@ -94,8 +94,8 @@ class Phenny(irc.Bot): def sub(pattern, self=self): # These replacements have significant order - pattern = pattern.replace('$nickname', self.nick) - return pattern.replace('$nick', r'%s[,:] +' % self.nick) + pattern = pattern.replace('$nickname', re.escape(self.nick)) + return pattern.replace('$nick', r'%s[,:] +' % re.escape(self.nick)) for name, func in self.variables.iteritems(): # print name, func diff --git a/modules/calc.py b/modules/calc.py index 88ac814..d8cad35 100755 --- a/modules/calc.py +++ b/modules/calc.py @@ -69,6 +69,8 @@ calc.example = '.calc 5 + 3' def c(phenny, input): """Google calculator.""" + if not input.group(2): + return phenny.reply("Nothing to calculate.") q = input.group(2).encode('utf-8') q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5 q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0 @@ -90,7 +92,7 @@ c.commands = ['c'] c.example = '.c 5 + 3' def py(phenny, input): - query = input.group(2) + query = input.group(2).encode('utf-8') uri = 'http://tumbolia.appspot.com/py/' answer = web.get(uri + web.urllib.quote(query)) if answer: @@ -99,6 +101,8 @@ def py(phenny, input): py.commands = ['py'] def wa(phenny, input): + if not input.group(2): + return phenny.reply("No search term.") query = input.group(2).encode('utf-8') uri = 'http://tumbolia.appspot.com/wa/' answer = web.get(uri + web.urllib.quote(query)) diff --git a/modules/dict.py b/modules/dict.py index 125f686..8f13e99 100755 --- a/modules/dict.py +++ b/modules/dict.py @@ -22,6 +22,8 @@ r_info = re.compile( ) def dict(phenny, input): + if not input.group(2): + return phenny.reply("Nothing to define.") word = input.group(2) word = urllib.quote(word.encode('utf-8')) diff --git a/modules/etymology.py b/modules/etymology.py index 1480191..55c5deb 100755 --- a/modules/etymology.py +++ b/modules/etymology.py @@ -46,7 +46,7 @@ def etymology(word): raise ValueError("Word too long: %s[...]" % word[:10]) word = {'axe': 'ax/axe'}.get(word, word) - bytes = web.get(etyuri % word) + bytes = web.get(etyuri % web.urllib.quote(word)) definitions = r_definition.findall(bytes) if not definitions: @@ -77,7 +77,7 @@ def etymology(word): def f_etymology(self, origin, match, args): word = match.group(2) - try: result = etymology(word.encode('utf-8')) + try: result = etymology(word.encode('iso-8859-1')) except IOError: msg = "Can't connect to etymonline.com (%s)" % (etyuri % word) self.msg(origin.sender, msg) @@ -92,7 +92,7 @@ def f_etymology(self, origin, match, args): msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri) self.msg(origin.sender, msg) # @@ Cf. http://swhack.com/logs/2006-01-04#T01-50-22 -f_etymology.rule = (['ety'], r"([A-Za-z0-9' .-]+)$") +f_etymology.rule = (['ety'], r"(.+?)$") f_etymology.thread = True f_etymology.priority = 'high' diff --git a/modules/ping.py b/modules/ping.py index 97e41e1..23219ac 100755 --- a/modules/ping.py +++ b/modules/ping.py @@ -11,7 +11,7 @@ def hello(phenny, input): greeting = random.choice(('Hi', 'Hey', 'Hello')) punctuation = random.choice(('', '!')) phenny.say(greeting + ' ' + input.nick + punctuation) -hello.rule = r'(?i)(hi|hello|hey) $nickname\b' +hello.rule = r'(?i)(hi|hello|hey) $nickname[ \t]*$' def interjection(phenny, input): phenny.say(input.nick + '!') diff --git a/modules/search.py b/modules/search.py index 1067531..2751bb5 100755 --- a/modules/search.py +++ b/modules/search.py @@ -10,17 +10,31 @@ http://inamidst.com/phenny/ import re import web +class Grab(web.urllib.URLopener): + def __init__(self, *args): + self.version = 'Mozilla/5.0 (Phenny)' + web.urllib.URLopener.__init__(self, *args) + self.addheader('Referer', 'https://github.com/sbp/phenny') + def http_error_default(self, url, fp, errcode, errmsg, headers): + return web.urllib.addinfourl(fp, [headers, errcode], "http:" + url) + def search(query): """Search using AjaxSearch, and return its JSON.""" uri = 'http://ajax.googleapis.com/ajax/services/search/web' args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8')) + handler = web.urllib._urlopener + web.urllib._urlopener = Grab() bytes = web.get(uri + args) + web.urllib._urlopener = handler return web.json(bytes) def result(query): results = search(query) try: return results['responseData']['results'][0]['unescapedUrl'] except IndexError: return None + except TypeError: + print results + return False def count(query): results = search(query) @@ -48,6 +62,7 @@ def g(phenny, input): if not hasattr(phenny.bot, 'last_seen_uri'): phenny.bot.last_seen_uri = {} phenny.bot.last_seen_uri[input.sender] = uri + elif uri is False: phenny.reply("Problem getting data from Google.") else: phenny.reply("No results found for '%s'." % query) g.commands = ['g'] g.priority = 'high' @@ -69,6 +84,8 @@ r_query = re.compile( ) def gcs(phenny, input): + if not input.group(2): + return phenny.reply("Nothing to compare.") queries = r_query.findall(input.group(2)) if len(queries) > 6: return phenny.reply('Sorry, can only compare up to six things.') diff --git a/modules/validate.py b/modules/validate.py index 185623f..85815d1 100755 --- a/modules/validate.py +++ b/modules/validate.py @@ -11,6 +11,8 @@ import web def val(phenny, input): """Check a webpage using the W3C Markup Validator.""" + if not input.group(2): + return phenny.reply("Nothing to validate.") uri = input.group(2) if not uri.startswith('http://'): uri = 'http://' + uri diff --git a/modules/wikipedia.py b/modules/wikipedia.py index 30a23f3..b476ba3 100755 --- a/modules/wikipedia.py +++ b/modules/wikipedia.py @@ -10,9 +10,9 @@ http://inamidst.com/phenny/ import re, urllib import web -wikiuri = 'http://en.wikipedia.org/wiki/%s' -wikisearch = 'http://en.wikipedia.org/wiki/Special:Search?' \ - + 'search=%s&fulltext=Search' +wikiuri = 'http://%s.wikipedia.org/wiki/%s' +# wikisearch = 'http://%s.wikipedia.org/wiki/Special:Search?' \ +# + 'search=%s&fulltext=Search' r_tr = re.compile(r'(?ims)
]*>.*?
|