hs: tweak search parameters, handle exceptions more gracefully

master
mutantmonkey 2011-08-31 14:05:47 -04:00
parent 8768c00063
commit eba3a3acd5
1 changed files with 36 additions and 29 deletions

View File

@ -1,13 +1,15 @@
#!/usr/bin/python2 #!/usr/bin/python2
""" """
hs.py - hokie stalker module hs.py - hokie stalker module
author: mutantmonkey <mutantmonkey@gmail.com> author: mutantmonkey <mutantmonkey@mutantmonkey.in>
""" """
import ldap import ldap
from urllib import quote as urlquote from urllib import quote as urlquote
LDAP_URI = "ldap://directory.vt.edu" LDAP_URI = "ldap://directory.vt.edu"
RESULTS_URL = "http://search.vt.edu/search/people.html?q={0}"
PERSON_URL = "http://search.vt.edu/search/person.html?person={0:d}"
l = ldap.initialize(LDAP_URI) l = ldap.initialize(LDAP_URI)
@ -26,24 +28,29 @@ 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))
# initially try search by PID try:
s = search('uupid=%s' % q) s = search('(|(uupid={0})(mail={0})(cn={1}))'.format(q[0], ' '.join(q)))
# try partial search on CN if no results for PID
if not s: if not s:
s = search('cn=*%s*' % '*'.join(q.split(' '))) s = search('(|(uupid=*{0}*)(mail=*{0}*)(cn=*{1}*))'.format(q[0], '*'.join(q)))
except ldap.FILTER_ERROR:
# try email address if no results found for PID or CN phenny.reply('Filter error; try to avoid injection attacks in the future please.')
if not s: return
s = search('mail=%s*' % q) 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
if s: if s:
if len(s) >1: if len(s) >1:
phenny.reply("Multiple results found; try http://search.vt.edu/search/people.html?q=%s" % urlquote(q)) phenny.reply("Multiple results found; try {0}".format(results))
else: else:
for dh, entry in s: for dh, entry in s:
phenny.reply("%s - http://search.vt.edu/search/person.html?person=%d" % (entry['cn'][0], int(entry['uid'][0]))) person = PERSON_URL.format(int(entry['uid'][0]))
phenny.reply("{0} - {1}".format(entry['cn'][0], person))
else: else:
phenny.reply("No results found") phenny.reply("No results found")
hs.rule = (['hs'], r'(.*)') hs.rule = (['hs'], r'(.*)')