From fe16d21212bacc9704a6cd84d3c85a6676936181 Mon Sep 17 00:00:00 2001
From: "Sean B. Palmer"
Date: Wed, 29 Oct 2008 16:03:12 +0000
Subject: [PATCH] Oblique stuff and fixes.
---
irc.py | 8 ++++++--
modules/clock.py | 6 +++---
modules/codepoints.py | 3 ++-
modules/head.py | 20 ++++++++++++++------
modules/oblique.py | 43 +++++++++++++++++++++++++++----------------
5 files changed, 52 insertions(+), 28 deletions(-)
diff --git a/irc.py b/irc.py
index e5bfdc3..281f5db 100755
--- a/irc.py
+++ b/irc.py
@@ -52,10 +52,14 @@ class Bot(asynchat.async_chat):
def write(self, args, text=None):
# This is a safe version of __write
+ def safe(input):
+ input = input.replace('\n', '')
+ input = input.replace('\r', '')
+ return input.encode('utf-8')
try:
- args = [arg.encode('utf-8') for arg in args]
+ args = [safe(arg) for arg in args]
if text is not None:
- text = text.encode('utf-8')
+ text = safe(text)
self.__write(args, text)
except Exception, e: pass
diff --git a/modules/clock.py b/modules/clock.py
index 2ef6b21..59c9eb1 100755
--- a/modules/clock.py
+++ b/modules/clock.py
@@ -96,8 +96,8 @@ TZ1 = {
}
TZ2 = {
- 'ACDT': -10.5,
- 'ACST': -9.5,
+ 'ACDT': 10.5,
+ 'ACST': 9.5,
'ADT': 3,
'AEDT': 11, # hmm
'AEST': 10, # hmm
@@ -251,7 +251,7 @@ def yi(phenny, input):
raels = quadraels * 4
extraraels, remainder = divide(remainder, 432000)
if extraraels == 4:
- return phenny.say('Yes!')
+ return phenny.say('Yes! PARTAI!')
else: phenny.say('Not yet...')
yi.commands = ['yi']
yi.priority = 'low'
diff --git a/modules/codepoints.py b/modules/codepoints.py
index e2fc39a..eb9c8bf 100755
--- a/modules/codepoints.py
+++ b/modules/codepoints.py
@@ -71,7 +71,8 @@ def u(phenny, input):
if not arg:
return phenny.reply('You gave me zero length input.')
elif not arg.strip(' '):
- return phenny.reply('%s SPACES' % len(arg))
+ if len(arg) > 1: return phenny.reply('%s SPACEs (U+0020)' % len(arg))
+ return phenny.reply('1 SPACE (U+0020)')
# @@ space
if set(arg.upper()) - set(
diff --git a/modules/head.py b/modules/head.py
index 8158958..274e1b0 100755
--- a/modules/head.py
+++ b/modules/head.py
@@ -7,7 +7,7 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
-import re, urllib, httplib, urlparse, time
+import re, urllib, urllib2, httplib, urlparse, time
from htmlentitydefs import name2codepoint
import web
from tools import deprecated
@@ -84,7 +84,11 @@ def f_title(self, origin, match, args):
try:
redirects = 0
while True:
- info = web.head(uri)
+ req = urllib2.Request(uri, headers={'Accept':'text/html'})
+ u = urllib2.urlopen(req)
+ info = u.info()
+ u.close()
+ # info = web.head(uri)
if not isinstance(info, list):
status = '200'
@@ -108,7 +112,7 @@ def f_title(self, origin, match, args):
self.msg(origin.sender, origin.nick + ": Document isn't HTML")
return
- u = urllib.urlopen(uri)
+ u = urllib2.urlopen(req)
bytes = u.read(32768)
u.close()
@@ -142,11 +146,15 @@ def f_title(self, origin, match, args):
title = r_entity.sub(e, title)
if title:
- try: title.decode('iso-8859-1')
- except: pass
- else: title = title.decode('iso-8859-1').encode('utf-8')
+ try: title.decode('utf-8')
+ except:
+ try: title = title.decode('iso-8859-1').encode('utf-8')
+ except: title = title.decode('cp1252').encode('utf-8')
+ else: pass
else: title = '[The title is empty.]'
+ title = title.replace('\n', '')
+ title = title.replace('\r', '')
self.msg(origin.sender, origin.nick + ': ' + title)
else: self.msg(origin.sender, origin.nick + ': No title found')
f_title.commands = ['title']
diff --git a/modules/oblique.py b/modules/oblique.py
index 55a27fc..a4aba17 100755
--- a/modules/oblique.py
+++ b/modules/oblique.py
@@ -7,18 +7,22 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
-import urllib
+import re, urllib
import web
+definitions = 'http://code.google.com/p/phenny-ws/wiki/ServiceDefinitions'
+
+r_item = re.compile(r'(?i)
(.*?)')
+r_tag = re.compile(r'<[^>]+>')
+
def mappings(uri):
result = {}
bytes = web.get(uri)
- for line in bytes.splitlines():
- if not line.startswith(''): continue
- line = line.strip()
- if not line.endswith(''): continue
+ for item in r_item.findall(bytes):
+ item = r_tag.sub('', item).strip(' \t\r\n')
+ if not ' ' in item: continue
- command, template = line[4:-5].split(' ', 1)
+ command, template = item.split(' ', 1)
if not template.startswith('http://'): continue
result[command] = template
return result
@@ -26,32 +30,39 @@ def mappings(uri):
def o(phenny, input):
"""Call a webservice."""
text = input.group(2)
+ if hasattr(phenny.config, 'services'):
+ services = phenny.config.services
+ else: services = definitions
if (not o.services) or (text == 'refresh'):
- if hasattr(phenny.config, 'services'):
- services = phenny.config.services
- else: services = 'http://swhack.jottit.com/services'
-
+ old = o.services
o.services = mappings(services)
if text == 'refresh':
- return phenny.reply('Okay, found %s services.' % len(o.services))
+ msg = 'Okay, found %s services.' % len(o.services)
+ added = set(o.services) - set(old)
+ if added:
+ msg += ' Added: ' + ', '.join(sorted(added)[:5])
+ if len(added) > 5: msg += ', &c.'
+ return phenny.reply(msg)
+
+ if not text:
+ return phenny.reply('Try %s for details.' % services)
if ' ' in text:
command, args = text.split(' ', 1)
else: command, args = text, ''
command = command.lower()
- args = urllib.quote(args)
if o.services.has_key(command):
template = o.services[command]
- template = template.replace('${args}', args)
- template = template.replace('${nick}', input.nick)
- uri = template.replace('${sender}', input.sender)
+ template = template.replace('${args}', urllib.quote(args.encode('utf-8')))
+ template = template.replace('${nick}', urllib.quote(input.nick))
+ uri = template.replace('${sender}', urllib.quote(input.sender))
bytes = web.get(uri)
lines = bytes.splitlines()
if lines:
- phenny.say(lines[0])
+ phenny.say(lines[0][:350])
else: phenny.reply('Sorry, the service is broken.')
else: phenny.reply('Sorry, no such service. See %s' % services)
o.commands = ['o']