summaryrefslogtreecommitdiffstats
path: root/modules/head.py
diff options
context:
space:
mode:
Diffstat (limited to 'modules/head.py')
-rwxr-xr-xmodules/head.py64
1 files changed, 43 insertions, 21 deletions
diff --git a/modules/head.py b/modules/head.py
index 193286a..8f687fa 100755
--- a/modules/head.py
+++ b/modules/head.py
@@ -7,22 +7,24 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
-import re, urllib, urlparse
+import re, urllib, urlparse, time
from htmlentitydefs import name2codepoint
import web
from tools import deprecated
-@deprecated
-def f_httphead(self, origin, match, args):
- """.head <URI> <FieldName>? - Perform an HTTP HEAD on URI."""
- if origin.sender == '#talis': return
- uri = match.group(2)
- header = match.group(3)
+def head(phenny, input):
+ """Provide HTTP HEAD information."""
+ uri = input.group(2)
+ uri = (uri or '').encode('utf-8')
+ if ' ' in uri:
+ uri, header = uri.rsplit(' ', 1)
+ else: uri, header = uri, None
+
+ if not uri and hasattr(phenny, 'last_seen_uri'):
+ uri = phenny.last_seen_uri
try: info = web.head(uri)
- except IOError:
- self.msg(origin.sender, "Can't connect to %s" % uri)
- return
+ except IOError: return phenny.say("Can't connect to %s" % uri)
if not isinstance(info, list):
info = dict(info)
@@ -33,17 +35,27 @@ def f_httphead(self, origin, match, args):
info = newInfo
if header is None:
- msg = 'Status: %s (for more, try ".head uri header")' % info['Status']
- self.msg(origin.sender, msg)
+ data = []
+ if info.has_key('Status'):
+ data.append(info['Status'])
+ if info.has_key('content-type'):
+ data.append(info['content-type'].replace('; charset=', ', '))
+ if info.has_key('last-modified'):
+ modified = info['last-modified']
+ modified = time.strptime(modified, '%a, %d %b %Y %H:%M:%S %Z')
+ data.append(time.strftime('%Y-%m-%d %H:%M:%S UTC', modified))
+ if info.has_key('content-length'):
+ data.append(info['content-length'] + ' bytes')
+ phenny.reply(', '.join(data))
else:
headerlower = header.lower()
if info.has_key(headerlower):
- self.msg(origin.sender, header + ': ' + info.get(headerlower))
+ phenny.say(header + ': ' + info.get(headerlower))
else:
msg = 'There was no %s header in the response.' % header
- self.msg(origin.sender, msg)
-f_httphead.rule = (['head'], r'(\S+)(?: +(\S+))?')
-f_httphead.thread = True
+ phenny.say(msg)
+head.commands = ['head']
+head.example = '.head http://www.w3.org/'
r_title = re.compile(r'(?ims)<title[^>]*>(.*?)</title\s*>')
r_entity = re.compile(r'&[A-Za-z0-9#]+;')
@@ -52,6 +64,11 @@ r_entity = re.compile(r'&[A-Za-z0-9#]+;')
def f_title(self, origin, match, args):
""".title <URI> - Return the title of URI."""
uri = match.group(2)
+ uri = (uri or '').encode('utf-8')
+
+ if not uri and hasattr(self, 'last_seen_uri'):
+ uri = self.last_seen_uri
+
if not ':' in uri:
uri = 'http://' + uri
@@ -74,10 +91,10 @@ def f_title(self, origin, match, args):
self.msg(origin.sender, origin.nick + ": Too many redirects")
return
- try: mtype = info['Content-Type']
+ try: mtype = info['content-type']
except:
- self.msg(origin.sender, origin.nick + ": Document isn't HTML")
- return
+ err = ": Couldn't get the Content-Type, sorry"
+ return self.msg(origin.sender, origin.nick + err)
if not (('/html' in mtype) or ('/xhtml' in mtype)):
self.msg(origin.sender, origin.nick + ": Document isn't HTML")
return
@@ -119,8 +136,13 @@ def f_title(self, origin, match, args):
title = '[Title is the empty document, "".]'
self.msg(origin.sender, origin.nick + ': ' + title)
else: self.msg(origin.sender, origin.nick + ': No title found')
-f_title.rule = (['title'], r'(\S+)')
-f_title.thread = True
+f_title.commands = ['title']
+
+def noteuri(phenny, input):
+ uri = input.group(1).encode('utf-8')
+ phenny.bot.last_seen_uri = uri
+noteuri.rule = r'.*(http://[^<> "]+)[,.]?'
+noteuri.priority = 'low'
if __name__ == '__main__':
print __doc__