summaryrefslogtreecommitdiff
path: root/bits/bbdb-vcard-import.el
blob: 27f592d433276dd487d30b7c941b8c69a3b26357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
;;; bbdb-vcard-import.el -- import vCards into BBDB
;; 
;; Copyright (c) 2008 Marcus Crestani
;;
;; bbdb-vcard-import.el is free software you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;;
;; This software is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;
;; Author: Marcus Crestani <crestani@informatik.uni-tuebingen.de>
;; Created: 2008-01-03
;; Version: $Id: bbdb-vcard-import.el,v 1.6 2008/01/31 16:19:15 cvs Exp $
;; Keywords: vcard bbdb
;;
;; This requires vcard.el by NoahFriedman for the importer to work.
;;
;;    http://www.splode.com/~friedman/software/emacs-lisp/src/vcard.el
;;
;; The implementation is based on Christopher Smiths very simple
;; version of `bbdb-vcard-snarf-buffer':
;;
;;   http://www.emacswiki.org/cgi-bin/wiki/BbdbImporters#toc3
;;

;;; Commentary

;;
;; To import all vCards that are in the file ~/vCards.vcf do:
;;
;;	M-x bbdb-vcard-import RET ~/vCards.vcf RET
;;

;;; Todo

;;
;; STREET ADDRESSES and PHONE NUMBERS are not yet imported.  See
;; comment in `bbdb-vcard-merge'.
;;

;;; ChangeLog

;;
;; 2008-01-31  Marcus Crestani  <crestani@informatik.uni-tuebingen.de>
;;   - Do not enforce (type . "internet") for email addresses.
;; 
;; 2008-01-03  Marcus Crestani  <crestani@informatik.uni-tuebingen.de>
;;   - Initial version.
;;

;;; Code:

(require 'vcard)
(require 'bbdb)

(defvar bbdb-vcard-merged-records nil)

(defun bbdb-vcard-filter-empty-values (values)
  "Filter out empty values."
  (if (consp values)
      (if (string= "" (car values))
	  (bbdb-vcard-filter-empty-values (cdr values))
	(cons (car values) (bbdb-vcard-filter-empty-values (cdr values))))))

(defun bbdb-vcard-values (record field)
  "Return the values of an RECORD's FIELD; empty string entries are filtered out."
  (let ((values (vcard-values record (list field))))
    (if values
	(mapconcat 'identity 
		   (bbdb-vcard-filter-empty-values (car values))
		   ", ")
      "")))

(defun bbdb-vcard-get-emails (record)
  "Return a list of email addresses."
  (let ((pref (vcard-ref record '("email" ("type" . "pref"))))
	(rest (vcard-ref record '("email") '(("type" . "pref")))))
    (mapcar (lambda (entry) (car (cdr entry))) 
	    (if pref 
		(cons (car pref) rest)
	      rest))))

(defun bbdb-vcard-get-phones (record)
  "Return a list of phone number objects."
  (let ((pref (vcard-ref record '("tel" ("type" . "pref"))))
	(rest (vcard-ref record '("tel") '(("type" . "pref")))))
    (mapcar (lambda (entry)
	      (let ((proplist (car entry))
		    (phone (car (cdr entry))))
		(vector
		 (vcard-get-property proplist "type")
		 phone)))
	    (if pref
		(cons (car pref) rest)
	      rest))))

(defun bbdb-vcard-get-addresses (record)
  "Return a list of adress objects."
  (let ((pref (vcard-ref record '("adr" ("type" . "pref"))))
	(rest (vcard-ref record '("adr") '(("type" . "pref")))))
    (mapcar (lambda (entry)
	      (let ((proplist (car entry))
		    (phone (car (cdr entry))))
		(vector
		 (vcard-get-property proplist "type")
		 phone)))
	    (if pref
		(cons (car pref) rest)
	      rest))))

(defun bbdb-vcard-merge-interactively (name company nets addrs phones notes)
  "Interactively add a new record; see \\[bbdb-merge-interactively]."
  (let*
      ((f-l-name (bbdb-divide-name name))
       (firstname (car f-l-name))
       (lastname (nth 1 f-l-name))
       (aka nil)
       (new-record
        (vector firstname lastname aka company phones addrs
                (if (listp nets) nets (list nets)) notes
                (make-vector bbdb-cache-length nil)))
       (old-record (bbdb-search-simple name nets)))
    (if old-record
	(progn
	  (setq new-record (bbdb-merge-internally old-record new-record))
	  (bbdb-delete-record-internal old-record)))
    ;; create  new record
    (bbdb-invoke-hook 'bbdb-create-hook new-record)
    (bbdb-change-record new-record t)
    (bbdb-hash-record new-record)
    new-record))

(defun bbdb-vcard-merge (record)
  "Merge data from vcard interactively into bbdb."
  (let* ((name (bbdb-vcard-values record "fn"))
	 (company (bbdb-vcard-values record "org"))
	 (net (bbdb-vcard-get-emails record))
	 (addrs (bbdb-vcard-get-addresses record))
	 (phones (bbdb-vcard-get-phones record))
	 (categories (bbdb-vcard-values record "categories"))
	 (notes (and (not (string= "" categories))
		     (list (cons 'categories categories))))
	 ;; TODO: addrs and phones are not yet imported.  To do this
	 ;; right, figure out a way to map the several labels to
	 ;; `bbdb-default-label-list'.  Also, some phone number
	 ;; conversion may break the format of numbers.
	 (new-record (bbdb-vcard-merge-interactively name company net nil nil notes)))
    (setq bbdb-vcard-merged-records (append bbdb-vcard-merged-records 
					    (list new-record)))))

(defun bbdb-vcard-snarf-region (begin end)
  "Bbdb-snarf each match."
  (let ((record (vcard-parse-region begin end)))
    (bbdb-vcard-merge record)))

(defun bbdb-vcard-snarf-buffer (buf)
  "Traverse BUF via regex.  Bbdb-snarf against each match."
  (setq bbdb-vcard-merged-records nil)
  (let ((bbdb-current-buffer (current-buffer))
	(bbdb-current-point (point-min))
	(bbdb-next-point (point-min)))
    (switch-to-buffer buf)
    (goto-char bbdb-current-point)
    (while (re-search-forward "END:VCARD" nil (message "%s done" buf))
      (setq bbdb-next-point (point))
      (bbdb-vcard-snarf-region bbdb-current-point (point))
      (switch-to-buffer buf)
      (goto-char bbdb-next-point)
      (setq bbdb-current-point (point)))
    (switch-to-buffer bbdb-current-buffer)
    (bbdb-display-records bbdb-vcard-merged-records)))

(defun bbdb-vcard-snarf-current-buffer ()
  "Snarf the vcards in the current buffer."
  (interactive)
  (bbdb-vcard-snarf-buffer (current-buffer)))

(defun bbdb-vcard-import-current-buffer ()
  "Import the vcards in the current buffer into your bbdb."
  (interactive)
  (bbdb-vcard-snarf-current-buffer))

(defun bbdb-vcard-import (file)
  "Import the vcards in FILE into your bbdb."
  (interactive "FvCard file to read from: ")
  (let ((buffer (find-file file)))
    (bbdb-vcard-snarf-buffer buffer)
    (revert-buffer buffer)
    (kill-buffer buffer)))

(provide 'bbdb-vcard-import)