gravatar: get-data more robust, sync function
authorJulien Danjou <julien@danjou.info>
Fri, 17 Dec 2010 15:05:13 +0000 (16:05 +0100)
committerJulien Danjou <julien@danjou.info>
Fri, 17 Dec 2010 15:06:28 +0000 (16:06 +0100)
Signed-off-by: Julien Danjou <julien@danjou.info>
lisp/ChangeLog
lisp/gravatar.el

index 8d38869..9d85005 100644 (file)
@@ -5,6 +5,11 @@
 
        * nnir.el (nnir-get-active): Ignore nnir-ignored-newsgroups if null.
 
+2010-12-17  Julien Danjou  <julien@danjou.info>
+
+       * gravatar.el (gravatar-retrieve-synchronously): New function.
+       (gravatar-get-data): Make more robust.
+
 2010-12-16  Katsumi Yamaoka  <yamaoka@jpl.org>
 
        * lpath.el: Bind epa-file-encrypt-to for Emacs 22 and XEmacs.
index 50b0ba1..c17bd20 100644 (file)
 
 (defun gravatar-get-data ()
   "Get data from current buffer."
-  (when (string-match "^HTTP/.+ 200 OK$"
-                      (buffer-substring (point-min) (line-end-position)))
-    (when (search-forward "\n\n" nil t)
-      (buffer-substring (point) (point-max)))))
+  (save-excursion
+    (goto-char (point-min))
+    (when (re-search-forward "^HTTP/.+ 200 OK$" nil (line-end-position))
+      (when (search-forward "\n\n" nil t)
+        (buffer-substring (point) (point-max))))))
 
 (eval-and-compile
   (cond ((featurep 'xemacs)
@@ -98,7 +99,7 @@
 If no image available, return 'error."
   (let ((data (gravatar-get-data)))
     (if data
-       (gravatar-create-image data  nil t)
+       (gravatar-create-image data nil t)
       'error)))
 
 ;;;###autoload
@@ -117,6 +118,23 @@ You can provide a list of argument to pass to CB in CBARGS."
                  (gravatar-data->image))
                cbargs))))
 
+;;;###autoload
+(defun gravatar-retrieve-synchronously (mail-address)
+  "Retrieve MAIL-ADDRESS gravatar and returns it."
+  (let ((url (gravatar-build-url mail-address)))
+    (if (gravatar-cache-expired url)
+        (with-current-buffer (url-retrieve-synchronously url)
+          (when gravatar-automatic-caching
+            (url-store-in-cache (current-buffer)))
+          (let ((data (gravatar-data->image)))
+            (kill-buffer (current-buffer))
+            data))
+      (with-temp-buffer
+        (mm-disable-multibyte)
+        (url-cache-extract (url-cache-create-filename url))
+        (gravatar-data->image)))))
+
+
 (defun gravatar-retrieved (status cb &optional cbargs)
   "Callback function used by `gravatar-retrieve'."
   ;; Store gravatar?