1 ;;; gravatar.el --- Get Gravatars
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
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/>.
31 (defgroup gravatar nil
36 (defcustom gravatar-automatic-caching t
37 "Whether to cache retrieved gravatars."
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
)
47 ;; FIXME Doc is tautological. What are the options?
48 (defcustom gravatar-rating
"g"
49 "Default rating for gravatar."
53 (defcustom gravatar-size
32
54 "Default size in pixels for gravatars."
58 (defconst gravatar-base-url
59 "http://www.gravatar.com/avatar"
60 "Base URL for getting gravatars.")
62 (defun gravatar-hash (mail-address)
63 "Create an hash from MAIL-ADDRESS."
64 (md5 (downcase mail-address
)))
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"
70 (gravatar-hash mail-address
)
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
)))
87 (defun gravatar-get-data ()
88 "Get data from current buffer."
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))))))
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)))
100 (create-image data nil t
)
103 (autoload 'help-function-arglist
"help-fns")
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
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
)))
118 (setq args
(nconc args
(list t
))))
119 (apply #'url-retrieve args
))
122 (set-buffer-multibyte nil
)
123 (url-cache-extract (url-cache-create-filename url
))
124 (gravatar-data->image
))
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))
139 (set-buffer-multibyte nil
)
140 (url-cache-extract (url-cache-create-filename url
))
141 (gravatar-data->image
)))))
144 (defun gravatar-retrieved (status cb
&optional cbargs
)
145 "Callback function used by `gravatar-retrieve'."
147 (when gravatar-automatic-caching
148 (url-store-in-cache (current-buffer)))
149 (if (plist-get status
:error
)
151 (apply cb
'error cbargs
)
152 (apply cb
(gravatar-data->image
) cbargs
))
153 (kill-buffer (current-buffer)))
157 ;;; gravatar.el ends here