lisp/net/{eudc,ldap}: Merge branch streamline-eudc-configuration
[emacs.git] / lisp / net / eudc.el
blob4dd80972e3fdd337b9081460fd9d29e2c0d76e25
1 ;;; eudc.el --- Emacs Unified Directory Client -*- coding: utf-8 -*-
3 ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <oscar@cpe.fr>
6 ;; Maintainer: Pavel Janík <Pavel@Janik.cz>
7 ;; Keywords: comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
25 ;; This package provides a common interface to query directory servers using
26 ;; different protocols such as LDAP, CCSO PH/QI or BBDB. Queries can be
27 ;; made through an interactive form or inline. Inline query strings in
28 ;; buffers are expanded with appropriately formatted query results
29 ;; (especially used to expand email addresses in message buffers). EUDC
30 ;; also interfaces with the BBDB package to let you register query results
31 ;; into your own BBDB database.
33 ;;; Usage:
34 ;; EUDC comes with an extensive documentation, please refer to it.
36 ;; The main entry points of EUDC are:
37 ;; `eudc-query-form': Query a directory server from a query form
38 ;; `eudc-expand-inline': Query a directory server for the e-mail address
39 ;; of the name before cursor and insert it in the
40 ;; buffer
41 ;; `eudc-get-phone': Get a phone number from a directory server
42 ;; `eudc-get-email': Get an e-mail address from a directory server
43 ;; `eudc-customize': Customize various aspects of EUDC
45 ;;; Code:
47 (require 'wid-edit)
49 (eval-and-compile
50 (if (not (fboundp 'make-overlay))
51 (require 'overlay)))
53 (unless (fboundp 'custom-menu-create)
54 (autoload 'custom-menu-create "cus-edit"))
56 (require 'eudc-vars)
60 ;;{{{ Internal cooking
62 ;;{{{ Internal variables and compatibility tricks
64 (defvar eudc-form-widget-list nil)
66 (defvar eudc-mode-map
67 (let ((map (make-sparse-keymap)))
68 (define-key map "q" 'kill-this-buffer)
69 (define-key map "x" 'kill-this-buffer)
70 (define-key map "f" 'eudc-query-form)
71 (define-key map "b" 'eudc-try-bbdb-insert)
72 (define-key map "n" 'eudc-move-to-next-record)
73 (define-key map "p" 'eudc-move-to-previous-record)
74 map))
75 (set-keymap-parent eudc-mode-map widget-keymap)
77 (defvar mode-popup-menu)
79 ;; List of variables that have server- or protocol-local bindings
80 (defvar eudc-local-vars nil)
82 ;; Protocol local. Query function
83 (defvar eudc-query-function nil)
85 ;; Protocol local. A function that retrieves a list of valid attribute names
86 (defvar eudc-list-attributes-function nil)
88 ;; Protocol local. A mapping between EUDC attribute names and corresponding
89 ;; protocol specific names. The following names are defined by EUDC and may be
90 ;; included in that list: `name' , `firstname', `email', `phone'
91 (defvar eudc-protocol-attributes-translation-alist nil)
93 ;; Protocol local. Mapping between protocol attribute names and BBDB field
94 ;; names
95 (defvar eudc-bbdb-conversion-alist nil)
97 ;; Protocol/Server local. Hook called upon switching to that server
98 (defvar eudc-switch-to-server-hook nil)
100 ;; Protocol/Server local. Hook called upon switching from that server
101 (defvar eudc-switch-from-server-hook nil)
103 ;; Protocol local. Whether the protocol supports queries with no specified
104 ;; attribute name
105 (defvar eudc-protocol-has-default-query-attributes nil)
107 (defun eudc-cadr (obj)
108 (car (cdr obj)))
110 (defun eudc-cdar (obj)
111 (cdr (car obj)))
113 (defun eudc-caar (obj)
114 (car (car obj)))
116 (defun eudc-cdaar (obj)
117 (cdr (car (car obj))))
119 (defun eudc-plist-member (plist prop)
120 "Return t if PROP has a value specified in PLIST."
121 (if (not (= 0 (% (length plist) 2)))
122 (error "Malformed plist"))
123 (catch 'found
124 (while plist
125 (if (eq prop (car plist))
126 (throw 'found t))
127 (setq plist (cdr (cdr plist))))
128 nil))
130 ;; Emacs's plist-get lacks third parameter
131 (defun eudc-plist-get (plist prop &optional default)
132 "Extract a value from a property list.
133 PLIST is a property list, which is a list of the form
134 \(PROP1 VALUE1 PROP2 VALUE2...). This function returns the value
135 corresponding to the given PROP, or DEFAULT if PROP is not
136 one of the properties on the list."
137 (if (eudc-plist-member plist prop)
138 (plist-get plist prop)
139 default))
141 (defun eudc-lax-plist-get (plist prop &optional default)
142 "Extract a value from a lax property list.
144 PLIST is a lax property list, which is a list of the form (PROP1
145 VALUE1 PROP2 VALUE2...), where comparisons between properties are done
146 using `equal' instead of `eq'. This function returns the value
147 corresponding to PROP, or DEFAULT if PROP is not one of the
148 properties on the list."
149 (if (not (= 0 (% (length plist) 2)))
150 (error "Malformed plist"))
151 (catch 'found
152 (while plist
153 (if (equal prop (car plist))
154 (throw 'found (car (cdr plist))))
155 (setq plist (cdr (cdr plist))))
156 default))
158 (if (not (fboundp 'split-string))
159 (defun split-string (string &optional pattern)
160 "Return a list of substrings of STRING which are separated by PATTERN.
161 If PATTERN is omitted, it defaults to \"[ \\f\\t\\n\\r\\v]+\"."
162 (or pattern
163 (setq pattern "[ \f\t\n\r\v]+"))
164 (let (parts (start 0))
165 (when (string-match pattern string 0)
166 (if (> (match-beginning 0) 0)
167 (setq parts (cons (substring string 0 (match-beginning 0)) nil)))
168 (setq start (match-end 0))
169 (while (and (string-match pattern string start)
170 (> (match-end 0) start))
171 (setq parts (cons (substring string start (match-beginning 0)) parts)
172 start (match-end 0))))
173 (nreverse (if (< start (length string))
174 (cons (substring string start) parts)
175 parts)))))
177 (defun eudc-replace-in-string (str regexp newtext)
178 "Replace all matches in STR for REGEXP with NEWTEXT.
179 Value is the new string."
180 (let ((rtn-str "")
181 (start 0)
182 match prev-start)
183 (while (setq match (string-match regexp str start))
184 (setq prev-start start
185 start (match-end 0)
186 rtn-str
187 (concat rtn-str
188 (substring str prev-start match)
189 newtext)))
190 (concat rtn-str (substring str start))))
192 ;;}}}
194 ;;{{{ Server and Protocol Variable Routines
196 (defun eudc-server-local-variable-p (var)
197 "Return non-nil if VAR has server-local bindings."
198 (eudc-plist-member (get var 'eudc-locals) 'server))
200 (defun eudc-protocol-local-variable-p (var)
201 "Return non-nil if VAR has protocol-local bindings."
202 (eudc-plist-member (get var 'eudc-locals) 'protocol))
204 (defun eudc-default-set (var val)
205 "Set the EUDC default value of VAR to VAL.
206 The current binding of VAR is not changed."
207 (put var 'eudc-locals
208 (plist-put (get var 'eudc-locals) 'default val))
209 (add-to-list 'eudc-local-vars var))
211 (defun eudc-protocol-set (var val &optional protocol)
212 "Set the PROTOCOL-local binding of VAR to VAL.
213 If omitted PROTOCOL defaults to the current value of `eudc-protocol'.
214 The current binding of VAR is changed only if PROTOCOL is omitted."
215 (if (eq 'unbound (eudc-variable-default-value var))
216 (eudc-default-set var (symbol-value var)))
217 (let* ((eudc-locals (get var 'eudc-locals))
218 (protocol-locals (eudc-plist-get eudc-locals 'protocol)))
219 (setq protocol-locals (plist-put protocol-locals (or protocol
220 eudc-protocol) val))
221 (setq eudc-locals
222 (plist-put eudc-locals 'protocol protocol-locals))
223 (put var 'eudc-locals eudc-locals)
224 (add-to-list 'eudc-local-vars var)
225 (unless protocol
226 (eudc-update-variable var))))
228 (defun eudc-server-set (var val &optional server)
229 "Set the SERVER-local binding of VAR to VAL.
230 If omitted SERVER defaults to the current value of `eudc-server'.
231 The current binding of VAR is changed only if SERVER is omitted."
232 (if (eq 'unbound (eudc-variable-default-value var))
233 (eudc-default-set var (symbol-value var)))
234 (let* ((eudc-locals (get var 'eudc-locals))
235 (server-locals (eudc-plist-get eudc-locals 'server)))
236 (setq server-locals (plist-put server-locals (or server
237 eudc-server) val))
238 (setq eudc-locals
239 (plist-put eudc-locals 'server server-locals))
240 (put var 'eudc-locals eudc-locals)
241 (add-to-list 'eudc-local-vars var)
242 (unless server
243 (eudc-update-variable var))))
246 (defun eudc-set (var val)
247 "Set the most local (server, protocol or default) binding of VAR to VAL.
248 The current binding of VAR is also set to VAL"
249 (cond
250 ((not (eq 'unbound (eudc-variable-server-value var)))
251 (eudc-server-set var val))
252 ((not (eq 'unbound (eudc-variable-protocol-value var)))
253 (eudc-protocol-set var val))
255 (eudc-default-set var val)))
256 (set var val))
258 (defun eudc-variable-default-value (var)
259 "Return the default binding of VAR.
260 Return `unbound' if VAR has no EUDC default value."
261 (let ((eudc-locals (get var 'eudc-locals)))
262 (if (and (boundp var)
263 eudc-locals)
264 (eudc-plist-get eudc-locals 'default 'unbound)
265 'unbound)))
267 (defun eudc-variable-protocol-value (var &optional protocol)
268 "Return the value of VAR local to PROTOCOL.
269 Return `unbound' if VAR has no value local to PROTOCOL.
270 PROTOCOL defaults to `eudc-protocol'"
271 (let* ((eudc-locals (get var 'eudc-locals))
272 protocol-locals)
273 (if (not (and (boundp var)
274 eudc-locals
275 (eudc-plist-member eudc-locals 'protocol)))
276 'unbound
277 (setq protocol-locals (eudc-plist-get eudc-locals 'protocol))
278 (eudc-lax-plist-get protocol-locals
279 (or protocol
280 eudc-protocol) 'unbound))))
282 (defun eudc-variable-server-value (var &optional server)
283 "Return the value of VAR local to SERVER.
284 Return `unbound' if VAR has no value local to SERVER.
285 SERVER defaults to `eudc-server'"
286 (let* ((eudc-locals (get var 'eudc-locals))
287 server-locals)
288 (if (not (and (boundp var)
289 eudc-locals
290 (eudc-plist-member eudc-locals 'server)))
291 'unbound
292 (setq server-locals (eudc-plist-get eudc-locals 'server))
293 (eudc-lax-plist-get server-locals
294 (or server
295 eudc-server) 'unbound))))
297 (defun eudc-update-variable (var)
298 "Set the value of VAR according to its locals.
299 If the VAR has a server- or protocol-local value corresponding
300 to the current `eudc-server' and `eudc-protocol' then it is set
301 accordingly. Otherwise it is set to its EUDC default binding"
302 (let (val)
303 (cond
304 ((not (eq 'unbound (setq val (eudc-variable-server-value var))))
305 (set var val))
306 ((not (eq 'unbound (setq val (eudc-variable-protocol-value var))))
307 (set var val))
308 ((not (eq 'unbound (setq val (eudc-variable-default-value var))))
309 (set var val)))))
311 (defun eudc-update-local-variables ()
312 "Update all EUDC variables according to their local settings."
313 (interactive)
314 (mapcar 'eudc-update-variable eudc-local-vars))
316 (eudc-default-set 'eudc-query-function nil)
317 (eudc-default-set 'eudc-list-attributes-function nil)
318 (eudc-default-set 'eudc-protocol-attributes-translation-alist nil)
319 (eudc-default-set 'eudc-bbdb-conversion-alist nil)
320 (eudc-default-set 'eudc-switch-to-server-hook nil)
321 (eudc-default-set 'eudc-switch-from-server-hook nil)
322 (eudc-default-set 'eudc-protocol-has-default-query-attributes nil)
323 (eudc-default-set 'eudc-attribute-display-method-alist nil)
325 ;;}}}
328 ;; Add PROTOCOL to the list of supported protocols
329 (defun eudc-register-protocol (protocol)
330 (unless (memq protocol eudc-supported-protocols)
331 (setq eudc-supported-protocols
332 (cons protocol eudc-supported-protocols))
333 (put 'eudc-protocol 'custom-type
334 `(choice :menu-tag "Protocol"
335 ,@(mapcar (lambda (s)
336 (list 'string ':tag (symbol-name s)))
337 eudc-supported-protocols))))
338 (or (memq protocol eudc-known-protocols)
339 (setq eudc-known-protocols
340 (cons protocol eudc-known-protocols))))
343 (defun eudc-translate-query (query)
344 "Translate attribute names of QUERY.
345 The translation is done according to
346 `eudc-protocol-attributes-translation-alist'."
347 (if eudc-protocol-attributes-translation-alist
348 (mapcar (lambda (attribute)
349 (let ((trans (assq (car attribute)
350 (symbol-value eudc-protocol-attributes-translation-alist))))
351 (if trans
352 (cons (cdr trans) (cdr attribute))
353 attribute)))
354 query)
355 query))
357 (defun eudc-translate-attribute-list (list)
358 "Translate a list of attribute names LIST.
359 The translation is done according to
360 `eudc-protocol-attributes-translation-alist'."
361 (if eudc-protocol-attributes-translation-alist
362 (let (trans)
363 (mapcar (lambda (attribute)
364 (setq trans (assq attribute
365 (symbol-value eudc-protocol-attributes-translation-alist)))
366 (if trans
367 (cdr trans)
368 attribute))
369 list))
370 list))
372 (defun eudc-select (choices beg end)
373 "Choose one from CHOICES using a completion.
374 BEG and END delimit the text which is to be replaced."
375 (let ((replacement))
376 (setq replacement
377 (completing-read "Multiple matches found; choose one: "
378 (mapcar 'list choices)))
379 (delete-region beg end)
380 (insert replacement)))
382 (defun eudc-query (query &optional return-attributes no-translation)
383 "Query the current directory server with QUERY.
384 QUERY is a list of cons cells (ATTR . VALUE) where ATTR is an attribute
385 name and VALUE the corresponding value.
386 If NO-TRANSLATION is non-nil, ATTR is translated according to
387 `eudc-protocol-attributes-translation-alist'.
388 RETURN-ATTRIBUTES is a list of attributes to return defaulting to
389 `eudc-default-return-attributes'."
390 (unless eudc-query-function
391 (error "Don't know how to perform the query"))
392 (if no-translation
393 (funcall eudc-query-function query (or return-attributes
394 eudc-default-return-attributes))
396 (funcall eudc-query-function
397 (eudc-translate-query query)
398 (cond
399 (return-attributes
400 (eudc-translate-attribute-list return-attributes))
401 ((listp eudc-default-return-attributes)
402 (eudc-translate-attribute-list eudc-default-return-attributes))
404 eudc-default-return-attributes)))))
406 (defun eudc-format-attribute-name-for-display (attribute)
407 "Format a directory attribute name for display.
408 ATTRIBUTE is looked up in `eudc-user-attribute-names-alist' and replaced
409 by the corresponding user name if any. Otherwise it is capitalized and
410 underscore characters are replaced by spaces."
411 (let ((match (assq attribute eudc-user-attribute-names-alist)))
412 (if match
413 (cdr match)
414 (capitalize
415 (mapconcat 'identity
416 (split-string (symbol-name attribute) "_")
417 " ")))))
419 (defun eudc-print-attribute-value (field)
420 "Insert the value of the directory FIELD at point.
421 The directory attribute name in car of FIELD is looked up in
422 `eudc-attribute-display-method-alist' and the corresponding method,
423 if any, is called to print the value in cdr of FIELD."
424 (let ((match (assoc (downcase (car field))
425 eudc-attribute-display-method-alist))
426 (col (current-column))
427 (val (cdr field)))
428 (if match
429 (progn
430 (eval (list (cdr match) val))
431 (insert "\n"))
432 (mapcar
433 (function
434 (lambda (val-elem)
435 (indent-to col)
436 (insert val-elem "\n")))
437 (cond
438 ((listp val) val)
439 ((stringp val) (split-string val "\n"))
440 ((null val) '(""))
441 (t (list val)))))))
443 (defun eudc-print-record-field (field column-width)
444 "Print the record field FIELD.
445 FIELD is a list (ATTR VALUE1 VALUE2 ...) or cons-cell (ATTR . VAL)
446 COLUMN-WIDTH is the width of the first display column containing the
447 attribute name ATTR."
448 (let ((field-beg (point)))
449 ;; The record field that is passed to this function has already been processed
450 ;; by `eudc-format-attribute-name-for-display' so we don't need to call it
451 ;; again to display the attribute name
452 (insert (format (concat "%" (int-to-string column-width) "s: ")
453 (car field)))
454 (put-text-property field-beg (point) 'face 'bold)
455 (indent-to (+ 2 column-width))
456 (eudc-print-attribute-value field)))
458 (defun eudc-display-records (records &optional raw-attr-names)
459 "Display the record list RECORDS in a formatted buffer.
460 If RAW-ATTR-NAMES is non-nil, the raw attribute names are displayed
461 otherwise they are formatted according to `eudc-user-attribute-names-alist'."
462 (let (inhibit-read-only
463 precords
464 (width 0)
466 first-record
467 attribute-name)
468 (with-output-to-temp-buffer "*Directory Query Results*"
469 (with-current-buffer standard-output
470 (setq buffer-read-only t)
471 (setq inhibit-read-only t)
472 (erase-buffer)
473 (insert "Directory Query Result\n")
474 (insert "======================\n\n\n")
475 (if (null records)
476 (insert "No match found.\n"
477 (if eudc-strict-return-matches
478 "Try setting `eudc-strict-return-matches' to nil or change `eudc-default-return-attributes'.\n"
479 ""))
480 ;; Replace field names with user names, compute max width
481 (setq precords
482 (mapcar
483 (function
484 (lambda (record)
485 (mapcar
486 (function
487 (lambda (field)
488 (setq attribute-name
489 (if raw-attr-names
490 (symbol-name (car field))
491 (eudc-format-attribute-name-for-display (car field))))
492 (if (> (length attribute-name) width)
493 (setq width (length attribute-name)))
494 (cons attribute-name (cdr field))))
495 record)))
496 records))
497 ;; Display the records
498 (setq first-record (point))
499 (mapc
500 (function
501 (lambda (record)
502 (setq beg (point))
503 ;; Map over the record fields to print the attribute/value pairs
504 (mapc (function
505 (lambda (field)
506 (eudc-print-record-field field width)))
507 record)
508 ;; Store the record internal format in some convenient place
509 (overlay-put (make-overlay beg (point))
510 'eudc-record
511 (car records))
512 (setq records (cdr records))
513 (insert "\n")))
514 precords))
515 (insert "\n")
516 (widget-create 'push-button
517 :notify (lambda (&rest _ignore)
518 (eudc-query-form))
519 "New query")
520 (widget-insert " ")
521 (widget-create 'push-button
522 :notify (lambda (&rest _ignore)
523 (kill-this-buffer))
524 "Quit")
525 (eudc-mode)
526 (widget-setup)
527 (if first-record
528 (goto-char first-record))))))
530 (defun eudc-process-form ()
531 "Process the query form in current buffer and display the results."
532 (let (query-alist
533 value)
534 (if (not (and (boundp 'eudc-form-widget-list)
535 eudc-form-widget-list))
536 (error "Not in a directory query form buffer")
537 (mapc (function
538 (lambda (wid-field)
539 (setq value (widget-value (cdr wid-field)))
540 (if (not (string= value ""))
541 (setq query-alist (cons (cons (car wid-field) value)
542 query-alist)))))
543 eudc-form-widget-list)
544 (kill-buffer (current-buffer))
545 (eudc-display-records (eudc-query query-alist) eudc-use-raw-directory-names))))
548 (defun eudc-filter-duplicate-attributes (record)
549 "Filter RECORD according to `eudc-duplicate-attribute-handling-method'."
550 (let ((rec record)
551 unique
552 duplicates
553 result)
555 ;; Search for multiple records
556 (while (and rec
557 (not (listp (eudc-cdar rec))))
558 (setq rec (cdr rec)))
560 (if (null (eudc-cdar rec))
561 (list record) ; No duplicate attrs in this record
562 (mapc (function
563 (lambda (field)
564 (if (listp (cdr field))
565 (setq duplicates (cons field duplicates))
566 (setq unique (cons field unique)))))
567 record)
568 (setq result (list unique))
569 ;; Map over the record fields that have multiple values
570 (mapc
571 (function
572 (lambda (field)
573 (let ((method (if (consp eudc-duplicate-attribute-handling-method)
574 (cdr
575 (assq
577 (car
578 (rassq
579 (car field)
580 (symbol-value
581 eudc-protocol-attributes-translation-alist)))
582 (car field))
583 eudc-duplicate-attribute-handling-method))
584 eudc-duplicate-attribute-handling-method)))
585 (cond
586 ((or (null method) (eq 'list method))
587 (setq result
588 (eudc-add-field-to-records field result)))
589 ((eq 'first method)
590 (setq result
591 (eudc-add-field-to-records (cons (car field)
592 (eudc-cadr field))
593 result)))
594 ((eq 'concat method)
595 (setq result
596 (eudc-add-field-to-records (cons (car field)
597 (mapconcat
598 'identity
599 (cdr field)
600 "\n")) result)))
601 ((eq 'duplicate method)
602 (setq result
603 (eudc-distribute-field-on-records field result)))))))
604 duplicates)
605 result)))
607 (defun eudc-filter-partial-records (records attrs)
608 "Eliminate records that do not contain all ATTRS from RECORDS."
609 (delq nil
610 (mapcar
611 (function
612 (lambda (rec)
613 (if (eval (cons 'and
614 (mapcar
615 (function
616 (lambda (attr)
617 (consp (assq attr rec))))
618 attrs)))
619 rec)))
620 records)))
622 (defun eudc-add-field-to-records (field records)
623 "Add FIELD to each individual record in RECORDS and return the resulting list."
624 (mapcar (function
625 (lambda (r)
626 (cons field r)))
627 records))
629 (defun eudc-distribute-field-on-records (field records)
630 "Duplicate each individual record in RECORDS according to value of FIELD.
631 Each copy is added a new field containing one of the values of FIELD."
632 (let (result
633 (values (cdr field)))
634 ;; Uniquify values first
635 (while values
636 (setcdr values (delete (car values) (cdr values)))
637 (setq values (cdr values)))
638 (mapc
639 (function
640 (lambda (value)
641 (let ((result-list (copy-sequence records)))
642 (setq result-list (eudc-add-field-to-records
643 (cons (car field) value)
644 result-list))
645 (setq result (append result-list result))
647 (cdr field))
648 result))
651 (define-derived-mode eudc-mode special-mode "EUDC"
652 "Major mode used in buffers displaying the results of directory queries.
653 There is no sense in calling this command from a buffer other than
654 one containing the results of a directory query.
656 These are the special commands of EUDC mode:
657 q -- Kill this buffer.
658 f -- Display a form to query the current directory server.
659 n -- Move to next record.
660 p -- Move to previous record.
661 b -- Insert record at point into the BBDB database."
662 (if (not (featurep 'xemacs))
663 (easy-menu-define eudc-emacs-menu eudc-mode-map "" (eudc-menu))
664 (setq mode-popup-menu (eudc-menu))))
666 ;;}}}
668 ;;{{{ High-level interfaces (interactive functions)
670 (defun eudc-customize ()
671 "Customize the EUDC package."
672 (interactive)
673 (customize-group 'eudc))
675 ;;;###autoload
676 (defun eudc-set-server (server protocol &optional no-save)
677 "Set the directory server to SERVER using PROTOCOL.
678 Unless NO-SAVE is non-nil, the server is saved as the default
679 server for future sessions."
680 (interactive (list
681 (read-from-minibuffer "Directory Server: ")
682 (intern (completing-read "Protocol: "
683 (mapcar (lambda (elt)
684 (cons (symbol-name elt)
685 elt))
686 eudc-known-protocols)))))
687 (unless (or (null protocol)
688 (member protocol
689 eudc-supported-protocols)
690 (load (concat "eudcb-" (symbol-name protocol)) t))
691 (error "Unsupported protocol: %s" protocol))
692 (run-hooks 'eudc-switch-from-server-hook)
693 (setq eudc-protocol protocol)
694 (setq eudc-server server)
695 (eudc-update-local-variables)
696 (run-hooks 'eudc-switch-to-server-hook)
697 (if (called-interactively-p 'interactive)
698 (message "Current directory server is now %s (%s)" eudc-server eudc-protocol))
699 (if (null no-save)
700 (eudc-save-options)))
702 ;;;###autoload
703 (defun eudc-get-email (name &optional error)
704 "Get the email field of NAME from the directory server.
705 If ERROR is non-nil, report an error if there is none."
706 (interactive "sName: \np")
707 (or eudc-server
708 (call-interactively 'eudc-set-server))
709 (let ((result (eudc-query (list (cons 'name name)) '(email)))
710 email)
711 (if (null (cdr result))
712 (setq email (eudc-cdaar result))
713 (error "Multiple match--use the query form"))
714 (if error
715 (if email
716 (message "%s" email)
717 (error "No record matching %s" name)))
718 email))
720 ;;;###autoload
721 (defun eudc-get-phone (name &optional error)
722 "Get the phone field of NAME from the directory server.
723 If ERROR is non-nil, report an error if there is none."
724 (interactive "sName: \np")
725 (or eudc-server
726 (call-interactively 'eudc-set-server))
727 (let ((result (eudc-query (list (cons 'name name)) '(phone)))
728 phone)
729 (if (null (cdr result))
730 (setq phone (eudc-cdaar result))
731 (error "Multiple match--use the query form"))
732 (if error
733 (if phone
734 (message "%s" phone)
735 (error "No record matching %s" name)))
736 phone))
738 (defun eudc-get-attribute-list ()
739 "Return a list of valid attributes for the current server.
740 When called interactively the list is formatted in a dedicated buffer
741 otherwise a list of symbols is returned."
742 (interactive)
743 (if eudc-list-attributes-function
744 (let ((entries (funcall eudc-list-attributes-function
745 (called-interactively-p 'interactive))))
746 (if entries
747 (if (called-interactively-p 'interactive)
748 (eudc-display-records entries t)
749 entries)))
750 (error "The %s protocol has no support for listing attributes" eudc-protocol)))
752 (defun eudc-format-query (words format)
753 "Use FORMAT to build a EUDC query from WORDS."
754 (let (query
755 query-alist
756 key val cell)
757 (if format
758 (progn
759 (while (and words format)
760 (setq query-alist (cons (cons (car format) (car words))
761 query-alist))
762 (setq words (cdr words)
763 format (cdr format)))
764 ;; If the same attribute appears more than once, merge
765 ;; the corresponding values
766 (while query-alist
767 (setq key (eudc-caar query-alist)
768 val (eudc-cdar query-alist)
769 cell (assq key query))
770 (if cell
771 (setcdr cell (concat (cdr cell) " " val))
772 (setq query (cons (car query-alist) query)))
773 (setq query-alist (cdr query-alist)))
774 query)
775 (if eudc-protocol-has-default-query-attributes
776 (mapconcat 'identity words " ")
777 (list (cons 'name (mapconcat 'identity words " ")))))))
779 (defun eudc-extract-n-word-formats (format-list n)
780 "Extract a list of N-long formats from FORMAT-LIST.
781 If none try N - 1 and so forth."
782 (let (formats)
783 (while (and (null formats)
784 (> n 0))
785 (setq formats
786 (delq nil
787 (mapcar (lambda (format)
788 (if (= n
789 (length format))
790 format
791 nil))
792 format-list)))
793 (setq n (1- n)))
794 formats))
797 ;;;###autoload
798 (defun eudc-expand-inline (&optional replace)
799 "Query the directory server, and expand the query string before point.
800 The query string consists of the buffer substring from the point back to
801 the preceding comma, colon or beginning of line.
802 The variable `eudc-inline-query-format' controls how to associate the
803 individual inline query words with directory attribute names.
804 After querying the server for the given string, the expansion specified by
805 `eudc-inline-expansion-format' is inserted in the buffer at point.
806 If REPLACE is non-nil, then this expansion replaces the name in the buffer.
807 `eudc-expansion-overwrites-query' being non-nil inverts the meaning of REPLACE.
808 Multiple servers can be tried with the same query until one finds a match,
809 see `eudc-inline-expansion-servers'"
810 (interactive)
811 (cond
812 ((eq eudc-inline-expansion-servers 'current-server)
813 (or eudc-server
814 (call-interactively 'eudc-set-server)))
815 ((eq eudc-inline-expansion-servers 'server-then-hotlist)
816 (or eudc-server
817 ;; Allow server to be nil if hotlist is set.
818 eudc-server-hotlist
819 (call-interactively 'eudc-set-server)))
820 ((eq eudc-inline-expansion-servers 'hotlist)
821 (or eudc-server-hotlist
822 (error "No server in the hotlist")))
824 (error "Wrong value for `eudc-inline-expansion-servers': %S"
825 eudc-inline-expansion-servers)))
826 (let* ((end (point))
827 (beg (save-excursion
828 (if (re-search-backward "\\([:,]\\|^\\)[ \t]*"
829 (point-at-bol) 'move)
830 (goto-char (match-end 0)))
831 (point)))
832 (query-words (split-string (buffer-substring-no-properties beg end)
833 "[ \t]+"))
834 query-formats
835 response
836 response-string
837 response-strings
838 (eudc-former-server eudc-server)
839 (eudc-former-protocol eudc-protocol)
840 servers)
842 ;; Prepare the list of servers to query
843 (setq servers (copy-sequence eudc-server-hotlist))
844 (setq servers
845 (cond
846 ((eq eudc-inline-expansion-servers 'hotlist)
847 eudc-server-hotlist)
848 ((eq eudc-inline-expansion-servers 'server-then-hotlist)
849 (if eudc-server
850 (cons (cons eudc-server eudc-protocol)
851 (delete (cons eudc-server eudc-protocol) servers))
852 eudc-server-hotlist))
853 ((eq eudc-inline-expansion-servers 'current-server)
854 (list (cons eudc-server eudc-protocol)))))
855 (if (and eudc-max-servers-to-query
856 (> (length servers) eudc-max-servers-to-query))
857 (setcdr (nthcdr (1- eudc-max-servers-to-query) servers) nil))
859 (unwind-protect
860 (progn
861 (setq response
862 (catch 'found
863 ;; Loop on the servers
864 (while servers
865 (eudc-set-server (eudc-caar servers) (eudc-cdar servers) t)
867 ;; Determine which formats apply in the query-format list
868 (setq query-formats
870 (eudc-extract-n-word-formats eudc-inline-query-format
871 (length query-words))
872 (if (null eudc-protocol-has-default-query-attributes)
873 '(name))))
875 ;; Loop on query-formats
876 (while query-formats
877 (setq response
878 (eudc-query
879 (eudc-format-query query-words (car query-formats))
880 (eudc-translate-attribute-list
881 (cdr eudc-inline-expansion-format))))
882 (if response
883 (throw 'found response))
884 (setq query-formats (cdr query-formats)))
885 (setq servers (cdr servers)))
886 ;; No more servers to try... no match found
887 nil))
890 (if (null response)
891 (error "No match")
893 ;; Process response through eudc-inline-expansion-format
894 (while response
895 (setq response-string
896 (apply 'format
897 (car eudc-inline-expansion-format)
898 (mapcar (function
899 (lambda (field)
900 (or (cdr (assq field (car response)))
901 "")))
902 (eudc-translate-attribute-list
903 (cdr eudc-inline-expansion-format)))))
904 (if (> (length response-string) 0)
905 (setq response-strings
906 (cons response-string response-strings)))
907 (setq response (cdr response)))
909 (if (or
910 (and replace (not eudc-expansion-overwrites-query))
911 (and (not replace) eudc-expansion-overwrites-query))
912 (kill-ring-save beg end))
913 (cond
914 ((or (= (length response-strings) 1)
915 (null eudc-multiple-match-handling-method)
916 (eq eudc-multiple-match-handling-method 'first))
917 (delete-region beg end)
918 (insert (car response-strings)))
919 ((eq eudc-multiple-match-handling-method 'select)
920 (eudc-select response-strings beg end))
921 ((eq eudc-multiple-match-handling-method 'all)
922 (delete-region beg end)
923 (insert (mapconcat 'identity response-strings ", ")))
924 ((eq eudc-multiple-match-handling-method 'abort)
925 (error "There is more than one match for the query")))))
926 (or (and (equal eudc-server eudc-former-server)
927 (equal eudc-protocol eudc-former-protocol))
928 (eudc-set-server eudc-former-server eudc-former-protocol t)))))
930 ;;;###autoload
931 (defun eudc-query-form (&optional get-fields-from-server)
932 "Display a form to query the directory server.
933 If given a non-nil argument GET-FIELDS-FROM-SERVER, the function first
934 queries the server for the existing fields and displays a corresponding form."
935 (interactive "P")
936 (let ((fields (or (and get-fields-from-server
937 (eudc-get-attribute-list))
938 eudc-query-form-attributes))
939 (buffer (get-buffer-create "*Directory Query Form*"))
940 prompts
941 widget
942 (width 0)
943 inhibit-read-only
945 (switch-to-buffer buffer)
946 (setq inhibit-read-only t)
947 (erase-buffer)
948 (kill-all-local-variables)
949 (make-local-variable 'eudc-form-widget-list)
950 (widget-insert "Directory Query Form\n")
951 (widget-insert "====================\n\n")
952 (widget-insert "Current server is: " (or eudc-server
953 (progn
954 (call-interactively 'eudc-set-server)
955 eudc-server))
956 "\n")
957 (widget-insert "Protocol : " (symbol-name eudc-protocol) "\n")
958 ;; Build the list of prompts
959 (setq prompts (if eudc-use-raw-directory-names
960 (mapcar 'symbol-name (eudc-translate-attribute-list fields))
961 (mapcar (function
962 (lambda (field)
963 (or (and (assq field eudc-user-attribute-names-alist)
964 (cdr (assq field eudc-user-attribute-names-alist)))
965 (capitalize (symbol-name field)))))
966 fields)))
967 ;; Loop over prompt strings to find the longest one
968 (mapc (function
969 (lambda (prompt)
970 (if (> (length prompt) width)
971 (setq width (length prompt)))))
972 prompts)
973 ;; Insert the first widget out of the mapcar to leave the cursor
974 ;; in the first field
975 (widget-insert "\n\n" (format (concat "%" (int-to-string width) "s: ") (car prompts)))
976 (setq pt (point))
977 (setq widget (widget-create 'editable-field :size 15))
978 (setq eudc-form-widget-list (cons (cons (car fields) widget)
979 eudc-form-widget-list))
980 (setq fields (cdr fields))
981 (setq prompts (cdr prompts))
982 (mapc (function
983 (lambda (field)
984 (widget-insert "\n\n" (format (concat "%" (int-to-string width) "s: ") (car prompts)))
985 (setq widget (widget-create 'editable-field
986 :size 15))
987 (setq eudc-form-widget-list (cons (cons field widget)
988 eudc-form-widget-list))
989 (setq prompts (cdr prompts))))
990 fields)
991 (widget-insert "\n\n")
992 (widget-create 'push-button
993 :notify (lambda (&rest _ignore)
994 (eudc-process-form))
995 "Query Server")
996 (widget-insert " ")
997 (widget-create 'push-button
998 :notify (lambda (&rest _ignore)
999 (eudc-query-form))
1000 "Reset Form")
1001 (widget-insert " ")
1002 (widget-create 'push-button
1003 :notify (lambda (&rest _ignore)
1004 (kill-this-buffer))
1005 "Quit")
1006 (goto-char pt)
1007 (use-local-map widget-keymap)
1008 (widget-setup))
1011 (defun eudc-bookmark-server (server protocol)
1012 "Add SERVER using PROTOCOL to the EUDC `servers' hotlist."
1013 (interactive "sDirectory server: \nsProtocol: ")
1014 (if (member (cons server protocol) eudc-server-hotlist)
1015 (error "%s:%s is already in the hotlist" protocol server)
1016 (setq eudc-server-hotlist (cons (cons server protocol) eudc-server-hotlist))
1017 (eudc-install-menu)
1018 (eudc-save-options)))
1020 (defun eudc-bookmark-current-server ()
1021 "Add current server to the EUDC `servers' hotlist."
1022 (interactive)
1023 (eudc-bookmark-server eudc-server eudc-protocol))
1025 (defun eudc-save-options ()
1026 "Save options to `eudc-options-file'."
1027 (interactive)
1028 (with-current-buffer (find-file-noselect eudc-options-file t)
1029 (goto-char (point-min))
1030 ;; delete the previous setq
1031 (let ((standard-output (current-buffer))
1032 provide-p
1033 set-hotlist-p
1034 set-server-p)
1035 (catch 'found
1036 (while t
1037 (let ((sexp (condition-case nil
1038 (read (current-buffer))
1039 (end-of-file (throw 'found nil)))))
1040 (if (listp sexp)
1041 (cond
1042 ((eq (car sexp) 'eudc-set-server)
1043 (delete-region (save-excursion
1044 (backward-sexp)
1045 (point))
1046 (point))
1047 (setq set-server-p t))
1048 ((and (eq (car sexp) 'setq)
1049 (eq (eudc-cadr sexp) 'eudc-server-hotlist))
1050 (delete-region (save-excursion
1051 (backward-sexp)
1052 (point))
1053 (point))
1054 (setq set-hotlist-p t))
1055 ((and (eq (car sexp) 'provide)
1056 (equal (eudc-cadr sexp) '(quote eudc-options-file)))
1057 (setq provide-p t)))
1058 (if (and provide-p
1059 set-hotlist-p
1060 set-server-p)
1061 (throw 'found t))))))
1062 (if (eq (point-min) (point-max))
1063 (princ ";; This file was automatically generated by eudc.el.\n\n"))
1064 (or provide-p
1065 (princ "(provide 'eudc-options-file)\n"))
1066 (or (bolp)
1067 (princ "\n"))
1068 (delete-blank-lines)
1069 (princ "(eudc-set-server ")
1070 (prin1 eudc-server)
1071 (princ " '")
1072 (prin1 eudc-protocol)
1073 (princ " t)\n")
1074 (princ "(setq eudc-server-hotlist '")
1075 (prin1 eudc-server-hotlist)
1076 (princ ")\n")
1077 (save-buffer))))
1079 (defun eudc-move-to-next-record ()
1080 "Move to next record, in a buffer displaying directory query results."
1081 (interactive)
1082 (if (not (derived-mode-p 'eudc-mode))
1083 (error "Not in a EUDC buffer")
1084 (let ((pt (next-overlay-change (point))))
1085 (if (< pt (point-max))
1086 (goto-char (1+ pt))
1087 (error "No more records after point")))))
1089 (defun eudc-move-to-previous-record ()
1090 "Move to previous record, in a buffer displaying directory query results."
1091 (interactive)
1092 (if (not (derived-mode-p 'eudc-mode))
1093 (error "Not in a EUDC buffer")
1094 (let ((pt (previous-overlay-change (point))))
1095 (if (> pt (point-min))
1096 (goto-char pt)
1097 (error "No more records before point")))))
1099 ;;}}}
1101 ;;{{{ Menus and keymaps
1103 (require 'easymenu)
1105 (defconst eudc-custom-generated-menu (cdr (custom-menu-create 'eudc)))
1107 (defconst eudc-tail-menu
1108 `(["---" nil nil]
1109 ["Query with Form" eudc-query-form
1110 :help "Display a form to query the directory server"]
1111 ["Expand Inline Query" eudc-expand-inline
1112 :help "Query the directory server, and expand the query string before point"]
1113 ["Insert Record into BBDB" eudc-insert-record-at-point-into-bbdb
1114 (and (or (featurep 'bbdb)
1115 (prog1 (locate-library "bbdb") (message "")))
1116 (overlays-at (point))
1117 (overlay-get (car (overlays-at (point))) 'eudc-record))
1118 :help "Insert record at point into the BBDB database"]
1119 ["Insert All Records into BBDB" eudc-batch-export-records-to-bbdb
1120 (and (derived-mode-p 'eudc-mode)
1121 (or (featurep 'bbdb)
1122 (prog1 (locate-library "bbdb") (message ""))))
1123 :help "Insert all the records returned by a directory query into BBDB"]
1124 ["---" nil nil]
1125 ["Get Email" eudc-get-email
1126 :help "Get the email field of NAME from the directory server"]
1127 ["Get Phone" eudc-get-phone
1128 :help "Get the phone field of name from the directory server"]
1129 ["List Valid Attribute Names" eudc-get-attribute-list
1130 :help "Return a list of valid attributes for the current server"]
1131 ["---" nil nil]
1132 ,(cons "Customize" eudc-custom-generated-menu)))
1135 (defconst eudc-server-menu
1136 '(["---" nil nil]
1137 ["Bookmark Current Server" eudc-bookmark-current-server
1138 :help "Add current server to the EUDC `servers' hotlist"]
1139 ["Edit Server List" eudc-edit-hotlist
1140 :help "Edit the hotlist of directory servers in a specialized buffer"]
1141 ["New Server" eudc-set-server
1142 :help "Set the directory server to SERVER using PROTOCOL"]))
1144 (defun eudc-menu ()
1145 (let (command)
1146 (append '("Directory Search")
1147 (list
1148 (append
1149 '("Server")
1150 (mapcar
1151 (function
1152 (lambda (servspec)
1153 (let* ((server (car servspec))
1154 (protocol (cdr servspec))
1155 (proto-name (symbol-name protocol)))
1156 (setq command (intern (concat "eudc-set-server-"
1157 server
1159 proto-name)))
1160 (if (not (fboundp command))
1161 (fset command
1162 `(lambda ()
1163 (interactive)
1164 (eudc-set-server ,server (quote ,protocol))
1165 (message "Selected directory server is now %s (%s)"
1166 ,server
1167 ,proto-name))))
1168 (vector (format "%s (%s)" server proto-name)
1169 command
1170 :style 'radio
1171 :selected `(equal eudc-server ,server)))))
1172 eudc-server-hotlist)
1173 eudc-server-menu))
1174 eudc-tail-menu)))
1176 (defun eudc-install-menu ()
1177 (cond
1178 ((and (featurep 'xemacs) (featurep 'menubar))
1179 (add-submenu '("Tools") (eudc-menu)))
1180 ((not (featurep 'xemacs))
1181 (cond
1182 ((fboundp 'easy-menu-create-menu)
1183 (define-key
1184 global-map
1185 [menu-bar tools directory-search]
1186 (cons "Directory Search"
1187 (easy-menu-create-menu "Directory Search" (cdr (eudc-menu))))))
1188 ((fboundp 'easy-menu-add-item)
1189 (let ((menu (eudc-menu)))
1190 (easy-menu-add-item nil '("tools") (easy-menu-create-menu (car menu)
1191 (cdr menu)))))
1192 ((fboundp 'easy-menu-create-keymaps)
1193 (easy-menu-define eudc-menu-map eudc-mode-map "Directory Client Menu" (eudc-menu))
1194 (define-key
1195 global-map
1196 [menu-bar tools eudc]
1197 (cons "Directory Search"
1198 (easy-menu-create-keymaps "Directory Search" (cdr (eudc-menu))))))
1200 (error "Unknown version of easymenu"))))
1204 ;;; Load time initializations :
1206 ;;; Load the options file
1207 (if (and (not noninteractive)
1208 (and (locate-library eudc-options-file)
1209 (progn (message "") t)) ; Remove mode line message
1210 (not (featurep 'eudc-options-file)))
1211 (load eudc-options-file))
1213 ;;; Install the full menu
1214 (unless (featurep 'infodock)
1215 (eudc-install-menu))
1218 ;;; The following installs a short menu for EUDC at XEmacs startup.
1220 ;;;###autoload
1221 (defun eudc-load-eudc ()
1222 "Load the Emacs Unified Directory Client.
1223 This does nothing except loading eudc by autoload side-effect."
1224 (interactive)
1225 nil)
1227 ;;;###autoload
1228 (cond
1229 ((not (featurep 'xemacs))
1230 (defvar eudc-tools-menu
1231 (let ((map (make-sparse-keymap "Directory Search")))
1232 (define-key map [phone]
1233 `(menu-item ,(purecopy "Get Phone") eudc-get-phone
1234 :help ,(purecopy "Get the phone field of name from the directory server")))
1235 (define-key map [email]
1236 `(menu-item ,(purecopy "Get Email") eudc-get-email
1237 :help ,(purecopy "Get the email field of NAME from the directory server")))
1238 (define-key map [separator-eudc-email] menu-bar-separator)
1239 (define-key map [expand-inline]
1240 `(menu-item ,(purecopy "Expand Inline Query") eudc-expand-inline
1241 :help ,(purecopy "Query the directory server, and expand the query string before point")))
1242 (define-key map [query]
1243 `(menu-item ,(purecopy "Query with Form") eudc-query-form
1244 :help ,(purecopy "Display a form to query the directory server")))
1245 (define-key map [separator-eudc-query] menu-bar-separator)
1246 (define-key map [new]
1247 `(menu-item ,(purecopy "New Server") eudc-set-server
1248 :help ,(purecopy "Set the directory server to SERVER using PROTOCOL")))
1249 (define-key map [load]
1250 `(menu-item ,(purecopy "Load Hotlist of Servers") eudc-load-eudc
1251 :help ,(purecopy "Load the Emacs Unified Directory Client")))
1252 map))
1253 (fset 'eudc-tools-menu (symbol-value 'eudc-tools-menu)))
1255 (let ((menu '("Directory Search"
1256 ["Load Hotlist of Servers" eudc-load-eudc t]
1257 ["New Server" eudc-set-server t]
1258 ["---" nil nil]
1259 ["Query with Form" eudc-query-form t]
1260 ["Expand Inline Query" eudc-expand-inline t]
1261 ["---" nil nil]
1262 ["Get Email" eudc-get-email t]
1263 ["Get Phone" eudc-get-phone t])))
1264 (if (not (featurep 'eudc-autoloads))
1265 (if (featurep 'xemacs)
1266 (if (and (featurep 'menubar)
1267 (not (featurep 'infodock)))
1268 (add-submenu '("Tools") menu))
1269 (require 'easymenu)
1270 (cond
1271 ((fboundp 'easy-menu-add-item)
1272 (easy-menu-add-item nil '("tools")
1273 (easy-menu-create-menu (car menu)
1274 (cdr menu))))
1275 ((fboundp 'easy-menu-create-keymaps)
1276 (define-key
1277 global-map
1278 [menu-bar tools eudc]
1279 (cons "Directory Search"
1280 (easy-menu-create-keymaps "Directory Search"
1281 (cdr menu)))))))))))
1283 ;;}}}
1285 (provide 'eudc)
1287 ;;; eudc.el ends here