From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Make `smtpmail-try-auth-method' a generic function. Date: Thu, 11 Jan 2018 09:09:29 -0500 Message-ID: References: <87373c3olr.fsf@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1515679728 11204 195.159.176.226 (11 Jan 2018 14:08:48 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 11 Jan 2018 14:08:48 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Jan 11 15:08:44 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eZdWx-0001nU-DM for ged-emacs-devel@m.gmane.org; Thu, 11 Jan 2018 15:08:31 +0100 Original-Received: from localhost ([::1]:45989 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZdYw-0008TM-V6 for ged-emacs-devel@m.gmane.org; Thu, 11 Jan 2018 09:10:34 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:46185) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZdYE-0008RG-0a for emacs-devel@gnu.org; Thu, 11 Jan 2018 09:09:51 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZdY9-0008GN-Ty for emacs-devel@gnu.org; Thu, 11 Jan 2018 09:09:49 -0500 Original-Received: from [195.159.176.226] (port=51418 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eZdY9-0008B7-MP for emacs-devel@gnu.org; Thu, 11 Jan 2018 09:09:45 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1eZdW6-0006oQ-M8 for emacs-devel@gnu.org; Thu, 11 Jan 2018 15:07:38 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 71 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:fOB9plCY/yL7lO6dgluJ+sEDRyw= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:221832 Archived-At: I can install that, but I'm not sure if it should go into emacs-26 or master. Stefan Cesar Crusius writes: > This allows for extra authentication mechanisms (such as XOAuth2) to > be added without having to advise any functions. > > * lisp/mail/smtpmail.el: Turn `smtpmail-try-auth-method' into a > generic function. > --- > lisp/mail/smtpmail.el | 33 +++++++++++++++++++++++++++++++-- > 1 file changed, 31 insertions(+), 2 deletions(-) > > diff --git a/lisp/mail/smtpmail.el b/lisp/mail/smtpmail.el > index 20cbeb5f4e..46c1639832 100644 > --- a/lisp/mail/smtpmail.el > +++ b/lisp/mail/smtpmail.el > @@ -181,7 +181,25 @@ This is relative to `smtpmail-queue-dir'." > > (defconst smtpmail-auth-supported '(cram-md5 plain login) > "List of supported SMTP AUTH mechanisms. > -The list is in preference order.") > +The list is in preference order. > + > +New mechanisms can be added by defining a method implementing the > +`smtpmail-try-auth-method' generic function for that mechanism, and > +then adding the mechanism to this list. As an example, the code below > +adds support for XOAuth2 authentication (assuming that there is an > +appropriate auth source extension that can provide the correct user > +and password): > + > +\(cl-defmethod smtpmail-try-auth-method > + (process (mech (eql xoauth2)) user password) > + (smtpmail-command-or-throw > + process > + (concat \"AUTH XOAUTH2 \" > + (base64-encode-string > + (concat \"user=\" user \"\\1auth=Bearer \" password \"\\1\\1\") t)) > + 235)) > + > +\(add-to-list 'smtpmail-try-auth-method 'xoauth2)") > > (defvar smtpmail-mail-address nil > "Value to use for envelope-from address for mail from ambient buffer.") > @@ -539,7 +557,18 @@ The list is in preference order.") > (funcall save-function)) > result)))) > > -(defun smtpmail-try-auth-method (process mech user password) > +(cl-defgeneric smtpmail-try-auth-method (process mech user password) > + "Try to authenticate to an SMTP process. > +Authenticate to PROCESS using the MECH authentication mechanism, with > +the given USER and PASSWORD. Implementations should either return the > +result code from the authentication command, or throw 'done with an > +error string. (This is usually done by calling > +`smtpmail-command-or-throw' as the last step in the function.)") > + > +(cl-defmethod smtpmail-try-auth-method (process mech user password) > + "Default `smtpmail-try-auth-method' implementation. > +This implementation provides support for the SMTP authentication > +mechanisms 'cram-md5, 'login, and 'plain." > (let (ret) > (cond > ((or (not mech) > -- > 2.15.0