1 ;;; eudcb-ldap.el --- Emacs Unified Directory Client - LDAP Backend
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <oscar@xemacs.org>
6 ;; Maintainer: Oscar Figueiredo <oscar@xemacs.org>
8 ;; Version: $Revision: 1.2 $
11 ;; This file is part of XEmacs
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
29 ;; This library provides specific LDAP protocol support for the
30 ;; Emacs Unified Directory Client package
33 ;; Install EUDC first. See EUDC documentation.
41 ;;{{{ Internal cooking
44 (if (fboundp 'ldap-get-host-parameter)
45 (fset 'eudc-ldap-get-host-parameter 'ldap-get-host-parameter)
46 (defun eudc-ldap-get-host-parameter (host parameter)
47 "Get the value of PARAMETER for HOST in `ldap-host-parameters-alist'."
48 (plist-get (cdr (assoc host ldap-host-parameters-alist))
51 (defvar eudc-ldap-attributes-translation-alist
53 (firstname . givenname)
55 (phone . telephonenumber))
56 "Alist mapping EUDC attribute names to LDAP names.")
58 (eudc-protocol-set 'eudc-query-function 'eudc-ldap-simple-query-internal
60 (eudc-protocol-set 'eudc-list-attributes-function 'eudc-ldap-get-field-list
62 (eudc-protocol-set 'eudc-protocol-attributes-translation-alist
63 'eudc-ldap-attributes-translation-alist 'ldap)
64 (eudc-protocol-set 'eudc-bbdb-conversion-alist
65 'eudc-ldap-bbdb-conversion-alist
67 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil 'ldap)
68 (eudc-protocol-set 'eudc-attribute-display-method-alist
69 '(("jpegphoto" . eudc-display-jpeg-inline)
70 ("labeledurl" . eudc-display-url)
71 ("audio" . eudc-display-sound)
72 ("labeledurl" . eudc-display-url)
73 ("url" . eudc-display-url))
75 (eudc-protocol-set 'eudc-switch-to-server-hook
76 '(eudc-ldap-check-base)
79 (defun eudc-ldap-cleanup-record-simple (record)
80 "Do some cleanup in a RECORD to make it suitable for EUDC."
84 (cons (intern (car field))
90 (defun eudc-filter-$ (string)
91 (mapconcat 'identity (split-string string "\\$") "\n"))
93 ;; Cleanup a LDAP record to make it suitable for EUDC:
94 ;; Make the record a cons-cell instead of a list if the it's single-valued
95 ;; Filter the $ character in addresses into \n if not done by the LDAP lib
96 (defun eudc-ldap-cleanup-record-filtering-addresses (record)
100 (let ((name (intern (car field)))
102 (if (memq name '(postaladdress registeredaddress))
103 (setq value (mapcar 'eudc-filter-$ value)))
110 (defun eudc-ldap-simple-query-internal (query &optional return-attrs)
111 "Query the LDAP server with QUERY.
112 QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid
113 LDAP attribute names.
114 RETURN-ATTRS is a list of attributes to return, defaulting to
115 `eudc-default-return-attributes'."
116 (let ((result (ldap-search (eudc-ldap-format-query-as-rfc1558 query)
118 (if (listp return-attrs)
119 (mapcar 'symbol-name return-attrs))))
121 (if (or (not (boundp 'ldap-ignore-attribute-codings))
122 ldap-ignore-attribute-codings)
124 (mapcar 'eudc-ldap-cleanup-record-filtering-addresses result))
125 (setq result (mapcar 'eudc-ldap-cleanup-record-simple result)))
127 (if (and eudc-strict-return-matches
129 (not (eq 'all return-attrs)))
130 (setq result (eudc-filter-partial-records result return-attrs)))
131 ;; Apply eudc-duplicate-attribute-handling-method
132 (if (not (eq 'list eudc-duplicate-attribute-handling-method))
134 (function (lambda (record)
136 (append (eudc-filter-duplicate-attributes record)
141 (defun eudc-ldap-get-field-list (dummy &optional objectclass)
142 "Return a list of valid attribute names for the current server.
143 OBJECTCLASS is the LDAP object class for which the valid
144 attribute names are returned. Default to `person'"
147 (call-interactively 'eudc-set-server))
148 (let ((ldap-host-parameters-alist
149 (list (cons eudc-server
150 '(scope subtree sizelimit 1)))))
151 (mapcar 'eudc-ldap-cleanup-record
153 (eudc-ldap-format-query-as-rfc1558
154 (list (cons "objectclass"
157 eudc-server nil t))))
159 (defun eudc-ldap-escape-query-special-chars (string)
160 "Value is STRING with characters forbidden in LDAP queries escaped."
161 ;; Note that * should also be escaped but in most situations I suppose
162 ;; the user doesn't want this
163 (eudc-replace-in-string
164 (eudc-replace-in-string
165 (eudc-replace-in-string
166 (eudc-replace-in-string
171 (char-to-string ?\0) "\\00"))
173 (defun eudc-ldap-format-query-as-rfc1558 (query)
174 "Format the EUDC QUERY list as a RFC1558 LDAP search filter."
177 (mapcar '(lambda (item)
180 (eudc-ldap-escape-query-special-chars (cdr item))))
186 ;;{{{ High-level interfaces (interactive functions)
188 (defun eudc-ldap-customize ()
189 "Customize the EUDC LDAP support."
191 (customize-group 'eudc-ldap))
193 (defun eudc-ldap-check-base ()
194 "Check if the current LDAP server has a configured search base."
195 (unless (or (eudc-ldap-get-host-parameter eudc-server 'base)
197 (null (y-or-n-p "No search base defined. Configure it now ?")))
198 ;; If the server is not in ldap-host-parameters-alist we add it for the
200 (if (null (assoc eudc-server ldap-host-parameters-alist))
201 (setq ldap-host-parameters-alist
202 (cons (list eudc-server) ldap-host-parameters-alist)))
203 (customize-variable 'ldap-host-parameters-alist)))
208 (eudc-register-protocol 'ldap)
210 (provide 'eudcb-ldap)
212 ;;; eudcb-ldap.el ends here