]> code.delx.au - gnu-emacs/blob - lisp/image/gravatar.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / image / gravatar.el
1 ;;; gravatar.el --- Get Gravatars
2
3 ;; Copyright (C) 2010-2016 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 (require 'image)
30
31 (defgroup gravatar nil
32 "Gravatar."
33 :version "24.1"
34 :group 'comm)
35
36 (defcustom gravatar-automatic-caching t
37 "Whether to cache retrieved gravatars."
38 :type 'boolean
39 :group 'gravatar)
40
41 ;; FIXME a time value is not the nicest format for a custom variable.
42 (defcustom gravatar-cache-ttl (days-to-time 30)
43 "Time to live for gravatar cache entries."
44 :type '(repeat integer)
45 :group 'gravatar)
46
47 ;; FIXME Doc is tautological. What are the options?
48 (defcustom gravatar-rating "g"
49 "Default rating for gravatar."
50 :type 'string
51 :group 'gravatar)
52
53 (defcustom gravatar-size 32
54 "Default size in pixels for gravatars."
55 :type 'integer
56 :group 'gravatar)
57
58 (defconst gravatar-base-url
59 "http://www.gravatar.com/avatar"
60 "Base URL for getting gravatars.")
61
62 (defun gravatar-hash (mail-address)
63 "Create an hash from MAIL-ADDRESS."
64 (md5 (downcase mail-address)))
65
66 (defun gravatar-build-url (mail-address)
67 "Return an URL to retrieve MAIL-ADDRESS gravatar."
68 (format "%s/%s?d=404&r=%s&s=%d"
69 gravatar-base-url
70 (gravatar-hash mail-address)
71 gravatar-rating
72 gravatar-size))
73
74 (defun gravatar-cache-expired (url)
75 "Check if URL is cached for more than `gravatar-cache-ttl'."
76 (cond (url-standalone-mode
77 (not (file-exists-p (url-cache-create-filename url))))
78 (t (let ((cache-time (url-is-cached url)))
79 (if cache-time
80 (time-less-p
81 (time-add
82 cache-time
83 gravatar-cache-ttl)
84 (current-time))
85 t)))))
86
87 (defun gravatar-get-data ()
88 "Get data from current buffer."
89 (save-excursion
90 (goto-char (point-min))
91 (when (re-search-forward "^HTTP/.+ 200 OK$" nil (line-end-position))
92 (when (search-forward "\n\n" nil t)
93 (buffer-substring (point) (point-max))))))
94
95 (defun gravatar-data->image ()
96 "Get data of current buffer and return an image.
97 If no image available, return 'error."
98 (let ((data (gravatar-get-data)))
99 (if data
100 (create-image data nil t)
101 'error)))
102
103 (autoload 'help-function-arglist "help-fns")
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 (let ((args (list url
112 'gravatar-retrieved
113 (list cb (when cbargs cbargs)))))
114 (when (> (length (if (featurep 'xemacs)
115 (cdr (split-string (function-arglist 'url-retrieve)))
116 (help-function-arglist 'url-retrieve)))
117 4)
118 (setq args (nconc args (list t))))
119 (apply #'url-retrieve args))
120 (apply cb
121 (with-temp-buffer
122 (set-buffer-multibyte nil)
123 (url-cache-extract (url-cache-create-filename url))
124 (gravatar-data->image))
125 cbargs))))
126
127 ;;;###autoload
128 (defun gravatar-retrieve-synchronously (mail-address)
129 "Retrieve MAIL-ADDRESS gravatar and returns it."
130 (let ((url (gravatar-build-url mail-address)))
131 (if (gravatar-cache-expired url)
132 (with-current-buffer (url-retrieve-synchronously url)
133 (when gravatar-automatic-caching
134 (url-store-in-cache (current-buffer)))
135 (let ((data (gravatar-data->image)))
136 (kill-buffer (current-buffer))
137 data))
138 (with-temp-buffer
139 (set-buffer-multibyte nil)
140 (url-cache-extract (url-cache-create-filename url))
141 (gravatar-data->image)))))
142
143
144 (defun gravatar-retrieved (status cb &optional cbargs)
145 "Callback function used by `gravatar-retrieve'."
146 ;; Store gravatar?
147 (when gravatar-automatic-caching
148 (url-store-in-cache (current-buffer)))
149 (if (plist-get status :error)
150 ;; Error happened.
151 (apply cb 'error cbargs)
152 (apply cb (gravatar-data->image) cbargs))
153 (kill-buffer (current-buffer)))
154
155 (provide 'gravatar)
156
157 ;;; gravatar.el ends here