Commit | Line | Data |
---|---|---|
61b1af82 G |
1 | ;;; gravatar.el --- Get Gravatars |
2 | ||
3 | ;; Copyright (C) 2010 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 | ||
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." | |
81 | (when (string-match "^HTTP/.+ 200 OK$" | |
82 | (buffer-substring (point-min) (line-end-position))) | |
83 | (when (search-forward "\n\n" nil t) | |
84 | (buffer-substring (point) (point-max))))) | |
85 | ||
4b36c6d4 KY |
86 | (eval-and-compile |
87 | (cond ((featurep 'xemacs) | |
88 | (require 'gnus-xmas) | |
89 | (defalias 'gravatar-create-image 'gnus-xmas-create-image)) | |
90 | ((featurep 'gnus-ems) | |
91 | (defalias 'gravatar-create-image 'gnus-create-image)) | |
92 | (t | |
93 | (require 'image) | |
94 | (defalias 'gravatar-create-image 'create-image)))) | |
95 | ||
61b1af82 G |
96 | (defun gravatar-data->image () |
97 | "Get data of current buffer and return an image. | |
98 | If no image available, return 'error." | |
99 | (let ((data (gravatar-get-data))) | |
100 | (if data | |
4b36c6d4 | 101 | (gravatar-create-image data nil t) |
61b1af82 G |
102 | 'error))) |
103 | ||
104 | ;;;###autoload | |
105 | (defun gravatar-retrieve (mail-address cb &optional cbargs) | |
106 | "Retrieve MAIL-ADDRESS gravatar and call CB on retrieval. | |
107 | You can provide a list of argument to pass to CB in CBARGS." | |
108 | (let ((url (gravatar-build-url mail-address))) | |
109 | (if (gravatar-cache-expired url) | |
110 | (url-retrieve url | |
111 | 'gravatar-retrieved | |
112 | (list cb (when cbargs cbargs))) | |
113 | (apply cb | |
114 | (with-temp-buffer | |
115 | (mm-disable-multibyte) | |
116 | (url-cache-extract (url-cache-create-filename url)) | |
117 | (gravatar-data->image)) | |
118 | cbargs)))) | |
119 | ||
120 | (defun gravatar-retrieved (status cb &optional cbargs) | |
121 | "Callback function used by `gravatar-retrieve'." | |
122 | ;; Store gravatar? | |
123 | (when gravatar-automatic-caching | |
124 | (url-store-in-cache (current-buffer))) | |
125 | (if (plist-get status :error) | |
126 | ;; Error happened. | |
127 | (apply cb 'error cbargs) | |
71e691a5 G |
128 | (apply cb (gravatar-data->image) cbargs)) |
129 | (kill-buffer (current-buffer))) | |
61b1af82 G |
130 | |
131 | (provide 'gravatar) | |
132 | ||
133 | ;;; gravatar.el ends here |