1 ;;; gravatar.el --- Get Gravatars
3 ;; Copyright (C) 2010-2011 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/>.
30 (defgroup gravatar nil
34 (defcustom gravatar-automatic-caching t
35 "Whether cache retrieved gravatar."
38 (defcustom gravatar-cache-ttl
(days-to-time 30)
39 "Time to live for gravatar cache entries."
42 (defcustom gravatar-rating
"g"
43 "Default rating for gravatar."
46 (defcustom gravatar-size
32
47 "Default size in pixels for gravatars."
50 (defconst gravatar-base-url
51 "http://www.gravatar.com/avatar"
52 "Base URL for getting gravatars.")
54 (defun gravatar-hash (mail-address)
55 "Create an hash from MAIL-ADDRESS."
56 (md5 (downcase mail-address
)))
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"
62 (gravatar-hash mail-address
)
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
)))
79 (defun gravatar-get-data ()
80 "Get data from current buffer."
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))))))
88 (cond ((featurep 'xemacs
)
90 (defalias 'gravatar-create-image
'gnus-xmas-create-image
))
92 (defalias 'gravatar-create-image
'gnus-create-image
))
95 (defalias 'gravatar-create-image
'create-image
))))
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)))
102 (gravatar-create-image data nil t
)
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 (mm-disable-multibyte)
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 (if (featurep 'xemacs
)
134 (url-retrieve-synchronously url
))
135 (when gravatar-automatic-caching
136 (url-store-in-cache (current-buffer)))
137 (let ((data (gravatar-data->image
)))
138 (kill-buffer (current-buffer))
141 (mm-disable-multibyte)
142 (url-cache-extract (url-cache-create-filename url
))
143 (gravatar-data->image
)))))
146 (defun gravatar-retrieved (status cb
&optional cbargs
)
147 "Callback function used by `gravatar-retrieve'."
149 (when gravatar-automatic-caching
150 (url-store-in-cache (current-buffer)))
151 (if (plist-get status
:error
)
153 (apply cb
'error cbargs
)
154 (apply cb
(gravatar-data->image
) cbargs
))
155 (kill-buffer (current-buffer)))
159 ;;; gravatar.el ends here