1 ;;; gnus-gravatar.el --- Gnus Gravatar support
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs 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.
15 ;; GNU Emacs 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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (defgroup gnus-gravatar nil
34 (defcustom gnus-gravatar-size
32
35 "How big should gravatars be displayed."
38 :group
'gnus-gravatar
)
40 (defcustom gnus-gravatar-properties
'(:ascent center
:relief
1)
41 "List of image properties applied to Gravatar images."
44 :group
'gnus-gravatar
)
46 (defcustom gnus-gravatar-too-ugly gnus-article-x-face-too-ugly
47 "Regexp matching posters whose avatar shouldn't be shown automatically."
48 :type
'(choice regexp
(const nil
))
50 :group
'gnus-gravatar
)
52 (defun gnus-gravatar-transform-address (header category
&optional force
)
53 (gnus-with-article-headers
55 (mail-header-parse-addresses
56 ;; mail-header-parse-addresses does not work (reliably) on
60 (mail-encode-encoded-word-string
61 (or (mail-fetch-field header
) "")))
62 (mail-fetch-field header
))))
63 (gravatar-size gnus-gravatar-size
)
65 (dolist (address addresses
)
66 (when (setq name
(cdr address
))
67 (setcdr address
(setq name
(mail-decode-encoded-word-string name
))))
69 (not (and gnus-gravatar-too-ugly
70 (or (string-match gnus-gravatar-too-ugly
73 (string-match gnus-gravatar-too-ugly
79 (list header address category
))))))))
81 (defun gnus-gravatar-insert (gravatar header address category
)
82 "Insert GRAVATAR for ADDRESS in HEADER in current article buffer.
83 Set image category to CATEGORY."
84 (unless (eq gravatar
'error
)
85 (gnus-with-article-headers
86 ;; The buffer can be gone at this time
87 (when (buffer-live-p (current-buffer))
88 (gnus-article-goto-header header
)
89 (mail-header-narrow-to-field)
90 (let ((real-name (cdr address
))
91 (mail-address (car address
)))
93 (re-search-forward (concat (regexp-quote real-name
) "\\|"
94 (regexp-quote mail-address
))
96 (search-forward mail-address nil t
))
97 (goto-char (1- (match-beginning 0)))
98 ;; If we're on the " quoting the name, go backward
99 (when (looking-at "[\"<]")
100 (goto-char (1- (point))))
101 ;; Do not do anything if there's already a gravatar. This can
102 ;; happens if the buffer has been regenerated in the mean time, for
103 ;; example we were fetching someaddress, and then we change to
104 ;; another mail with the same someaddress.
105 (unless (memq 'gnus-gravatar
(text-properties-at (point)))
106 (let ((inhibit-read-only t
)
108 (unless (featurep 'xemacs
)
109 (setq gravatar
(append gravatar gnus-gravatar-properties
)))
110 (gnus-put-image gravatar nil category
)
111 (put-text-property point
(point) 'gnus-gravatar address
)
112 (gnus-add-wash-type category
)
113 (gnus-add-image category gravatar
)))))))))
116 (defun gnus-treat-from-gravatar (&optional force
)
117 "Display gravatar in the From header.
118 If gravatar is already displayed, remove it."
119 (interactive (list t
)) ;; When type `W D g'
120 (gnus-with-article-buffer
121 (if (memq 'from-gravatar gnus-article-wash-types
)
122 (gnus-delete-images 'from-gravatar
)
123 (gnus-gravatar-transform-address "from" 'from-gravatar force
))))
126 (defun gnus-treat-mail-gravatar (&optional force
)
127 "Display gravatars in the Cc and To headers.
128 If gravatars are already displayed, remove them."
129 (interactive (list t
)) ;; When type `W D h'
130 (gnus-with-article-buffer
131 (if (memq 'mail-gravatar gnus-article-wash-types
)
132 (gnus-delete-images 'mail-gravatar
)
133 (gnus-gravatar-transform-address "cc" 'mail-gravatar force
)
134 (gnus-gravatar-transform-address "to" 'mail-gravatar force
))))
136 (provide 'gnus-gravatar
)
138 ;;; gnus-gravatar.el ends here