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