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