def val(ircbot, input): """Check a webpage using the W3C Markup Validator.""" if not input.group(2): return ircbot.reply("Nothing to validate.") uri = input.group(2) if not uri.startswith('http://'): uri = 'http://' + uri path = '/check?uri=%s;output=xml' % web.urllib.quote(uri) info = web.head('http://validator.w3.org' + path) result = uri + ' is ' if isinstance(info, list): return ircbot.say('Got HTTP response %s' % info[1]) if info.has_key('X-W3C-Validator-Status'): result += str(info['X-W3C-Validator-Status']) if info['X-W3C-Validator-Status'] != 'Valid': if info.has_key('X-W3C-Validator-Errors'): n = int(info['X-W3C-Validator-Errors'].split(' ')[0]) if n != 1: result += ' (%s errors)' % n else: result += ' (%s error)' % n else: result += 'Unvalidatable: no X-W3C-Validator-Status' ircbot.reply(result)
def head(ircbot, 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(ircbot, 'last_seen_uri'): try: uri = ircbot.last_seen_uri[input.sender] except KeyError: return ircbot.say('?') if not uri.startswith('htt'): uri = 'http://' + uri try: info = web.head(uri) except IOError: return ircbot.say("Can't connect to %s" % uri) except httplib.InvalidURL: return ircbot.say("Not a valid URI, sorry.") if not isinstance(info, list): try: info = dict(info) except TypeError: return ircbot.reply( 'Try .head http://example.org/ [optional header]') info['Status'] = '200' else: newInfo = dict(info[0]) newInfo['Status'] = str(info[1]) info = newInfo if header is None: 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') ircbot.reply(', '.join(data)) else: headerlower = header.lower() if info.has_key(headerlower): ircbot.say(header + ': ' + info.get(headerlower)) else: msg = 'There was no %s header in the response.' % header ircbot.say(msg)
def head(ircbot, 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(ircbot, 'last_seen_uri'): try: uri = ircbot.last_seen_uri[input.sender] except KeyError: return ircbot.say('?') if not uri.startswith('htt'): uri = 'http://' + uri try: info = web.head(uri) except IOError: return ircbot.say("Can't connect to %s" % uri) except httplib.InvalidURL: return ircbot.say("Not a valid URI, sorry.") if not isinstance(info, list): try: info = dict(info) except TypeError: return ircbot.reply('Try .head http://example.org/ [optional header]') info['Status'] = '200' else: newInfo = dict(info[0]) newInfo['Status'] = str(info[1]) info = newInfo if header is None: 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') ircbot.reply(', '.join(data)) else: headerlower = header.lower() if info.has_key(headerlower): ircbot.say(header + ': ' + info.get(headerlower)) else: msg = 'There was no %s header in the response.' % header ircbot.say(msg)
def service(ircbot, input, command, args): t = o.services[command] template = t.replace('${args}', urllib.quote(args.encode('utf-8'), '')) template = template.replace('${nick}', urllib.quote(input.nick, '')) uri = template.replace('${sender}', urllib.quote(input.sender, '')) info = web.head(uri) if isinstance(info, list): info = info[0] if not 'text/plain' in info.get('content-type', '').lower(): return ircbot.reply("Sorry, the service didn't respond in plain text.") bytes = web.get(uri) lines = bytes.splitlines() if not lines: return ircbot.reply("Sorry, the service didn't respond any output.") ircbot.say(lines[0][:350])