Patchwork D10471: mail: split out the SMTP login to allow the keyring extension to wrap it

login
register
mail settings
Submitter phabricator
Date April 19, 2021, 11:40 p.m.
Message ID <differential-rev-PHID-DREV-akq5b7rruirizldjwst2-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48788/
State Superseded
Headers show

Comments

phabricator - April 19, 2021, 11:40 p.m.
mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The keyring extension only needs to tweak this tiny section of the larger
  function.  But without any place to intercept the username/password fetching, it
  copy/pasted the entire function, and has grown a bunch of compatibility hacks to
  support older versions of Mercurial as well.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D10471

AFFECTED FILES
  mercurial/mail.py

CHANGE DETAILS




To: mharbison72, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/mail.py b/mercurial/mail.py
--- a/mercurial/mail.py
+++ b/mercurial/mail.py
@@ -151,6 +151,29 @@ 
     if starttls or smtps:
         ui.note(_(b'(verifying remote certificate)\n'))
         sslutil.validatesocket(s.sock)
+
+    _smtp_login(ui, s, mailhost, mailport)
+
+    def send(sender, recipients, msg):
+        try:
+            return s.sendmail(sender, recipients, msg)
+        except smtplib.SMTPRecipientsRefused as inst:
+            recipients = [r[1] for r in inst.recipients.values()]
+            raise error.Abort(b'\n' + b'\n'.join(recipients))
+        except smtplib.SMTPException as inst:
+            raise error.Abort(inst)
+
+    return send
+
+
+def _smtp_login(ui, smtp, mailhost, mailport):
+    """A hook for the keyring extension to perform the actual SMTP login.
+
+    An already connected SMTP object of the proper type is provided, based on
+    the current configuration.  The host and port to which the connection was
+    established are provided for accessibility, since the SMTP object doesn't
+    provide an accessor.
+    """
     username = ui.config(b'smtp', b'username')
     password = ui.config(b'smtp', b'password')
     if username:
@@ -164,21 +187,10 @@ 
         ui.note(_(b'(authenticating to mail server as %s)\n') % username)
         username = encoding.strfromlocal(username)
         try:
-            s.login(username, password)
+            smtp.login(username, password)
         except smtplib.SMTPException as inst:
             raise error.Abort(stringutil.forcebytestr(inst))
 
-    def send(sender, recipients, msg):
-        try:
-            return s.sendmail(sender, recipients, msg)
-        except smtplib.SMTPRecipientsRefused as inst:
-            recipients = [r[1] for r in inst.recipients.values()]
-            raise error.Abort(b'\n' + b'\n'.join(recipients))
-        except smtplib.SMTPException as inst:
-            raise error.Abort(inst)
-
-    return send
-
 
 def _sendmail(ui, sender, recipients, msg):
     '''send mail using sendmail.'''