forked from jkitchin/scimax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scimax-ldap.el
215 lines (196 loc) · 6.24 KB
/
scimax-ldap.el
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
;;; scimax-ldap.el --- An ivy interface to LDAP services
;;; Commentary:
;; An ivy interface to an LDAP server
;;; Code:
(defvar ldap-server "ldap.andrew.cmu.edu"
"Hostname of your LDAP server.")
(defun ldap-query (query-string)
"Send QUERY-STRING to our ldap server.
Parse results into a list of p-lists for each entry returned."
(interactive "sLDAP query: ")
(let ((output (butlast (split-string
(shell-command-to-string
(format (concat "ldapsearch -x -LLL "
"-h %s "
"-b ou=Person,dc=cmu,dc=edu %s")
ldap-server
query-string))
"\n")))
(lines '())
(result '())
(results '(())))
;; cleanup trailing lines and ignore initial lines
(loop for line in output
do
(cond
;; ignore starting lines
((s-starts-with? "SASL" line)
nil)
;; join lines that run over
((s-starts-with? " " line)
(setf (car (last lines))
(concat (car (last lines)) line)))
;; ignore this
((string-match "Size limit exceeded" line)
nil)
(t
(add-to-list 'lines line t))))
;; now we need to parse the lines. A new entry starts with a dn: line.
(dolist (line lines)
(cond
((s-starts-with? "dn:" line)
;; add new entry
(add-to-list 'results `(:dn ,line)))
((string-match ":" line)
(let* ((s (split-string line ":"))
(prop (intern (concat ":" (s-trim (car s)))))
(val (s-trim (cadr s))))
(setf (car results) (plist-put (car results) prop val))))))
;; last result seems to be nil so we drop it
(-filter (lambda (x) (not (null x))) results)))
;; [2020-07-28 Tue] This seems to be broken now, commenting it out.
;; (defun cmu-directory (name)
;; "Look up NAME in CMU directory.
;; NAME can be partial name or andrewid."
;; (interactive "sNAme or AndrewID: ")
;; (let* ((url-request-method "POST")
;; (url-request-data
;; (mapconcat
;; 'identity
;; `(,(format "search[generic_search_terms]=%s" name)
;; "authenticity_token=foGA4weA2fcDn3HIQ4eSeLUxss/g9pujB/zIIcnvl5U="
;; "commit=Search")
;; "&"))
;; (url "https://directory.andrew.cmu.edu/search/basic/results"))
;; (with-current-buffer (url-retrieve-synchronously url)
;; (let ((content (buffer-substring
;; url-http-end-of-headers (point-max)))
;; (fname (make-temp-file "cmu" nil ".html")))
;; (setq content
;; (replace-regexp-in-string
;; "href=\"/search"
;; "href=\"https://directory.andrew.cmu.edu/search"
;; content))
;; (setq content
;; (replace-regexp-in-string
;; "action=\"/search"
;; "action=\"https://directory.andrew.cmu.edu/search"
;; content))
;; (with-temp-file fname
;; (insert content))
;; (browse-url fname)))))
(defun ivy-ldap (query-string)
(interactive "sLDAP query: ")
(let ((candidates (mapcar
(lambda (x)
(cons
(format
"%20s|%30s|%30s|%20s|%s"
(s-truncate
20
(or (plist-get x :title) " "))
(or (plist-get x :cn) "")
(or (plist-get x :mail) "")
(or (plist-get x :cmuDisplayAddress) "")
(or (plist-get x :telephoneNumber) " "))
x))
(ldap-query
(if (string-match "=" query-string)
query-string
(concat "cn=*" query-string "*"))))))
(ivy-read "Select: " candidates
:action '(1
("e" (lambda (x)
(compose-mail)
(message-goto-to)
(insert (or
(plist-get x :cmuPreferredMail)
(plist-get x :mail)
(concat (plist-get x :cmuAndrewID)
"@andrew.cmu.edu")))
(message-goto-subject))
"Email")
("P" (lambda (x)
(org-entry-put (point) "EMAIL" (or
(plist-get (cdr x) :cmuPreferredMail)
(plist-get (cdr x) :mail)
(concat (plist-get (cdr x) :cmuAndrewID)
"@andrew.cmu.edu"))))
"Add EMAIL property")
;; ("p" (lambda (x)
;; (cisco-call
;; (plist-get (cdr x) :telephoneNumber)))
;; "Call")
("c" (lambda (x)
(org-insert-heading)
(insert (plist-get (cdr x) :cn) "\n")
(cl-loop for key in (plist-get-keys (cdr x))
do
(insert (format "- %s :: %s\n" key (plist-get (cdr x) key))))
(org-entry-put (point) "EMAIL" (or
(plist-get (cdr x) :cmuPreferredMail)
(plist-get (cdr x) :mail)
(concat (plist-get (cdr x) :cmuAndrewID)
"@andrew.cmu.edu"))))
"Insert contact heading")
("w" (lambda (x)
(kill-new
(format
"%s <%s>"
(plist-get (cdr x) :cn)
(plist-get (cdr x) :mail))))
"Copy name and email")
("n" (lambda (x)
(insert
(format
"%s <%s>"
(plist-get (cdr x) :cn)
(or
(plist-get (cdr x) :cmuPreferredMail)
(plist-get (cdr x) :mail)
(concat (plist-get (cdr x) :cmuAndrewID)
"@andrew.cmu.edu")))))
"insert name and email")
("f" (lambda (x)
(switch-to-buffer
(get-buffer-create "*ivy ldap*"))
(erase-buffer)
(org-mode)
(dolist (key (cl-loop
for key in x by #'cddr
collect key))
(insert (format "|%s | %s|\n"
key (plist-get (cdr x) key))))
(previous-line)
(org-ctrl-c-ctrl-c)
(goto-char (point-max))
(insert "\n")
(when (plist-get x :telephoneNumber)
(insert
(format "[[elisp:(cisco-call \"%s\")][Call]] "
(plist-get x :telephoneNumber))))
(when (or
(plist-get (cdr x) :cmuPreferredMail)
(plist-get (cdr x) :mail)
(concat (plist-get (cdr x) :cmuAndrewID)
"@andrew.cmu.edu"))
(insert
(format "[[elisp:(progn (compose-mail) (message-goto-to) (insert \"%s\")(message-goto-subject))][Send email]]"
(or
(plist-get (cdr x) :cmuPreferredMail)
(plist-get (cdr x) :mail)
(concat (plist-get (cdr x) :cmuAndrewID)
"@andrew.cmu.edu")))))
(insert "\n\npress q to quit.")
(setq buffer-read-only t)
(use-local-map (copy-keymap org-mode-map))
(local-set-key "q"
#'(lambda ()
(interactive)
(quit-window t))))
"Information")
("r" (lambda (candidate)
(ivy-ldap (read-string "LDAP Query: ")))
"New search")))))
(provide 'scimax-ldap)
;;; scimax-ldap.el ends here