-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathebdb-vcard.el
401 lines (354 loc) · 12.4 KB
/
ebdb-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
;;; ebdb-vcard.el --- vCard export and import routine for EBDB -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2024 Free Software Foundation, Inc.
;; Author: Eric Abrahamsen <[email protected]>
;; 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 3 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains formatting and parsing functions used to export
;; EBDB contacts to vcard format, and to create contacts from vcard
;; files. It only supports VCard versions 3.0 and 4.0.
;; https://tools.ietf.org/html/rfc6350
;;; Code:
(require 'ebdb-format)
(autoload 'calendar-gregorian-from-absolute "calendar")
(defclass ebdb-formatter-vcard (ebdb-formatter-freeform)
((coding-system :initform 'utf-8-dos)
(version-string
:type string
:allocation :class
:documentation "The string to insert for this formatter's
version."))
:abstract t
:documentation "Base formatter for vCard export.")
(defclass ebdb-formatter-vcard-30 (ebdb-formatter-vcard)
((version-string
:initform "3.0"))
:documentation "Formatter for vCard format 3.0.")
(defclass ebdb-formatter-vcard-40 (ebdb-formatter-vcard)
((version-string
:initform "4.0"))
:documentation "Formatter for vCard format 4.0.")
(defgroup ebdb-vcard nil
"Customization options for EBDB vCard support."
:group 'ebdb)
(defcustom ebdb-vcard-default-40-formatter
(make-instance 'ebdb-formatter-vcard-40
:label "VCard 4.0 (default)"
:combine nil
:collapse nil
:include '(ebdb-field-uuid
ebdb-field-timestamp
ebdb-field-mail
ebdb-field-name
ebdb-field-address
ebdb-field-url
ebdb-field-role
ebdb-field-anniversary
ebdb-field-relation
ebdb-field-phone
ebdb-field-notes
ebdb-field-tags)
:header nil)
"The default formatter for VCard 4.0 exportation."
:type 'ebdb-formatter-vcard)
(defcustom ebdb-vcard-default-30-formatter
(make-instance 'ebdb-formatter-vcard-30
:label "VCard 3.0 (default)"
:combine nil
:collapse nil
:include '(ebdb-field-uuid
ebdb-field-timestamp
ebdb-field-mail
ebdb-field-name
ebdb-field-address
ebdb-field-url
ebdb-field-role
ebdb-field-anniversary
ebdb-field-relation
ebdb-field-phone
ebdb-field-notes
ebdb-field-tags)
:header nil)
"The default formatter for VCard 3.0 exportation."
:type 'ebdb-formatter-vcard)
(defcustom ebdb-vcard-label-alist
'(("REV" . ebdb-field-timestamp)
("NOTE" . ebdb-field-notes)
("X-CREATION-DATE" . ebdb-field-creation-date)
("UID" . ebdb-field-uuid)
("EMAIL" . ebdb-field-mail)
("TEL" . ebdb-field-phone)
("RELATED" . ebdb-field-relation)
("CATEGORIES" . ebdb-field-tags)
("BDAY" . ebdb-field-anniversary)
("ANNIVERSARY" . ebdb-field-anniversary)
("URL" . ebdb-field-url)
("ADR" . ebdb-field-address)
("NICKNAME" . ebdb-field-name-complex))
"Correspondences between VCard properties and EBDB field classes.
This alist is neither exhaustive nor authoritative. It's purpose
is to simplify property labeling during the export process, and
to classify properties during import. The import process does
not always respect these headings."
:type '(repeat
(cons string symbol)))
(defsubst ebdb-vcard-escape (str)
"Escape commas, semi-colons and newlines in STR."
(replace-regexp-in-string
"\\([^\\]\\)\\([\n]+\\)" "\\1\\\\n"
(replace-regexp-in-string "\\([^\\]\\)\\([,;]\\)" "\\1\\\\\\2" str)))
(defsubst ebdb-vcard-unescape (str)
"Unescape escaped commas, semicolons and newlines in STR."
(replace-regexp-in-string
"\\\\n" "\n"
(replace-regexp-in-string
"\\\\\\([,;]\\)" "\\1" str)))
;; The RFC says fold any lines longer than 75 octets, excluding the
;; line break. Folded lines are delineated by a CRLF plus a space or
;; tab. Multibyte characters must not be broken.
;; TODO: This implementation assumes that Emacs' internal coding
;; system is similar enough to the utf-8 that the file will eventually
;; be written in that `string-bytes' (which returns a length according
;; to Emacs' own coding) will map accurately to what eventually goes
;; in the file. Eli notes this is not really true, and could result
;; in unexpected behavior, and he recommends using
;; `filepos-to-bufferpos' instead. Presumably that would involve
;; /first/ writing the vcf file, then backtracking and checking for
;; correctness.
(defun ebdb-vcard-fold-lines (text)
"Fold lines in TEXT, which represents a vCard contact."
(let ((lines (split-string text "\n"))
outlines)
(dolist (l lines)
(while (> (string-bytes l) 75) ; Line is too long.
(if (> (string-bytes l) (length l))
;; Multibyte characters.
(let ((acc (string-to-vector l)))
(setq l nil)
(while (> (string-bytes (concat acc)) 75)
;; Pop characters off the end of acc and stick them
;; back in l, until acc is short enough to go in
;; outlines. Probably hideously inefficient.
(push (aref acc (1- (length acc))) l)
(setq acc (substring acc 0 -1)))
(push acc outlines)
(setq l (concat " " l)))
;; No multibyte characters.
(push (substring l 0 75) outlines)
(setq l (concat " " (substring l 75)))))
(push l outlines))
(mapconcat #'identity (nreverse outlines) "\n")))
(defun ebdb-vcard-unfold-lines (text)
"Unfold lines in TEXT, which represents a vCard contact."
(replace-regexp-in-string "\n[\s\t]" "" text))
(cl-defmethod ebdb-fmt-process-fields ((_f ebdb-formatter-vcard)
(_record ebdb-record)
field-list)
field-list)
(cl-defmethod ebdb-fmt-process-fields ((fmt ebdb-formatter-vcard)
(record ebdb-record-person)
field-list)
"Process fields in FIELD-LIST.
All this does is split role instances into multiple fields."
(let (org out-list)
(dolist (f field-list)
(if (object-of-class-p f 'ebdb-field-role)
;; Split it apart.
(with-slots (org-uuid mail fields defunct) f
(unless defunct
(setq org (ebdb-gethash org-uuid 'uuid))
;; Store the name of the organization in the TYPE
;; parameter of the various properties. I'd rather
;; stick a UUID somewhere, but haven't immediately
;; figured out how that would be done.
(push (cons "ORG"
(ebdb-record-name-string org))
out-list)
(push (cons (format "TITLE;TYPE=\"%s\""
(ebdb-record-name-string org))
(slot-value f 'label))
out-list)
(when (or mail fields)
(dolist (elt (cons mail fields))
(push (cons
(format
"%s;%s"
(ebdb-fmt-field-label fmt elt 'normal record)
(format "TYPE=\"%s\"" (ebdb-record-name-string org)))
(ebdb-fmt-field fmt elt 'normal record))
out-list)))))
(push f out-list)))
out-list))
(cl-defmethod ebdb-fmt-record ((f ebdb-formatter-vcard)
(r ebdb-record))
"Format a single record R in VCARD format."
;; Because of the simplicity of the VCARD format, we only collect
;; the fields, there's no need to sort them, and the only processing
;; that happens is for role fields.
(let ((fields (ebdb-fmt-process-fields
f r
(ebdb-fmt-collect-fields f r)))
header-fields body-fields)
(setq header-fields
(list (slot-value r 'name))
body-fields
(mapcar
(lambda (fld)
;; This is a silly hack, but...
(if (consp fld)
fld
(cons (ebdb-fmt-field-label f fld 'normal r)
(ebdb-fmt-field f fld 'normal r))))
fields))
(concat
(format "BEGIN:VCARD\nVERSION:%s\n"
(slot-value f 'version-string))
(ebdb-fmt-record-header f r header-fields)
(ebdb-fmt-record-body f r body-fields)
"\nEND:VCARD\n")))
(cl-defmethod ebdb-fmt-record-header ((f ebdb-formatter-vcard)
(r ebdb-record)
(fields list))
"Format the header of a VCARD record.
VCARDs don't really have the concept of a \"header\", so this
method is just responsible for formatting the record name."
(let ((name (car fields)))
(concat
(format "FN:%s\n" (ebdb-string name))
(format "N;SORT-AS=\"%s\":%s\n"
(ebdb-record-sortkey r)
(ebdb-fmt-field f name 'normal r)))))
(cl-defmethod ebdb-fmt-record-body ((_f ebdb-formatter-vcard)
(_r ebdb-record)
(fields list))
(mapconcat
(lambda (f)
(format "%s:%s"
(car f) (cdr f)))
fields
"\n"))
(cl-defmethod ebdb-fmt-record-body :around ((_f ebdb-formatter-vcard-40)
(_r ebdb-record-person)
(_fields list))
(let ((str (cl-call-next-method)))
(concat str "\nKIND:individual")))
(cl-defmethod ebdb-fmt-record-body :around ((_f ebdb-formatter-vcard-40)
(_r ebdb-record-organization)
(_fields list))
(let ((str (cl-call-next-method)))
(concat str "\nKIND:org")))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(field ebdb-field)
_style
_record)
(ebdb-vcard-escape (ebdb-string field)))
(cl-defmethod ebdb-fmt-field-label ((_f ebdb-formatter-vcard)
(field ebdb-field)
_style
_record)
(car (rassoc (eieio-class-name
(eieio-object-class field))
ebdb-vcard-label-alist)))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(mail ebdb-field-mail)
_style
_record)
(slot-value mail 'mail))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(ts ebdb-field-timestamp)
_style
_record)
(format-time-string "%Y%m%dT%H%M%S%z" (slot-value ts 'timestamp) t))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(name ebdb-field-name-complex)
_style
_record)
(with-slots (surname given-names prefix suffix) name
(format
"%s;%s;%s;%s"
(or surname "")
(if given-names (mapconcat #'identity given-names ","))
(or prefix "")
(or suffix ""))))
(cl-defmethod ebdb-fmt-field-label ((_f ebdb-formatter-vcard)
(_field ebdb-field-uuid)
_style
_record)
(concat (cl-call-next-method) ":urn:uuid"))
(cl-defmethod ebdb-fmt-field-label ((_f ebdb-formatter-vcard)
(mail ebdb-field-mail)
_style
_record)
(with-slots (priority) mail
(concat
(cl-call-next-method)
(pcase priority
('primary ";PREF=1")
('normal ";PREF=10")
('defunct ";PREF=100")
(_ "")))))
(cl-defmethod ebdb-fmt-field-label ((_f ebdb-formatter-vcard)
(field ebdb-field-labeled)
_style
_record)
(let ((ret (cl-call-next-method))
(lab (slot-value field 'label)))
(if lab
(concat ret
";TYPE=" (ebdb-vcard-escape lab))
ret)))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(addr ebdb-field-address)
_style
_record)
(with-slots (streets locality region postcode country) addr
(concat ";;"
(mapconcat
#'ebdb-vcard-escape
streets ",")
(format
";%s;%s;%s;%s"
(or locality "")
(or region "")
(or postcode "")
(or country "")))))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(rel ebdb-field-relation)
_style
_record)
(concat "urn:uuid:" (slot-value rel 'rel-uuid)))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(tags ebdb-field-tags)
_style
_record)
(ebdb-concat "," (slot-value tags 'tags)))
(cl-defmethod ebdb-fmt-field-label ((_f ebdb-formatter-vcard)
(ann ebdb-field-anniversary)
_style
_record)
(let* ((label (slot-value ann 'label))
(label-string
(if (string= label "birthday")
"BDAY"
(concat "ANNIVERSARY;TYPE=" label))))
(concat label-string (format ";CALSCALE=%s"
(slot-value ann 'calendar)))))
(cl-defmethod ebdb-fmt-field ((_f ebdb-formatter-vcard)
(ann ebdb-field-anniversary)
_style
_record)
(pcase-let ((`(,month ,day ,year)
(slot-value ann 'date)))
(if (integerp year)
(format "%d%02d%02d" year month day)
(format "%02d%02d" month day))))
(provide 'ebdb-vcard)
;;; ebdb-vcard.el ends here