forked from trebb/bbdb-vcard
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bbdb-vcard.el
1466 lines (1374 loc) · 62.2 KB
/
bbdb-vcard.el
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; bbdb-vcard.el --- vCard import/export for BBDB
;; Copyright (c) 2010 Bert Burgemeister
;; Author: Bert Burgemeister <trebbu@googlemail.com>
;; Toke Høiland-Jørgensen
;; Kevin Brubeck Unhammer
;; Steve Purcell
;; Vincent Geddes <vincent.geddes@gmail.com>
;; Keywords: data calendar mail news
;; URL: https://github.com/tohojo/bbdb-vcard
;; Package-Requires: ((bbdb "3.0"))
;; Version: 0.4.1
;; This program 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 program 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, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;
;; The exporter functionality is based on code from
;; bbdb-vcard-export.el by Jim Hourihan and Alex Schroeder.
;;; Commentary:
;;
;; Import and export of vCards as defined in RFC 2425 and RFC 2426
;; to/from The Insidious Big Brother Database (BBDB) v3.
;;
;; For user and developer documentation, refer to the bundled BBDB vCard
;; info manual.
;;
;; Mapping of vCard types to BBDB types:
;;
;; +-------------------------+-----------------------------------------+
;; | VCARD TYPE;PARAMETERS | STORAGE IN BBDB |
;; | | |
;; |-------------------------+-----------------------------------------|
;; | VERSION | - |
;; | REV | - |
;; |-------------------------+-----------------------------------------|
;; | N | First occurrence: |
;; | | Firstname |
;; | | Lastname |
;; | | |
;; | | Rest: |
;; | | AKAs (append) |
;; |-------------------------+-----------------------------------------|
;; | FN | AKAs (append) |
;; | NICKNAME | AKAs (append) |
;; |-------------------------+-----------------------------------------|
;; | ORG | Organizations (append) |
;; |-------------------------+-----------------------------------------|
;; | ADR;TYPE=x,HOME,y | Addresses<Home |
;; | ADR;TYPE=x;TYPE=HOME | Addresses<Home |
;; | ADR;TYPE=x,WORK,y | Addresses<Office |
;; | ADR;TYPE=x;TYPE=WORK | Addresses<Office |
;; | ADR;TYPE=x,y,z | Addresses<x,y,z |
;; | ADR;TYPE=x;TYPE=y | Addresses<x,y |
;; | ADR | Addresses<Office |
;; |-------------------------+-----------------------------------------|
;; | TEL;TYPE=x,HOME,y | Phones<Home (append) |
;; | TEL;TYPE=x;TYPE=HOME | Phones<Home (append) |
;; | TEL;TYPE=x,WORK,y | Phones<Office (append) |
;; | TEL;TYPE=x;TYPE=WORK | Phones<Office (append) |
;; | TEL;TYPE=x,CELL,y | Phones<Mobile (append) |
;; | TEL;TYPE=x;TYPE=CELL | Phones<Mobile (append) |
;; | TEL;TYPE=x,y,z | Phones<x,y,z (append) |
;; | TEL;TYPE=x;TYPE=y | Phones<x,y (append) |
;; | TEL | Phones<Office (append) |
;; |-------------------------+-----------------------------------------|
;; | EMAIL;TYPE=x,y,z | Net-Addresses (append) |
;; | URL | Xfields<url |
;; |-------------------------+-----------------------------------------|
;; | BDAY | Xfields<anniversary (append as birthday)|
;; | X-BBDB-ANNIVERSARY | Xfields<anniversary (append) |
;; |-------------------------+-----------------------------------------|
;; | PHOTO (inline base64) | Xfields<image-filename |
;; | (uri) | Xfields<image-uri |
;; |-------------------------+-----------------------------------------|
;; | SOUND (inline base64) | Xfields<sound-filename |
;; | (uri) | Xfields<sound-uri |
;; |-------------------------+-----------------------------------------|
;; | KEY (inline base64) | Xfields<gpg-key-filename |
;; | (uri) | Xfields<gpg-key-uri |
;; |-------------------------+-----------------------------------------|
;; | NOTE | Xfields<notes (append) |
;; | CATEGORIES | Xfields<mail-alias (append) |
;; | SORT-STRING | Xfields<sort-string |
;; | GEO | Xfields<geo |
;; | TZ | Xfields<tz |
;; | LABEL | Xfields<label |
;; | LOGO | Xfields<logo |
;; | TITLE | Xfields<title |
;; | ROLE | Xfields<role |
;; | AGENT | Xfields<agent |
;; | MAILER | Xfields<mailer |
;; | UID | Xfields<vcard-uid |
;; | PRODID | Xfields<prodid |
;; | CLASS | Xfields<class |
;; | X-BBDB-FOO | Xfields<foo |
;; | X-FOO | Xfields<x-foo |
;; |-------------------------+-----------------------------------------|
;; | ANYJUNK;a=x;b=y | Xfields<anyjunk;a=x;b=y |
;; +-------------------------+-----------------------------------------+
;;
;;; Code:
(require 'cl-lib)
(require 'bbdb)
(require 'bbdb-vcard-vcard21)
(require 'bbdb-com)
(defconst bbdb-vcard-version "0.4.1"
"Version of the vCard importer/exporter.
The major part increases on user-visible changes.")
;;; Custom Variables:
(defgroup bbdb-vcard nil
"Customizations for vCards"
:group 'bbdb)
(defcustom bbdb-vcard-directory
(file-name-as-directory
(concat (file-name-as-directory user-emacs-directory)
"bbdb-vcard"))
"The directory under which vCard media attachments are stored"
:group 'bbdb-vcard
:type 'directory
:set-after '(user-emacs-directory)
)
(defcustom bbdb-vcard-skip-on-import '("^X-GSM-")
"Regexps-list describing vCard elements that are to be discarded during import.
For example: (\"^X-GSM-\" \"^X-MS-\")"
:group 'bbdb-vcard
:type '(repeat regexp))
(defcustom bbdb-vcard-skip-on-export nil
"Regexp-list describing bbdb fields that are to be discarded during export.
Example: `(\"field1\\|field2\" \"field3\")'."
:group 'bbdb-vcard
:type '(repeat regexp))
(defcustom bbdb-vcard-skip-valueless t
"Skip vCard element types with an empty value.
Nil means insert empty types into BBDB."
:group 'bbdb-vcard
:type 'boolean)
(defcustom bbdb-vcard-import-translation-table
'(("CELL\\|CAR" . "cell")
("WORK" . "work")
("DOM\\|HOME" . "home"))
"Label translation on vCard import.
Alist with translations of location labels for addresses and phone
numbers. Cells are (VCARD-LABEL-REGEXP . BBDB-LABEL). One entry
should map a default BBDB label to the empty string (`\"^$\"') which
corresponds to unlabelled vCard elements."
:group 'bbdb-vcard
:type '(alist :key-type
(choice regexp (const :tag "Empty (as default)" "^$"))
:value-type string))
(defcustom bbdb-vcard-try-merge t
"Try to merge vCards into existing BBDB records.
Nil means create a fresh bbdb record each time a vCard is read."
:group 'bbdb-vcard
:type 'boolean)
(defcustom bbdb-vcard-type-canonicalizer 'upcase
"Function to apply to vCard type names on export.
Most reasonable choices are `upcase' and `downcase'."
:group 'bbdb-vcard
:type 'function)
(defcustom bbdb-vcard-x-bbdb-candidates
'(attribution
finger-host
gnus-score
mark-char
mail-name
face
tex-name
aka) ; not sure what this is for
"List of translatable BBDB user field names.
On export to a vCard, they are transformed into vCard-compliant
extended types by prepending `X-BBDB-'. On (re-)import, this prefix
is removed again."
:group 'bbdb-vcard
:type '(repeat symbol))
(defcustom bbdb-vcard-export-translation-table
'(("Mobile" . "cell")
("Office" . "work"))
"Label translation on vCard export.
Alist with translations of location labels for addresses and phone
numbers. Cells are (BBDB-LABEL-REGEXP . VCARD-LABEL)."
:group 'bbdb-vcard
:type '(alist :key-type
(choice regexp (const :tag "Empty (as default)" "^$"))
:value-type string))
(defcustom bbdb-vcard-export-coding-system
'utf-8-dos ; dos line endings mandatory according to RFC 2426
"Coding system to use when writing vCard files."
:group 'bbdb-vcard
:type 'symbol)
(defcustom bbdb-vcard-default-dir "~/exported-vcards/"
"Default storage directory for exported vCards.
Nil means current directory."
:group 'bbdb-vcard
:type '(choice directory (const :tag "Current directory" nil)))
(defcustom bbdb-vcard-name-imported-priority
'(first-last formated-name bbdb-vcard-generate-bbdb-name)
"Set which name should be store into bbdb database when import a vcard.
the valid name first found will be used.
User can add a function into this list, the return value of function will
regard as a candidate of bbdb name. first-name, last-name and formated-name
of vcard will be inputed as arguments. user can see reference function:
`bbdb-vcard-generate-bbdb-name'."
:group 'bbdb-vcard
:type '(repeat (choice symbol function)))
(defvar bbdb-vcard-media-directory
(file-name-as-directory "media")
"The relative subdirectory under `bbdb-vcard-directory' where
media objects are stored")
(defun bbdb-vcard-image-basename (record)
"Returns the image filename (sans suffix), for a record.
This is meant to be bound to `bbdb-image', so that BBBD can lookup the
image for the record."
(let ((image-filename (bbdb-vcard-bbdb-record-field record 'image-filename)))
(if image-filename
(file-name-base image-filename)
nil)))
(defvar bbdb-vcard-media-types
;; sounds
'(("basic" ("sound" "snd" "audio/basic"))
("wav" ("sound" "wav" "audio/wav"))
("ogg" ("sound" "ogg" "audio/ogg"))
("mp3" ("sound" "mp3" "audio/mpeg"))
("m4a" ("sound" "m4a" "audio/mpeg"))
("aac" ("sound" "aac" "audio/aac"))
;; images
("png" ("image" "png" "image/png"))
("jpeg" ("image" "jpg" "image/jpeg"))
("gif" ("image" "gif" "image/gif"))
("tiff" ("image" "tiff" "image/tiff"))
("xbm" ("image" "xbm" "image/xbm"))
("xpm" ("image" "xpm" "image/xpm"))
;; keys
("gpg" ("key" "asc" "application/pgp-keys"))
("pgp" ("key" "asc" "application/pgp-keys")))
"A list of supported media types. Each item is a media descriptor of
the form (TYPE (PREFIX SUFFIX MIMETYPE)). TYPE is equivalent to the
corresponding 'TYPE' parameter value in a vCard field. The string PREFIX and
SUFFIX are used to form a unique filename for the media object in order to
form a unique filename. MIMETYPE is currently unused.")
(defvar bbdb-vcard-mime-types
'(("audio/basic" . "basic")
("audio/wav" . "wav")
("audio/ogg" . "ogg")
("audio/mp3" . "mp3")
("audio/mpeg" . "mp3")
("audio/mp4" . "m4a")
("audio/aac" . "aac")
("image/png" . "png")
("image/jpeg" . "jpeg")
("image/gif" . "gif")
("image/tiff" . "tiff")
("image/xbm" . "xbm")
("image/xpm" . "xpm")
("application/pgp-keys" . "gpg"))
"Maps a mime-type to the name of a media descriptor")
(defun bbdb-vcard-initialize ()
"Initializes bbdb-vcard, particularly adding the media directory
to `bbdb-image-path'."
;; bbdb-image-path
(if (and bbdb-image-path (listp bbdb-image-path))
(add-to-list 'bbdb-image-path
(concat bbdb-vcard-directory bbdb-vcard-media-directory))
(setq bbdb-image-path
(list (concat bbdb-vcard-directory bbdb-vcard-media-directory))))
;; bbdb-image
(unless bbdb-image
(setq bbdb-image #'bbdb-vcard-image-basename)))
;;;###autoload
(defun bbdb-vcard-import-region (begin end &optional source-name)
"Import the vCards between BEGIN and END into BBDB.
Existing BBDB records may be altered."
(interactive "r")
(let ((results
(cl-remove-if 'null
(bbdb-vcard-iterate-vcards
'bbdb-vcard-import-vcard
(buffer-substring-no-properties begin end)))))
(cond
((and (null results) source-name)
(message "No vCard objects were found in %s" source-name))
((null results)
(message "No vCard objects were found in region"))
((= (length results) 1)
(message "Imported 1 vCard object into BBDB"))
(t
(message "Imported %s vCard objects into BBDB" (length results))))))
;;;###autoload
(defun bbdb-vcard-import-buffer (vcard-buffer)
"Import vCards from VCARD-BUFFER into BBDB.
Existing BBDB records may be altered."
(interactive (list (current-buffer)))
(set-buffer vcard-buffer)
(bbdb-vcard-import-region (point-min) (point-max) (buffer-name vcard-buffer)))
;;;###autoload
(defun bbdb-vcard-import-file (vcard-file)
"Import vCards from VCARD-FILE into BBDB.
If VCARD-FILE is a wildcard, import each matching file. Existing BBDB
records may be altered."
(interactive "fvCard file: ")
(with-temp-buffer
(insert-file-contents vcard-file)
(bbdb-vcard-import-region (point-min) (point-max) vcard-file)))
;;;###autoload
(defun bbdb-vcard-export
(filename-or-directory all-records-p one-file-per-record-p &optional allow-overwrite)
"From Buffer *BBDB*, write one or more record(s) as vCard(s) to file(s).
\\<bbdb-mode-map>\
If \"\\[bbdb-apply-next-command-to-all-records]\\[bbdb-vcard-export]\"\
is used instead of simply \"\\[bbdb-vcard-export]\", then export all \
records currently
in the *BBDB* buffer. If used with prefix argument, store records
in individual files."
(interactive
(let ((default-filename ; argument filename-or-directory
(bbdb-vcard-make-file-name (bbdb-current-record nil)))
(all-records-p (bbdb-do-all-records)))
(list
(if all-records-p
(if current-prefix-arg
(read-directory-name "Write vCard files to directory: "
bbdb-vcard-default-dir nil 42)
(read-file-name
"Write vCards to file: "
bbdb-vcard-default-dir
nil nil
(format-time-string "%Y-%m-%dT%H%M.vcf" (current-time))))
(read-file-name "Write current record to vCard file: "
bbdb-vcard-default-dir nil nil default-filename))
all-records-p ; argument all-records-p
current-prefix-arg))) ; argument one-file-per-record-p
(if all-records-p
(let ((records (progn (set-buffer bbdb-buffer-name)
(mapcar 'car bbdb-records)))
used-up-basenames) ; keep them unique
(if one-file-per-record-p
(progn
(dolist (record records)
(with-temp-buffer
(let ((basename
(bbdb-vcard-make-file-name record
used-up-basenames)))
(insert (bbdb-vcard-from record))
(bbdb-vcard-write-buffer
(concat filename-or-directory basename)
allow-overwrite)
(push basename used-up-basenames))))
(message "Wrote %d vCards to %s"
(length used-up-basenames) filename-or-directory))
(with-temp-buffer ; all visible BBDB records in one file
(dolist (record records)
(insert (bbdb-vcard-from record)))
(bbdb-vcard-write-buffer filename-or-directory allow-overwrite))))
(let ((vcard (bbdb-vcard-from (bbdb-current-record nil)))) ; current record
(with-temp-buffer
(insert vcard)
(bbdb-vcard-write-buffer filename-or-directory allow-overwrite)))))
;;;###autoload
(defun bbdb-vcard-export-to-kill-ring (all-records-p)
"From Buffer *BBDB*, copy one or more record(s) as vCard(s) to the kill ring.
\\<bbdb-mode-map>\
If \"\\[bbdb-apply-next-command-to-all-records]\
\\[bbdb-vcard-export-to-kill-ring]\"\
is used instead of simply \"\\[bbdb-vcard-export-to-kill-ring]\", \
then export all records currently in
the *BBDB* buffer."
(interactive (let ((all-records-p (bbdb-do-all-records)))
(list all-records-p)))
(if all-records-p
(let ((records (progn (set-buffer bbdb-buffer-name)
(mapcar 'car bbdb-records))))
(kill-new "")
(dolist (record records)
(kill-append (bbdb-vcard-from record) nil))
(message "Saved %d records as vCards" (length records)))
(kill-new (bbdb-vcard-from (bbdb-current-record nil)))
(message "Saved record as vCard")))
(defun bbdb-vcard-default-keybindings ()
"Assign the default bbdb-vcard keybindings."
(define-key bbdb-mode-map [(v)] 'bbdb-vcard-export)
(define-key bbdb-mode-map [(V)] 'bbdb-vcard-export-to-kill-ring))
(defun bbdb-vcard-iterate-vcards (vcard-processor vcards)
"Apply VCARD-PROCESSOR successively to each vCard in string VCARDS.
When VCARDS is nil, return nil. Otherwise, return t."
(with-temp-buffer
(insert vcards)
(goto-char (point-min))
;; Change CRLF into CR if necessary, dealing with inconsistent line
;; endings.
(while (re-search-forward "\r\n" nil t)
(replace-match "\n"))
(let ((vcards-normalized (bbdb-vcard-unfold-lines (buffer-string)))
(results nil))
(erase-buffer)
(insert vcards-normalized)
(goto-char (point-min))
(while (re-search-forward
"^\\([[:alnum:]-]*\\.\\)?*BEGIN:VCARD[\n[:print:][:cntrl:]]*?\\(^\\([[:alnum:]-]*\\.\\)?END:VCARD\\)"
nil t)
(let ((vcard (match-string 0)))
(if (string= "3.0" (bbdb-vcard-version-of vcard))
(push (funcall vcard-processor vcard) results)
(push (funcall vcard-processor ; probably a v2.1 vCard
(bbdb-vcard-unfold-lines
(bbdb-vcard-convert-to-3.0 vcard)))
results))))
results)))
(defun bbdb-vcard-version-of (vcard)
"Return version number string of VCARD."
(with-temp-buffer
(insert vcard)
(car (bbdb-vcard-values-of-type "version" "content"))))
(defcustom bbdb-vcard-type-spec
'(("FN" * nil nil t)
("N" * t t t)
("NICKNAME" * nil t t)
("ORG" * t nil t)
("EMAIL" * nil nil t)
("TEL" * nil nil t)
("ADR" * t t t)
("URL" * nil nil t)
("NOTE" * nil nil t)
("BDAY" * nil nil t)
("CATEGORIES" * nil t t)
("MAILER" * nil nil t)
("PHOTO" * nil nil t)
("SOUND" * nil nil t)
("KEY" * nil nil t)
("UID" * nil nil t)
("X-BBDB-ANNIVERSARY" * nil nil t)
("X-BBDB-WEDDING" * nil nil t)
("X-ABUID" * nil nil t))
"A list of recognized vCard entries. Each member is of the
form (TYPE CARDINALITY STRUCTURED-P LIST-P UNESCAPE-P). TYPE is the name of the
entry, STRUCTURED-P indicates that the value is structured and each component is
separated by ';'. LIST-P indicates that the value is a list of text items,
separated by ','. If both STRUCTURED-P and LIST-P are non-nil, then the
value is considered a structured value where each component is a
list of text items. if UNESCAPE-P is non-nil the value is unescaped"
:group 'bbdb-vcard
:type '(repeat sexp))
(defun bbdb-vcard-scardize (vcard)
"Converts a vCard into an Sexp-Card of the form:
((TYPE (((PARAM . VALUE) ...) ...)) ...)
TYPE is a symbol and a member of `bbdb-vcard-import-types'. PARAM is a
a symbol that represents a vCard property parameter. VALUE is a string or
nil.
PARAM is not limited to, but can be equal to any of the
following values:
`content': The actual content of the vCard field.
`value': The 'VALUE' property parameter
`type': The 'TYPE' property parameter
See http://tools.ietf.org/search/rfc6350#section-5 for a full list
of possible property parameters"
(with-temp-buffer
(insert vcard)
(let ((scard
(cl-remove-if
(lambda (element)
(null (cadr element)))
(mapcar 'bbdb-vcard-scardize-type bbdb-vcard-type-spec))))
scard)))
(defun bbdb-vcard-scardize-type (type)
(cl-destructuring-bind
(name cardinality structured-p list-p unescape-p) type
(list name
(mapcar
(lambda (element)
(mapcar
(lambda (param)
(let ((value
(if (and (equal (car param) "content") list-p)
(bbdb-vcard-process-strings
(lambda (value)
(bbdb-vcard-split-structured-text value ","))
(cdr param))
(cdr param))))
(list (car param)
(if (listp value)
(mapcar
(lambda (value)
(cond
((and unescape-p (listp value))
(mapcar
(lambda (list-of-strings)
(bbdb-vcard-unescape-strings list-of-strings))
value))
((and unescape-p (stringp value))
(bbdb-vcard-unescape-strings value))
(t value)))
value)
(if unescape-p
(bbdb-vcard-unescape-strings value)
value)))))
element))
(bbdb-vcard-elements-of-type name nil structured-p)))))
(defun bbdb-vcard-search (scard type &optional param)
"Search bbdb records from `scard' by `type' and `param'.
if `type' name match `bbdb-vcard-skip-on-import' or its
elements (when it is a regexps-list), return `nil'."
(let* ((elements (cadr (assoc type scard)))
(regexps bbdb-vcard-skip-on-import)
(skip-import-p
(if (stringp regexps)
(string-match-p regexps type)
(cl-some
#'(lambda (regexp)
(when regexp
(string-match-p regexp type)))
regexps))))
(unless skip-import-p
(if param
(cl-remove-if 'null
(mapcar (lambda (element)
(cadr (assoc param element)))
elements))
elements))))
(defmacro bbdb-vcard-search-intersection
(records &optional name organization mail xfields phone)
"Search RECORDS for records that match each non-nil argument."
(let*
((phone-search
(if phone `(when ,phone
(bbdb-search ,records nil nil nil nil ,phone nil))
records))
(xfields-search
(if xfields `(when ,xfields
(bbdb-search ,phone-search nil nil nil ,xfields nil nil))
phone-search))
(mail-search
(if mail `(when ,mail
(bbdb-search ,xfields-search nil nil ,mail nil nil nil))
xfields-search))
(organization-search
(if organization `(when ,organization
(bbdb-search
,mail-search nil ,organization nil nil nil nil))
mail-search))
(name-search
(if name `(when ,name (bbdb-search ,organization-search ,name))
organization-search)))
name-search))
(defun bbdb-vcard-import-vcard (vcard)
(condition-case-unless-debug err
(bbdb-vcard-import-vcard-internal vcard)
((error nil)
(progn
(message "Error encountered while parsing vcard: %s" err)
nil))))
(defun bbdb-vcard-generate-bbdb-name (first-name last-name formated-name)
"Generate bbdb name depend on `first-name', `last-name' and `formated-name',
Please see also `bbdb-vcard-name-imported-priority'."
(let ((first-name (or first-name ""))
(last-name (or last-name "")))
(cond
((and (string-match-p "\\cc" first-name)
(string-match-p "\\cc" last-name))
(concat last-name first-name))
(t (concat first-name " " last-name)))))
(defun bbdb-vcard-import-vcard-internal (vcard-or-scard)
"Store VCARD (version 3.0) in BBDB.
Extend existing BBDB records where possible."
(let* ((scard (if (stringp vcard-or-scard)
(bbdb-vcard-scardize vcard-or-scard)
vcard-or-scard))
(raw-name (car (bbdb-vcard-search scard "N" "content")))
(name-components (bbdb-vcard-unvcardize-name raw-name))
(vcard-formatted-name (car (bbdb-vcard-search scard "FN" "content")))
;; Name suitable for storing in BBDB
(name (cl-find-if
#'(lambda (x)
(if (stringp x)
(> (length x) 0)
(or (car x) (cdr x))))
(mapcar
#'(lambda (x)
(cond
((eq x 'first-last)
(cons (nth 0 name-components)
(nth 1 name-components)))
((eq x 'formated-name) vcard-formatted-name)
((functionp x)
(funcall x (nth 0 name-components)
(nth 1 name-components)
vcard-formatted-name))))
bbdb-vcard-name-imported-priority)))
;; A unique name, which will replace origin name
;; when encounter "need-solved-by-hand" conflict.
(name-used-mark-conflict
(concat "@Conflict"
(format-time-string "%M%S" nil t)
"@-"
(or vcard-formatted-name
(bbdb-vcard-generate-bbdb-name
(nth 0 name-components)
(nth 1 name-components)
vcard-formatted-name)
"???")))
(name-need-manual-edit-p nil)
;; Affixes suitable for storing in BBDB
(vcard-affixes (nth 2 name-components))
;; Name to search for in BBDB now:
(name-to-search-for
(when raw-name (if (stringp raw-name)
raw-name
(let* ((given-name (nth 1 raw-name))
(family-name (nth 0 raw-name))
(separator
(if (or (not given-name) ; if given-name or family-name
(not family-name) ; is `nil', whitespace is needless.
;; No whitespace needed between
;; given-name and family-name for CJK users.
(string-match-p "\\cc" (or given-name ""))
(string-match-p "\\cc" (or family-name "")))
" *"
" +")))
(concat "^\\(" given-name separator family-name "\\)$\\|"
"^\\(" family-name separator given-name "\\)$")))))
(vcard-nicknames
(bbdb-vcard-flatten (bbdb-vcard-search scard "NICKNAME" "content")))
(vcard-nicknames-backup nil)
;; Organization suitable for storing in BBDB:
(vcard-org
(mapcar (lambda (org)
(bbdb-vcard-unvcardize-org org))
(bbdb-vcard-search scard "ORG" "content")))
;; Organization to search for in BBDB now:
(org-to-search-for (car vcard-org))
;; Email suitable for storing in BBDB:
(vcard-email (bbdb-vcard-search scard "EMAIL" "content"))
;; Email to search for in BBDB now:
(email-to-search-for
(when vcard-email
(concat "\\(" (bbdb-join vcard-email "\\)\\|\\(") "\\)")))
;; Phone numbers suitable for storing in BBDB:
(vcard-email-backup nil)
(vcard-tels
(mapcar (lambda (tel)
(vector (bbdb-vcard-translate (cadr (assoc "type" tel)))
(cadr (assoc "content" tel))))
(bbdb-vcard-search scard "TEL")))
;; Phone numbers to search for in BBDB now:
(tel-to-search-for
(when vcard-tels
(concat "\\("
(mapconcat (lambda (x) (elt x 1))
vcard-tels "\\)\\|\\(")
"\\)")))
(vcard-tels-backup nil)
;; Addresses
(vcard-adrs
(mapcar 'bbdb-vcard-unvcardize-adr
(bbdb-vcard-search scard "ADR")))
;; URLs
(vcard-url (car (bbdb-vcard-search scard "URL" "content")))
;; Notes
(vcard-notes (car (bbdb-vcard-search scard "NOTE" "content")))
;; Bdays
(vcard-bday (bbdb-vcard-unvcardize-date-time
(car (bbdb-vcard-search scard "BDAY" "content"))))
(bday-to-search-for vcard-bday)
;; Non-birthday anniversaries, probably exported by ourselves:
(vcard-x-bbdb-anniversaries
(car (bbdb-vcard-search scard "X-BBDB-ANNIVERSARY" "content")))
(vcard-x-bbdb-weddings
(car (bbdb-vcard-search scard "X-BBDB-WEDDING" "content")))
;; UIDs
(vcard-uid
(car (bbdb-vcard-search scard "UID" "content")))
(vcard-x-abuid
(car (bbdb-vcard-search scard "X-ABUID" "content")))
;; Categories
(vcard-categories
(bbdb-concat 'mail-alias
(car (bbdb-vcard-search scard "CATEGORIES" "content"))))
(vcard-photo (car (bbdb-vcard-search scard "PHOTO")))
(vcard-sound (car (bbdb-vcard-search scard "SOUND")))
(vcard-key (car (bbdb-vcard-search scard "KEY")))
(vcard-xfields (cl-remove-if
'null
(mapcar
(lambda (spec)
(if (and (string-prefix-p "X-BBDB-" (car spec))
(not (string= "X-BBDB-ANNIVERSARY" (car spec)))
(not (string= "X-BBDB-WEDDING" (car spec))))
`(,(intern (downcase (string-remove-prefix "X-BBDB-" (car spec))))
. ,(cadaar (bbdb-vcard-search scard (car spec))))
nil))
bbdb-vcard-type-spec)))
(record
(or
;; (a) try organization and mail and name:
(car (and bbdb-vcard-try-merge
(bbdb-vcard-search-intersection
(bbdb-records)
name-to-search-for
org-to-search-for email-to-search-for)))
;; (b) try organization and name:
(car (and bbdb-vcard-try-merge
(bbdb-vcard-search-intersection
(bbdb-records) name-to-search-for org-to-search-for)))
;; (c) try net and name; we may change organization here:
(car (and bbdb-vcard-try-merge
(bbdb-vcard-search-intersection
(bbdb-records)
name-to-search-for nil email-to-search-for)))
;; (d) try birthday and name; we may change organization here:
(car (and bbdb-vcard-try-merge
(bbdb-vcard-search-intersection
(bbdb-records)
name-to-search-for nil nil bday-to-search-for)))
;; (e) try phone and name; we may change organization here:
(car (and bbdb-vcard-try-merge
(bbdb-vcard-search-intersection
(bbdb-records)
name-to-search-for nil nil nil tel-to-search-for)))
;; (f) if only match name; import with a new name, which format
;; is: <name>-imported-<time-string>
;; use can merge it by hand.
(and bbdb-vcard-try-merge
(let ((name-exist-p
(bbdb-vcard-search-intersection
(bbdb-records)
name-to-search-for))
(email-exist-p
(bbdb-vcard-search-intersection
(bbdb-records)
nil nil email-to-search-for))
(tels-exist-p
(bbdb-vcard-search-intersection
(bbdb-records)
nil nil nil nil tel-to-search-for)))
(when (or name-exist-p email-exist-p tels-exist-p)
;; When encounter contacts conflict which must resolved by hand.
;; we rename origin contact name to a unique name.
(setq name-need-manual-edit-p t)
(setq vcard-nicknames-backup t))
(when email-exist-p
;; When current imported email is existed BBDB database,
;; We will backup it in new bbdb field: backup-mail,
;; Which can tell user to merge by hand in BBDB buffer.
(setq vcard-email-backup t))
(when tels-exist-p
;; When current imported phone is existed BBDB database,
;; We will backup it in new bbdb field: backup-phone,
;; Which can tell user to merge by hand in BBDB buffer.
(setq vcard-tels-backup t))
nil))
;; No existing record found; make a fresh one:
(let ((record (bbdb-empty-record)))
record))))
(when name-need-manual-edit-p
(message (format "Need edit by hand: %S in your *contacts source* and %S in your *BBDB buffer*."
name
name-used-mark-conflict))
(setq name name-used-mark-conflict))
(when name
(if (stringp name)
(bbdb-record-set-field record 'name name)
(progn
(bbdb-record-set-field record 'firstname (car name))
(bbdb-record-set-field record 'lastname (cdr name)))))
(when vcard-nicknames
(if vcard-nicknames-backup
(bbdb-record-set-field record 'backup-aka
(mapconcat #'identity vcard-nicknames ", ")
t)
(let* ((fn (bbdb-vcard-bbdb-record-field record 'firstname))
(ln (bbdb-vcard-bbdb-record-field record 'lastname))
(aka
(nreverse
(cl-set-difference
(cl-reduce (lambda (x y) (cl-union x y :test 'string=))
(list vcard-nicknames))
(list (concat fn " " ln) fn ln)
:test 'string=))))
(bbdb-record-set-field record 'aka aka t))))
(when vcard-affixes
(bbdb-record-set-field
record 'affix vcard-affixes t))
(when vcard-org
(bbdb-record-set-field
record 'organization vcard-org t))
(when vcard-email
(if vcard-email-backup
(bbdb-record-set-field record 'backup-mail
(mapconcat #'identity vcard-email ", ")
t)
(bbdb-record-set-field
record 'mail vcard-email t)))
(when vcard-adrs
(bbdb-record-set-field
record 'address vcard-adrs t))
(when vcard-tels
(if vcard-tels-backup
(bbdb-record-set-field record 'backup-phone
(mapconcat
#'(lambda (x)
(elt x 1))
vcard-tels ", ")
t)
(bbdb-record-set-field
record 'phone vcard-tels t)))
(when vcard-url
(bbdb-record-set-field
record 'url vcard-url t))
(when vcard-notes
(bbdb-record-set-field
record 'notes vcard-notes t))
(when vcard-bday
(bbdb-record-set-field
record 'birthday vcard-bday t))
(when vcard-bday
(bbdb-record-set-field
record 'birthday vcard-bday t))
(when vcard-x-bbdb-weddings
(bbdb-record-set-field
record 'wedding vcard-x-bbdb-weddings t))
(when vcard-x-bbdb-anniversaries
(bbdb-record-set-field
record 'anniversary vcard-x-bbdb-anniversaries t))
(when vcard-photo
(bbdb-vcard-import-media record 'image 'image-uri vcard-photo))
(when vcard-key
(bbdb-vcard-import-media record 'gpg-key 'gpg-key-uri vcard-key))
(when vcard-sound
(bbdb-vcard-import-media record 'sound 'sound-uri vcard-sound))
(when vcard-categories
(bbdb-record-set-field
record 'mail-alias vcard-categories t))
(when vcard-uid
(bbdb-record-set-field
record 'vcard-uid vcard-uid t))
(when vcard-x-abuid
(bbdb-record-set-field
record 'vcard-x-abuid vcard-x-abuid t))
(while nil
(when (string-match "^\\([[:alnum:]-]*\\.\\)?AGENT"
(symbol-name (car other-vcard-type)))
;; Notice other vCards inside the current one.
(bbdb-vcard-iterate-vcards
'bbdb-vcard-import-vcard ; needed for inner v2.1 vCards:
(replace-regexp-in-string "\\\\" "" (cdr other-vcard-type)))))
(bbdb-record-set-field
record 'xfields vcard-xfields t)
(bbdb-change-record record t t)
record))
(defun bbdb-vcard-import-media (record field-fn field-uri vcard-media)
(if (and (equal "b" (cadr (assoc "encoding" vcard-media)))
(cadr (assoc "content" vcard-media)))
(let ((filename (bbdb-vcard-import-inline-media vcard-media)))
(when filename
(bbdb-record-set-field record field-fn filename)))
(bbdb-record-set-field
record field-uri
(bbdb-vcard-unescape-strings (cadr (assoc "content" vcard-media))))))
(defun bbdb-vcard-flatten (objects)
"Flattens nested lists. For example, when applied to
list ((A B C) D (E F)), the result would be (A B C D E F)"
(apply 'append (mapcar (lambda (obj)
(if (not (listp obj))
(list obj)
obj))
objects)))
(defun bbdb-vcard-bbdb-record-field (record field)
"For bbdb RECORD return the value of FIELD, if `field' name match
`bbdb-vcard-skip-on-export' or its elements (when it is a regexp list),
return `nil'."
(let* ((field-name (symbol-name field))
(regexps bbdb-vcard-skip-on-export)
(skip-export-p
(if (stringp regexps)
(string-match-p regexps field-name)
(cl-some
#'(lambda (regexp)
(when regexp
(string-match-p regexp field-name)))
regexps))))
(unless skip-export-p
(bbdb-record-field record field))))
(defun bbdb-vcard-from (record)
"Return BBDB RECORD as a vCard."
(with-temp-buffer
(let* ((name (bbdb-vcard-bbdb-record-field record 'name))
(first-name (bbdb-vcard-bbdb-record-field record 'firstname))
(last-name (bbdb-vcard-bbdb-record-field record 'lastname))
(affix (bbdb-vcard-bbdb-record-field record 'affix))
(aka (bbdb-vcard-bbdb-record-field record 'aka))
(organization (bbdb-vcard-bbdb-record-field record 'organization))
(net (bbdb-vcard-bbdb-record-field record 'mail))
(phones (bbdb-vcard-bbdb-record-field record 'phone))
(addresses (bbdb-vcard-bbdb-record-field record 'address))
(url (bbdb-vcard-bbdb-record-field record 'url))
(notes (bbdb-vcard-bbdb-record-field record 'notes))
(vcard-uid (bbdb-vcard-bbdb-record-field record 'vcard-uid))
(raw-anniversaries
(let ((anniversary (bbdb-vcard-bbdb-record-field record 'anniversary)))
(when anniversary
(bbdb-vcard-split-structured-text anniversary "\n" t))))
(birthday-regexp
"\\([0-9]\\{4\\}-[01][0-9]-[0-3][0-9][t:0-9]*[-+z:0-9]*\\)\\([[:blank:]]+birthday\\)?\\'")
(birthday
(when raw-anniversaries
(car (bbdb-vcard-split-structured-text
(cl-find-if (lambda (x) (string-match birthday-regexp x))
raw-anniversaries)
" " t))))
(other-anniversaries
(when raw-anniversaries
(cl-remove-if (lambda (x) (string-match birthday-regexp x))
raw-anniversaries :count 1)))
(timestamp (bbdb-vcard-bbdb-record-field record 'timestamp))
(mail-aliases (bbdb-vcard-bbdb-record-field record 'mail-alias))
(raw-notes (copy-alist (bbdb-record-xfields record))))
(bbdb-vcard-insert-vcard-element "BEGIN" "VCARD")
(bbdb-vcard-insert-vcard-element "VERSION" "3.0")
(when name
(bbdb-vcard-insert-vcard-element "FN" (bbdb-vcard-escape-strings name)))
(when (or last-name first-name)
(bbdb-vcard-insert-vcard-element
"N" (bbdb-vcard-escape-strings last-name)
";" (bbdb-vcard-escape-strings first-name)
";" ; Additional Names
";" (if (and affix (car affix))
(car affix)
"")
";" (if (and affix (cadr affix))
(cadr affix)
"")))
(when aka
(bbdb-vcard-insert-vcard-element
"NICKNAME" (bbdb-join (bbdb-vcard-escape-strings aka) ",")))
(dolist (org organization)
(bbdb-vcard-insert-vcard-element
"ORG" (bbdb-vcard-escape-strings org)))