forked from mutantmonkey/hokiestalker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhs
executable file
·115 lines (86 loc) · 3.27 KB
/
hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python3
###############################################################################
# hs.py - Hokie Stalker
# Query the Virginia Tech people search service for information about a person.
# Available under the ISC License.
#
# https://github.com/mutantmonkey/hokiestalker
# author: mutantmonkey <[email protected]>
###############################################################################
import sys
import urllib.parse
import urllib.request
from hokiestalker import dsml
from hokiestalker import parse_addr
SEARCH_URL = "https://webapps.middleware.vt.edu/peoplesearch/PeopleSearch?"\
"query={0}&dsml-version=2"
def row(rows, name, data):
"""Return a formatted row for printing."""
if data is None:
return
if type(data) == str:
rows.append("{0:20s}{1}".format(name + ':', data))
else:
rows.append("{0:20s}{1}".format(name + ':', data[0]))
# print additional lines if necessary, trimming off the first row
if len(data) > 1:
for line in data[1:]:
rows.append("{0:20s}{1}".format('', line))
def search(query):
"""Search LDAP using the argument as a query. Argument must be
a valid LDAP query."""
query = urllib.parse.quote(query)
r = urllib.request.Request(SEARCH_URL.format(query), headers={
'User-agent': 'hokiestalker/2.0',
})
f = urllib.request.urlopen(r)
has_results = False
results = dsml.DSMLParser(f)
for entry in results:
has_results = True
rows = []
names = []
if hasattr(entry, 'displayName'):
names.append(entry.displayName)
if hasattr(entry, 'givenName') and hasattr(entry, 'sn'):
if hasattr(entry, 'middleName'):
names.append('{0} {1} {2}'.format(
entry.givenName,
entry.middleName,
entry.sn))
else:
names.append('{0} {1}'.format(
entry.givenName,
entry.sn))
row(rows, 'Name', names)
if hasattr(entry, 'uid'):
row(rows, 'UID', entry.uid)
if hasattr(entry, 'uupid'):
row(rows, 'PID', entry.uupid)
if hasattr(entry, 'major'):
row(rows, 'Major', entry.major)
elif hasattr(entry, 'department'):
row(rows, 'Department', entry.department)
if hasattr(entry, 'title'):
row(rows, 'Title', entry.title)
if hasattr(entry, 'postalAddress'):
row(rows, 'Office', parse_addr(entry.postalAddress))
if hasattr(entry, 'mailStop'):
row(rows, 'Mail Stop', entry.mailStop)
if hasattr(entry, 'telephoneNumber'):
row(rows, 'Office Phone', entry.telephoneNumber)
if hasattr(entry, 'localPostalAddress'):
row(rows, 'Mailing Address', parse_addr(
entry.localPostalAddress))
if hasattr(entry, 'localPhone'):
row(rows, 'Phone Number', entry.localPhone)
if hasattr(entry, 'mail'):
row(rows, 'Email Address', entry.mail)
print("\n".join(rows))
print()
return has_results
if __name__ == '__main__':
q = sys.argv[1:]
s = search(' '.join(q))
if not s:
print("No results found")