1 ;;; vcard.el --- vcard parsing and display routines
3 ;; Copyright (C) 1997, 1999, 2000 Noah S. Friedman
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: vcard, mail, news
10 ;; <http://www.splode.com/users/friedman/software/emacs-lisp/>
11 ;; Id: vcard.el,v 1.11 2000/06/29 17:07:55 friedman Exp
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; if not, you can either send email to this
25 ;; program's maintainer or write to: The Free Software Foundation,
26 ;; Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 ;; Unformatted vcards are just plain ugly. But if you live in the MIME
31 ;; world, they are a better way of exchanging contact information than
32 ;; freeform signatures since the former can be automatically parsed and
33 ;; stored in a searchable index.
35 ;; This library of routines provides the back end necessary for parsing
36 ;; vcards so that they can eventually go into an address book like BBDB
37 ;; (although this library does not implement that itself). Also included
38 ;; is a sample pretty-printer which MUAs can use which do not provide their
39 ;; own vcard formatters.
41 ;; This library does not interface directly with any mail user agents. For
42 ;; an example of bindings for the VM MUA, see vm-vcard.el available from
44 ;; http://www.splode.com/~friedman/software/emacs-lisp/index.html#mail
46 ;; Updates to vcard.el should be available there too.
48 ;; The main entry point to this package is `vcard-pretty-print' although
49 ;; any documented variable or function is considered part of the API for
50 ;; operating on vcard data.
52 ;; The vcard 2.1 format is defined by the versit consortium.
53 ;; See http://www.imc.org/pdi/vcard-21.ps
55 ;; RFC 2426 defines the vcard 3.0 format.
56 ;; See ftp://ftp.rfc-editor.org/in-notes/rfc2426.txt
58 ;; A parsed vcard is a list of attributes of the form
60 ;; (proplist value1 value2 ...)
62 ;; Where proplist is a list of property names and parameters, e.g.
64 ;; (property1 (property2 . parameter2) ...)
66 ;; Each property has an associated implicit or explicit parameter value
67 ;; (not to be confused with attribute values; in general this API uses
68 ;; `parameter' to refer to property values and `value' to refer to attribute
69 ;; values to avoid confusion). If a property has no explicit parameter value,
70 ;; the parameter value is considered to be `t'. Any property which does not
71 ;; exist for an attribute is considered to have a nil parameter.
74 ;; * Finish supporting the 3.0 extensions.
75 ;; Currently, only the 2.1 standard is supported.
76 ;; * Handle nested vcards and grouped attributes?
77 ;; (I've never actually seen one of these in use.)
78 ;; * Handle multibyte charsets.
79 ;; * Inverse of vcard-parse-string: write .VCF files from alist
80 ;; * Implement a vcard address book? Or is using BBDB preferable?
81 ;; * Improve the sample formatter.
86 "Support for the vCard electronic business card format."
92 (defcustom vcard-pretty-print-function 'vcard-format-sample-box
93 "*Formatting function used by `vcard-pretty-print'."
98 (defcustom vcard-standard-filters
100 vcard-filter-adr-newlines
101 vcard-filter-tel-normalize
102 vcard-filter-textprop-cr)
103 "*Standard list of filters to apply to parsed vcard data.
104 These filters are applied sequentially to vcard attributes when
105 the function `vcard-standard-filter' is supplied as the second argument to
111 ;;; No user-settable options below.
113 ;; XEmacs 21 ints and chars are disjoint types.
114 ;; For all else, treat them as the same.
115 (defalias 'vcard-char-to-int
116 (if (fboundp 'char-to-int) 'char-to-int 'identity))
118 ;; This is just the version number for this package; it does not refer to
119 ;; the vcard format specification. Currently, this package does not yet
120 ;; support the full vcard 3.0 specification.
122 ;; Whenever any part of the API defined in this package change in a way
123 ;; that is not backward-compatible, the major version number here should be
124 ;; incremented. Backward-compatible additions to the API should be
125 ;; indicated by increasing the minor version number.
126 (defconst vcard-api-version "2.0")
128 ;; The vcard standards allow specifying the encoding for an attribute using
129 ;; these values as immediate property names, rather than parameters of the
130 ;; `encoding' property. If these are encountered while parsing, associate
131 ;; them as parameters of the `encoding' property in the returned structure.
132 (defvar vcard-encoding-tags
133 '("quoted-printable" "base64" "8bit" "7bit"))
135 ;; The vcard parser will auto-decode these encodings when they are
136 ;; encountered. These methods are invoked via vcard-parse-region-value.
137 (defvar vcard-region-decoder-methods
138 '(("quoted-printable" . vcard-region-decode-quoted-printable)
139 ("base64" . vcard-region-decode-base64)))
141 ;; This is used by vcard-region-decode-base64
142 (defvar vcard-region-decode-base64-table
143 (let* ((a "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
145 (tbl (make-vector 123 nil))
148 (aset tbl (vcard-char-to-int (aref a i)) i)
153 ;;; This function can be used generically by applications to obtain
154 ;;; a printable representation of a vcard.
157 (defun vcard-pretty-print (vcard)
158 "Format VCARD into a string suitable for display to user.
159 VCARD can be an unparsed string containing raw VCF vcard data
160 or a parsed vcard alist as returned by `vcard-parse-string'.
162 The result is a string with formatted vcard information suitable for
163 insertion into a mime presentation buffer.
165 The function specified by the variable `vcard-pretty-print-function'
166 actually performs the formatting. That function will always receive a
169 (setq vcard (vcard-parse-string vcard)))
170 (funcall vcard-pretty-print-function vcard))
176 (defun vcard-parse-string (raw &optional filter)
177 "Parse RAW vcard data as a string, and return an alist representing data.
179 If the optional function FILTER is specified, apply that filter to each
180 attribute. If no filter is specified, `vcard-standard-filter' is used.
182 Filters should accept two arguments: the property list and the value list.
183 Modifying in place the property or value list will affect the resulting
184 attribute in the vcard alist.
186 Vcard data is normally in the form
190 prop2a;prop2b;prop2c=param2c: value2a
191 prop3a;prop3b: value3a;value3b;value3c
194 \(Whitespace around the `:' separating properties and values is optional.\)
195 If supplied to this function an alist of the form
197 \(\(\(\"prop1a\"\) \"value1a\"\)
198 \(\(\"prop2a\" \"prop2b\" \(\"prop2c\" . \"param2c\"\)\) \"value2a\"\)
199 \(\(\"prop3a\" \"prop3b\"\) \"value3a\" \"value3b\" \"value3c\"\)\)
203 (buf (generate-new-buffer " *vcard parser work*")))
207 ;; Make sure last line is newline-terminated.
208 ;; An extra trailing newline is harmless.
210 (setq vcard (vcard-parse-region (point-min) (point-max) filter)))
215 (defun vcard-parse-region (beg end &optional filter)
216 "Parse the raw vcard data in region, and return an alist representing data.
217 This function is just like `vcard-parse-string' except that it operates on
218 a region of the current buffer rather than taking a string as an argument.
220 Note: this function modifies the buffer!"
222 (setq filter 'vcard-standard-filter))
223 (let ((case-fold-search t)
226 (newpos (make-marker))
229 (narrow-to-region beg end)
231 ;; Unfold folded lines and delete naked carriage returns
232 (goto-char (point-min))
233 (while (re-search-forward "\r$\\|\n[ \t]" nil t)
234 (goto-char (match-beginning 0))
237 (goto-char (point-min))
238 (re-search-forward "^begin:[ \t]*vcard[ \t]*\n")
239 (set-marker pos (point))
240 (while (and (not (looking-at "^end[ \t]*:[ \t]*vcard[ \t]*$"))
241 (re-search-forward ":[ \t]*" nil t))
242 (set-marker newpos (match-end 0))
244 (vcard-parse-region-properties pos (match-beginning 0)))
245 (set-marker pos (marker-position newpos))
246 (re-search-forward "[ \t]*\n")
247 (set-marker newpos (match-end 0))
249 (vcard-parse-region-value properties pos (match-beginning 0)))
250 (set-marker pos (marker-position newpos))
252 (funcall filter properties value)
253 (setq vcard-data (cons (cons properties value) vcard-data)))))
254 (nreverse vcard-data)))
256 (defun vcard-parse-region-properties (beg end)
257 (downcase-region beg end)
258 (let* ((proplist (vcard-split-string (buffer-substring beg end) ";"))
263 (cond ((string-match "=" (car props))
264 (setq split (vcard-split-string (car props) "=" 2))
265 (setcar props (cons (car split) (car (cdr split)))))
266 ((member (car props) vcard-encoding-tags)
267 (setcar props (cons "encoding" (car props)))))
268 (setq props (cdr props))))
271 (defun vcard-parse-region-value (proplist beg end)
272 (let* ((encoding (vcard-get-property proplist "encoding"))
273 (decoder (cdr (assoc encoding vcard-region-decoder-methods)))
274 result pos match-beg match-end)
276 (narrow-to-region beg end)
278 ;; Each `;'-separated field needs to be decoded and saved
279 ;; separately; if the entire region were decoded at once, we
280 ;; would not be able to distinguish between the original `;'
281 ;; chars and those which were encoded in order to quote them
282 ;; against being treated as field separators.
284 (setq pos (set-marker (make-marker) (point)))
285 (setq match-beg (make-marker))
286 (setq match-end (make-marker))
288 (while (< pos (point-max))
289 (cond ((search-forward ";" nil t)
290 (set-marker match-beg (match-beginning 0))
291 (set-marker match-end (match-end 0)))
293 (set-marker match-beg (point-max))
294 (set-marker match-end (point-max))))
295 (funcall decoder pos match-beg)
296 (setq result (cons (buffer-substring pos match-beg) result))
297 (set-marker pos (marker-position match-end))))
298 (setq result (nreverse result))
299 (vcard-set-property proplist "encoding" nil))
301 (setq result (vcard-split-string (buffer-string) ";")))))
302 (goto-char (point-max))
306 ;;; Functions for retrieving property or value information from parsed
307 ;;; vcard attributes.
309 (defun vcard-values (vcard have-props &optional non-props limit)
310 "Return the values in VCARD.
311 This function is like `vcard-ref' and takes the same arguments, but return
312 only the values, not the associated property lists."
313 (mapcar 'cdr (vcard-ref vcard have-props non-props limit)))
315 (defun vcard-ref (vcard have-props &optional non-props limit)
316 "Return the attributes in VCARD with HAVE-PROPS properties.
317 Optional arg NON-PROPS is a list of properties which candidate attributes
319 Optional arg LIMIT means return no more than that many attributes.
321 The attributes in VCARD which have all properties specified by HAVE-PROPS
322 but not having any specified by NON-PROPS are returned. The first element
323 of each attribute is the actual property list; the remaining elements are
326 If a specific property has an associated parameter \(e.g. an encoding\),
327 use the syntax \(\"property\" . \"parameter\"\) to specify it. If property
328 parameter is not important or it has no specific parameter, just specify
329 the property name as a string."
333 (while (and attrs (or (null limit) (< count limit)))
334 (and (vcard-proplist-all-properties (car (car attrs)) have-props)
335 (not (vcard-proplist-any-properties (car (car attrs)) non-props))
336 (setq result (cons (car attrs) result)
338 (setq attrs (cdr attrs)))
341 (defun vcard-proplist-all-properties (proplist props)
342 "Returns nil unless PROPLIST contains all properties specified in PROPS."
344 (while (and result props)
345 (or (vcard-get-property proplist (car props))
347 (setq props (cdr props)))
350 (defun vcard-proplist-any-properties (proplist props)
351 "Returns `t' if PROPLIST contains any of the properties specified in PROPS."
353 (while (and (not result) props)
354 (and (vcard-get-property proplist (car props))
356 (setq props (cdr props)))
359 (defun vcard-get-property (proplist property)
360 "Return the value from PROPLIST of PROPERTY.
361 PROPLIST is a vcard attribute property list, which is normally the first
362 element of each attribute entry in a vcard."
363 (or (and (member property proplist) t)
364 (cdr (assoc property proplist))))
366 (defun vcard-set-property (proplist property value)
367 "In PROPLIST, set PROPERTY to VALUE.
368 PROPLIST is a vcard attribute property list.
369 If VALUE is nil, PROPERTY is deleted."
372 (vcard-delete-property proplist property))
373 ((setq elt (member property proplist))
374 (and value (not (eq value t))
375 (setcar elt (cons property value))))
376 ((setq elt (assoc property proplist))
378 (setq elt (memq elt proplist))
379 (setcar elt property))
381 (setcdr elt value))))
383 (nconc proplist (cons property nil)))
385 (nconc proplist (cons (cons property value) nil))))))
387 (defun vcard-delete-property (proplist property)
388 "Delete from PROPLIST the specified property PROPERTY.
389 This will not succeed in deleting the first member of the proplist, but
390 that element should never be deleted since it is the primary key."
392 (cond ((setq elt (member property proplist))
393 (delq (car elt) proplist))
394 ((setq elt (assoc property proplist))
395 (delq (car (memq elt proplist)) proplist)))))
398 ;;; Vcard data filters.
400 ;;; Filters receive both the property list and value list and may modify
401 ;;; either in-place. The return value from the filters are ignored.
403 ;;; These filters can be used for purposes such as removing HTML tags or
404 ;;; normalizing phone numbers into a standard form.
406 (defun vcard-standard-filter (proplist values)
407 "Apply filters in `vcard-standard-filters' to attributes."
408 (vcard-filter-apply-filter-list vcard-standard-filters proplist values))
410 ;; This function could be used to dispatch other filter lists.
411 (defun vcard-filter-apply-filter-list (filter-list proplist values)
413 (funcall (car filter-list) proplist values)
414 (setq filter-list (cdr filter-list))))
416 ;; Some lusers put HTML (or even javascript!) in their vcards under the
417 ;; misguided notion that it's a standard feature of vcards just because
418 ;; Netscape supports this feature. That is wrong; the vcard specification
419 ;; does not define any html content semantics and most MUAs cannot do
420 ;; anything with html text except display them unparsed, which is ugly.
422 ;; Thank Netscape for abusing the standard and damned near rendering it
423 ;; useless for interoperability between MUAs.
425 ;; This filter does a very rudimentary job.
426 (defun vcard-filter-html (proplist values)
427 "Remove HTML tags from attribute values."
430 (while (string-match "<[^<>\n]+>" (car values))
431 (setcar values (replace-match "" t t (car values))))
432 (setq values (cdr values)))))
434 (defun vcard-filter-adr-newlines (proplist values)
435 "Replace newlines with \"; \" in `adr' values."
436 (and (vcard-get-property proplist "adr")
439 (while (string-match "[\r\n]+" (car values))
440 (setcar values (replace-match "; " t t (car values))))
441 (setq values (cdr values))))))
443 (defun vcard-filter-tel-normalize (proplist values)
444 "Normalize telephone numbers in `tel' values.
445 Spaces and hyphens are replaced with `.'.
446 US domestic telephone numbers are replaced with international format."
447 (and (vcard-get-property proplist "tel")
450 (while (string-match "[\t._-]+" (car values))
451 (setcar values (replace-match " " t t (car values))))
452 (and (string-match "^(?\\(\\S-\\S-\\S-\\))? ?\
453 \\(\\S-\\S-\\S- \\S-\\S-\\S-\\S-\\)"
456 (replace-match "+1 \\1 \\2" t nil (car values))))
457 (setq values (cdr values))))))
459 (defun vcard-filter-textprop-cr (proplist values)
460 "Strip carriage returns from text values."
461 (and (vcard-proplist-any-properties
462 proplist '("adr" "email" "fn" "label" "n" "org" "tel" "title" "url"))
465 (while (string-match "\r+" (car values))
466 (setcar values (replace-match "" t t (car values))))
467 (setq values (cdr values))))))
470 ;;; Decoding methods.
472 (defmacro vcard-hexstring-to-ascii (s)
473 (if (string-lessp emacs-version "20")
474 `(format "%c" (car (read-from-string (format "?\\x%s" ,s))))
475 `(format "%c" (string-to-number ,s 16))))
477 (defun vcard-region-decode-quoted-printable (&optional beg end)
481 (narrow-to-region (or beg (point-min)) (or end (point-max)))
482 (goto-char (point-min))
483 (while (re-search-forward "=\n" nil t)
484 (delete-region (match-beginning 0) (match-end 0)))
485 (goto-char (point-min))
486 (while (re-search-forward "=[0-9A-Za-z][0-9A-Za-z]" nil t)
487 (let ((s (buffer-substring (1+ (match-beginning 0)) (match-end 0))))
488 (replace-match (vcard-hexstring-to-ascii s) t t)))))))
490 (defun vcard-region-decode-base64 (&optional beg end)
492 (narrow-to-region (or beg (point-min)) (or end (point-max)))
494 (goto-char (point-min))
495 (while (re-search-forward "[ \t\r\n]+" nil t)
496 (delete-region (match-beginning 0) (match-end 0))))
497 (goto-char (point-min))
502 (setq c (char-after (point)))
504 (cond ((char-equal c ?=)
508 (insert (lsh n -16) (logand 255 (lsh n -8))))
509 (delete-region (point) (point-max)))
511 (setq n (+ n (aref vcard-region-decode-base64-table
512 (vcard-char-to-int c))))
513 (setq count (1+ count))
515 (insert (logand 255 (lsh n -16))
516 (logand 255 (lsh n -8))
520 (setq n (lsh n 6))))))))))
523 (defun vcard-split-string (string &optional separator limit)
524 "Split STRING at occurences of SEPARATOR. Return a list of substrings.
525 Optional argument SEPARATOR can be any regexp, but anything matching the
526 separator will never appear in any of the returned substrings.
527 If not specified, SEPARATOR defaults to \"[ \\f\\t\\n\\r\\v]+\".
528 If optional arg LIMIT is specified, split into no more than that many
529 fields \(though it may split into fewer\)."
530 (or separator (setq separator "[ \f\t\n\r\v]+"))
531 (let ((string-list nil)
532 (len (length string))
538 (setq splits (1+ splits))
541 (setq str (substring string pos))
543 ((string-match separator string pos)
544 (setq str (substring string pos (match-beginning 0)))
545 (setq pos (match-end 0)))
547 (setq str (substring string pos))
548 (setq pos (1+ len))))
549 (setq string-list (cons str string-list))))
550 (nreverse string-list)))
552 (defun vcard-copy-tree (tree)
553 "Make a deep copy of nested conses."
556 (cons (vcard-copy-tree (car tree))
557 (vcard-copy-tree (cdr tree))))
560 (defun vcard-flatten (l)
562 (apply 'nconc (mapcar 'vcard-flatten l))
566 ;;; Sample formatting routines.
568 (defun vcard-format-sample-box (vcard)
569 "Like `vcard-format-sample-string', but put an ascii box around text."
570 (let* ((lines (vcard-format-sample-lines vcard))
571 (len (vcard-format-sample-max-length lines))
572 (edge (concat "\n+" (make-string (+ len 2) ?-) "+\n"))
573 (line-fmt (format "| %%-%ds |" len))
575 (mapconcat (function (lambda (s) (format line-fmt s))) lines "\n")))
576 (if (string= formatted-lines "")
578 (concat edge formatted-lines edge))))
580 (defun vcard-format-sample-string (vcard)
581 "Format VCARD into a string suitable for display to user.
582 VCARD should be a parsed vcard alist. The result is a string
583 with formatted vcard information which can be inserted into a mime
584 presentation buffer."
585 (mapconcat 'identity (vcard-format-sample-lines vcard) "\n"))
587 (defun vcard-format-sample-lines (vcard)
588 (let* ((name (vcard-format-sample-get-name vcard))
589 (title (vcard-format-sample-values-concat vcard '("title") 1 "; "))
590 (org (vcard-format-sample-values-concat vcard '("org") 1 "; "))
591 (addr (vcard-format-sample-get-address vcard))
592 (tel (vcard-format-sample-get-telephone vcard))
593 (lines (delete nil (vcard-flatten (list name title org addr))))
594 (col-template (format "%%-%ds%%s"
595 (vcard-format-sample-offset lines tel)))
598 (setcar l (format col-template (car l) (car tel)))
599 ;; If we stripped away too many nil slots from l, add empty strings
600 ;; back in so setcar above will work on next iteration.
603 (setcdr l (cons "" nil)))
605 (setq tel (cdr tel)))
608 (defun vcard-format-sample-get-name (vcard)
609 (let ((name (car (car (vcard-values vcard '("fn") nil 1))))
610 (email (car (vcard-format-sample-values
611 vcard '((("email" "pref"))
612 (("email" "internet"))
614 (cond ((and name email)
615 (format "%s <%s>" name email))
620 (defun vcard-format-sample-get-telephone (vcard)
621 (let ((fields '(("Work: "
622 (("tel" "work" "pref") . ("fax" "pager" "cell"))
623 (("tel" "work" "voice") . ("fax" "pager" "cell"))
624 (("tel" "work") . ("fax" "pager" "cell")))
626 (("tel" "home" "pref") . ("fax" "pager" "cell"))
627 (("tel" "home" "voice") . ("fax" "pager" "cell"))
628 (("tel" "home") . ("fax" "pager" "cell"))
629 (("tel") . ("fax" "pager" "cell" "work")))
631 (("tel" "cell" "pref"))
634 (("tel" "pref" "fax"))
635 (("tel" "work" "fax"))
636 (("tel" "home" "fax"))
641 (setq result (vcard-format-sample-values vcard (cdr (car fields))))
644 (cons (concat (car (car fields)) (car (car result))) phones))
645 (setq result (cdr result)))
646 (setq fields (cdr fields)))
649 (defun vcard-format-sample-get-address (vcard)
650 (let* ((addr (vcard-format-sample-values vcard '((("adr" "pref" "work"))
654 (street (delete "" (list (nth 0 addr) (nth 1 addr) (nth 2 addr))))
655 (city-list (delete "" (nthcdr 3 addr)))
656 (city (cond ((null (car city-list)) nil)
660 (mapconcat 'identity (cdr city-list) " ")))
661 (t (car city-list)))))
663 (append street (list city))
666 (defun vcard-format-sample-values-concat (vcard have-props limit sep)
667 (let ((l (car (vcard-values vcard have-props nil limit))))
668 (and l (mapconcat 'identity (delete "" (vcard-copy-tree l)) sep))))
670 (defun vcard-format-sample-values (vcard proplists &optional limit)
671 (let ((result (vcard-format-sample-ref vcard proplists limit)))
674 (mapcar 'cdr result))))
676 (defun vcard-format-sample-ref (vcard proplists &optional limit)
678 (while (and (null result) proplists)
679 (setq result (vcard-ref vcard
680 (car (car proplists))
681 (cdr (car proplists))
683 (setq proplists (cdr proplists)))
685 (vcard-copy-tree (car result))
686 (vcard-copy-tree result))))
688 (defun vcard-format-sample-offset (row1 row2 &optional maxwidth)
689 (or maxwidth (setq maxwidth (frame-width)))
690 (let ((max1 (vcard-format-sample-max-length row1))
691 (max2 (vcard-format-sample-max-length row2)))
694 (+ max1 (min 5 (max 1 (- maxwidth (+ max1 max2))))))))
696 (defun vcard-format-sample-max-length (strings)
699 (setq maxlen (max maxlen (length (car strings))))
700 (setq strings (cdr strings)))
705 ;;; vcard.el ends here