eb1de9d7ed4c5f4c106fb307f92232d877213f0b
[bbdb-csv-import] / bbdb-csv-import.el
1 ;;; bbdb-csv-import.el --- import csv to bbdb version 3+
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.1
9 ;; Package-Requires: ((pcsv "1.3.3") (dash "2.5.0") (bbdb "20140412.1949"))
10 ;; Keywords: csv, util, bbdb
11 ;; Homepage: https://gitlab.com/iankelling/bbdb-csv-import
12
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 of the License, or
16 ;; (at your option) any later version.
17
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.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Importer of csv (comma separated value) text into Emacs’s bbdb database,
29 ;; version 3+. Works out of the box with csv exported from Thunderbird, Gmail,
30 ;; Linkedin, Outlook.com/hotmail, and probably others.
31 ;; Easily extensible to handle new formats.
32
33 ;;; Installation:
34 ;;
35 ;; If you installed this file with a package manager, just
36 ;;
37 ;; (require 'bbdb-csv-import)
38 ;;
39 ;; Else, note the min versions of dependencies above in "Package-Requires:",
40 ;; and load this file. The exact minimum bbdb version is unknown, something 3+.
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 `bbdb-csv-import-buffer' or `bbdb-csv-import-file'.
48 ;; When called interactively, they prompt for file or buffer arguments.
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 custom field mapping
58 ;; variable. It should not be too hard. Use the existing tables as an
59 ;; example. By default, we use a combination of most predefined mappings, and
60 ;; look for all of their fields, but it is probably best to avoid that kind of
61 ;; table when setting up your own as it is an unnecessary complexity in that
62 ;; case. If you have a problem with data from a supported export program, start
63 ;; by testing its specific mapping table instead of the combined one. Here is a
64 ;; handy template to set each of the predefined mapping tables if you would
65 ;; rather avoid the configure interface:
66 ;;
67 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-combined)
68 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-thunderbird)
69 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-gmail)
70 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-gmail-typed-email)
71 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-linkedin)
72 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-outlook-web)
73 ;; (setq bbdb-csv-import-mapping-table bbdb-csv-import-outlook-typed-email)
74 ;;
75 ;; In addition to the examples, the doc string for `bbdb-create-internal' may
76 ;; also be useful. Please send any new mapping tables to the maintainer listed
77 ;; in this file. The maintainer should be able to help with any issues and may
78 ;; create a new mapping table given sample data.
79 ;;
80 ;; Misc tips/troubleshooting:
81 ;; - ASynK looks promising for syncing bbdb/google/outlook.
82 ;; - The git repo contains a test folder with exactly tested version info and working
83 ;; test data.
84 ;; - bbdb doesn't work if you delete the bbdb database file in
85 ;; the middle of an emacs session. If you want to empty the current bbdb database,
86 ;; do M-x bbdb then .* then C-u * d on the beginning of a record.
87 ;; - After changing a mapping table variable, don't forget to re-execute
88 ;; (setq bbdb-csv-import-mapping-table ...) so that it propagates.
89
90
91 ;;; Code:
92 (require 'pcsv)
93 (require 'dash)
94 (require 'bbdb-com)
95 (eval-when-compile (require 'cl))
96
97
98 (defconst bbdb-csv-import-thunderbird
99 '((:namelist "First Name" "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 bbdb-csv-import-linkedin
120 '((:namelist "First Name" "Middle Name" "Last Name")
121 (:affix "Suffix")
122 (:mail "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
123 (:phone
124 "Assistant's Phone" "Business Fax" "Business Phone"
125 "Business Phone 2" "Callback" "Car Phone"
126 "Company Main Phone" "Home Fax" "Home Phone"
127 "Home Phone 2" "ISDN" "Mobile Phone"
128 "Other Fax" "Other Phone" "Pager"
129 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
130 (:address
131 (("business address"
132 (("Business Street" "Business Street 2" "Business Street 3")
133 "Business City" "Business State"
134 "Business Postal Code" "Business Country"))
135 ("home address"
136 (("Home Street" "Home Street 2" "Home Street 3")
137 "Home City" "Home State"
138 "Home Postal Code" "Home Country"))
139 ("other address"
140 (("Other Street" "Other Street 2" "Other Street 3")
141 "Other City" "Other State"
142 "Other Postal Code" "Other Country"))))
143 (:organization "Company")
144 (:xfields
145 "Department" "Job Title" "Assistant's Name"
146 "Birthday" "Manager's Name" "Notes" "Other Address PO Box"
147 "Spouse" "Web Page" "Personal Web Page"))
148 "Linkedin export in the Outlook csv format.")
149
150
151 (defconst bbdb-csv-import-gmail
152 '((:namelist "Given Name" "Family Name")
153 (:name "Name")
154 (:affix "Name Prefix" "Name Suffix")
155 (:aka "Nickname")
156 (:mail (repeat "E-mail 1 - Value"))
157 (:phone (repeat ("Phone 1 - Type" "Phone 1 - Value")))
158 (:address
159 (repeat (("Address 1 - Type")
160 (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
161 "Address 1 - City" "Address 1 - Region"
162 "Address 1 - Postal Code" "Address 1 - Country"))))
163 (:organization (repeat "Organization 1 - Name"))
164 (:xfields
165 "Additional Name" "Yomi Name" "Given Name Yomi"
166 "Additional Name Yomi" "Family Name Yomi"
167 "Initials" "Short Name" "Maiden Name" "Birthday"
168 "Gender" "Location" "Billing Information"
169 "Directory Server" "Mileage" "Occupation"
170 "Hobby" "Sensitivity" "Priority"
171 "Subject" "Notes" "Group Membership"
172 ;; Gmail wouldn't let me add more than 1 organization in its web interface,
173 ;; but no harm in looking for multiple since the field name implies the
174 ;; possibility.
175 (repeat
176 "Organization 1 - Type" "Organization 1 - Yomi Name"
177 "Organization 1 - Title" "Organization 1 - Department"
178 "Organization 1 - Symbol" "Organization 1 - Location"
179 "Organization 1 - Job Description")
180 (repeat ("Relation 1 - Type" "Relation 1 - Value"))
181 (repeat ("Website 1 - Type" "Website 1 - Value"))
182 (repeat ("Event 1 - Type" "Event 1 - Value"))
183 (repeat ("Custom Field 1 - Type" "Custom Field 1 - Value"))))
184 "Gmail csv export format. Note some fields don't map perfectly,
185 feel free to modify them as you wish. \"PO Box\" and \"Extended
186 Address\" are added as additional address street lines if they
187 exist. Some special name fields are made custom instead of put in
188 name, which gets a single string. We map Gmail's \"Name Prefix\"
189 and \"Name Suffix\" to bbdb's affix (a list of strings). We lose
190 the prefix/suffix label, but those are usually obvious.")
191
192
193 (defconst bbdb-csv-import-gmail-typed-email
194 (append (car (last bbdb-csv-import-gmail)) '((repeat "E-mail 1 - Type")))
195 "Like the first Gmail mapping, but use custom fields to store
196 Gmail's email labels. This is separate because I assume most
197 people don't use those labels and using the default labels
198 would create useless custom fields.")
199
200 (defconst bbdb-csv-import-outlook-web
201 '((:namelist "First Name" "Middle Name" "Last Name")
202 (:mail "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
203 (:affix "Suffix")
204 (:phone
205 "Assistant's Phone" "Business Fax" "Business Phone"
206 "Business Phone 2" "Callback" "Car Phone"
207 "Company Main Phone" "Home Fax" "Home Phone"
208 "Home Phone 2" "ISDN" "Mobile Phone"
209 "Other Fax" "Other Phone" "Pager"
210 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
211 (:address
212 (("business address"
213 (("Business Street")
214 "Business City" "Business State"
215 "Business Postal Code" "Business Country"))
216 ("home address"
217 (("Home Street")
218 "Home City" "Home State"
219 "Home Postal Code" "Home Country"))
220 ("other address"
221 (("Other Street")
222 "Other City" "Other State"
223 "Other Postal Code" "Other Country"))))
224 (:organization "Company")
225 (:xfields
226 "Anniversary" "Family Name Yomi" "Given Name Yomi"
227 "Department" "Job Title" "Birthday" "Manager's Name" "Notes"
228 "Spouse" "Web Page"))
229 "Hotmail.com, outlook.com, live.com, etc.
230 Based on 'Export for outlook.com and other services',
231 not the export for Outlook 2010 and 2013.")
232
233 (defconst bbdb-csv-import-outlook-typed-email
234 (append (car (last bbdb-csv-import-outlook-web)) '((repeat "E-mail 1 - Type")))
235 "Like bbdb-csv-import-gmail-typed-email, but for outlook-web.
236 Adds email labels as custom fields.")
237
238
239 (defun bbdb-csv-import-flatten1 (list)
240 "Flatten LIST by 1 level."
241 (--reduce-from (if (consp it)
242 (-concat acc it)
243 (-snoc acc it))
244 nil list))
245
246
247 (defun bbdb-csv-import-merge-map (root)
248 "Combine two root mappings for making a combined mapping."
249 (bbdb-csv-import-flatten1
250 (list root
251 (-distinct
252 (append
253 (cdr (assoc root bbdb-csv-import-thunderbird))
254 (cdr (assoc root bbdb-csv-import-linkedin))
255 (cdr (assoc root bbdb-csv-import-gmail))
256 (cdr (assoc root bbdb-csv-import-outlook-web)))))))
257
258
259 (defconst bbdb-csv-import-combined
260 (list
261 ;; manually combined for proper ordering
262 '(:namelist "First Name" "Given Name" "Middle Name" "Last Name" "Family Name")
263 (bbdb-csv-import-merge-map :name)
264 (bbdb-csv-import-merge-map :affix)
265 (bbdb-csv-import-merge-map :aka)
266 (bbdb-csv-import-merge-map :mail)
267 (bbdb-csv-import-merge-map :phone)
268 ;; manually combined the addresses. Because it was easier.
269 '(:address
270 (repeat (("Address 1 - Type")
271 (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
272 "Address 1 - City" "Address 1 - Region"
273 "Address 1 - Postal Code" "Address 1 - Country")))
274 (("business address"
275 (("Business Street" "Business Street 2" "Business Street 3")
276 "Business City" "Business State"
277 "Business Postal Code" "Business Country"))
278 ("home address"
279 (("Home Street" "Home Street 2" "Home Street 3"
280 "Home Address" "Home Address 2")
281 "Home City" "Home State"
282 "Home Postal Code" "Home ZipCode" "Home Country"))
283 ("work address"
284 (("Work Address" "Work Address 2")
285 "Work City" "Work State"
286 "Work ZipCode" "Work Country"))
287 ("other address"
288 (("Other Street" "Other Street 2" "Other Street 3")
289 "Other City" "Other State"
290 "Other Postal Code" "Other Country"))))
291 (bbdb-csv-import-merge-map :organization)
292 (bbdb-csv-import-merge-map :xfields)))
293
294 (defcustom bbdb-csv-import-mapping-table bbdb-csv-import-combined
295 "The table which maps bbdb fields to csv fields. The default should work for most cases.
296 See the commentary section of this file for more details."
297 :group 'bbdb-csv-import
298 :type 'symbol)
299
300
301 (defun bbdb-csv-import-expand-repeats (csv-fields list)
302 "Return new list where elements from LIST in form (repeat elem1 ...)
303 become ((elem1 ...) [(elem2 ...)] ...) for as many repeating
304 numbered fields exist in the csv fields. elem can be a string or
305 a tree (a list with lists inside it)"
306 (cl-flet ((replace-num (num string)
307 ;; in STRING, replace all groups of numbers with NUM
308 (replace-regexp-in-string "[0-9]+"
309 (number-to-string num)
310 string)))
311 (--reduce-from
312 (if (not (and (consp it) (eq (car it) 'repeat)))
313 (cons it acc)
314 (setq it (cdr it))
315 (let* ((i 1)
316 (first-field (car (flatten it))))
317 (setq acc (cons it acc))
318 ;; use first-field to test if there is another repetition.
319 (while (member
320 (replace-num (setq i (1+ i)) first-field)
321 csv-fields)
322 (cl-labels ((fun (cell)
323 (if (consp cell)
324 (mapcar #'fun cell)
325 (replace-num i cell))))
326 (setq acc (cons (fun it) acc))))
327 acc))
328 nil list)))
329
330 (defun bbdb-csv-import-map-bbdb (csv-fields root)
331 "ROOT is a root element from bbdb-csv-import-mapping-table. Get
332 the csv-fields for root in the mapping format, including variably
333 repeated ones. Flatten by one because repeated fields are put in
334 sub-lists, but after expanding them, that extra depth is no
335 longer useful. Small trade off: address mappings without 'repeat need
336 to be grouped in a list because they contain sublists that we
337 don't want flattened."
338 (bbdb-csv-import-flatten1
339 (bbdb-csv-import-expand-repeats
340 csv-fields
341 (cdr (assoc root bbdb-csv-import-mapping-table)))))
342
343 ;;;###autoload
344 (defun bbdb-csv-import-file (filename)
345 "Parse and import csv file FILENAME to bbdb."
346 (interactive "fCSV file containg contact data: ")
347 (bbdb-csv-import-buffer (find-file-noselect filename)))
348
349 ;;;###autoload
350 (defun bbdb-csv-import-buffer (&optional buffer-or-name)
351 "Parse and import csv BUFFER-OR-NAME to bbdb.
352 Argument is a buffer or name of a buffer.
353 Defaults to current buffer."
354 (interactive "bBuffer containing CSV contact data: ")
355 (when (null bbdb-csv-import-mapping-table)
356 (error "error: `bbdb-csv-import-mapping-table' is nil. Please set it and rerun."))
357 (let* ((csv-data (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
358 (csv-fields (car csv-data))
359 (csv-data (cdr csv-data))
360 (initial-duplicate-value bbdb-allow-duplicates)
361 csv-record rd assoc-plus map-bbdb)
362 ;; convenient function names
363 (fset 'rd 'bbdb-csv-import-rd)
364 (fset 'assoc-plus 'bbdb-csv-import-assoc-plus)
365 (fset 'map-bbdb (-partial 'bbdb-csv-import-map-bbdb csv-fields))
366 ;; better to allow duplicates rather than fail
367 (setq bbdb-allow-duplicates t)
368 ;; loop over the csv records
369 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-data)))
370 (cl-flet*
371 ((ca (key list) (cdr (assoc key list))) ;; utility function
372 (rd-assoc (root)
373 ;; given ROOT, return a list of data, ignoring empty fields
374 (rd (lambda (elem) (assoc-plus elem csv-record)) (map-bbdb root)))
375 (assoc-expand (e)
376 ;; E = data-field-name | (field-name-field data-field)
377 ;; get data from the csv-record and return (field-name data) or nil.
378 (let ((data-name (if (consp e) (ca (car e) csv-record) e))
379 (data (assoc-plus (if (consp e) (cadr e) e) csv-record)))
380 (if data (list data-name data)))))
381 ;; set the arguments to bbdb-create-internal, then call it, the end.
382 (let ((name (let ((name (rd-assoc :namelist)))
383 ;; prioritize any combination of first middle last over :name
384 (if (>= (length name) 2)
385 (mapconcat 'identity name " ")
386 (car (rd-assoc :name)))))
387 (affix (rd-assoc :affix))
388 (aka (rd-assoc :aka))
389 (organization (rd-assoc :organization))
390 (mail (rd-assoc :mail))
391 (phone (rd 'vconcat (rd #'assoc-expand (map-bbdb :phone))))
392 (address (rd (lambda (e)
393
394 (let ((al (rd (lambda (elem) ;; al = address lines
395 (assoc-plus elem csv-record))
396 (caadr e)))
397 ;; to use bbdb-csv-import-combined, we can't mapcar
398 (address-data (--reduce-from (if (member it csv-fields)
399 (cons (ca it csv-record) acc)
400 acc)
401 nil (cdadr e)))
402 (elem-name (car e)))
403 (setq al (nreverse al))
404 (setq address-data (nreverse address-data))
405 ;; make it a list of at least 2 elements
406 (setq al (append al
407 (-repeat (- 2 (length al)) "")))
408 (when (consp elem-name)
409 (setq elem-name (ca (car elem-name) csv-record)))
410
411 ;; determine if non-nil and put together the minimum set
412 (when (or (not (--all? (zerop (length it)) address-data))
413 (not (--all? (zerop (length it)) al)))
414 (when (> 2 (length al))
415 (setcdr (max 2 (nthcdr (--find-last-index (not (null it))
416 al)
417 al)) nil))
418 (vconcat (list elem-name) (list al) address-data))))
419 (map-bbdb :address)))
420 (xfields (rd (lambda (list)
421 (let ((e (car list)))
422 (while (string-match "-" e)
423 (setq e (replace-match "" nil nil e)))
424 (while (string-match " +" e)
425 (setq e (replace-match "-" nil nil e)))
426 (setq e (make-symbol (downcase e)))
427 (cons e (cadr list)))) ;; change from (a b) to (a . b)
428 (rd #'assoc-expand (map-bbdb :xfields)))))
429 (bbdb-create-internal name affix aka organization mail phone address xfields t))))
430 (setq bbdb-allow-duplicates initial-duplicate-value)))
431
432 (defun bbdb-csv-import-rd (func list)
433 "like mapcar but don't build nil results into the resulting list"
434 (--reduce-from (let ((funcreturn (funcall func it)))
435 (if funcreturn
436 (cons funcreturn acc)
437 acc))
438 nil list))
439
440 (defun bbdb-csv-import-assoc-plus (key list)
441 "Like (cdr assoc ...) but turn an empty string result to nil."
442 (let ((result (cdr (assoc key list))))
443 (when (not (string= "" result))
444 result)))
445
446 (provide 'bbdb-csv-import)
447
448 ;;; bbdb-csv-import.el ends here