34f910705c9af12f44f48151cb19266b4db00e1d
[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 ;; Maintainer: Ian Kelling <ian@iankelling.org>
6 ;; Author: Ian Kelling <ian@iankelling.org>
7 ;; Created: 1 Apr 2014
8 ;; Version: 1.0
9 ;; Keywords: csv, util, bbdb
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Importer of csv (comma separated value) text into Emacs’s bbdb database,
27 ;; version 3+. Works out of the box with csv exported from Thunderbird, Gmail,
28 ;; Linkedin, Outlook.com/hotmail, and probably others.
29 ;; Easily extensible to handle new formats.
30
31 ;;; Installation:
32 ;;
33 ;; dependencies: pcsv.el, dash.el, bbdb
34 ;; These are available via marmalade/melpa or the internet
35 ;;
36 ;; Add to init file or execute manually as this may be a one time usage:
37 ;; (load-file FILENAME-OF-THIS-FILE)
38 ;; or
39 ;; (add-to-list 'load-path DIRECTORY-CONTAINING-THIS-FILE)
40 ;; (require 'bbdb3-csv-import)
41
42 ;;; Usage:
43 ;;
44 ;; You may want to back up existing data in ~/.bbdb and ~/.emacs.d/bbdb in case
45 ;; you don't like the newly imported data.
46 ;;
47 ;; Simply M-x `bbdb3-csv-import-buffer' or `bbdb3-csv-import-file'.
48 ;; Interactively they prompt for file or buffer.
49 ;;
50 ;; Tested to work with thunderbird, gmail, linkedin, outlook.com/hotmail.com For
51 ;; those programs, if it's exporter has an option of what kind of csv format,
52 ;; choose it's own native format if available, if not, choose an outlook
53 ;; compatible format. If you're exporting from some other program, and its csv
54 ;; exporter claims outlook compatibility, there is a good chance it will work
55 ;; out of the box.
56 ;;
57 ;; If things don't work, you can probably fix it with a field mapping variable.
58 ;; By default, we use a combination of all predefined mappings, and look for
59 ;; every known field. If you have data that is from something we've already
60 ;; tested, try using it's specific mapping table in case that works better.
61 ;; Here is a handy template to set each of the predefined mapping tables:
62 ;;
63 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-combined)
64 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-thunderbird)
65 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-gmail)
66 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-linkedin)
67 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-outlook-web)
68 ;;
69 ;; If you need to define your own mapping table, it should not be too hard. Use
70 ;; the existing tables as an example. Probably best to ignore the combined table
71 ;; as it is an unnecessary complexity when working on a new table. The doc
72 ;; string for `bbdb-create-internal' may also be useful. The test csv data &
73 ;; test version info within this project could also be helpful. Please send any
74 ;; new mapping tables to the maintainer listed in this file. The maintainer
75 ;; should be able to help with any issues and may create a new mapping table
76 ;; given sample data.
77 ;;
78 ;; Misc tips:
79 ;; - ASynK looks promising for syncing bbdb/google/outlook.
80 ;; - bbdb doesn't work if you delete the bbdb database file in
81 ;; the middle of an emacs session. If you want to empty the current bbdb database,
82 ;; do M-x bbdb then .* then C-u * d on the beginning of a record.
83 ;; - After changing a mapping table, don't forget to re-execute
84 ;; (setq bbdb3-csv-import-mapping-table ...) so that it propagates.
85
86
87 (require 'pcsv)
88 (require 'dash)
89 (require 'bbdb-com)
90 (eval-when-compile (require 'cl))
91
92 (defvar bbdb3-csv-import-mapping-table bbdb3-csv-import-combined
93 "The table which maps bbdb3 fields to csv fields. The default should work for most cases.
94 See the commentary section of this file for more details.
95 ")
96
97 (defconst bbdb3-csv-import-thunderbird
98 '(("firstname" "First Name")
99 ("lastname" "Last Name")
100 ("name" "Display Name")
101 ("aka" "Nickname")
102 ("mail" "Primary Email" "Secondary Email")
103 ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
104 ("address"
105 (("home address"
106 (("Home Address" "Home Address 2")
107 "Home City" "Home State"
108 "Home ZipCode" "Home Country"))
109 ("work address"
110 (("Work Address" "Work Address 2")
111 "Work City" "Work State"
112 "Work ZipCode" "Work Country"))))
113 ("organization" "Organization")
114 ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
115 "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
116 "Custom 4" "Notes" "Job Title"))
117 "Thunderbird csv format")
118
119 (defconst bbdb3-csv-import-linkedin
120 '(("firstname" "First Name")
121 ("lastname" "Last Name")
122 ("middlename" "Middle Name")
123 ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
124 ("phone"
125 "Assistant's Phone" "Business Fax" "Business Phone"
126 "Business Phone 2" "Callback" "Car Phone"
127 "Company Main Phone" "Home Fax" "Home Phone"
128 "Home Phone 2" "ISDN" "Mobile Phone"
129 "Other Fax" "Other Phone" "Pager"
130 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
131 ("address"
132 (("business address"
133 (("Business Street" "Business Street 2" "Business Street 3")
134 "Business City" "Business State"
135 "Business Postal Code" "Business Country"))
136 ("home address"
137 (("Home Street" "Home Street 2" "Home Street 3")
138 "Home City" "Home State"
139 "Home Postal Code" "Home Country"))
140 ("other address"
141 (("Other Street" "Other Street 2" "Other Street 3")
142 "Other City" "Other State"
143 "Other Postal Code" "Other Country"))))
144 ("organization" "Company")
145 ("xfields"
146 "Suffix" "Department" "Job Title" "Assistant's Name"
147 "Birthday" "Manager's Name" "Notes" "Other Address PO Box"
148 "Spouse" "Web Page" "Personal Web Page"))
149 "Linkedin export in the Outlook csv format.")
150
151
152 ;; note. PO Box and Extended Address are added as additional address street lines if they exist.
153 ;; If you don't like this, just delete them from this fiel.
154 ;; If you want some other special handling, it will need to be coded.
155 (defconst bbdb3-csv-import-gmail
156 '(("firstname" "Given Name")
157 ("lastname" "Family Name")
158 ("name" "Name")
159 ("mail" (repeat "E-mail 1 - Value"))
160 ("phone" (repeat ("Phone 1 - Type" "Phone 1 - Value")))
161 ("address"
162 (repeat (("Address 1 - Type")
163 (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
164 "Address 1 - City" "Address 1 - Region"
165 "Address 1 - Postal Code" "Address 1 - Country"))))
166 ("organization" (repeat "Organization 1 - Name"))
167 ("xfields"
168 "Additional Name" "Yomi Name" "Given Name Yomi"
169 "Additional Name Yomi" "Family Name Yomi" "Name Prefix"
170 "Name Suffix" "Initials" "Nickname"
171 "Short Name" "Maiden Name" "Birthday"
172 "Gender" "Location" "Billing Information"
173 "Directory Server" "Mileage" "Occupation"
174 "Hobby" "Sensitivity" "Priority"
175 "Subject" "Notes" "Group Membership"
176 ;; Gmail wouldn't let me add more than 1 organization, but no harm in
177 ;; looking for multiple since the field name implies the possibility.
178 (repeat
179 "Organization 1 - Type" "Organization 1 - Yomi Name"
180 "Organization 1 - Title" "Organization 1 - Department"
181 "Organization 1 - Symbol" "Organization 1 - Location"
182 "Organization 1 - Job Description")
183 (repeat ("Relation 1 - Type" "Relation 1 - Value"))
184 (repeat ("Website 1 - Type" "Website 1 - Value"))
185 (repeat ("Event 1 - Type" "Event 1 - Value"))
186 (repeat ("Custom Field 1 - Type" "Custom Field 1 - Value"))))
187 "Gmail csv export format")
188
189
190 (defconst bbdb3-csv-import-gmail-typed-email
191 (append (car (last bbdb3-csv-import-gmail)) '((repeat "E-mail 1 - Type")))
192 "Like the first Gmail mapping, but use custom fields to store
193 Gmail's email labels. This is separate because I assume most
194 people don't use those labels and using the default labels
195 would create useless custom fields.")
196
197 (defconst bbdb3-csv-import-outlook-typed-email
198 (append (car (last bbdb3-csv-import-outlook-web)) '((repeat "E-mail 1 - Type")))
199 "Like the previous var, but for outlook-web.
200 Adds email labels as custom fields.")
201
202
203 (defconst bbdb3-csv-import-outlook-web
204 '(("firstname" "First Name")
205 ("lastname" "Last Name")
206 ("middlename" "Middle Name")
207 ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
208 ("phone"
209 "Assistant's Phone" "Business Fax" "Business Phone"
210 "Business Phone 2" "Callback" "Car Phone"
211 "Company Main Phone" "Home Fax" "Home Phone"
212 "Home Phone 2" "ISDN" "Mobile Phone"
213 "Other Fax" "Other Phone" "Pager"
214 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
215 ("address"
216 (("business address"
217 (("Business Street")
218 "Business City" "Business State"
219 "Business Postal Code" "Business Country"))
220 ("home address"
221 (("Home Street")
222 "Home City" "Home State"
223 "Home Postal Code" "Home Country"))
224 ("other address"
225 (("Other Street")
226 "Other City" "Other State"
227 "Other Postal Code" "Other Country"))))
228 ("organization" "Company")
229 ("xfields"
230 "Anniversary" "Family Name Yomi" "Given Name Yomi"
231 "Suffix" "Department" "Job Title" "Birthday" "Manager's Name" "Notes"
232 "Spouse" "Web Page"))
233 "Hotmail.com, outlook.com, live.com, etc.
234 Based on 'Export for outlook.com and other services',
235 not the export for Outlook 2010 and 2013.")
236
237 (defconst bbdb3-csv-import-combined
238 (list
239 (bbdb3-csv-import-merge-map "firstname")
240 (bbdb3-csv-import-merge-map "middlename")
241 (bbdb3-csv-import-merge-map "lastname")
242 (bbdb3-csv-import-merge-map "name")
243 (bbdb3-csv-import-merge-map "aka")
244 (bbdb3-csv-import-merge-map "mail")
245 (bbdb3-csv-import-merge-map "phone")
246 ;; manually combined the addresses. Because it was easier.
247 '("address"
248 (repeat (("Address 1 - Type")
249 (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
250 "Address 1 - City" "Address 1 - Region"
251 "Address 1 - Postal Code" "Address 1 - Country")))
252 (("business address"
253 (("Business Street" "Business Street 2" "Business Street 3")
254 "Business City" "Business State"
255 "Business Postal Code" "Business Country"))
256 ("home address"
257 (("Home Street" "Home Street 2" "Home Street 3"
258 "Home Address" "Home Address 2")
259 "Home City" "Home State"
260 "Home Postal Code" "Home ZipCode" "Home Country"))
261 ("work address"
262 (("Work Address" "Work Address 2")
263 "Work City" "Work State"
264 "Work ZipCode" "Work Country"))
265 ("other address"
266 (("Other Street" "Other Street 2" "Other Street 3")
267 "Other City" "Other State"
268 "Other Postal Code" "Other Country"))))
269 (bbdb3-csv-import-merge-map "organization")
270 (bbdb3-csv-import-merge-map "xfields")))
271
272 (defun bbdb3-csv-import-merge-map (root)
273 (bbdb3-csv-import-flatten1
274 (list root
275 (-distinct
276 (append
277 (cdr (assoc root bbdb3-csv-import-thunderbird))
278 (cdr (assoc root bbdb3-csv-import-linkedin))
279 (cdr (assoc root bbdb3-csv-import-gmail))
280 (cdr (assoc root bbdb3-csv-import-outlook-web)))))))
281
282
283
284 ;;;###autoload
285 (defun bbdb3-csv-import-file (filename)
286 "Parse and import csv file FILENAME to bbdb3."
287 (interactive "fCSV file containg contact data: ")
288 (bbdb3-csv-import-buffer (find-file-noselect filename)))
289
290
291 ;;;###autoload
292 (defun bbdb3-csv-import-buffer (&optional buffer-or-name)
293 "Parse and import csv BUFFER-OR-NAME to bbdb3.
294 Argument is a buffer or name of a buffer.
295 Defaults to current buffer."
296 (interactive "bBuffer containing CSV contact data: ")
297 (when (null bbdb3-csv-import-mapping-table)
298 (error "error: `bbdb3-csv-import-mapping-table' is nil. Please set it and rerun."))
299 (let* ((csv-fields (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
300 (csv-contents (cdr csv-fields))
301 (csv-fields (car csv-fields))
302 (initial-duplicate-value bbdb-allow-duplicates)
303 csv-record rd assoc-plus flatten1)
304 ;; convenient function names
305 (fset 'rd 'bbdb3-csv-import-rd)
306 (fset 'assoc-plus 'bbdb3-csv-import-assoc-plus)
307 (fset 'flatten1 'bbdb3-csv-import-flatten1)
308 ;; Easier to allow duplicates and handle them post import vs failing as
309 ;; soon as we find one.
310 (setq bbdb-allow-duplicates t)
311 ;; loop over the csv records
312 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
313 (cl-flet*
314 ((replace-num (num string)
315 ;; in STRING, replace all groups of numbers with NUM
316 (replace-regexp-in-string "[0-9]+" (number-to-string num) string))
317 (expand-repeats (list)
318 ;; return new list where elements from LIST in form
319 ;; (repeat elem1 ...) become ((elem1 ...) [(elem2 ...)] ...)
320 ;; For as many repeating numbered fields exist in the csv fields.
321 ;; elem can be a string or a tree (a list with possibly lists inside it)
322 (--reduce-from (if (not (and (consp it) (eq (car it) 'repeat)))
323 (cons it acc)
324 (setq it (cdr it))
325 (let* ((i 1)
326 (first-field (car (flatten it))))
327 (setq acc (cons it acc))
328 ;; use first-field to test if there is another repetition.
329 (while (member (replace-num (setq i (1+ i)) first-field) csv-fields)
330 (cl-labels ((fun (cell)
331 (if (consp cell)
332 (mapcar #'fun cell)
333 (replace-num i cell))))
334 (setq acc (cons (fun it) acc))))
335 acc))
336 nil list))
337 (map-bbdb3 (root)
338 ;; ROOT = a root element from bbdb3-csv-import-mapping-table.
339 ;; Get the actual csv-fields, including variably repeated ones. flattened
340 ;; by one because repeated fields are put in sub-lists, but
341 ;; after expanding them, that extra depth is no longer
342 ;; useful. Small quirk: address mappings without 'repeat
343 ;; need to be grouped in a list because they contain sublists that we
344 ;; don't want flattened. Better this than more complex code.
345 (flatten1 (expand-repeats (cdr (assoc root bbdb3-csv-import-mapping-table)))))
346 (rd-assoc (root)
347 ;; given ROOT, return a list of data, ignoring empty fields
348 (rd (lambda (elem) (assoc-plus elem csv-record)) (map-bbdb3 root)))
349 (assoc-expand (e)
350 ;; E = data-field-name | (field-name-field data-field)
351 ;; get data from the csv-record and return
352 ;; (field-name data) or nil.
353 (let ((data-name (if (consp e) (cdr (assoc (car e) csv-record)) e))
354 (data (assoc-plus (if (consp e) (cadr e) e) csv-record)))
355 (if data (list data-name data))))
356 (map-assoc (field)
357 ;; For simple mappings, get a single result
358 (car (rd-assoc field))))
359
360 (let ((name (let ((first (map-assoc "firstname"))
361 (middle (map-assoc "middlename"))
362 (last (map-assoc "lastname"))
363 (name (map-assoc "name")))
364 ;; prioritize any combination of first middle last over just "name"
365 (if (or (and first last) (and first middle) (and middle last))
366 ;; purely historical note.
367 ;; using (cons first last) as argument works the same as (concat first " " last)
368 (concat (or first middle) " " (or middle last) (when (and first middle) (concat " " last) ))
369 (or name first middle last ""))))
370 (phone (rd 'vconcat (rd #'assoc-expand (map-bbdb3 "phone"))))
371 (mail (rd-assoc "mail"))
372 (xfields (rd (lambda (list)
373 (let ((e (car list)))
374 (while (string-match "-" e)
375 (setq e (replace-match "" nil nil e)))
376 (while (string-match " +" e)
377 (setq e (replace-match "-" nil nil e)))
378 (setq e (make-symbol (downcase e)))
379 (cons e (cadr list)))) ;; change from (a b) to (a . b)
380 (rd #'assoc-expand (map-bbdb3 "xfields"))))
381 (address (rd (lambda (e)
382
383 (let ((address-lines (rd (lambda (elem)
384 (assoc-plus elem csv-record))
385 (caadr e)))
386 ;; little bit of special handling so we can
387 ;; use the combined mapping
388 (address-data (--reduce-from (if (member it csv-fields)
389 (cons (cdr (assoc it csv-record)) acc)
390 acc)
391 nil (cdadr e)))
392 (elem-name (car e)))
393 (setq address-lines (nreverse address-lines))
394 (setq address-data (nreverse address-data))
395 ;; make it a list of at least 2 elements
396 (setq address-lines (append address-lines
397 (-repeat (- 2 (length address-lines)) "")))
398 (when (consp elem-name)
399 (setq elem-name (cdr (assoc (car elem-name) csv-record))))
400
401 ;; determine if non-nil and put together the minimum set
402 (when (or (not (--all? (zerop (length it)) address-data))
403 (not (--all? (zerop (length it)) address-lines)))
404 (when (> 2 (length address-lines))
405 (setcdr (max 2 (nthcdr (--find-last-index (not (null it))
406 address-lines)
407 address-lines)) nil))
408 (vconcat (list elem-name) (list address-lines) address-data))))
409 (map-bbdb3 "address")))
410 (organization (rd-assoc "organization"))
411 (affix (map-assoc "affix"))
412 (aka (rd-assoc "aka")))
413 (bbdb-create-internal name affix aka organization mail phone address xfields t))))
414 (setq bbdb-allow-duplicates initial-duplicate-value)))
415
416 ;;;###autoload
417 (defun bbdb3-csv-import-flatten1 (list)
418 "flatten LIST by 1 level."
419 (--reduce-from (if (consp it)
420 (-concat acc it)
421 (-snoc acc it))
422 nil list))
423
424 ;;;###autoload
425 (defun bbdb3-csv-import-rd (func list)
426 "like mapcar but don't build nil results into the resulting list"
427 (--reduce-from (let ((funcreturn (funcall func it)))
428 (if funcreturn
429 (cons funcreturn acc)
430 acc))
431 nil list))
432
433 ;;;###autoload
434 (defun bbdb3-csv-import-assoc-plus (key list)
435 "Like (cdr assoc ...) but turn an empty string result to nil."
436 (let ((result (cdr (assoc key list))))
437 (when (not (string= "" result))
438 result)))
439
440
441
442 (provide 'bbdb3-csv-import)
443
444 ;;; bbdb3-csv-import.el ends here