gnus-art.el (gnus-article-add-buttons): Simplify condition.
[emacs/old-mirror.git] / lisp / gnus / gnus-gravatar.el
blob53d1310a0949a3083e7c24ef59cb7d298a2a0d87
1 ;;; gnus-gravatar.el --- Gnus Gravatar support
3 ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: news
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'gravatar)
28 (require 'gnus-art)
29 (require 'mail-extr) ;; Because of binding `mail-extr-disable-voodoo'.
31 (defgroup gnus-gravatar nil
32 "Gnus Gravatar."
33 :group 'gnus-visual)
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)
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)
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)
54 (defun gnus-gravatar-transform-address (header category &optional force)
55 (gnus-with-article-headers
56 (let* ((mail-extr-disable-voodoo t)
57 (mail-extr-ignore-realname-equals-mailbox-name nil)
58 (addresses (mail-extract-address-components
59 (or (mail-fetch-field header) "") t))
60 (gravatar-size (or gnus-gravatar-size gravatar-size))
61 name)
62 (dolist (address addresses)
63 (when (and (setq name (car address))
64 (string-match "\\` +" name))
65 (setcar address (setq name (substring name (match-end 0)))))
66 (when (or force
67 (not (and gnus-gravatar-too-ugly
68 (or (string-match gnus-gravatar-too-ugly
69 (or (cadr address) ""))
70 (and name
71 (string-match gnus-gravatar-too-ugly
72 name))))))
73 (ignore-errors
74 (gravatar-retrieve
75 (cadr address)
76 'gnus-gravatar-insert
77 (list header address category))))))))
79 (defun gnus-gravatar-insert (gravatar header address category)
80 "Insert GRAVATAR for ADDRESS in HEADER in current article buffer.
81 Set image category to CATEGORY."
82 (unless (eq gravatar 'error)
83 (gnus-with-article-headers
84 ;; The buffer can be gone at this time
85 (when (buffer-live-p (current-buffer))
86 (gnus-article-goto-header header)
87 (mail-header-narrow-to-field)
88 (let ((real-name (car address))
89 (mail-address (cadr address)))
90 (when (if real-name
91 (re-search-forward
92 (concat (gnus-replace-in-string
93 (regexp-quote real-name) "[\t ]+" "[\t\n ]+")
94 "\\|"
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 ((point (point)))
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)))))))))
115 ;;;###autoload
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))))
125 ;;;###autoload
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