Do not set gnus-gravatar-size by default
[gnus] / lisp / gnus-gravatar.el
1 ;;; gnus-gravatar.el --- Gnus Gravatar support
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'gravatar)
28 (require 'gnus-art)
29
30 (defgroup gnus-gravatar nil
31   "Gnus Gravatar."
32   :group 'gnus-visual)
33
34 (defcustom gnus-gravatar-size nil
35   "How big should gravatars be displayed.
36 If nil, default to `gravatar-size'."
37   :type 'integer
38   :version "24.1"
39   :group 'gnus-gravatar)
40
41 (defcustom gnus-gravatar-properties '(:ascent center :relief 1)
42   "List of image properties applied to Gravatar images."
43   :type 'list
44   :version "24.1"
45   :group 'gnus-gravatar)
46
47 (defcustom gnus-gravatar-too-ugly gnus-article-x-face-too-ugly
48   "Regexp matching posters whose avatar shouldn't be shown automatically."
49   :type '(choice regexp (const nil))
50   :version "24.1"
51   :group 'gnus-gravatar)
52
53 (defun gnus-gravatar-transform-address (header category &optional force)
54   (gnus-with-article-headers
55     (let ((addresses
56            (mail-header-parse-addresses
57             ;; mail-header-parse-addresses does not work (reliably) on
58             ;; decoded headers.
59             (or
60              (ignore-errors
61                (mail-encode-encoded-word-string
62                 (or (mail-fetch-field header) "")))
63              (mail-fetch-field header))))
64           (gravatar-size (or gnus-gravatar-size gravatar-size))
65           name)
66       (dolist (address addresses)
67         (when (setq name (cdr address))
68           (setcdr address (setq name (mail-decode-encoded-word-string name))))
69         (when (or force
70                   (not (and gnus-gravatar-too-ugly
71                             (or (string-match gnus-gravatar-too-ugly
72                                               (car address))
73                                 (and name
74                                      (string-match gnus-gravatar-too-ugly
75                                                    name))))))
76           (ignore-errors
77             (gravatar-retrieve
78              (car address)
79              'gnus-gravatar-insert
80              (list header address category))))))))
81
82 (defun gnus-gravatar-insert (gravatar header address category)
83   "Insert GRAVATAR for ADDRESS in HEADER in current article buffer.
84 Set image category to CATEGORY."
85   (unless (eq gravatar 'error)
86     (gnus-with-article-headers
87       ;; The buffer can be gone at this time
88       (when (buffer-live-p (current-buffer))
89         (gnus-article-goto-header header)
90         (mail-header-narrow-to-field)
91         (let ((real-name (cdr address))
92               (mail-address (car address)))
93           (when (if real-name
94                     (re-search-forward (concat (regexp-quote real-name) "\\|"
95                                                (regexp-quote mail-address))
96                                        nil t)
97                   (search-forward mail-address nil t))
98             (goto-char (1- (match-beginning 0)))
99             ;; If we're on the " quoting the name, go backward
100             (when (looking-at "[\"<]")
101               (goto-char (1- (point))))
102             ;; Do not do anything if there's already a gravatar. This can
103             ;; happens if the buffer has been regenerated in the mean time, for
104             ;; example we were fetching someaddress, and then we change to
105             ;; another mail with the same someaddress.
106             (unless (memq 'gnus-gravatar (text-properties-at (point)))
107               (let ((inhibit-read-only t)
108                     (point (point)))
109                 (unless (featurep 'xemacs)
110                   (setq gravatar (append gravatar gnus-gravatar-properties)))
111                 (gnus-put-image gravatar nil category)
112                 (put-text-property point (point) 'gnus-gravatar address)
113                 (gnus-add-wash-type category)
114                 (gnus-add-image category gravatar)))))))))
115
116 ;;;###autoload
117 (defun gnus-treat-from-gravatar (&optional force)
118   "Display gravatar in the From header.
119 If gravatar is already displayed, remove it."
120   (interactive (list t)) ;; When type `W D g'
121   (gnus-with-article-buffer
122     (if (memq 'from-gravatar gnus-article-wash-types)
123         (gnus-delete-images 'from-gravatar)
124       (gnus-gravatar-transform-address "from" 'from-gravatar force))))
125
126 ;;;###autoload
127 (defun gnus-treat-mail-gravatar (&optional force)
128   "Display gravatars in the Cc and To headers.
129 If gravatars are already displayed, remove them."
130   (interactive (list t)) ;; When type `W D h'
131     (gnus-with-article-buffer
132       (if (memq 'mail-gravatar gnus-article-wash-types)
133           (gnus-delete-images 'mail-gravatar)
134         (gnus-gravatar-transform-address "cc" 'mail-gravatar force)
135         (gnus-gravatar-transform-address "to" 'mail-gravatar force))))
136
137 (provide 'gnus-gravatar)
138
139 ;;; gnus-gravatar.el ends here