gnus-gravatar.el 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ;;; gnus-gravatar.el --- Gnus Gravatar support
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Julien Danjou <julien@danjou.info>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'gravatar)
  19. (require 'gnus-art)
  20. (require 'mail-extr) ;; Because of binding `mail-extr-disable-voodoo'.
  21. (defgroup gnus-gravatar nil
  22. "Gnus Gravatar."
  23. :group 'gnus-visual)
  24. (defcustom gnus-gravatar-size nil
  25. "How big should gravatars be displayed.
  26. If nil, default to `gravatar-size'."
  27. :type 'integer
  28. :version "24.1"
  29. :group 'gnus-gravatar)
  30. (defcustom gnus-gravatar-properties '(:ascent center :relief 1)
  31. "List of image properties applied to Gravatar images."
  32. :type 'list
  33. :version "24.1"
  34. :group 'gnus-gravatar)
  35. (defcustom gnus-gravatar-too-ugly gnus-article-x-face-too-ugly
  36. "Regexp matching posters whose avatar shouldn't be shown automatically."
  37. :type '(choice regexp (const nil))
  38. :version "24.1"
  39. :group 'gnus-gravatar)
  40. (defun gnus-gravatar-transform-address (header category &optional force)
  41. (gnus-with-article-headers
  42. (let* ((mail-extr-disable-voodoo t)
  43. (mail-extr-ignore-realname-equals-mailbox-name nil)
  44. (addresses (mail-extract-address-components
  45. (or (mail-fetch-field header) "") t))
  46. (gravatar-size (or gnus-gravatar-size gravatar-size))
  47. name)
  48. (dolist (address addresses)
  49. (when (and (setq name (car address))
  50. (string-match "\\` +" name))
  51. (setcar address (setq name (substring name (match-end 0)))))
  52. (when (or force
  53. (not (and gnus-gravatar-too-ugly
  54. (or (string-match gnus-gravatar-too-ugly
  55. (or (cadr address) ""))
  56. (and name
  57. (string-match gnus-gravatar-too-ugly
  58. name))))))
  59. (ignore-errors
  60. (gravatar-retrieve
  61. (cadr address)
  62. 'gnus-gravatar-insert
  63. (list header address category))))))))
  64. (defun gnus-gravatar-insert (gravatar header address category)
  65. "Insert GRAVATAR for ADDRESS in HEADER in current article buffer.
  66. Set image category to CATEGORY."
  67. (unless (eq gravatar 'error)
  68. (gnus-with-article-buffer
  69. (let ((mark (point-marker))
  70. (inhibit-point-motion-hooks t)
  71. (case-fold-search t))
  72. (save-restriction
  73. (article-narrow-to-head)
  74. ;; The buffer can be gone at this time
  75. (when (buffer-live-p (current-buffer))
  76. (gnus-article-goto-header header)
  77. (mail-header-narrow-to-field)
  78. (let ((real-name (car address))
  79. (mail-address (cadr address)))
  80. (when (if real-name
  81. (re-search-forward
  82. (concat (gnus-replace-in-string
  83. (regexp-quote real-name) "[\t ]+" "[\t\n ]+")
  84. "\\|"
  85. (regexp-quote mail-address))
  86. nil t)
  87. (search-forward mail-address nil t))
  88. (goto-char (1- (match-beginning 0)))
  89. ;; If we're on the " quoting the name, go backward
  90. (when (looking-at "[\"<]")
  91. (goto-char (1- (point))))
  92. ;; Do not do anything if there's already a gravatar. This can
  93. ;; happens if the buffer has been regenerated in the mean time, for
  94. ;; example we were fetching someaddress, and then we change to
  95. ;; another mail with the same someaddress.
  96. (unless (memq 'gnus-gravatar (text-properties-at (point)))
  97. (let ((point (point)))
  98. (unless (featurep 'xemacs)
  99. (setq gravatar (append gravatar gnus-gravatar-properties)))
  100. (gnus-put-image gravatar (buffer-substring (point) (1+ point)) category)
  101. (put-text-property point (point) 'gnus-gravatar address)
  102. (gnus-add-wash-type category)
  103. (gnus-add-image category gravatar)))))))
  104. (goto-char (marker-position mark))))))
  105. ;;;###autoload
  106. (defun gnus-treat-from-gravatar (&optional force)
  107. "Display gravatar in the From header.
  108. If gravatar is already displayed, remove it."
  109. (interactive (list t)) ;; When type `W D g'
  110. (gnus-with-article-buffer
  111. (if (memq 'from-gravatar gnus-article-wash-types)
  112. (gnus-delete-images 'from-gravatar)
  113. (gnus-gravatar-transform-address "from" 'from-gravatar force))))
  114. ;;;###autoload
  115. (defun gnus-treat-mail-gravatar (&optional force)
  116. "Display gravatars in the Cc and To headers.
  117. If gravatars are already displayed, remove them."
  118. (interactive (list t)) ;; When type `W D h'
  119. (gnus-with-article-buffer
  120. (if (memq 'mail-gravatar gnus-article-wash-types)
  121. (gnus-delete-images 'mail-gravatar)
  122. (gnus-gravatar-transform-address "cc" 'mail-gravatar force)
  123. (gnus-gravatar-transform-address "to" 'mail-gravatar force))))
  124. (provide 'gnus-gravatar)
  125. ;;; gnus-gravatar.el ends here