From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Philip K." Newsgroups: gmane.emacs.bugs Subject: bug#40354: [PATCH] Fix Libravatar federation handling Date: Tue, 18 Aug 2020 20:53:37 +0200 Message-ID: <87y2mb3hvy.fsf@posteo.net> References: <20200331180324.2943-1-philip@warpmail.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21612"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 40354@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Aug 18 20:54:10 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k86kM-0005Ua-O1 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 18 Aug 2020 20:54:10 +0200 Original-Received: from localhost ([::1]:35212 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k86kL-0003e0-QL for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 18 Aug 2020 14:54:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51498) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k86kE-0003cs-LJ for bug-gnu-emacs@gnu.org; Tue, 18 Aug 2020 14:54:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:53698) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1k86kE-0001Wr-CM for bug-gnu-emacs@gnu.org; Tue, 18 Aug 2020 14:54:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1k86kE-0005s6-Bk for bug-gnu-emacs@gnu.org; Tue, 18 Aug 2020 14:54:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: "Philip K." Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 18 Aug 2020 18:54:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 40354 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 40354-submit@debbugs.gnu.org id=B40354.159777683822560 (code B ref 40354); Tue, 18 Aug 2020 18:54:02 +0000 Original-Received: (at 40354) by debbugs.gnu.org; 18 Aug 2020 18:53:58 +0000 Original-Received: from localhost ([127.0.0.1]:37011 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k86k3-0005rh-Ag for submit@debbugs.gnu.org; Tue, 18 Aug 2020 14:53:58 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:33993) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1k86jx-0005rJ-6y for 40354@debbugs.gnu.org; Tue, 18 Aug 2020 14:53:49 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id C3EA116005F for <40354@debbugs.gnu.org>; Tue, 18 Aug 2020 20:53:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1597776818; bh=iTwCaTZHUWSvIvmvUu8WfKd4ZIV8H/gxOwns+qkE9UI=; h=From:To:Cc:Subject:Date:From; b=h2s4m1d0+geZUuDUFPySdydyvITtfw+Mlohw+e6/lGtFU9TF2M5L6l8ggozAjJIlR uhZpmwO8DK4jsLJOL/OvBrSxNPGbTpimkM1DW9gh0LCpirbnwIMkxyJDLYCfopeyep TqkcpYoZ+9nC2aangU6hNl/47mdyKEsHUs/KJOfqeSnxFDMQU37oO7nvtV+GaTgxMX vYTkMvTx4SRcOrqQdMGJILulghqEWEJg95SHuOErDXsUK1kAEOjKvQ8O504fF7OJcq M9Elbsrnqo+xX6Ml8+vjLdWvzoINnk8xfVO0xCWBu3UDzWtMjqR+2DUvoYxix2ODr3 +9D4B75J3eohA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4BWKnG19X8z6tmX; Tue, 18 Aug 2020 20:53:38 +0200 (CEST) In-Reply-To: <87o8n8j9g7.fsf@gnus.org> (message from Lars Ingebrigtsen on Tue, 18 Aug 2020 16:49:12 +0200) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:185540 Archived-At: --=-=-= Content-Type: text/plain Lars Ingebrigtsen writes: > Lars Ingebrigtsen writes: > >> Philip K writes: >> >>> Previous implementation didn't correctly handle the result of >>> dns-query, and didn't implement the necessary record selection >>> algorithm as specified on the wiki (https://wiki.libravatar.org/api/, >>> "Federated servers"). >>> >>> * lisp/image/gravatar.el (gravatar--service-libravatar): Fix >>> libravatar image host resolver algorithm. >> >> Looks good to me, although this didn't apply either, since the code has >> been rewritten to be asynchronous. Could you re-spin this patch, too, >> and I'll get it applied... > > Philip, have you had time to look at this? The patch below should implement the fixes from bug#40354. I found a libravatar user here[0], and could test it by evaluating (gravatar--service-libravatar "who@zerokspot.com" #'message) or (gravatar--service-libravatar "who@gnu.org" #'message) as an example for a domain that doesn't use libravatar. Both work as expected, and don't lag. [0]: https://zerokspot.com/weblog/2020/05/26/avatars-without-gravatar/ -- Philip K. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Fix-Libravatar-federation-handling.patch >From 9b345b965cce8f79aa31b6a8989ff466c84b6798 Mon Sep 17 00:00:00 2001 From: Philip K Date: Tue, 18 Aug 2020 20:50:07 +0200 Subject: [PATCH] Fix Libravatar federation handling Previous implementation didn't correctly handle the result of dns-query, and didn't implement the necessary record selection algorithm as specified on the wiki (https://wiki.libravatar.org/api/, "Federated servers"). * lisp/image/gravatar.el (gravatar--service-libravatar): Implement correct algorithm --- lisp/image/gravatar.el | 49 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el index e917033562..cca737656c 100644 --- a/lisp/image/gravatar.el +++ b/lisp/image/gravatar.el @@ -158,18 +158,53 @@ gravatar--service-libravatar (setq func (lambda (result) (cond - (result - (funcall callback (format "%s://%s/avatar" - (cdar records) result))) - ((> (length records) 1) - (pop records) + ((and result ;there is a result + (let* ((data (mapcar (lambda (record) + (dns-get 'data (cdr record))) + (dns-get 'answers result))) + (priorities (mapcar (lambda (r) (dns-get 'priority r)) + data)) + (max-priority (if priorities + (apply #'max priorities) + 0)) + (sum 0) top) + ;; Attempt to find all records with the same maximal + ;; priority, and calculate the sum of their weights. + (dolist (ent data) + (when (= max-priority (dns-get 'priority ent)) + (setq sum (+ sum (dns-get 'weight ent))) + (push ent top))) + ;; In case there is more than one maximal priority + ;; record, choose one at random, while taking the + ;; individual record weights into consideration. + (catch 'done + (dolist (ent top) + (when (and (or (= 0 sum) + (<= 0 (random sum) (dns-get 'weight ent))) + ;; Ensure that port and domain data are + ;; valid. In case non of the results + ;; were valid, `catch' will evaluate to + ;; nil, and the next cond clause will be + ;; tested. + (<= 1 (dns-get 'port ent) 65535) + (string-match-p "\\`[-.0-9A-Za-z]+\\'" + (dns-get 'target ent))) + (funcall callback + (url-normalize-url (format "%s://%s:%s/avatar" + (cdar records) + (dns-get 'target ent) + (dns-get 'port ent)))) + (throw 'done t)) + (setq sum (- sum (dns-get 'weight ent)))))))) + ((setq records (cdr records)) ;in case there are at least two methods (dns-query-asynchronous (concat (caar records) "._tcp." domain) func 'SRV)) - (t + (t ;fallback (funcall callback "https://seccdn.libravatar.org/avatar"))))) (dns-query-asynchronous - (concat (caar records) "._tcp." domain) func 'SRV))))) + (concat (caar records) "._tcp." domain) + func 'SRV t))))) (defun gravatar-hash (mail-address) "Return the Gravatar hash for MAIL-ADDRESS." -- 2.26.2 --=-=-=--