annotate lisp/gnus/gravatar.el @ 110960:f5a478bc42bc

* nextstep/AUTHORS: Remove email addresses.
author Glenn Morris <rgm@gnu.org>
date Tue, 12 Oct 2010 20:11:08 -0700
parents 0fe64d68a522
children c92e0a5c96e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110555
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
1 ;;; gravatar.el --- Get Gravatars
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
2
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
4
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
5 ;; Author: Julien Danjou <julien@danjou.info>
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
6 ;; Keywords: news
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
7
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
9
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
12 ;; the Free Software Foundation, either version 3 of the License, or
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
13 ;; (at your option) any later version.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
14
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
19
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
22
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
23 ;;; Commentary:
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
24
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
25 ;;; Code:
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
26
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
27 (require 'url)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
28 (require 'url-cache)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
29
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
30 (defgroup gravatar nil
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
31 "Gravatar."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
32 :group 'comm)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
33
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
34 (defcustom gravatar-automatic-caching t
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
35 "Whether cache retrieved gravatar."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
36 :group 'gravatar)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
37
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
38 (defcustom gravatar-cache-ttl (days-to-time 30)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
39 "Time to live for gravatar cache entries."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
40 :group 'gravatar)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
41
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
42 (defcustom gravatar-rating "g"
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
43 "Default rating for gravatar."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
44 :group 'gravatar)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
45
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
46 (defcustom gravatar-size 32
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
47 "Default size in pixels for gravatars."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
48 :group 'gravatar)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
49
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
50 (defconst gravatar-base-url
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
51 "http://www.gravatar.com/avatar"
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
52 "Base URL for getting gravatars.")
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
53
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
54 (defun gravatar-hash (mail-address)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
55 "Create an hash from MAIL-ADDRESS."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
56 (md5 (downcase mail-address)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
57
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
58 (defun gravatar-build-url (mail-address)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
59 "Return an URL to retrieve MAIL-ADDRESS gravatar."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
60 (format "%s/%s?d=404&r=%s&s=%d"
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
61 gravatar-base-url
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
62 (gravatar-hash mail-address)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
63 gravatar-rating
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
64 gravatar-size))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
65
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
66 (defun gravatar-cache-expired (url)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
67 "Check if URL is cached for more than `gravatar-cache-ttl'."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
68 (cond (url-standalone-mode
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
69 (not (file-exists-p (url-cache-create-filename url))))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
70 (t (let ((cache-time (url-is-cached url)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
71 (if cache-time
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
72 (time-less-p
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
73 (time-add
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
74 cache-time
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
75 gravatar-cache-ttl)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
76 (current-time))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
77 t)))))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
78
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
79 (defun gravatar-get-data ()
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
80 "Get data from current buffer."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
81 (when (string-match "^HTTP/.+ 200 OK$"
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
82 (buffer-substring (point-min) (line-end-position)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
83 (when (search-forward "\n\n" nil t)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
84 (buffer-substring (point) (point-max)))))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
85
110670
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
86 (eval-and-compile
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
87 (cond ((featurep 'xemacs)
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
88 (require 'gnus-xmas)
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
89 (defalias 'gravatar-create-image 'gnus-xmas-create-image))
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
90 ((featurep 'gnus-ems)
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
91 (defalias 'gravatar-create-image 'gnus-create-image))
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
92 (t
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
93 (require 'image)
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
94 (defalias 'gravatar-create-image 'create-image))))
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
95
110555
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
96 (defun gravatar-data->image ()
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
97 "Get data of current buffer and return an image.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
98 If no image available, return 'error."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
99 (let ((data (gravatar-get-data)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
100 (if data
110670
bd7b628a1f67 gravatar.el (gravatar-create-image): New function.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110555
diff changeset
101 (gravatar-create-image data nil t)
110555
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
102 'error)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
103
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
104 ;;;###autoload
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
105 (defun gravatar-retrieve (mail-address cb &optional cbargs)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
106 "Retrieve MAIL-ADDRESS gravatar and call CB on retrieval.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
107 You can provide a list of argument to pass to CB in CBARGS."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
108 (let ((url (gravatar-build-url mail-address)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
109 (if (gravatar-cache-expired url)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
110 (url-retrieve url
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
111 'gravatar-retrieved
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
112 (list cb (when cbargs cbargs)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
113 (apply cb
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
114 (with-temp-buffer
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
115 (mm-disable-multibyte)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
116 (url-cache-extract (url-cache-create-filename url))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
117 (gravatar-data->image))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
118 cbargs))))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
119
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
120 (defun gravatar-retrieved (status cb &optional cbargs)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
121 "Callback function used by `gravatar-retrieve'."
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
122 ;; Store gravatar?
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
123 (when gravatar-automatic-caching
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
124 (url-store-in-cache (current-buffer)))
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
125 (if (plist-get status :error)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
126 ;; Error happened.
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
127 (apply cb 'error cbargs)
110763
0fe64d68a522 Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110670
diff changeset
128 (apply cb (gravatar-data->image) cbargs))
0fe64d68a522 Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents: 110670
diff changeset
129 (kill-buffer (current-buffer)))
110555
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
130
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
131 (provide 'gravatar)
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
132
e07971bb896c Merge changes made in Gnus trunk.
Katsumi Yamaoka <yamaoka@jpl.org>
parents:
diff changeset
133 ;;; gravatar.el ends here