Doc fix for window-scroll-functions.
[emacs.git] / lisp / gnus / gravatar.el
blob4b0c9a16283210bd44c1a8a3e7280d91b4dc871d
1 ;;; gravatar.el --- Get Gravatars
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Keywords: news
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'url)
28 (require 'url-cache)
30 (defgroup gravatar nil
31 "Gravatar."
32 :group 'comm)
34 (defcustom gravatar-automatic-caching t
35 "Whether cache retrieved gravatar."
36 :group 'gravatar)
38 (defcustom gravatar-cache-ttl (days-to-time 30)
39 "Time to live for gravatar cache entries."
40 :group 'gravatar)
42 (defcustom gravatar-rating "g"
43 "Default rating for gravatar."
44 :group 'gravatar)
46 (defcustom gravatar-size 32
47 "Default size in pixels for gravatars."
48 :group 'gravatar)
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"
61 gravatar-base-url
62 (gravatar-hash mail-address)
63 gravatar-rating
64 gravatar-size))
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)))))
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))))))
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))
94 (require '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)))
101 (if data
102 (gravatar-create-image data nil t)
103 'error)))
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)))
118 (setq args (nconc args (list t))))
119 (apply #'url-retrieve args))
120 (apply cb
121 (with-temp-buffer
122 (mm-disable-multibyte)
123 (url-cache-extract (url-cache-create-filename url))
124 (gravatar-data->image))
125 cbargs))))
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 (if (featurep 'xemacs)
133 (url-retrieve url)
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))
139 data))
140 (with-temp-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'."
148 ;; Store gravatar?
149 (when gravatar-automatic-caching
150 (url-store-in-cache (current-buffer)))
151 (if (plist-get status :error)
152 ;; Error happened.
153 (apply cb 'error cbargs)
154 (apply cb (gravatar-data->image) cbargs))
155 (kill-buffer (current-buffer)))
157 (provide 'gravatar)
159 ;;; gravatar.el ends here