eudcb-mab.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; eudcb-mab.el --- Emacs Unified Directory Client - AddressBook backend
  2. ;; Copyright (C) 2003-2012 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw@newartisans.com>
  4. ;; Maintainer: FSF
  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. ;; This library provides an interface to use the Mac's AddressBook,
  20. ;; by way of the "contacts" command-line utility which can be found
  21. ;; by searching on the Net.
  22. ;;; Code:
  23. (require 'eudc)
  24. (require 'executable)
  25. ;;{{{ Internal cooking
  26. (defvar eudc-mab-conversion-alist nil)
  27. (defvar eudc-buffer-time nil)
  28. (defvar eudc-contacts-file
  29. "~/Library/Application Support/AddressBook/AddressBook.data")
  30. (eudc-protocol-set 'eudc-query-function 'eudc-mab-query-internal 'mab)
  31. (eudc-protocol-set 'eudc-list-attributes-function nil 'mab)
  32. (eudc-protocol-set 'eudc-mab-conversion-alist nil 'mab)
  33. (eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil 'mab)
  34. (defun eudc-mab-query-internal (query &optional return-attrs)
  35. "Query MAB with QUERY.
  36. QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid
  37. MAB attribute names.
  38. RETURN-ATTRS is a list of attributes to return, defaulting to
  39. `eudc-default-return-attributes'."
  40. (let ((fmt-string "%ln:%fn:%p:%e")
  41. (mab-buffer (get-buffer-create " *mab contacts*"))
  42. (modified (nth 5 (file-attributes eudc-contacts-file)))
  43. result)
  44. (with-current-buffer mab-buffer
  45. (make-local-variable 'eudc-buffer-time)
  46. (goto-char (point-min))
  47. (when (or (eobp) (time-less-p eudc-buffer-time modified))
  48. (erase-buffer)
  49. (call-process (executable-find "contacts") nil t nil
  50. "-H" "-l" "-f" fmt-string)
  51. (setq eudc-buffer-time modified))
  52. (goto-char (point-min))
  53. (while (not (eobp))
  54. (let* ((args (split-string (buffer-substring (point)
  55. (line-end-position))
  56. "\\s-*:\\s-*"))
  57. (lastname (nth 0 args))
  58. (firstname (nth 1 args))
  59. (phone (nth 2 args))
  60. (mail (nth 3 args))
  61. (matched t))
  62. (if (string-match "\\s-+\\'" mail)
  63. (setq mail (replace-match "" nil nil mail)))
  64. (dolist (term query)
  65. (cond
  66. ((eq (car term) 'name)
  67. (unless (string-match (cdr term)
  68. (concat firstname " " lastname))
  69. (setq matched nil)))
  70. ((eq (car term) 'email)
  71. (unless (string= (cdr term) mail)
  72. (setq matched nil)))
  73. ((eq (car term) 'phone))))
  74. (when matched
  75. (setq result
  76. (cons `((firstname . ,firstname)
  77. (lastname . ,lastname)
  78. (name . ,(concat firstname " " lastname))
  79. (phone . ,phone)
  80. (email . ,mail)) result))))
  81. (forward-line)))
  82. (if (null return-attrs)
  83. result
  84. (let (eudc-result)
  85. (dolist (entry result)
  86. (let (entry-attrs abort)
  87. (dolist (attr entry)
  88. (when (memq (car attr) return-attrs)
  89. (if (= (length (cdr attr)) 0)
  90. (setq abort t)
  91. (setq entry-attrs
  92. (cons attr entry-attrs)))))
  93. (if (and entry-attrs (not abort))
  94. (setq eudc-result
  95. (cons entry-attrs eudc-result)))))
  96. eudc-result))))
  97. ;;}}}
  98. ;;{{{ High-level interfaces (interactive functions)
  99. (defun eudc-mab-set-server (dummy)
  100. "Set the EUDC server to MAB."
  101. (interactive)
  102. (eudc-set-server dummy 'mab)
  103. (message "MAB server selected"))
  104. ;;}}}
  105. (eudc-register-protocol 'mab)
  106. (provide 'eudcb-mab)
  107. ;;; eudcb-mab.el ends here