#!/usr/bin/env python
###
# Copyright (c) 2002, Jeremiah Fincher
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Provides several commands that go out to websites and get things.
"""
from baseplugin import *
import re
import sets
import time
import random
import urllib
import urllib2
import utils
import debug
import privmsgs
import callbacks
import structures
class FreshmeatException(Exception):
pass
class Http(callbacks.Privmsg):
threaded = True
def __init__(self):
callbacks.Privmsg.__init__(self)
self.deepthoughtq = structures.queue()
self.deepthoughts = sets.Set()
def deepthought(self, irc, msg, args):
"""takes no arguments
Returns a Deep Thought by Jack Handey.
"""
url = 'http://www.tremorseven.com/aim/deepaim.php?job=view'
thought = ' ' * 512
now = time.time()
while self.deepthoughtq and now - self.deepthoughtq[0][0] > 86400:
s = self.deepthoughtq.dequeue()[1]
self.deepthoughts.remove(s)
while len(thought) > 430 or thought in self.deepthoughts:
fd = urllib2.urlopen(url)
s = fd.read()
thought = s.split('
')[2]
thought = ' '.join(thought.split())
self.deepthoughtq.enqueue((now, thought))
self.deepthoughts.add(thought)
irc.reply(msg, thought)
_titleRe = re.compile(r'
\n', 2)[1]
text = text.replace('.\n', '. ')
text = text.replace('\n', ' ')
text = utils.htmlToText(text)
text = text.strip()
if text:
irc.reply(msg, text)
else:
search = urllib.unquote(search)
s = 'There appears to be no definition for %s.' % search
irc.reply(msg, s)
_zipcode = re.compile(r'Local Forecast for (.*), (.*?) ')
def zipcode(self, irc, msg, args):
""" (.*?)]+>(?:)?([^<]+)(?:)?')
def acronym(self, irc, msg, args):
"""
Displays the first acronym matches from acronymfinder.com
"""
acronym = privmsgs.getArgs(args)
try:
url = 'http://www.acronymfinder.com/' \
'af-query.asp?String=exact&Acronym=%s' % acronym
request = urllib2.Request(url, headers={'User-agent':
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)'})
fd = urllib2.urlopen(request)
except urllib2.URLError:
irc.error(msg, 'Couldn\'t connect to acronymfinder.com')
return
html = fd.read()
fd.close()
# The following definitions are stripped and empties are removed.
defs = filter(None, map(str.strip, self._acronymre.findall(html)))
debug.printf(defs)
if len(defs) == 0:
irc.reply(msg, 'No definitions found.')
else:
s = ircutils.privmsgPayload(defs, ', or ')
irc.reply(msg, '%s could be %s' % (acronym, s))
_netcraftre = re.compile(r'whatos text -->(.*?)
Returns Netcraft.com's determination of what operating system and
webserver is running on the host given.
"""
hostname = privmsgs.getArgs(args)
url = 'http://uptime.netcraft.com/up/graph/?host=%s' % hostname
fd = urllib2.urlopen(url)
html = fd.read()
fd.close()
m = self._netcraftre.search(html)
if m:
html = m.group(1)
s = utils.htmlToText(html, tagReplace='').strip('\xa0 ')
irc.reply(msg, s[9:]) # Snip off "the site"
elif html.find('We could not get any results') != -1:
irc.reply(msg, 'No results found for %s.' % hostname)
else:
irc.error(msg, 'The format of the was odd.')
_debreflags = re.DOTALL | re.IGNORECASE
_debpkgre = re.compile(r' (\S+)\s*?td>', _debreflags)
_debtablere = re.compile(r' ]*>(.*?)
', _debreflags)
_debnumpkgsre = re.compile(r'out of total of (\d+)', _debreflags)
_debBranches = ('stable', 'testing', 'unstable', 'experimental')
def debversion(self, irc, msg, args):
"""