eudc-export.el 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; eudc-export.el --- functions to export EUDC query results
  2. ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
  3. ;; Author: Oscar Figueiredo <oscar@cpe.fr>
  4. ;; Maintainer: Pavel Janík <Pavel@Janik.cz>
  5. ;; Keywords: comm
  6. ;; Package: eudc
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Usage:
  20. ;; See the corresponding info file
  21. ;;; Code:
  22. (require 'eudc)
  23. (if (not (featurep 'bbdb))
  24. (load-library "bbdb"))
  25. (if (not (featurep 'bbdb-com))
  26. (load-library "bbdb-com"))
  27. (defun eudc-create-bbdb-record (record &optional silent)
  28. "Create a BBDB record using the RECORD alist.
  29. RECORD is an alist of (KEY . VALUE) where KEY is a directory attribute name
  30. symbol and VALUE is the corresponding value for the record.
  31. If SILENT is non-nil then the created BBDB record is not displayed."
  32. ;; This function runs in a special context where lisp symbols corresponding
  33. ;; to field names in record are bound to the corresponding values
  34. (eval
  35. `(let* (,@(mapcar (lambda (c)
  36. (list (car c) (if (listp (cdr c))
  37. (list 'quote (cdr c))
  38. (cdr c))))
  39. record)
  40. bbdb-name
  41. bbdb-company
  42. bbdb-net
  43. bbdb-address
  44. bbdb-phones
  45. bbdb-notes
  46. spec
  47. bbdb-record
  48. value
  49. (conversion-alist (symbol-value eudc-bbdb-conversion-alist)))
  50. ;; BBDB standard fields
  51. (setq bbdb-name (eudc-parse-spec (cdr (assq 'name conversion-alist)) record nil)
  52. bbdb-company (eudc-parse-spec (cdr (assq 'company conversion-alist)) record nil)
  53. bbdb-net (eudc-parse-spec (cdr (assq 'net conversion-alist)) record nil)
  54. bbdb-notes (eudc-parse-spec (cdr (assq 'notes conversion-alist)) record nil))
  55. (setq spec (cdr (assq 'address conversion-alist)))
  56. (setq bbdb-address (delq nil (eudc-parse-spec (if (listp (car spec))
  57. spec
  58. (list spec))
  59. record t)))
  60. (setq spec (cdr (assq 'phone conversion-alist)))
  61. (setq bbdb-phones (delq nil (eudc-parse-spec (if (listp (car spec))
  62. spec
  63. (list spec))
  64. record t)))
  65. ;; BBDB custom fields
  66. (setq bbdb-notes (append (list (and bbdb-notes (cons 'notes bbdb-notes)))
  67. (mapcar (function
  68. (lambda (mapping)
  69. (if (and (not (memq (car mapping)
  70. '(name company net address phone notes)))
  71. (setq value (eudc-parse-spec (cdr mapping) record nil)))
  72. (cons (car mapping) value))))
  73. conversion-alist)))
  74. (setq bbdb-notes (delq nil bbdb-notes))
  75. (setq bbdb-record (bbdb-create-internal bbdb-name
  76. bbdb-company
  77. bbdb-net
  78. bbdb-address
  79. bbdb-phones
  80. bbdb-notes))
  81. (or silent
  82. (bbdb-display-records (list bbdb-record))))))
  83. (defun eudc-parse-spec (spec record recurse)
  84. "Parse the conversion SPEC using RECORD.
  85. If RECURSE is non-nil then SPEC may be a list of atomic specs."
  86. (cond
  87. ((or (stringp spec)
  88. (symbolp spec)
  89. (and (listp spec)
  90. (symbolp (car spec))
  91. (fboundp (car spec))))
  92. (condition-case nil
  93. (eval spec)
  94. (void-variable nil)))
  95. ((and recurse
  96. (listp spec))
  97. (mapcar (lambda (spec-elem)
  98. (eudc-parse-spec spec-elem record nil))
  99. spec))
  100. (t
  101. (error "Invalid specification for `%s' in `eudc-bbdb-conversion-alist'" spec))))
  102. (defun eudc-bbdbify-address (addr location)
  103. "Parse ADDR into a vector compatible with BBDB.
  104. ADDR should be an address string of no more than four lines or a
  105. list of lines.
  106. The last two lines are searched for the zip code, city and state name.
  107. LOCATION is used as the address location for bbdb."
  108. (let* ((addr-components (if (listp addr)
  109. (reverse addr)
  110. (reverse (split-string addr "\n"))))
  111. (last1 (pop addr-components))
  112. (last2 (pop addr-components))
  113. zip city state)
  114. (setq addr-components (nreverse addr-components))
  115. ;; If not containing the zip code the last line is supposed to contain a
  116. ;; country name and the address is supposed to be in european style
  117. (if (not (string-match "[0-9][0-9][0-9]" last1))
  118. (progn
  119. (setq state last1)
  120. (if (string-match "\\([0-9]+\\)[ \t]+\\(.*\\)" last2)
  121. (setq city (match-string 2 last2)
  122. zip (string-to-number (match-string 1 last2)))
  123. (error "Cannot parse the address")))
  124. (cond
  125. ;; American style
  126. ((string-match "\\(\\w+\\)\\W*\\([A-Z][A-Z]\\)\\W*\\([0-9]+\\)" last1)
  127. (setq city (match-string 1 last1)
  128. state (match-string 2 last1)
  129. zip (string-to-number (match-string 3 last1))))
  130. ;; European style
  131. ((string-match "\\([0-9]+\\)[ \t]+\\(.*\\)" last1)
  132. (setq city (match-string 2 last1)
  133. zip (string-to-number (match-string 1 last1))))
  134. (t
  135. (error "Cannot parse the address"))))
  136. (vector location
  137. (or (nth 0 addr-components) "")
  138. (or (nth 1 addr-components) "")
  139. (or (nth 2 addr-components) "")
  140. (or city "")
  141. (or state "")
  142. zip)))
  143. ;; External.
  144. (declare-function bbdb-parse-phone-number "ext:bbdb-com"
  145. (string &optional number-type))
  146. (declare-function bbdb-string-trim "ext:bbdb" (string))
  147. (defun eudc-bbdbify-phone (phone location)
  148. "Parse PHONE into a vector compatible with BBDB.
  149. PHONE is either a string supposedly containing a phone number or
  150. a list of such strings which are concatenated.
  151. LOCATION is used as the phone location for BBDB."
  152. (cond
  153. ((stringp phone)
  154. (let (phone-list)
  155. (condition-case err
  156. (setq phone-list (bbdb-parse-phone-number phone))
  157. (error
  158. (if (string= "phone number unparsable." (eudc-cadr err))
  159. (if (not (y-or-n-p (format "BBDB claims %S to be unparsable--insert anyway? " phone)))
  160. (error "Phone number unparsable")
  161. (setq phone-list (list (bbdb-string-trim phone))))
  162. (signal (car err) (cdr err)))))
  163. (if (= 3 (length phone-list))
  164. (setq phone-list (append phone-list '(nil))))
  165. (apply 'vector location phone-list)))
  166. ((listp phone)
  167. (vector location (mapconcat 'identity phone ", ")))
  168. (t
  169. (error "Invalid phone specification"))))
  170. (defun eudc-batch-export-records-to-bbdb ()
  171. "Insert all the records returned by a directory query into BBDB."
  172. (interactive)
  173. (goto-char (point-min))
  174. (let ((nbrec 0)
  175. record)
  176. (while (eudc-move-to-next-record)
  177. (and (overlays-at (point))
  178. (setq record (overlay-get (car (overlays-at (point))) 'eudc-record))
  179. (1+ nbrec)
  180. (eudc-create-bbdb-record record t)))
  181. (message "%d records imported into BBDB" nbrec)))
  182. ;;;###autoload
  183. (defun eudc-insert-record-at-point-into-bbdb ()
  184. "Insert record at point into the BBDB database.
  185. This function can only be called from a directory query result buffer."
  186. (interactive)
  187. (let ((record (and (overlays-at (point))
  188. (overlay-get (car (overlays-at (point))) 'eudc-record))))
  189. (if (null record)
  190. (error "Point is not over a record")
  191. (eudc-create-bbdb-record record))))
  192. ;;;###autoload
  193. (defun eudc-try-bbdb-insert ()
  194. "Call `eudc-insert-record-at-point-into-bbdb' if on a record."
  195. (interactive)
  196. (and (or (featurep 'bbdb)
  197. (prog1 (locate-library "bbdb") (message "")))
  198. (overlays-at (point))
  199. (overlay-get (car (overlays-at (point))) 'eudc-record)
  200. (eudc-insert-record-at-point-into-bbdb)))
  201. ;;; eudc-export.el ends here