27f65c040946ca12342096e67bc99e5612bddb38
[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 (require 'mail-extr) ;; Because of binding `mail-extr-disable-voodoo'.
30
31 (defgroup gnus-gravatar nil
32   "Gnus Gravatar."
33   :group 'gnus-visual)
34
35 (defcustom gnus-gravatar-size nil
36   "How big should gravatars be displayed.
37 If nil, default to `gravatar-size'."
38   :type 'integer
39   :version "24.1"
40   :group 'gnus-gravatar)
41
42 (defcustom gnus-gravatar-properties '(:ascent center :relief 1)
43   "List of image properties applied to Gravatar images."
44   :type 'list
45   :version "24.1"
46   :group 'gnus-gravatar)
47
48 (defcustom gnus-gravatar-too-ugly gnus-article-x-face-too-ugly
49   "Regexp matching posters whose avatar shouldn't be shown automatically."
50   :type '(choice regexp (const nil))
51   :version "24.1"
52   :group 'gnus-gravatar)
53
54 (defun gnus-gravatar-transform-address (header category &optional force)
55   (gnus-with-article-headers
56     (let* ((mail-extr-disable-voodoo t)
57            (addresses (mail-extract-address-components
58                        (or (mail-fetch-field header) "") t))
59            (gravatar-size gnus-gravatar-size)
60            name)
61       (dolist (address addresses)
62         (when (and (setq name (car address))
63                    (string-match "\\` +" name))
64           (setcar address (setq name (substring name (match-end 0)))))
65         (when (or force
66                   (not (and gnus-gravatar-too-ugly
67                             (or (string-match gnus-gravatar-too-ugly
68                                               (cadr address))
69                                 (and name
70                                      (string-match gnus-gravatar-too-ugly
71                                                    name))))))
72           (ignore-errors
73             (gravatar-retrieve
74              (cadr 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 (car address))
88               (mail-address (cadr address)))
89           (when (if real-name
90                     (re-search-forward
91                      (concat (gnus-replace-in-string
92                               (regexp-quote real-name) "[\t ]+" "[\t\n ]+")
93                              "\\|"
94                              (regexp-quote mail-address))
95                      nil t)
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 ((point (point)))
107                 (unless (featurep 'xemacs)
108                   (setq gravatar (append gravatar gnus-gravatar-properties)))
109                 (gnus-put-image gravatar nil category)
110                 (put-text-property point (point) 'gnus-gravatar address)
111                 (gnus-add-wash-type category)
112                 (gnus-add-image category gravatar)))))))))
113
114 ;;;###autoload
115 (defun gnus-treat-from-gravatar (&optional force)
116   "Display gravatar in the From header.
117 If gravatar is already displayed, remove it."
118   (interactive (list t)) ;; When type `W D g'
119   (gnus-with-article-buffer
120     (if (memq 'from-gravatar gnus-article-wash-types)
121         (gnus-delete-images 'from-gravatar)
122       (gnus-gravatar-transform-address "from" 'from-gravatar force))))
123
124 ;;;###autoload
125 (defun gnus-treat-mail-gravatar (&optional force)
126   "Display gravatars in the Cc and To headers.
127 If gravatars are already displayed, remove them."
128   (interactive (list t)) ;; When type `W D h'
129     (gnus-with-article-buffer
130       (if (memq 'mail-gravatar gnus-article-wash-types)
131           (gnus-delete-images 'mail-gravatar)
132         (gnus-gravatar-transform-address "cc" 'mail-gravatar force)
133         (gnus-gravatar-transform-address "to" 'mail-gravatar force))))
134
135 (provide 'gnus-gravatar)
136
137 ;;; gnus-gravatar.el ends here