Initial Commit
[packages] / xemacs-packages / eudc / eudcb-bbdb.el
1 ;;; eudcb-bbdb.el --- Emacs Unified Directory Client - BBDB Backend
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: Oscar Figueiredo <oscar@xemacs.org>
6 ;; Maintainer: Oscar Figueiredo <oscar@xemacs.org>
7 ;; Created: Feb 1998
8 ;; Version: $Revision: 1.3 $
9 ;; Keywords: help
10
11 ;; This file is part of XEmacs
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to 
25 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29 ;;    This library provides an interface to use BBDB as a backend of 
30 ;;    the Emacs Unified Directory Client.
31
32 ;;; Code:
33
34 (require 'eudc)
35 (require 'bbdb)
36 (require 'bbdb-com)
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   (catch 'unmatch
76     (progn
77       (mapcar 
78        (function 
79         (lambda (condition)
80           (let ((attr (car condition))
81                 (val (cdr condition))
82                 (case-fold-search t)
83                 bbdb-val)
84             (or (and (memq attr '(firstname lastname aka company phones 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
94                                                                      subval))
95                                                   bbdb-val)))
96                              (member (downcase val)
97                                      (mapcar 'downcase bbdb-val)))
98                          (if eudc-bbdb-enable-substring-matches
99                              (string-match val bbdb-val)
100                            (string-equal (downcase val) (downcase bbdb-val))))))
101                 (throw 'unmatch nil)))))
102        eudc-bbdb-current-query)
103       record)))
104
105 (defun eudc-bbdb-extract-phones (record)
106   (mapcar (function
107            (lambda (phone)
108              (if eudc-bbdb-use-locations-as-attribute-names
109                  (cons (intern (bbdb-phone-location phone))
110                        (bbdb-phone-string phone))
111                (cons 'phones (format "%s: %s" 
112                                      (bbdb-phone-location phone)
113                                      (bbdb-phone-string phone))))))
114           (bbdb-record-phones record)))
115
116 (defun eudc-bbdb-extract-addresses (record)
117   (let (s c val)
118     (mapcar (function
119              (lambda (address)
120                (cond ((>= bbdb-file-format 6)
121                       (setq val (concat
122                                  (bbdb-join
123                                   (bbdb-address-streets address) "\n")
124                                  "\n"
125                                  (progn 
126                                    (setq c (bbdb-address-city address))
127                                    (setq s (bbdb-address-state address))
128                                    (if (and (> (length c) 0) (> (length s) 0))
129                                        (concat c ", " s " ")
130                                      (concat c " ")))
131                                         (bbdb-address-zip-string address))))
132                      (t
133                       (setq val (concat (unless (= 0 (length (setq s (bbdb-address-street1 address))))
134                                           (concat s "\n"))
135                                         (unless (= 0 (length (setq s (bbdb-address-street2 address))))
136                                           (concat s "\n"))
137                                         (unless (= 0 (length (setq s (bbdb-address-street3 address))))
138                                           (concat s "\n"))
139                                         (progn 
140                                           (setq c (bbdb-address-city address))
141                                           (setq s (bbdb-address-state address))
142                                           (if (and (> (length c) 0) (> (length s) 0))
143                                               (concat c ", " s " ")
144                                             (concat c " ")))
145                                         (bbdb-address-zip-string address)))))
146                (if eudc-bbdb-use-locations-as-attribute-names
147                    (cons (intern (bbdb-address-location address)) val)
148                  (cons 'addresses (concat (bbdb-address-location address) "\n" val)))))
149             (bbdb-record-addresses record))))
150
151 (defun eudc-bbdb-format-record-as-result (record)
152   "Format the BBDB RECORD as a EUDC query result record.
153 The record is filtered according to `eudc-bbdb-current-return-attributes'"
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         (setq val "Unknown BBDB attribute")))
175       (if val
176         (cond 
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          ((and (>= bbdb-file-format 6) (= (length val) 0)))
185          (t
186           (error "Unexpected attribute value")))))
187     (nreverse eudc-rec)))
188         
189
190
191 (defun eudc-bbdb-query-internal (query &optional return-attrs)
192   "Query BBDB  with QUERY.
193 QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid 
194 BBDB attribute names.  
195 RETURN-ATTRS is a list of attributes to return, defaulting to 
196 `eudc-default-return-attributes'."
197
198   (let ((eudc-bbdb-current-query query)
199         (eudc-bbdb-current-return-attributes return-attrs)
200         (query-attrs (eudc-bbdb-format-query query))
201         bbdb-attrs
202         (records (bbdb-records))
203         result
204         filtered)
205     ;; BBDB ORs its query attributes while EUDC ANDs them, hence we need to
206     ;; call bbdb-search iteratively on the returned records for each of the
207     ;; requested attributes
208     (while (and records (> (length query-attrs) 0))
209       (setq bbdb-attrs (append bbdb-attrs (list (car query-attrs))))
210       (if (car query-attrs)
211           (setq records (eval `(bbdb-search ,(quote records) ,@bbdb-attrs))))
212       (setq query-attrs (cdr query-attrs)))
213     (mapcar (function
214              (lambda (record)
215                (setq filtered (eudc-filter-duplicate-attributes record))
216                ;; If there were duplicate attributes reverse the order of the
217                ;; record so the unique attributes appear first
218                (if (> (length filtered) 1)
219                    (setq filtered (mapcar (function 
220                                            (lambda (rec)
221                                              (reverse rec)))
222                                           filtered)))
223                (setq result (append result filtered))))
224             (delq nil
225                   (mapcar 'eudc-bbdb-format-record-as-result 
226                           (delq nil 
227                                 (mapcar 'eudc-bbdb-filter-non-matching-record 
228                                         records)))))
229     result))
230
231 ;;}}}        
232
233 ;;{{{      High-level interfaces (interactive functions)
234
235 (defun eudc-bbdb-set-server (dummy)
236   "Set the EUDC server to BBDB."
237   (interactive)
238   (eudc-set-server dummy 'bbdb)
239   (message "BBDB server selected"))
240
241 ;;;}}}
242
243
244 (eudc-register-protocol 'bbdb)
245
246 (provide 'eudcb-bbdb)
247
248 ;;; eudcb-bbdb.el ends here