-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgmail2bbdb.el
126 lines (106 loc) · 4.59 KB
/
gmail2bbdb.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
;;; gmail2bbdb.el --- import email and name into bbdb from vcard.
;; Copyright (C) 2014, 2015 Chen Bin
;; Author: Chen Bin <chenbin.sh@gmail.com>
;; URL: http://github.com/redguardtoo/gmail2bbdb
;; Keywords: vcard bbdb email contact gmail
;; Version: 0.0.6
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program ; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; To setup, insert below code into ~/.emacs:
;; (add-to-list 'load-path "~/.emacs.d/lisp/")
;; (autoload 'gmail2bbdb-import-file "gmail2bbdb" nil t nil)
;;
;; Usage:
;; - Click "More->Export->vCard format->Export" at Google Contacts.
;; Download "contacts.vcf".
;; - Run "M-x gmail2bbdb-import-file" and select contacts.vcf.
;; "~/.bbdb" is created.
;; - Keep using BBDB.
;;
;;; Code:
(defvar gmail2bbdb-bbdb-file "~/.bbdb"
"Where BBDB file are exported.")
(defvar gmail2bbdb-excluded-email-regex-list '("^noreply.*"
"notify.*@disqus.net"
".*@noreply.github.com$"
"reply.*@reply.github.com"
"do-not-reply@stackoverflow.com")
"Email matching any regex of the list are NOT exported.")
(defvar gmail2bbdb-exclude-people-without-name nil
"Exclude people without name.")
(defun gmail2bbdb--is-valid-email (eml)
(let* ((i 0) excluded)
(while (and (not excluded)
(< i (length gmail2bbdb-excluded-email-regex-list)))
(if (string-match (nth i gmail2bbdb-excluded-email-regex-list)
eml)
(setq excluded t))
(setq i (1+ i)))
(not excluded)))
;; ["Spolsky" "Joel" nil ("Spolsky Joel") nil nil nil ("spolsky@fogcreek.com") nil nil]
(defun gmail2bbdb--extract-item (str)
(let* ((lines (split-string str "[\r\n]+"))
fullname family-name given-name emails rlt eml)
(dolist (l lines)
(cond
((string-match "^FN:\\(.*\\)" l)
(setq fullname (match-string 1 l)))
((string-match "^N:\\([^;]*\\);\\([^;]*\\);.*" l)
(setq family-name (match-string 1 l))
(setq given-name (match-string 2 l)))
((string-match "TYPE=[A-Z]+:\\([^ @]+@[^ @]+\\)" l)
(setq eml (match-string 1 l))
(when (and (or (not gmail2bbdb-exclude-people-without-name)
(not (string= "" family-name))
(not (string= "" given-name)))
(gmail2bbdb--is-valid-email eml))
(message "family-name=%s given-name=%s " family-name given-name)
(add-to-list 'emails eml)))))
(when emails
(setq rlt (vector given-name family-name nil (list fullname) nil nil nil emails nil nil))
(format "%S" rlt))))
;;;###autoload
(defun gmail2bbdb-import-file (vcard-file)
"Import vCards from VCARD-FILE into BBDB.
If VCARD-FILE is a wildcard, import each matching file.
Existing BBDB records will be *overrided*."
(interactive "FvCard file (wildcard could be used): ")
(let* ((dst-file (file-truename gmail2bbdb-bbdb-file))
rlt)
(with-temp-buffer
(insert-file-contents vcard-file)
(goto-char (point-min))
;; Change CRLF into CR if necessary
;; (with-temp-file
;; )
(while (re-search-forward "\r\n" nil t)
(replace-match "\n" nil nil nil 1))
(goto-char (point-min))
(while (re-search-forward
"^\\([[:alnum:]-]*\\.\\)?*BEGIN:VCARD[\n[:print:][:cntrl:]]*?\\(^\\([[:alnum:]-]*\\.\\)?END:VCARD\\)"
nil t)
(let* ((vcard (match-string 0))
(item (gmail2bbdb--extract-item vcard)))
(if item
(add-to-list 'rlt item)))))
(if rlt
(with-temp-buffer
(insert ";; -*- mode: Emacs-Lisp; coding: utf-8; -*-\n")
(insert ";;; file-format: 7\n")
(insert (mapconcat 'identity rlt "\n"))
(write-file dst-file))
(message "No email found"))))
(provide 'gmail2bbdb)
;;; gmail2bbdb.el ends here