unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Make `smtpmail-try-auth-method' a generic function.
@ 2018-01-11  6:18 Cesar Crusius
  2018-01-11 14:09 ` Stefan Monnier
  2018-01-11 14:42 ` Robert Pluim
  0 siblings, 2 replies; 9+ messages in thread
From: Cesar Crusius @ 2018-01-11  6:18 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2507 bytes --]

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

-- 
Cesar Crusius

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 658 bytes --]

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11  6:18 [PATCH] Make `smtpmail-try-auth-method' a generic function Cesar Crusius
@ 2018-01-11 14:09 ` Stefan Monnier
  2018-01-11 14:54   ` Cesar Crusius
  2018-01-11 16:11   ` Eli Zaretskii
  2018-01-11 14:42 ` Robert Pluim
  1 sibling, 2 replies; 9+ messages in thread
From: Stefan Monnier @ 2018-01-11 14:09 UTC (permalink / raw)
  To: emacs-devel

I can install that, but I'm not sure if it should go into
emacs-26 or master.


        Stefan


Cesar Crusius <cesar.crusius@gmail.com> 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




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11  6:18 [PATCH] Make `smtpmail-try-auth-method' a generic function Cesar Crusius
  2018-01-11 14:09 ` Stefan Monnier
@ 2018-01-11 14:42 ` Robert Pluim
  2018-01-11 17:00   ` Cesar Crusius
  1 sibling, 1 reply; 9+ messages in thread
From: Robert Pluim @ 2018-01-11 14:42 UTC (permalink / raw)
  To: Cesar Crusius; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1805 bytes --]

Cesar Crusius <cesar.crusius@gmail.com> 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)")
                  ^^^^^^^^^^^^^^^^^^^^^^^^
This should say smtpmail-auth-supported, no?

FWIW, I've tried your patch, and it doesn't break anything for me with
gmail, although I haven't tried enabling XOAuth2 yet.

Robert

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 818 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11 14:09 ` Stefan Monnier
@ 2018-01-11 14:54   ` Cesar Crusius
  2018-01-11 16:11   ` Eli Zaretskii
  1 sibling, 0 replies; 9+ messages in thread
From: Cesar Crusius @ 2018-01-11 14:54 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 431 bytes --]


On Thu 11 Jan 2018 at 14:09, Stefan Monnier wrote:

> I can install that, but I'm not sure if it should go into
> emacs-26 or master.

My vote would be for 26, as there is no code logic changed here. Up to
you and the others, though, as I may be missing other reasons.

On another topic, if/when you install it, could you use
"ccrusius@google.com" as the author (requirement from my job)? Thanks!

-- 
Cesar Crusius

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 658 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11 14:09 ` Stefan Monnier
  2018-01-11 14:54   ` Cesar Crusius
@ 2018-01-11 16:11   ` Eli Zaretskii
  2018-01-11 16:57     ` Stefan Monnier
  1 sibling, 1 reply; 9+ messages in thread
From: Eli Zaretskii @ 2018-01-11 16:11 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

> From: Stefan Monnier <monnier@iro.umontreal.ca>
> Date: Thu, 11 Jan 2018 09:09:29 -0500
> 
> I can install that, but I'm not sure if it should go into
> emacs-26 or master.

Master, please.  This is too much for the release branch, and smtpmail
is a very important package used by many Emacs email-related features.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11 16:11   ` Eli Zaretskii
@ 2018-01-11 16:57     ` Stefan Monnier
  0 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2018-01-11 16:57 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

>> I can install that, but I'm not sure if it should go into
>> emacs-26 or master.
> Master, please.

Thanks, done.


        Stefan



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11 14:42 ` Robert Pluim
@ 2018-01-11 17:00   ` Cesar Crusius
  2018-01-11 19:33     ` Stefan Monnier
  0 siblings, 1 reply; 9+ messages in thread
From: Cesar Crusius @ 2018-01-11 17:00 UTC (permalink / raw)
  To: Robert Pluim; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 650 bytes --]


On Thu 11 Jan 2018 at 14:42, Robert Pluim wrote:

> Cesar Crusius <cesar.crusius@gmail.com> writes:
>
>> (snip)
>> +
>> +\(add-to-list 'smtpmail-try-auth-method 'xoauth2)")
>                   ^^^^^^^^^^^^^^^^^^^^^^^^
> This should say smtpmail-auth-supported, no?

Oops, yes. Sorry about that.

> FWIW, I've tried your patch, and it doesn't break anything for me with
> gmail, although I haven't tried enabling XOAuth2 yet.

That will need an upcoming 'auth-source-xoauth2' package, as getting the
password involves doing some HTTP fetching. I have the code, just need
to find some time to add it to MELPA.

-- 
Cesar Crusius

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 658 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11 17:00   ` Cesar Crusius
@ 2018-01-11 19:33     ` Stefan Monnier
  2018-01-11 21:36       ` Cesar Crusius
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2018-01-11 19:33 UTC (permalink / raw)
  To: emacs-devel

> That will need an upcoming 'auth-source-xoauth2' package, as getting the
> password involves doing some HTTP fetching. I have the code, just need
> to find some time to add it to MELPA.

BTW, your library can still work with Emacs-26 (and even Emacs-25) using
a hack like:

    (unless (cl-generic-p 'smtpmail-try-auth-method)
      (let ((f (symbol-function 'smtpmail-try-auth-method)))
        (fset 'smtpmail-try-auth-method nil)
        (cl-generic-define-method 'smtpmail-try-auth-method nil
                                  '(process mech user password)
                                  nil f)))

which turns the "regular function" smtpmail-try-auth-method into
a "generic function".


        Stefan




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make `smtpmail-try-auth-method' a generic function.
  2018-01-11 19:33     ` Stefan Monnier
@ 2018-01-11 21:36       ` Cesar Crusius
  0 siblings, 0 replies; 9+ messages in thread
From: Cesar Crusius @ 2018-01-11 21:36 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1024 bytes --]


On Thu 11 Jan 2018 at 19:33, Stefan Monnier wrote:

>> That will need an upcoming 'auth-source-xoauth2' package, as getting the
>> password involves doing some HTTP fetching. I have the code, just need
>> to find some time to add it to MELPA.
>
> BTW, your library can still work with Emacs-26 (and even Emacs-25) using
> a hack like:
>
>     (unless (cl-generic-p 'smtpmail-try-auth-method)
>       (let ((f (symbol-function 'smtpmail-try-auth-method)))
>         (fset 'smtpmail-try-auth-method nil)
>         (cl-generic-define-method 'smtpmail-try-auth-method nil
>                                   '(process mech user password)
>                                   nil f)))
>
> which turns the "regular function" smtpmail-try-auth-method into
> a "generic function".

Yes - and it can also work with Emacs-24 by advising the function
instead, which is probably what I'm going to do: if the cl-* functions
are there, and smtpmail is one, specialize it, otherwise advise it.

-- 
Cesar Crusius

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 658 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-01-11 21:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-01-11  6:18 [PATCH] Make `smtpmail-try-auth-method' a generic function Cesar Crusius
2018-01-11 14:09 ` Stefan Monnier
2018-01-11 14:54   ` Cesar Crusius
2018-01-11 16:11   ` Eli Zaretskii
2018-01-11 16:57     ` Stefan Monnier
2018-01-11 14:42 ` Robert Pluim
2018-01-11 17:00   ` Cesar Crusius
2018-01-11 19:33     ` Stefan Monnier
2018-01-11 21:36       ` Cesar Crusius

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).