2010-09-24 22:33:34 +00:00
|
|
|
;;; gravatar.el --- Get Gravatars
|
|
|
|
|
2017-01-01 03:14:01 +00:00
|
|
|
;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
|
2010-09-24 22:33:34 +00:00
|
|
|
|
|
|
|
;; Author: Julien Danjou <julien@danjou.info>
|
|
|
|
;; Keywords: news
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'url)
|
|
|
|
(require 'url-cache)
|
2016-02-11 16:56:28 +11:00
|
|
|
(require 'image)
|
2010-09-24 22:33:34 +00:00
|
|
|
|
|
|
|
(defgroup gravatar nil
|
|
|
|
"Gravatar."
|
Add missing :version tags to new defgroups and defcustoms
* window.el (window-sides-slots):
* tool-bar.el (tool-bar-position):
* term/xterm.el (xterm-extra-capabilities):
* ses.el (ses-self-reference-early-detection):
* progmodes/verilog-mode.el (verilog-auto-declare-nettype)
(verilog-auto-wire-type)
(verilog-auto-delete-trailing-whitespace)
(verilog-auto-reset-blocking-in-non, verilog-auto-inst-sort)
(verilog-auto-tieoff-declaration):
* progmodes/sql.el (sql-login-hook, sql-ansi-statement-starters)
(sql-oracle-statement-starters, sql-oracle-scan-on):
* progmodes/prolog.el (prolog-align-comments-flag)
(prolog-indent-mline-comments-flag, prolog-object-end-to-0-flag)
(prolog-left-indent-regexp, prolog-paren-indent-p)
(prolog-paren-indent, prolog-parse-mode, prolog-keywords)
(prolog-types, prolog-mode-specificators)
(prolog-determinism-specificators, prolog-directives)
(prolog-electric-newline-flag, prolog-hungry-delete-key-flag)
(prolog-electric-dot-flag)
(prolog-electric-dot-full-predicate-template)
(prolog-electric-underscore-flag, prolog-electric-tab-flag)
(prolog-electric-if-then-else-flag, prolog-electric-colon-flag)
(prolog-electric-dash-flag, prolog-old-sicstus-keys-flag)
(prolog-program-switches, prolog-prompt-regexp)
(prolog-debug-on-string, prolog-debug-off-string)
(prolog-trace-on-string, prolog-trace-off-string)
(prolog-zip-on-string, prolog-zip-off-string)
(prolog-use-standard-consult-compile-method-flag)
(prolog-use-prolog-tokenizer-flag, prolog-imenu-flag)
(prolog-imenu-max-lines, prolog-info-predicate-index)
(prolog-underscore-wordchar-flag, prolog-use-sicstus-sd)
(prolog-char-quote-workaround):
* progmodes/cc-vars.el (c-defun-tactic):
* net/tramp.el (tramp-encoding-command-interactive)
(tramp-local-end-of-line):
* net/soap-client.el (soap-client):
* net/netrc.el (netrc-file):
* net/gnutls.el (gnutls):
* minibuffer.el (completion-category-overrides)
(completion-cycle-threshold)
(completion-pcm-complete-word-inserts-delimiters):
* man.el (Man-name-local-regexp):
* mail/feedmail.el (feedmail-display-full-frame):
* international/characters.el (glyphless-char-display-control):
* eshell/em-ls.el (eshell-ls-date-format):
* emacs-lisp/cl-indent.el (lisp-lambda-list-keyword-alignment)
(lisp-lambda-list-keyword-parameter-indentation)
(lisp-lambda-list-keyword-parameter-alignment):
* doc-view.el (doc-view-image-width, doc-view-unoconv-program):
* dired-x.el (dired-omit-verbose):
* cus-theme.el (custom-theme-allow-multiple-selections):
* calc/calc.el (calc-highlight-selections-with-faces)
(calc-lu-field-reference, calc-lu-power-reference)
(calc-note-threshold):
* battery.el (battery-mode-line-limit):
* arc-mode.el (archive-7z-extract, archive-7z-expunge)
(archive-7z-update):
* allout.el (allout-prefixed-keybindings)
(allout-unprefixed-keybindings)
(allout-inhibit-auto-fill-on-headline)
(allout-flattened-numbering-abbreviation):
* allout-widgets.el (allout-widgets-auto-activation)
(allout-widgets-icons-dark-subdir)
(allout-widgets-icons-light-subdir, allout-widgets-icon-types)
(allout-widgets-theme-dark-background)
(allout-widgets-theme-light-background)
(allout-widgets-item-image-properties-emacs)
(allout-widgets-item-image-properties-xemacs)
(allout-widgets-run-unit-tests-on-load)
(allout-widgets-time-decoration-activity)
(allout-widgets-hook-error-post-time)
(allout-widgets-track-decoration):
* gnus/sieve-manage.el (sieve-manage-default-stream):
* gnus/shr.el (shr):
* gnus/nnir.el (nnir-ignored-newsgroups, nnir-summary-line-format)
(nnir-retrieve-headers-override-function)
(nnir-imap-default-search-key, nnir-notmuch-program)
(nnir-notmuch-additional-switches, nnir-notmuch-remove-prefix)
(nnir-method-default-engines):
* gnus/message.el (message-cite-reply-position):
* gnus/gssapi.el (gssapi-program):
* gnus/gravatar.el (gravatar):
* gnus/gnus-sum.el (gnus-refer-thread-use-nnir):
* gnus/gnus-registry.el (gnus-registry-unfollowed-addresses)
(gnus-registry-max-pruned-entries):
* gnus/gnus-picon.el (gnus-picon-inhibit-top-level-domains):
* gnus/gnus-int.el (gnus-after-set-mark-hook)
(gnus-before-update-mark-hook):
* gnus/gnus-async.el (gnus-async-post-fetch-function):
* gnus/auth-source.el (auth-source-cache-expiry):
Add missing :version tags to new defcustoms and defgroups.
2012-02-11 14:13:29 -08:00
|
|
|
:version "24.1"
|
2010-09-24 22:33:34 +00:00
|
|
|
:group 'comm)
|
|
|
|
|
|
|
|
(defcustom gravatar-automatic-caching t
|
2013-12-28 00:21:33 -08:00
|
|
|
"Whether to cache retrieved gravatars."
|
|
|
|
:type 'boolean
|
2010-09-24 22:33:34 +00:00
|
|
|
:group 'gravatar)
|
|
|
|
|
2013-12-28 00:21:33 -08:00
|
|
|
;; FIXME a time value is not the nicest format for a custom variable.
|
2010-09-24 22:33:34 +00:00
|
|
|
(defcustom gravatar-cache-ttl (days-to-time 30)
|
|
|
|
"Time to live for gravatar cache entries."
|
2013-12-28 00:21:33 -08:00
|
|
|
:type '(repeat integer)
|
2010-09-24 22:33:34 +00:00
|
|
|
:group 'gravatar)
|
|
|
|
|
2013-12-28 00:21:33 -08:00
|
|
|
;; FIXME Doc is tautological. What are the options?
|
2010-09-24 22:33:34 +00:00
|
|
|
(defcustom gravatar-rating "g"
|
|
|
|
"Default rating for gravatar."
|
2013-12-28 00:21:33 -08:00
|
|
|
:type 'string
|
2010-09-24 22:33:34 +00:00
|
|
|
:group 'gravatar)
|
|
|
|
|
|
|
|
(defcustom gravatar-size 32
|
|
|
|
"Default size in pixels for gravatars."
|
2013-12-28 00:21:33 -08:00
|
|
|
:type 'integer
|
2010-09-24 22:33:34 +00:00
|
|
|
:group 'gravatar)
|
|
|
|
|
|
|
|
(defconst gravatar-base-url
|
|
|
|
"http://www.gravatar.com/avatar"
|
|
|
|
"Base URL for getting gravatars.")
|
|
|
|
|
|
|
|
(defun gravatar-hash (mail-address)
|
|
|
|
"Create an hash from MAIL-ADDRESS."
|
|
|
|
(md5 (downcase mail-address)))
|
|
|
|
|
|
|
|
(defun gravatar-build-url (mail-address)
|
|
|
|
"Return an URL to retrieve MAIL-ADDRESS gravatar."
|
|
|
|
(format "%s/%s?d=404&r=%s&s=%d"
|
|
|
|
gravatar-base-url
|
|
|
|
(gravatar-hash mail-address)
|
|
|
|
gravatar-rating
|
|
|
|
gravatar-size))
|
|
|
|
|
|
|
|
(defun gravatar-cache-expired (url)
|
|
|
|
"Check if URL is cached for more than `gravatar-cache-ttl'."
|
|
|
|
(cond (url-standalone-mode
|
|
|
|
(not (file-exists-p (url-cache-create-filename url))))
|
|
|
|
(t (let ((cache-time (url-is-cached url)))
|
|
|
|
(if cache-time
|
|
|
|
(time-less-p
|
|
|
|
(time-add
|
|
|
|
cache-time
|
|
|
|
gravatar-cache-ttl)
|
|
|
|
(current-time))
|
|
|
|
t)))))
|
|
|
|
|
|
|
|
(defun gravatar-get-data ()
|
|
|
|
"Get data from current buffer."
|
2010-12-17 22:45:16 +00:00
|
|
|
(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))))))
|
2010-09-24 22:33:34 +00:00
|
|
|
|
|
|
|
(defun gravatar-data->image ()
|
|
|
|
"Get data of current buffer and return an image.
|
|
|
|
If no image available, return 'error."
|
|
|
|
(let ((data (gravatar-get-data)))
|
|
|
|
(if data
|
2016-02-11 16:56:28 +11:00
|
|
|
(create-image data nil t)
|
2010-09-24 22:33:34 +00:00
|
|
|
'error)))
|
|
|
|
|
2013-05-28 19:40:43 -07:00
|
|
|
(autoload 'help-function-arglist "help-fns")
|
|
|
|
|
2010-09-24 22:33:34 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defun gravatar-retrieve (mail-address cb &optional cbargs)
|
|
|
|
"Retrieve MAIL-ADDRESS gravatar and call CB on retrieval.
|
|
|
|
You can provide a list of argument to pass to CB in CBARGS."
|
|
|
|
(let ((url (gravatar-build-url mail-address)))
|
|
|
|
(if (gravatar-cache-expired url)
|
2011-01-24 04:10:19 +00:00
|
|
|
(let ((args (list url
|
|
|
|
'gravatar-retrieved
|
|
|
|
(list cb (when cbargs cbargs)))))
|
|
|
|
(when (> (length (if (featurep 'xemacs)
|
|
|
|
(cdr (split-string (function-arglist 'url-retrieve)))
|
|
|
|
(help-function-arglist 'url-retrieve)))
|
|
|
|
4)
|
|
|
|
(setq args (nconc args (list t))))
|
|
|
|
(apply #'url-retrieve args))
|
2010-09-24 22:33:34 +00:00
|
|
|
(apply cb
|
|
|
|
(with-temp-buffer
|
2016-02-24 13:07:34 +11:00
|
|
|
(set-buffer-multibyte nil)
|
2010-09-24 22:33:34 +00:00
|
|
|
(url-cache-extract (url-cache-create-filename url))
|
|
|
|
(gravatar-data->image))
|
|
|
|
cbargs))))
|
|
|
|
|
2010-12-17 22:45:16 +00:00
|
|
|
;;;###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)
|
2014-03-23 23:13:36 +00:00
|
|
|
(with-current-buffer (url-retrieve-synchronously url)
|
2011-03-18 13:45:04 +00:00
|
|
|
(when gravatar-automatic-caching
|
2010-12-17 22:45:16 +00:00
|
|
|
(url-store-in-cache (current-buffer)))
|
|
|
|
(let ((data (gravatar-data->image)))
|
|
|
|
(kill-buffer (current-buffer))
|
|
|
|
data))
|
|
|
|
(with-temp-buffer
|
2016-02-24 13:07:34 +11:00
|
|
|
(set-buffer-multibyte nil)
|
2010-12-17 22:45:16 +00:00
|
|
|
(url-cache-extract (url-cache-create-filename url))
|
|
|
|
(gravatar-data->image)))))
|
|
|
|
|
|
|
|
|
2010-09-24 22:33:34 +00:00
|
|
|
(defun gravatar-retrieved (status cb &optional cbargs)
|
|
|
|
"Callback function used by `gravatar-retrieve'."
|
|
|
|
;; Store gravatar?
|
|
|
|
(when gravatar-automatic-caching
|
|
|
|
(url-store-in-cache (current-buffer)))
|
|
|
|
(if (plist-get status :error)
|
|
|
|
;; Error happened.
|
|
|
|
(apply cb 'error cbargs)
|
2010-10-04 22:26:51 +00:00
|
|
|
(apply cb (gravatar-data->image) cbargs))
|
|
|
|
(kill-buffer (current-buffer)))
|
2010-09-24 22:33:34 +00:00
|
|
|
|
|
|
|
(provide 'gravatar)
|
|
|
|
|
|
|
|
;;; gravatar.el ends here
|