]> code.delx.au - gnu-emacs/blob - lisp/net/eudcb-bbdb.el
Recognize more format variation. Automatically reshow decrypted text.
[gnu-emacs] / lisp / net / eudcb-bbdb.el
1 ;;; eudcb-bbdb.el --- Emacs Unified Directory Client - BBDB Backend -*- 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 ;; Package: eudc
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27 ;; This library provides an interface to use BBDB as a backend of
28 ;; the Emacs Unified Directory Client.
29
30 ;;; Code:
31
32 (require 'eudc)
33
34 ;; Make it loadable on systems without bbdb.
35 (require 'bbdb nil t)
36 (require 'bbdb-com nil t)
37
38 ;;{{{ Internal cooking
39
40 ;; I don't like this but mapcar does not accept a parameter to the function and
41 ;; I don't want to use mapcar*
42 (defvar eudc-bbdb-current-query nil)
43 (defvar eudc-bbdb-current-return-attributes nil)
44
45 (defvar eudc-bbdb-attributes-translation-alist
46 '((name . lastname)
47 (email . net)
48 (phone . phones))
49 "Alist mapping EUDC attribute names to BBDB names.")
50
51 (eudc-protocol-set 'eudc-query-function 'eudc-bbdb-query-internal 'bbdb)
52 (eudc-protocol-set 'eudc-list-attributes-function nil 'bbdb)
53 (eudc-protocol-set 'eudc-protocol-attributes-translation-alist
54 'eudc-bbdb-attributes-translation-alist 'bbdb)
55 (eudc-protocol-set 'eudc-bbdb-conversion-alist nil 'bbdb)
56 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil 'bbdb)
57
58 (defun eudc-bbdb-format-query (query)
59 "Format a EUDC query alist into a list suitable to `bbdb-search'."
60 (let* ((firstname (cdr (assq 'firstname query)))
61 (lastname (cdr (assq 'lastname query)))
62 (name (or (and firstname lastname
63 (concat firstname " " lastname))
64 firstname
65 lastname))
66 (company (cdr (assq 'company query)))
67 (net (cdr (assq 'net query)))
68 (notes (cdr (assq 'notes query)))
69 (phone (cdr (assq 'phone query))))
70 (list name company net notes phone)))
71
72
73 (defun eudc-bbdb-filter-non-matching-record (record)
74 "Return RECORD if it matches `eudc-bbdb-current-query', nil otherwise."
75 (require 'bbdb)
76 (catch 'unmatch
77 (progn
78 (dolist (condition eudc-bbdb-current-query)
79 (let ((attr (car condition))
80 (val (cdr condition))
81 (case-fold-search t)
82 bbdb-val)
83 (or (and (memq attr '(firstname lastname aka company phones
84 addresses net))
85 (progn
86 (setq bbdb-val
87 (eval (list (intern (concat "bbdb-record-"
88 (symbol-name attr)))
89 'record)))
90 (if (listp bbdb-val)
91 (if eudc-bbdb-enable-substring-matches
92 (eval `(or ,@(mapcar (lambda (subval)
93 (string-match val subval))
94 bbdb-val)))
95 (member (downcase val)
96 (mapcar 'downcase bbdb-val)))
97 (if eudc-bbdb-enable-substring-matches
98 (string-match val bbdb-val)
99 (string-equal (downcase val) (downcase bbdb-val))))))
100 (throw 'unmatch nil))))
101 record)))
102
103 ;; External.
104 (declare-function bbdb-phone-location "ext:bbdb" t) ; via bbdb-defstruct
105 (declare-function bbdb-phone-string "ext:bbdb" (phone))
106 (declare-function bbdb-record-phones "ext:bbdb" t) ; via bbdb-defstruct
107 (declare-function bbdb-address-streets "ext:bbdb" t) ; via bbdb-defstruct
108 (declare-function bbdb-address-city "ext:bbdb" t) ; via bbdb-defstruct
109 (declare-function bbdb-address-state "ext:bbdb" t) ; via bbdb-defstruct
110 (declare-function bbdb-address-zip "ext:bbdb" t) ; via bbdb-defstruct
111 (declare-function bbdb-address-location "ext:bbdb" t) ; via bbdb-defstruct
112 (declare-function bbdb-record-addresses "ext:bbdb" t) ; via bbdb-defstruct
113 (declare-function bbdb-records "ext:bbdb"
114 (&optional dont-check-disk already-in-db-buffer))
115
116 (defun eudc-bbdb-extract-phones (record)
117 (require 'bbdb)
118 (mapcar (function
119 (lambda (phone)
120 (if eudc-bbdb-use-locations-as-attribute-names
121 (cons (intern (bbdb-phone-location phone))
122 (bbdb-phone-string phone))
123 (cons 'phones (format "%s: %s"
124 (bbdb-phone-location phone)
125 (bbdb-phone-string phone))))))
126 (bbdb-record-phones record)))
127
128 (defun eudc-bbdb-extract-addresses (record)
129 (require 'bbdb)
130 (let (s c val)
131 (mapcar (lambda (address)
132 (setq c (bbdb-address-streets address))
133 (dotimes (n 3)
134 (unless (zerop (length (setq s (nth n c))))
135 (setq val (concat val s "\n"))))
136 (setq c (bbdb-address-city address)
137 s (bbdb-address-state address))
138 (setq val (concat val
139 (if (and (> (length c) 0) (> (length s) 0))
140 (concat c ", " s)
141 c)
142 " "
143 (bbdb-address-zip address)))
144 (if eudc-bbdb-use-locations-as-attribute-names
145 (cons (intern (bbdb-address-location address)) val)
146 (cons 'addresses (concat (bbdb-address-location address)
147 "\n" val))))
148 (bbdb-record-addresses record))))
149
150 (defun eudc-bbdb-format-record-as-result (record)
151 "Format the BBDB RECORD as a EUDC query result record.
152 The record is filtered according to `eudc-bbdb-current-return-attributes'"
153 (require 'bbdb)
154 (let ((attrs (or eudc-bbdb-current-return-attributes
155 '(firstname lastname aka company phones addresses net notes)))
156 attr
157 eudc-rec
158 val)
159 (while (prog1
160 (setq attr (car attrs))
161 (setq attrs (cdr attrs)))
162 (cond
163 ((eq attr 'phones)
164 (setq val (eudc-bbdb-extract-phones record)))
165 ((eq attr 'addresses)
166 (setq val (eudc-bbdb-extract-addresses record)))
167 ((memq attr '(firstname lastname aka company net notes))
168 (setq val (eval
169 (list (intern
170 (concat "bbdb-record-"
171 (symbol-name attr)))
172 'record))))
173 (t
174 (error "Unknown BBDB attribute")))
175 (cond
176 ((or (not val) (equal val ""))) ; do nothing
177 ((memq attr '(phones addresses))
178 (setq eudc-rec (append val eudc-rec)))
179 ((and (listp val)
180 (= 1 (length val)))
181 (setq eudc-rec (cons (cons attr (car val)) eudc-rec)))
182 ((> (length val) 0)
183 (setq eudc-rec (cons (cons attr val) eudc-rec)))
184 (t
185 (error "Unexpected attribute value"))))
186 (nreverse eudc-rec)))
187
188
189
190 (defun eudc-bbdb-query-internal (query &optional return-attrs)
191 "Query BBDB with QUERY.
192 QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid
193 BBDB attribute names.
194 RETURN-ATTRS is a list of attributes to return, defaulting to
195 `eudc-default-return-attributes'."
196 (require 'bbdb)
197 (let ((eudc-bbdb-current-query query)
198 (eudc-bbdb-current-return-attributes return-attrs)
199 (query-attrs (eudc-bbdb-format-query query))
200 bbdb-attrs
201 (records (bbdb-records))
202 result
203 filtered)
204 ;; BBDB ORs its query attributes while EUDC ANDs them, hence we need to
205 ;; call bbdb-search iteratively on the returned records for each of the
206 ;; requested attributes
207 (while (and records (> (length query-attrs) 0))
208 (setq bbdb-attrs (append bbdb-attrs (list (car query-attrs))))
209 (if (car query-attrs)
210 (setq records (eval `(bbdb-search ,(quote records) ,@bbdb-attrs))))
211 (setq query-attrs (cdr query-attrs)))
212 (mapc (function
213 (lambda (record)
214 (setq filtered (eudc-filter-duplicate-attributes record))
215 ;; If there were duplicate attributes reverse the order of the
216 ;; record so the unique attributes appear first
217 (if (> (length filtered) 1)
218 (setq filtered (mapcar (function
219 (lambda (rec)
220 (reverse rec)))
221 filtered)))
222 (setq result (append result filtered))))
223 (delq nil
224 (mapcar 'eudc-bbdb-format-record-as-result
225 (delq nil
226 (mapcar 'eudc-bbdb-filter-non-matching-record
227 records)))))
228 result))
229
230 ;;}}}
231
232 ;;{{{ High-level interfaces (interactive functions)
233
234 (defun eudc-bbdb-set-server (dummy)
235 "Set the EUDC server to BBDB."
236 (interactive)
237 (eudc-set-server dummy 'bbdb)
238 (message "BBDB server selected"))
239
240 ;;}}}
241
242
243 (eudc-register-protocol 'bbdb)
244
245 (provide 'eudcb-bbdb)
246
247 ;;; eudcb-bbdb.el ends here