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