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