Skip to content
Newer
Older
100644 94 lines (71 sloc) 2.86 KB
c900e15 start off with what was in eighthbit-website
James S authored
1 (defpackage #:nisp.ldap
ab17ba9 move 8b stuff to its own package
James S authored
2 (:use :cl)
1a07d66 add one-line-ldif
James S authored
3 (:export :*connections* #:with-ldap #:get-single-entry
0a1202a add describing-ldif-search
James S authored
4 #:one-line-ldif #:make-ldap #:describing-ldif-search))
c900e15 start off with what was in eighthbit-website
James S authored
5
6 (in-package :nisp.ldap)
7
4410cb0 move while out of my way
James S authored
8 ;;; Needs to be moved to a seperate utility file or something.
9 (defmacro while (test &body body)
10 "While, like in most other languages."
11 `(do ()
12 ((not ,test))
13 ,@body))
14
c900e15 start off with what was in eighthbit-website
James S authored
15
b72cbcf defparameter instead of defvar
James S authored
16 (defvar *connections* '()
f27e4e7 add *connections* to hold a list of connections for 8b
James S authored
17 "Property list of connections to LDAP.")
18
c900e15 start off with what was in eighthbit-website
James S authored
19 ;;; Load an optional config file, the lack of this should not cause this
20 ;;; program to become unusable.
7b8e767 define 8b related ldap objects in their own file as well as get the
James S authored
21
c900e15 start off with what was in eighthbit-website
James S authored
22
a99a82d add make-ldap, given a keyword or an ldap:ldap object, return an
James S authored
23
24 (defun make-ldap (ldap-or-keyword
25 &optional (connections *connections*))
26 "Return an LDAP object, so long as input is an ldap object or a
27 keyword referincing an ldap object."
28 (if (typep ldap-or-keyword 'ldap:ldap)
29 ldap-or-keyword
30 (getf connections ldap-or-keyword)))
31
ab17ba9 move 8b stuff to its own package
James S authored
32
c900e15 start off with what was in eighthbit-website
James S authored
33
abb5d46 fix up with-ldap to make use of make-ldap
James S authored
34 (defmacro with-ldap (ldap-or-keyword &body body)
c900e15 start off with what was in eighthbit-website
James S authored
35 "Execute BODY in the context of LDAP bound to the ldap server."
abb5d46 fix up with-ldap to make use of make-ldap
James S authored
36 ;; I think this is fine to do instead of using gensyms. I'll consult
37 ;; on lisp on this later.
38 (let ((ldap (gensym)))
39 `(let ((,ldap (make-ldap ,ldap-or-keyword)))
40 (prog2
41 (ldap:bind ,ldap)
42 (progn ,@body)
43 (ldap:unbind ,ldap)))))
c900e15 start off with what was in eighthbit-website
James S authored
44
0a1202a add describing-ldif-search
James S authored
45 (defun describing-ldif-search (search-string &optional (ldap :anon))
46 "Development helper that prints a description of all search matches to
47 standard output in ldif form."
48 (ldap:dosearch (it (ldap:search (make-ldap ldap) search-string))
49 (princ (concatenate 'string (ldap:ldif it) (list #\NewLine)))))
50
51
c900e15 start off with what was in eighthbit-website
James S authored
52 (defun strip-newlines (string &optional (replace-char nil))
53 "Given a string, remove all newlines.
54
55 This is very irc specific where lines need to be all on one line.
56
57 Note that the newline is not replaced by a space!"
58 (coerce
59 (loop for char in (coerce string 'list)
60 when (and replace-char (eq char #\Newline)) collect replace-char
61 unless (eq char #\Newline) collect char)
62 'string))
63
775ace2 split print-single-entry up so we have get-single-entry
James S authored
64 (defun get-single-entry (search-string &key (ldap :anon)
65 attrs)
66 "Get a single trivial-ldap:entry object by binding and searching."
67 (with-ldap ldap
68 (ldap:search (make-ldap ldap) search-string
69 :attributes attrs)
70 (ldap:next-search-result (make-ldap ldap))))
71
1a07d66 add one-line-ldif
James S authored
72 (defgeneric one-line-ldif (entry)
73 (:documentation "ldif on one line.")
74 (:method ((entry ldap:entry))
75 (strip-newlines (ldap:ldif entry) #\ )))
76
02adada we should also default to the keyword :anon
James S authored
77 (defun print-single-entry (search-string &key (ldap :anon)
c900e15 start off with what was in eighthbit-website
James S authored
78 attrs)
79 (strip-newlines
80 (ldap:ldif
959fa93 lets use make-ldap to ensure we pass an ldap object.
James S authored
81 (get-single-entry search-string :ldap (make-ldap ldap) :attrs attrs))
c900e15 start off with what was in eighthbit-website
James S authored
82 #\ ))
83
ab17ba9 move 8b stuff to its own package
James S authored
84 (defun list-search-results (search-string &optional (ldap :anon))
c900e15 start off with what was in eighthbit-website
James S authored
85 "List of entries from a search."
86 (with-ldap ldap
87 (ldap:search ldap search-string))
88 (let (result)
89 (while (ldap:results-pending-p ldap)
90 (push (ldap:next-search-result ldap) result))
23b1f81 fix typo of localhost
James S authored
91 (nreverse (cdr result))))
92
93
Something went wrong with that request. Please try again.