Added Gmail's native csv format, requiring new features.
[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 ;; Importer of csv (comma separated value) text into Emacs’s bbdb database,
26 ;; version 3+. Programs such as Thunderbird, Gmail, Linkedin, and Outlook allow
27 ;; for exporting contact data as csv files. See ASynK for syncing bbdb/google/outlook.
28
29 ;;; Installation:
30 ;;
31 ;; dependencies: pcsv.el, dash.el, bbdb
32 ;; These are available via marmalade/melpa or the internet
33 ;;
34 ;; Add to init file or execute manually as this may be a one time usage:
35 ;; (load-file FILENAME-OF-THIS-FILE)
36 ;; or
37 ;; (add-to-list 'load-path DIRECTORY-CONTAINING-THIS-FILE)
38 ;; (require 'bbdb3-csv-import)
39
40 ;;; Usage:
41 ;;
42 ;; Backup or rename any existing ~/.bbdb and ~/.emacs.d/bbdb while testing that
43 ;; the import works correctly.
44 ;;
45 ;; Assign a mapping table. Predefined ones listed here:
46 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-thunderbird)
47 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-gmail)
48 ;; (setq bbdb3-csv-import-mapping-table bbdb3-csv-import-linkedin)
49 ;;
50 ;; Simply call `bbdb3-csv-import-buffer' or
51 ;; `bbdb3-csv-import-file'. Interactively they prompt for file/buffer. Use
52 ;; non-interactively for no prompts.
53 ;;
54 ;; If you need to define your own mapping table, it should not be too hard. Use
55 ;; the existing tables as an example, and perhaps the test data within this
56 ;; project. Please send any new mapping tables to the maintainer listed in this
57 ;; file. The maintainer should be able to help with any issues and may create a
58 ;; new mapping table given sample data.
59 ;;
60 ;; Tips for testing:
61 ;; - bbdb doesn't work if you delete the bbdb database file in
62 ;; the middle of an emacs session. If you want to empty the current bbdb database,
63 ;; do M-x bbdb then .* then C-u * d on the beginning of a record.
64 ;; - After changing a mapping table, don't forget to re-execute
65 ;; (setq bbdb3-csv-import-mapping-table ...) so that it propagates.
66 ;;
67
68 (require 'pcsv)
69 (require 'dash)
70 (require 'bbdb-com)
71 (eval-when-compile (require 'cl))
72
73 (defconst bbdb3-csv-import-thunderbird
74 '(("firstname" "First Name")
75 ("lastname" "Last Name")
76 ("name" "Display Name")
77 ("aka" "Nickname")
78 ("mail" "Primary Email" "Secondary Email")
79 ("phone" "Work Phone" "Home Phone" "Fax Number" "Pager Number" "Mobile Number")
80 ("address"
81 (("home address"
82 (("Home Address" "Home Address 2")
83 "Home City" "Home State"
84 "Home ZipCode" "Home Country"))
85 ("work address"
86 (("Work Address" "Work Address 2")
87 "Work City" "Work State"
88 "Work ZipCode" "Work Country"))))
89 ("organization" "Organization")
90 ("xfields" "Web Page 1" "Web Page 2" "Birth Year" "Birth Month"
91 "Birth Day" "Department" "Custom 1" "Custom 2" "Custom 3"
92 "Custom 4" "Notes" "Job Title"))
93 "Thunderbird csv format")
94
95 (defconst bbdb3-csv-import-linkedin
96 '(("firstname" "First Name")
97 ("lastname" "Last Name")
98 ("middlename" "Middle Name")
99 ("mail" "E-mail Address" "E-mail 2 Address" "E-mail 3 Address")
100 ("phone"
101 "Assistant's Phone" "Business Fax" "Business Phone"
102 "Business Phone 2" "Callback" "Car Phone"
103 "Company Main Phone" "Home Fax" "Home Phone"
104 "Home Phone 2" "ISDN" "Mobile Phone"
105 "Other Fax" "Other Phone" "Pager"
106 "Primary Phone" "Radio Phone" "TTY/TDD Phone" "Telex")
107 ("address"
108 (("business address"
109 (("Business Street" "Business Street 2" "Business Street 3")
110 "Business City" "Business State"
111 "Business Postal Code" "Business Country"))
112 ("home address"
113 (("Home Street" "Home Street 2" "Home Street 3")
114 "Home City" "Home State"
115 "Home Postal Code" "Home Country"))
116 ("other address"
117 (("Other Street" "Other Street 2" "Other Street 3")
118 "Other City" "Other State"
119 "Other Postal Code" "Other Country"))))
120 ("organization" "Company")
121 ("xfields"
122 "Suffix" "Department" "Job Title" "Assistant's Name"
123 "Birthday" "Manager's Name" "Notes" "Other Address PO Box"
124 "Spouse" "Web Page" "Personal Web Page"))
125 "Linkedin export in the Outlook csv format.")
126
127
128 ;; note. PO Box and Extended Address are added as additional address street lines if they exist.
129 ;; If you don't like this, you can remove them. If you want some other special handling, it will need to be coded.
130 (defconst bbdb3-csv-import-gmail
131 '(("firstname" "Given Name")
132 ("lastname" "Family Name")
133 ("name" "Name")
134 ("mail" (repeat "E-mail 1 - Value"))
135 ("phone" (repeat ("Phone 1 - Type" "Phone 1 - Value")))
136 ("address"
137 (repeat (("Address 1 - Type")
138 (("Address 1 - Street" "Address 1 - PO Box" "Address 1 - Extended Address")
139 "Address 1 - City" "Address 1 - Region"
140 "Address 1 - Postal Code" "Address 1 - Country"))))
141 ("organization" (repeat "Organization 1 - Name"))
142 ("xfields"
143 "Additional Name" "Yomi Name" "Given Name Yomi"
144 "Additional Name Yomi" "Family Name Yomi" "Name Prefix"
145 "Name Suffix" "Initials" "Nickname"
146 "Short Name" "Maiden Name" "Birthday"
147 "Gender" "Location" "Billing Information"
148 "Directory Server" "Mileage" "Occupation"
149 "Hobby" "Sensitivity" "Priority"
150 "Subject" "Notes" "Group Membership"
151 ;; Gmail wouldn't let me add more than 1 organization, but no harm in
152 ;; looking for multiple since the field name implies the possibility.
153 (repeat
154 "Organization 1 - Type" "Organization 1 - Yomi Name"
155 "Organization 1 - Title" "Organization 1 - Department"
156 "Organization 1 - Symbol" "Organization 1 - Location"
157 "Organization 1 - Job Description")
158 (repeat ("Relation 1 - Type" "Relation 1 - Value"))
159 (repeat ("Website 1 - Type" "Website 1 - Value"))
160 (repeat ("Event 1 - Type" "Event 1 - Value"))
161 (repeat ("Custom Field 1 - Type" "Custom Field 1 - Value"))))
162 "Gmail csv export format")
163
164 (defconst bbdb3-csv-import-gmail-typed-email
165 (append (car (last bbdb3-csv-import-gmail)) '((repeat "E-mail 1 - Type")))
166 "Like the first Gmail mapping, but use custom fields to store
167 Gmail's email labels. This is separate because I assume most
168 people don't use those labels and using the default labels
169 would create useless custom fields.")
170
171 (defvar bbdb3-csv-import-mapping-table nil
172 "The table which maps bbdb3 fields to csv fields.
173 Use the default as an example to map non-thunderbird data.
174 Name used is firstname + lastname or name.
175 After the car, all names should map to whatever csv
176 field names are used in the first row of csv data.
177 Many fields are optional. If you aren't sure if one is,
178 best to just try it. The doc string for `bbdb-create-internal'
179 may be useful for determining which fields are required.")
180
181 ;;;###autoload
182 (defun bbdb3-csv-import-file (filename)
183 "Parse and import csv file FILENAME to bbdb3."
184 (interactive "fCSV file containg contact data: ")
185 (bbdb3-csv-import-buffer (find-file-noselect filename)))
186
187
188 ;;;###autoload
189 (defun bbdb3-csv-import-buffer (&optional buffer-or-name)
190 "Parse and import csv BUFFER-OR-NAME to bbdb3.
191 Argument is a buffer or name of a buffer.
192 Defaults to current buffer."
193 (interactive "bBuffer containing CSV contact data: ")
194 (when (null bbdb3-csv-import-mapping-table)
195 (error "error: `bbdb3-csv-import-mapping-table' is nil. Please set it and rerun."))
196 (let* ((csv-fields (pcsv-parse-buffer (get-buffer (or buffer-or-name (current-buffer)))))
197 (csv-contents (cdr csv-fields))
198 (csv-fields (car csv-fields))
199 (initial-duplicate-value bbdb-allow-duplicates)
200 csv-record rd assoc-plus flatten1)
201 ;; convenient function names
202 (fset 'rd 'bbdb3-csv-import-rd)
203 (fset 'assoc-plus 'bbdb3-csv-import-assoc-plus)
204 (fset 'flatten1 'bbdb3-csv-import-flatten1)
205 ;; Easier to allow duplicates and handle them post import vs failing as
206 ;; soon as we find one.
207 (setq bbdb-allow-duplicates t)
208 ;; loop over the csv records
209 (while (setq csv-record (map 'list 'cons csv-fields (pop csv-contents)))
210 (cl-flet*
211 ((rd-assoc (list)
212 ;; given LIST of fields, return a list of data, ignoring empty fields
213 (rd (lambda (elem) (assoc-plus elem csv-record)) list))
214 (mapcar-assoc (list)
215 ;; given LIST of fields,return a list of data with nil in place of an empty field
216 (mapcar (lambda (elem) (cdr (assoc elem csv-record))) list))
217 (assoc-expand (e)
218 ;; E = data-field-name | (field-name-field data-field)
219 ;; get data from the csv-record and return
220 ;; (field-name data) or nil.
221 (let ((data-name (if (consp e) (cdr (assoc (car e) csv-record)) e))
222 (data (assoc-plus (if (consp e) (cadr e) e) csv-record)))
223 (if data (list data-name data))))
224 (replace-num (num string)
225 ;; in STRING, replace all groups of numbers with NUM
226 (replace-regexp-in-string "[0-9]+" (number-to-string num) string))
227 (expand-repeats (list)
228 ;; return new list where elements from LIST in form
229 ;; (repeat elem1 ...) become ((elem1 ...) [(elem2 ...)] ...)
230 ;; For as many repeating numbered fields exist in the csv fields.
231 ;; elem can be a string or a tree (a list with possibly lists inside it)
232 (-reduce-from (lambda (acc arg)
233 (if (not (and (consp arg) (eq (car arg) 'repeat)))
234 (cons arg acc)
235 (setq arg (cdr arg))
236 (let* ((i 1)
237 (first-field (car (flatten arg))))
238 (setq acc (cons arg acc))
239 ;; use first-field to test if there is another repetition.
240 (while (member (replace-num (setq i (1+ i)) first-field) csv-fields)
241 (cl-labels ((fun (cell)
242 (if (consp cell)
243 (mapcar #'fun cell)
244 (replace-num i cell))))
245 (setq acc (cons (fun arg) acc))))
246 acc))) nil list))
247
248 (map-bbdb3 (root-mapping)
249 ;; ROOT-MAPPING = a root element from bbdb3-csv-import-mapping-table.
250 ;;
251 ;; Get the actual csv-fields, including variably repeated ones flattened
252 ;; by one because potentially repeated fields are put in sub-lists so they
253 ;; can be as one thing, but after they are, that extra depth is no longer
254 ;; useful. This makes for a little quirk: address mappings without 'repeat
255 ;; need to be grouped in a list because they contain sublists that we
256 ;; don't want flattened. I've decided that is a better trade off than more
257 ;; complex code.
258 (flatten1 (expand-repeats (cdr (assoc bbdb-arg bbdb3-csv-import-mapping-table)))))
259 (map-assoc (field)
260 ;; For mappings with just 1 simple csv-field, get it's data
261 (assoc-plus (car (map-bbdb3 field)) csv-record)))
262
263
264 (let ((name (let ((first (map-assoc "firstname"))
265 (middle (map-assoc "middlename"))
266 (last (map-assoc "lastname"))
267 (name (map-assoc "name")))
268 ;; prioritize any combination of first middle last over just "name"
269 (if (or (and first last) (and first middle) (and middle last))
270 ;; purely historical note.
271 ;; using (cons first last) as argument works the same as (concat first " " last)
272 (concat (or first middle) " " (or middle last) (when (and first middle) (concat " " last) ))
273 (or name first middle last ""))))
274 (phone (rd 'vconcat (rd #'assoc-expand (map-bbdb3 "phone"))))
275 (mail (rd-assoc (map-bbdb3 "mail")))
276 (xfields (rd (lambda (list)
277 (let ((e (car list)))
278 (while (string-match "-" e)
279 (setq e (replace-match "" nil nil e)))
280 (while (string-match " +" e)
281 (setq e (replace-match "-" nil nil e)))
282 (setq e (make-symbol (downcase e)))
283 (cons e (cadr list)))) ;; change from (a b) to (a . b)
284 (rd #'assoc-expand (map-bbdb3 "xfields"))))
285 (address (rd (lambda (mapping-elem)
286 (let ((address-lines (mapcar-assoc (caadr mapping-elem)))
287 (address-data (mapcar-assoc (cdadr mapping-elem)))
288 (elem-name (car mapping-elem)))
289
290 (when (consp elem-name)
291 (setq elem-name (cdr (assoc (car elem-name) csv-record))))
292
293 ;; determine if non-nil and put together the minimum set
294 (when (or (not (-all? '(lambda (arg) (zerop (length arg))) address-data))
295 (not (-all? '(lambda (arg) (zerop (length arg))) address-lines)))
296 (when (> 2 (length address-lines))
297 (setcdr (max 2 (nthcdr (-find-last-index (lambda (mapping-elem) (not (null mapping-elem)))
298 address-lines)
299 address-lines)) nil))
300 (vconcat (list elem-name) (list address-lines) address-data))))
301 (map-bbdb3 "address")))
302 (organization (rd-assoc (map-bbdb3 "organization")))
303 (affix (map-assoc "affix"))
304 (aka (rd-assoc (map-bbdb3 "aka"))))
305 (bbdb-create-internal name affix aka organization mail phone address xfields t))))
306 (setq bbdb-allow-duplicates initial-duplicate-value)))
307
308
309 (defun bbdb3-csv-import-flatten1 (list)
310 "flatten LIST by 1 level."
311 (-reduce-from (lambda (acc elem)
312 (if (consp elem)
313 (-concat acc elem)
314 (-snoc acc elem)))
315 nil list))
316
317 ;;;###autoload
318 (defun bbdb3-csv-import-rd (func list)
319 "like mapcar but don't build nil results into the resulting list"
320 (-reduce-from (lambda (acc elem)
321 (let ((funcreturn (funcall func elem)))
322 (if funcreturn
323 (cons funcreturn acc)
324 acc)))
325 nil list))
326
327 ;;;###autoload
328 (defun bbdb3-csv-import-assoc-plus (key list)
329 "Like `assoc' but turn an empty string result to nil."
330 (let ((result (cdr (assoc key list))))
331 (when (not (string= "" result))
332 result)))
333
334
335
336 (provide 'bbdb3-csv-import)
337
338 ;;; bbdb3-csv-import.el ends here