Advertisement
NovaYoshi

phoney

Dec 14th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.36 KB | None | 0 0
  1. __module_name__ = "Phoney"
  2. __module_author__ = "NovaSquirrel"
  3. __module_description__ = "Misc phenny/skybot theft module"
  4. __module_version__ = "0.03"
  5.  
  6. import xchat
  7. import random
  8.  
  9. import re, urllib, gzip, StringIO, socket, hashlib
  10. from htmlentitydefs import name2codepoint
  11.  
  12. #######################################################################################
  13.  
  14. class Grab(urllib.URLopener):
  15.    def __init__(self, *args):
  16.       self.version = 'Mozilla/5.0 (Phenny)'
  17.       urllib.URLopener.__init__(self, *args)
  18.       self.addheader('Referer', 'https://github.com/sbp/phenny')
  19.    def http_error_default(self, url, fp, errcode, errmsg, headers):
  20.       return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
  21. urllib._urlopener = Grab()
  22.  
  23. def webget(uri):
  24.    if not uri.startswith('http'):
  25.       return
  26.    u = urllib.urlopen(uri)
  27.    bytes = u.read()
  28.    u.close()
  29.    return bytes
  30.  
  31. def webhead(uri):
  32.    if not uri.startswith('http'):
  33.       return
  34.    u = urllib.urlopen(uri)
  35.    info = u.info()
  36.    u.close()
  37.    return info
  38.  
  39. def webpost(uri, query):
  40.    if not uri.startswith('http'):
  41.       return
  42.    data = urllib.urlencode(query)
  43.    u = urllib.urlopen(uri, data)
  44.    bytes = u.read()
  45.    u.close()
  46.    return bytes
  47.  
  48. r_entity = re.compile(r'&([^;\s]+);')
  49.  
  50. def webentity(match):
  51.    value = match.group(1).lower()
  52.    if value.startswith('#x'):
  53.       return unichr(int(value[2:], 16))
  54.    elif value.startswith('#'):
  55.       return unichr(int(value[1:]))
  56.    elif name2codepoint.has_key(value):
  57.       return unichr(name2codepoint[value])
  58.    return '[' + value + ']'
  59.  
  60. def webdecode(html):
  61.    return r_entity.sub(entity, html)
  62.  
  63. r_string = re.compile(r'("(\\.|[^"\\])*")')
  64. r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
  65. env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
  66.  
  67. def webjson(text):
  68.    """Evaluate JSON text safely (we hope)."""
  69.    if r_json.match(r_string.sub('', text)):
  70.       text = r_string.sub(lambda m: 'u' + m.group(1), text)
  71.       return eval(text.strip(' \t\r\n'), env, {})
  72.    raise ValueError('Input must be serialised JSON.')
  73. #######################################################################################
  74.  
  75. #######################################################################################
  76.  
  77. def SafeAnswer(answer):
  78.     answer = (answer[:420] + '..') if len(answer) > 420 else answer
  79.     return answer.replace('\n', '').replace('\r', '')
  80.  
  81. def Suggest(query):
  82.     uri = 'http://websitedev.de/temp-bin/suggest.pl?q='
  83.     answer = webget(uri + urllib.quote(query).replace('+', '%2B'))
  84.     if answer:
  85.         return SafeAnswer(answer)
  86.     else:
  87.         return 'Sorry, no result.'
  88.  
  89. def google_ajax(query):
  90.    if isinstance(query, unicode):
  91.       query = query.encode('utf-8')
  92.    uri = 'http://ajax.googleapis.com/ajax/services/search/web'
  93.    args = '?v=1.0&safe=off&q=' + urllib.quote(query)
  94.    bytes = webget(uri + args)
  95.    return webjson(bytes)
  96.  
  97. def google_search(query, number):
  98.    results = google_ajax(query)
  99.    try:
  100.      result = results['responseData']['results'][number]['unescapedUrl']
  101.      return result
  102.    except IndexError: return None
  103.    except TypeError:
  104.       print results
  105.       return False
  106.  
  107. def Google4(query, ReplyCmd):
  108.    results = google_ajax(query)
  109.    try:
  110.      xchat.command(ReplyCmd + "1. " + results['responseData']['results'][0]['unescapedUrl']);
  111.      xchat.command(ReplyCmd + "2. " + results['responseData']['results'][1]['unescapedUrl']);
  112.      xchat.command(ReplyCmd + "3. " + results['responseData']['results'][2]['unescapedUrl']);
  113.      xchat.command(ReplyCmd + "4. " + results['responseData']['results'][3]['unescapedUrl']);
  114.    except IndexError: return None
  115.    except TypeError:
  116.       print results
  117.       return False
  118.  
  119. def GoogleSearch(query, number):
  120.    uri = google_search(query, number)
  121.    if uri:
  122.       return uri
  123.    elif uri is False: return "Problem getting data from Google."
  124.    else: return "No results found for '" + query + "'"
  125.  
  126.  
  127. def Python(query):
  128.    uri = 'http://tumbolia.appspot.com/py/'
  129.    answer = webget(uri + urllib.quote(query))
  130.    if answer:
  131.      return SafeAnswer(answer)
  132.    else: return "Sorry, no result"
  133.  
  134.  
  135. def ExtCmdHandler(word, word_eol, userdata):
  136.     ReplyCmd = word[1] + " "
  137.     NBCmd = word[2]
  138.     Nick = word[3]
  139.  
  140.     ArgStart = 0
  141.     ArgString = ""
  142.     for i in range(4,len(word)):
  143.         if word[i] == "-A":
  144.             if len(word) >= i+2:
  145.                 ArgStart = i+1
  146.                 ArgString = word_eol[i+1]
  147.                 break
  148.  
  149.     takes_string = ["py","suggest","g","g4","dns","md5","sha1"]
  150.     if NBCmd in takes_string:
  151.         if ArgStart == 0:
  152.             xchat.command(ReplyCmd + "Oops, that command needs parameters after the command name to work")
  153.             return xchat.EAT_ALL
  154.     if NBCmd == "py": xchat.command(ReplyCmd + Python(ArgString)); return xchat.EAT_ALL
  155.     if NBCmd == "suggest": xchat.command(ReplyCmd + Suggest(ArgString)); return xchat.EAT_ALL
  156.     if NBCmd == "g": xchat.command(ReplyCmd + GoogleSearch(ArgString, 0)); return xchat.EAT_ALL
  157.     if NBCmd == "g4": Google4(ArgString, ReplyCmd); return xchat.EAT_ALL
  158.     if NBCmd == "dns": xchat.command(ReplyCmd + socket.gethostbyname(ArgString)); return xchat.EAT_ALL
  159.     if NBCmd == "md5": xchat.command(ReplyCmd + hashlib.md5(ArgString).hexdigest()); return xchat.EAT_ALL
  160.     if NBCmd == "sha1": xchat.command(ReplyCmd + hashlib.sha1(ArgString).hexdigest()); return xchat.EAT_ALL
  161.  
  162.  
  163.     return xchat.EAT_NONE
  164.  
  165. xchat.hook_command("NB_ExtCmd", ExtCmdHandler)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement