Merge from emacs-24; up to 2012-05-08T15:19:18Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / gnus / gravatar.el
CommitLineData
61b1af82
G
1;;; gravatar.el --- Get Gravatars
2
acaf905b 3;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
61b1af82
G
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
61b1af82
G
27(require 'url)
28(require 'url-cache)
29
30(defgroup gravatar nil
31 "Gravatar."
2bed3f04 32 :version "24.1"
61b1af82
G
33 :group 'comm)
34
35(defcustom gravatar-automatic-caching t
36 "Whether cache retrieved gravatar."
37 :group 'gravatar)
38
39(defcustom gravatar-cache-ttl (days-to-time 30)
40 "Time to live for gravatar cache entries."
41 :group 'gravatar)
42
43(defcustom gravatar-rating "g"
44 "Default rating for gravatar."
45 :group 'gravatar)
46
47(defcustom gravatar-size 32
48 "Default size in pixels for gravatars."
49 :group 'gravatar)
50
51(defconst gravatar-base-url
52 "http://www.gravatar.com/avatar"
53 "Base URL for getting gravatars.")
54
55(defun gravatar-hash (mail-address)
56 "Create an hash from MAIL-ADDRESS."
57 (md5 (downcase mail-address)))
58
59(defun gravatar-build-url (mail-address)
60 "Return an URL to retrieve MAIL-ADDRESS gravatar."
61 (format "%s/%s?d=404&r=%s&s=%d"
62 gravatar-base-url
63 (gravatar-hash mail-address)
64 gravatar-rating
65 gravatar-size))
66
67(defun gravatar-cache-expired (url)
68 "Check if URL is cached for more than `gravatar-cache-ttl'."
69 (cond (url-standalone-mode
70 (not (file-exists-p (url-cache-create-filename url))))
71 (t (let ((cache-time (url-is-cached url)))
72 (if cache-time
73 (time-less-p
74 (time-add
75 cache-time
76 gravatar-cache-ttl)
77 (current-time))
78 t)))))
79
80(defun gravatar-get-data ()
81 "Get data from current buffer."
70041e9a
G
82 (save-excursion
83 (goto-char (point-min))
84 (when (re-search-forward "^HTTP/.+ 200 OK$" nil (line-end-position))
85 (when (search-forward "\n\n" nil t)
86 (buffer-substring (point) (point-max))))))
61b1af82 87
4b36c6d4
KY
88(eval-and-compile
89 (cond ((featurep 'xemacs)
90 (require 'gnus-xmas)
91 (defalias 'gravatar-create-image 'gnus-xmas-create-image))
92 ((featurep 'gnus-ems)
93 (defalias 'gravatar-create-image 'gnus-create-image))
94 (t
95 (require 'image)
96 (defalias 'gravatar-create-image 'create-image))))
97
61b1af82
G
98(defun gravatar-data->image ()
99 "Get data of current buffer and return an image.
100If no image available, return 'error."
101 (let ((data (gravatar-get-data)))
102 (if data
70041e9a 103 (gravatar-create-image data nil t)
61b1af82
G
104 'error)))
105
106;;;###autoload
107(defun gravatar-retrieve (mail-address cb &optional cbargs)
108 "Retrieve MAIL-ADDRESS gravatar and call CB on retrieval.
109You can provide a list of argument to pass to CB in CBARGS."
110 (let ((url (gravatar-build-url mail-address)))
111 (if (gravatar-cache-expired url)
cb51ba08
LI
112 (let ((args (list url
113 'gravatar-retrieved
114 (list cb (when cbargs cbargs)))))
115 (when (> (length (if (featurep 'xemacs)
116 (cdr (split-string (function-arglist 'url-retrieve)))
117 (help-function-arglist 'url-retrieve)))
118 4)
119 (setq args (nconc args (list t))))
120 (apply #'url-retrieve args))
61b1af82
G
121 (apply cb
122 (with-temp-buffer
123 (mm-disable-multibyte)
124 (url-cache-extract (url-cache-create-filename url))
125 (gravatar-data->image))
126 cbargs))))
127
70041e9a
G
128;;;###autoload
129(defun gravatar-retrieve-synchronously (mail-address)
130 "Retrieve MAIL-ADDRESS gravatar and returns it."
131 (let ((url (gravatar-build-url mail-address)))
132 (if (gravatar-cache-expired url)
1518e4f0
G
133 (with-current-buffer (if (featurep 'xemacs)
134 (url-retrieve url)
135 (url-retrieve-synchronously url))
136 (when gravatar-automatic-caching
70041e9a
G
137 (url-store-in-cache (current-buffer)))
138 (let ((data (gravatar-data->image)))
139 (kill-buffer (current-buffer))
140 data))
141 (with-temp-buffer
142 (mm-disable-multibyte)
143 (url-cache-extract (url-cache-create-filename url))
144 (gravatar-data->image)))))
145
146
61b1af82
G
147(defun gravatar-retrieved (status cb &optional cbargs)
148 "Callback function used by `gravatar-retrieve'."
149 ;; Store gravatar?
150 (when gravatar-automatic-caching
151 (url-store-in-cache (current-buffer)))
152 (if (plist-get status :error)
153 ;; Error happened.
154 (apply cb 'error cbargs)
71e691a5
G
155 (apply cb (gravatar-data->image) cbargs))
156 (kill-buffer (current-buffer)))
61b1af82
G
157
158(provide 'gravatar)
159
160;;; gravatar.el ends here