initial version
[bbdb-csv-import] / bbdb3-csv-import.el
1 ;;; bbdb3-csv-import.el --- import csv to bbdb version 3+ -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2014 by Ian Kelling
4
5 ;; Author: Ian Kelling <ian@iankelling.org>
6 ;; Created: 1 Apr 2014
7 ;; Version: 1.0
8 ;; Keywords: csv, util, bbdb
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Some tools such as Thunderbird and Outlook allow for exporting contact data as
26 ;; CSV (Comma Separated Value) files. This package, `bbdb3-csv-import.el', allows
27 ;; for importing such files into Emacs's bbdb database, version 3+.
28
29 ;;; Installation:
30 ;;
31 ;; dependencies. Available in marmalade/melpa or via the internet
32 ;; pcsv.el, dash.el, bbdb
33 ;;
34 ;; (load-file FILENAME-OF-THIS-FILE)
35 ;; or
36 ;; (add-to-list 'load-path DIRECTORY-CONTAINING-THIS-FILE)
37 ;; (require 'bbdb3-csv-import)
38
39 ;;; Usage:
40 ;;
41 ;; Backup or rename ~/.bbdb while testing that the import works correctly
42 ;;
43 ;; Simply call `bbdb3-csv-import-buffer' or `bbdb3-csv-import-file'. Interactively
44 ;; they prompt for file/buffer. Use non-interactively for no prompts.
45 ;;
46 ;; Thunderbird csv data works out of the box. Otherwise you will need to create
47 ;; a mapping table to suit your data and assign it to
48 ;; bbdb3-csv-import-mapping-table. Please send any new mapping tables upstream so
49 ;; I can add it to this file for other's benefit. Ian Kelling is willing to help
50 ;; if any issues arise.
51 ;;
52
53 (require 'pcsv)
54 (require 'bbdb)
55 (require 'dash)
56
57
58 (defvar bbdb3-csv-import-mapping-table bbdb3-csv-import-thunderbird
59 "The table which maps bbdb3 fields to csv fields.
60 Use the default as an example to map non-thunderbird data.
61 Name used is firstname + lastname or name.
62 After the car, all names should map to whatever csv
63 field names are used in the first row of csv data.
64 Many fields are optional. If you aren't sure if one is,
65 best to just try it. The doc string for `bbdb-create-internal'
66 may be useful for determining which fields are required.")
67
68 (defconst bbdb3-csv-import-thunderbird
69 '(("firstname" "First Name")
70 ("lastname" "Last Name")
71 ("name" "Display Name")
72 ("aka" "Nickname")
73 ("mail" "Primary Email" "Secondary Email")
74 ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
75 ("address"
76 ("home address" (("Home Address"
77 "Home Address 2")
78 "Home City"
79 "Home State"
80 "Home ZipCode"
81 "Home Country"))
82 ("work address" (("Work Address"
83 "Work Address 2")
84 "Work City"
85 "Work State"
86 "Work ZipCode"
87 "Work Country")))
88 ("organization" ("Organization"))
89 ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
90 "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
91 "Custom 4" "Notes" "Job Title")))
92
93
94 ;;;###autoload
95 (defun bbdb3-csv-import-file (filename)
96 "Parse and import csv file FILENAME to bbdb3."
97 (interactive "fCSV file containg contact data: ")
98 (bbdb3-csv-import-buffer (find-file-noselect filename)))
99
100
101 ;;;###autoload
102 (defun bbdb3-csv-import-buffer (&optional buffer-or-name)
103 "Parse and import csv BUFFER-OR-NAME to bbdb3.
104 Argument is a buffer or name of a buffer.
105 Defaults to current buffer."
106 (interactive "bBuffer containing CSV contact data: ")
107 (let* ((csv-fields (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
108 (csv-contents (cdr csv-fields))
109 (csv-fields (car csv-fields))
110 (initial-duplicate-value bbdb-allow-duplicates)
111 csv-record)
112 ;; Easier to allow duplicates and handle them post import vs failing as
113 ;; soon as we find one.
114 (setq bbdb-allow-duplicates t)
115 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
116 (cl-flet*
117 ((rd-assoc (list) (rd (lambda (elem) (assoc-plus elem csv-record)) list))
118 (mapcar-assoc (list) (mapcar (lambda (elem) (cdr (assoc elem csv-record))) list))
119 (field-map (field) (cdr (assoc field bbdb3-csv-import-mapping-table)))
120 (map-assoc (field) (assoc-plus (car (field-map field)) csv-record))
121 ;; EPA compliance (Emacs pollution agency)
122 (rd bbdb3-csv-import-reduce)
123 (assoc-plus bbdb3-csv-import-assoc-plus))
124 (let ((name (let ((first (map-assoc "firstname"))
125 (last (map-assoc "lastname"))
126 (name (map-assoc "name")))
127 (if (and first last)
128 (cons first last)
129 (or name first last ""))))
130 (phone (rd (lambda (elem)
131 (let ((data (assoc-plus elem csv-record)))
132 (if data (vconcat (list elem data)))))
133 (field-map "phone")))
134 (xfields (rd (lambda (field)
135 (let ((value (assoc-plus field csv-record)))
136 (when value
137 (while (string-match " " field)
138 ;; turn csv field names into symbols for extra fields
139 (setq field (replace-match "" nil nil field)))
140 (cons (make-symbol (downcase field)) value))))
141 (field-map "xfields")))
142 (address (rd (lambda (x)
143 (let ((address-lines (mapcar-assoc (caadr x)))
144 (address-data (mapcar-assoc (cdadr x))))
145 ;; determine if non-nil and put together the minimum set
146 (when (or (not (-all? '(lambda (arg) (zerop (length arg))) address-data))
147 (not (-all? '(lambda (arg) (zerop (length arg))) address-lines)))
148 (when (> 2 (length address-lines))
149 (setcdr (max 2 (nthcdr (-find-last-index (lambda (x) (not (null x)))
150 address-lines)
151 address-lines)) nil))
152 (vconcat (list (car x)) (list address-lines) address-data))))
153 (cdr (assoc "address" bbdb3-csv-import-mapping-table))))
154 (mail (rd-assoc (field-map "mail")))
155 (organization (rd-assoc (field-map "organization")))
156 (affix (map-assoc "affix"))
157 (aka (rd-assoc (field-map "aka"))))
158 (bbdb-create-internal name affix aka organization mail
159 phone address xfields t)))))
160 (setq bbdb-allow-duplicates initial-duplicate-value))
161
162
163 ;;;###autoload
164 (defun bbdb3-csv-import-reduce (func list)
165 "like mapcar but don't build nil results into the resulting list"
166 (-reduce-from (lambda (acc elem)
167 (let ((funcreturn (funcall func elem)))
168 (if funcreturn
169 (cons funcreturn acc)
170 acc)))
171 nil list))
172
173 ;;;###autoload
174 (defun bbdb3-csv-import-assoc-plus (key list)
175 "Like `assoc' but turn an empty string result to nil."
176 (let ((result (cdr (assoc key list))))
177 (when (not (string= "" result))
178 result)))
179
180 (provide 'bbdb3-csv-import)
181
182 ;;; bbdb3-csv-import.el ends here