Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / gnus / gravatar.el
CommitLineData
61b1af82
G
1;;; gravatar.el --- Get Gravatars
2
acaf905b 3;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
61b1af82
G
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
61b1af82
G
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."
70041e9a
G
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))))))
61b1af82 86
4b36c6d4
KY
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
61b1af82
G
97(defun gravatar-data->image ()
98 "Get data of current buffer and return an image.
99If no image available, return 'error."
100 (let ((data (gravatar-get-data)))
101 (if data
70041e9a 102 (gravatar-create-image data nil t)
61b1af82
G
103 'error)))
104
105;;;###autoload
106(defun gravatar-retrieve (mail-address cb &optional cbargs)
107 "Retrieve MAIL-ADDRESS gravatar and call CB on retrieval.
108You 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)
cb51ba08
LI
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))
61b1af82
G
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))))
126
70041e9a
G
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)
1518e4f0
G
132 (with-current-buffer (if (featurep 'xemacs)
133 (url-retrieve url)
134 (url-retrieve-synchronously url))
135 (when gravatar-automatic-caching
70041e9a
G
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)))))
144
145
61b1af82
G
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)
71e691a5
G
154 (apply cb (gravatar-data->image) cbargs))
155 (kill-buffer (current-buffer)))
61b1af82
G
156
157(provide 'gravatar)
158
159;;; gravatar.el ends here