1 ;;; -*- Mode:Emacs-Lisp -*-
3 ;;; This file is the part of the Insidious Big Brother Database (aka BBDB),
4 ;;; copyright (c) 1991, 1992, 1993, 1995 Jamie Zawinski <jwz@lucid.com>.
5 ;;; Converting a BBDB database to a Netscape Address Book.
6 ;;; last change21-feb-97.
8 ;;; The Insidious Big Brother Database is free software; you can redistribute
9 ;;; it and/or modify it under the terms of the GNU General Public License as
10 ;;; published by the Free Software Foundation; either version 2, or (at your
11 ;;; option) any later version.
13 ;;; BBDB is distributed in the hope that it will be useful, but WITHOUT ANY
14 ;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 ;;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;;; This file attempts to convert a BBDB database to a Netscape Address Book
23 ;;; file. It doesn't work very well. If you fix it, let me know. -- jwz
27 (defun bbdb-mozilla-insert-url (string)
31 (goto-char (prog1 p (setq p (point))))
33 (skip-chars-forward "-a-zA-Z0-9.@/_\r\n" p)
35 (setq c (following-char))
37 (insert (format "%%%02X" c))
41 (defun bbdb-mozilla-insert-html (string)
45 (goto-char (prog1 p (setq p (point))))
47 (skip-chars-forward "^&<>" p)
49 (setq c (following-char))
51 (cond ((= c ?&) (insert "&") (setq p (+ p 4)))
52 ((= c ?<) (insert "<") (setq p (+ p 3)))
53 (t (insert ">") (setq p (+ p 3)))))
56 (defun bbdb-mozilla-emit-record (record aliases)
58 (cond ((setq addr (car (bbdb-record-net record)))
59 (insert " <DT><A HREF=\"mailto:")
60 (bbdb-mozilla-insert-url addr)
64 (insert " NICKNAME=\"")
65 (bbdb-mozilla-insert-html nick)
68 (let ((name (or (bbdb-record-name record)
69 (bbdb-record-company record)
71 (bbdb-mozilla-insert-html name))
79 (defun bbdb-to-netscape ()
80 (let* ((target (cons bbdb-define-all-aliases-field
82 (records1 (bbdb-search (bbdb-records)
85 nil ;"netscape\\.com" ; net
89 result record aliases match
94 (message "%d" (length records1))
96 (setq record (car records))
97 (setq aliases (bbdb-record-getprop record bbdb-define-all-aliases-field))
98 (setq aliases (and aliases (bbdb-split aliases ",")))
100 (if (setq match (assoc (car aliases) result))
101 (nconc match (cons record nil))
102 (setq result (cons (list (car aliases) record) result)))
103 (setq aliases (cdr aliases)))
104 (setq records (cdr records)))
106 (let ((alias (downcase (car (car result))))
107 (expansion (cdr (car result))))
110 (setq lists (cons (cons alias expansion) lists)))
112 (setq single-aliases (cons (cons (car expansion) alias)
114 (setq result (cdr result))))
116 ; (setq records (bbdb-records))
117 (setq records records1)
118 (set-buffer (get-buffer-create "*netscape-address-book*"))
120 (insert "<!DOCTYPE NETSCAPE-Addressbook-file-1>\n"
121 "<!-- This is an automatically generated file.\n"
122 "It will be read and overwritten.\n"
124 "<TITLE>" (user-full-name) "'s Address book</TITLE>\n"
125 "<H1>" (user-full-name) "'s Address book</H1>\n"
129 (setq record (car records))
130 (insert " <DT><A HREF=\"mailto:")
131 (let ((net (car (bbdb-record-net record))))
132 (if net (insert net))
133 (insert "\" ALIASID=\"")
134 (prin1 count (current-buffer))
136 (message "%d..." count)
137 (setq count (1+ count))
138 (cond ((setq match (cdr (assq record single-aliases)))
139 (insert " NICKNAME=\"")
140 (princ match (current-buffer))
143 (insert (or (bbdb-record-name record)
145 (bbdb-record-company record)
149 (let ((phones (bbdb-record-phones record))
150 (addrs (bbdb-record-addresses record))
151 (aka (bbdb-record-aka record))
158 (setq phone (car phones))
160 (insert (format " %14s: " (bbdb-phone-location phone)))
161 (insert (bbdb-phone-string phone) "\n<BR>")
162 (setq phones (cdr phones)))
165 (setq addr (car addrs))
167 (insert (format " %14s: " (bbdb-address-location addr)))
168 (if (= 0 (length (setq s (bbdb-address-street1 addr)))) nil
169 (indent-to 17) (insert s "\n<BR>"))
170 (if (= 0 (length (setq s (bbdb-address-street2 addr)))) nil
171 (indent-to 17) (insert s "\n<BR>"))
172 (if (= 0 (length (setq s (bbdb-address-street3 addr)))) nil
173 (indent-to 17) (insert s "\n<BR>"))
175 (insert (setq c (bbdb-address-city addr)))
176 (setq s (bbdb-address-state addr))
177 (if (and (> (length c) 0) (> (length s) 0)) (insert ", "))
179 (insert (bbdb-address-zip-string addr) "\n<BR>")
180 (setq addrs (cdr addrs))))
183 (insert (format " %14s: %s\n<BR>" "AKA"
184 (mapconcat (function identity) aka ", ")))))
185 (let ((notes (bbdb-record-raw-notes record)))
187 (setq notes (list (cons 'notes notes))))
189 (if (memq (car (car notes))
190 '(mail-alias password bbdb mail-name face mark-char aka))
193 (insert (format " %14s: " (car (car notes))))
195 (insert (cdr (car notes)))
198 (narrow-to-region p (1- (point)))
200 (while (search-forward "\n" nil t)
204 (insert (make-string 17 ?\ )))))
206 (setq notes (cdr notes)))))
208 (or match (delete-char -4))
210 (setq records (cdr records))
212 (insert "</DL><p>\n")