fix hs module to work with python3

master
mutantmonkey 2011-10-19 18:56:04 -04:00
parent 029ef79458
commit 4990fb63cf
2 changed files with 30 additions and 29 deletions

View File

@ -1,25 +1,39 @@
#!/usr/bin/python2 #!/usr/bin/python3
""" """
hs.py - hokie stalker module hs.py - hokie stalker module
author: mutantmonkey <mutantmonkey@mutantmonkey.in> author: mutantmonkey <mutantmonkey@mutantmonkey.in>
""" """
import ldap import web
from urllib.parse import quote as urlquote import lxml.etree
LDAP_URI = "ldap://directory.vt.edu" SEARCH_URL = "https://webapps.middleware.vt.edu/peoplesearch/PeopleSearch?query={0}&dsml-version=2"
RESULTS_URL = "http://search.vt.edu/search/people.html?q={0}" RESULTS_URL = "http://search.vt.edu/search/people.html?q={0}"
PERSON_URL = "http://search.vt.edu/search/person.html?person={0:d}" PERSON_URL = "http://search.vt.edu/search/person.html?person={0:d}"
NS = NS = '{urn:oasis:names:tc:DSML:2:0:core}'
l = ldap.initialize(LDAP_URI) """Search the people search database using the argument as a query."""
"""Search LDAP using the argument as a query. Argument must be a valid LDAP query."""
def search(query): def search(query):
result = l.search_s('ou=People,dc=vt,dc=edu', ldap.SCOPE_SUBTREE, query) query = web.quote(query)
if len(result) <= 0: try:
req = web.get(SEARCH_URL.format(query))
except (HTTPError, IOError):
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
return
xml = lxml.etree.fromstring(req.encode('utf-8'))
results = xml.findall('{0}searchResponse/{0}searchResultEntry'.format(NS))
if len(results) <= 0:
return False return False
return result ret = []
for entry in results:
entry_data = {}
for attr in entry:
entry_data[attr.attrib['name']] = attr[0].text
ret.append(entry_data)
return ret
def hs(phenny, input): def hs(phenny, input):
""".hs <pid/name/email> - Search for someone on Virginia Tech People Search.""" """.hs <pid/name/email> - Search for someone on Virginia Tech People Search."""
@ -28,29 +42,16 @@ def hs(phenny, input):
if q is None: if q is None:
return return
q = q.strip() q = q.strip()
results = RESULTS_URL.format(urlquote(q)) results = RESULTS_URL.format(web.quote(q))
try:
s = search('(|(uupid={0})(mail={0})(cn={0}))'.format(q))
if not s:
s = search('(|(uupid=*{0}*)(mail=*{0}*)(cn=*{1}*))'.format(q, '*'.join(q.split(' '))))
except ldap.FILTER_ERROR:
phenny.reply('Filter error; try to avoid injection attacks in the future please.')
return
except ldap.SIZELIMIT_EXCEEDED:
phenny.reply('Too many results to display here; check out {0}'.format(results))
return
except ldap.TIMELIMIT_EXCEEDED:
phenny.reply('Time limit exceeded; check out {0}'.format(results))
return
s = search(q)
if s: if s:
if len(s) >1: if len(s) >1:
phenny.reply("Multiple results found; try {0}".format(results)) phenny.reply("Multiple results found; try {0}".format(results))
else: else:
for dh, entry in s: for entry in s:
person = PERSON_URL.format(int(entry['uid'][0])) person = PERSON_URL.format(int(entry['uid']))
phenny.reply("{0} - {1}".format(entry['cn'][0], person)) phenny.reply("{0} - {1}".format(entry['cn'], person))
else: else:
phenny.reply("No results found") phenny.reply("No results found")
hs.rule = (['hs'], r'(.*)') hs.rule = (['hs'], r'(.*)')

View File

@ -1,4 +1,4 @@
#!/usr/bin/python2 #!/usr/bin/python3
""" """
tfw.py - the fucking weather module tfw.py - the fucking weather module
author: mutantmonkey <mutantmonkey@gmail.com> author: mutantmonkey <mutantmonkey@gmail.com>