ecomplete.el (ecomplete-highlight-match-line): Fix typos in comment.
[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 32
35   "How big should gravatars be displayed."
36   :type 'integer
37   :version "24.1"
38   :group 'gnus-gravatar)
39
40 (defcustom gnus-gravatar-properties '(:ascent center :relief 1)
41   "List of image properties applied to Gravatar images."
42   :type 'list
43   :version "24.1"
44   :group 'gnus-gravatar)
45
46 (defcustom gnus-gravatar-too-ugly (if (boundp 'gnus-article-x-face-too-ugly)
47                                       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)
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       (let ((gravatar-size gnus-gravatar-size))
65         (dolist (address addresses)
66           (unless (and gnus-gravatar-too-ugly
67                        (or (string-match gnus-gravatar-too-ugly
68                                          (car address))
69                            (and (cdr address)
70                                 (string-match gnus-gravatar-too-ugly
71                                               (cdr address)))))
72             (ignore-errors
73               (gravatar-retrieve
74                (car address)
75                'gnus-gravatar-insert
76                (list header address category)))))))))
77
78 (defun gnus-gravatar-insert (gravatar header address category)
79   "Insert GRAVATAR for ADDRESS in HEADER in current article buffer.
80 Set image category to CATEGORY."
81   (unless (eq gravatar 'error)
82     (gnus-with-article-headers
83       ;; The buffer can be gone at this time
84       (when (buffer-live-p (current-buffer))
85         (gnus-article-goto-header header)
86         (mail-header-narrow-to-field)
87         (let ((real-name (cdr address))
88               (mail-address (car address)))
89           (when (if real-name             ; have a realname, go for it!
90                     (and (search-forward real-name nil t)
91                          (search-backward real-name nil t))
92                   (and (search-forward mail-address nil t)
93                        (search-backward mail-address nil t)))
94             (goto-char (1- (point)))
95             ;; If we're on the " quoting the name, go backward
96             (when (looking-at "[\"<]")
97               (goto-char (1- (point))))
98             ;; Do not do anything if there's already a gravatar. This can
99             ;; happens if the buffer has been regenerated in the mean time, for
100             ;; example we were fetching someaddress, and then we change to
101             ;; another mail with the same someaddress.
102             (unless (memq 'gnus-gravatar (text-properties-at (point)))
103               (let ((inhibit-read-only t)
104                     (point (point)))
105                 (unless (featurep 'xemacs)
106                   (setq gravatar (append gravatar gnus-gravatar-properties)))
107                 (gnus-put-image gravatar nil category)
108                 (put-text-property point (point) 'gnus-gravatar address)
109                 (gnus-add-wash-type category)
110                 (gnus-add-image category gravatar)))))))))
111
112 ;;;###autoload
113 (defun gnus-treat-from-gravatar ()
114   "Display gravatar in the From header.
115 If gravatar is already displayed, remove it."
116   (interactive)
117   (gnus-with-article-buffer
118     (if (memq 'from-gravatar gnus-article-wash-types)
119         (gnus-delete-images 'from-gravatar)
120       (let ((gnus-gravatar-too-ugly
121              (unless buffer-read-only ;; When type `W D g'
122                gnus-gravatar-too-ugly)))
123         (gnus-gravatar-transform-address "from" 'from-gravatar)))))
124
125 ;;;###autoload
126 (defun gnus-treat-mail-gravatar ()
127   "Display gravatars in the Cc and To headers.
128 If gravatars are already displayed, remove them."
129   (interactive)
130     (gnus-with-article-buffer
131       (if (memq 'mail-gravatar gnus-article-wash-types)
132           (gnus-delete-images 'mail-gravatar)
133         (let ((gnus-gravatar-too-ugly
134                (unless buffer-read-only ;; When type `W D h'
135                  gnus-gravatar-too-ugly)))
136           (gnus-gravatar-transform-address "cc" 'mail-gravatar)
137           (gnus-gravatar-transform-address "to" 'mail-gravatar)))))
138
139 (provide 'gnus-gravatar)
140
141 ;;; gnus-gravatar.el ends here