Skip to content
Snippets Groups Projects
Commit 83c0d144ef8d authored by Matt Harbison's avatar Matt Harbison
Browse files

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

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.

Differential Revision: https://phab.mercurial-scm.org/D10471
parent 0b569c75d180
Branches
Tags
No related merge requests found
...@@ -151,16 +151,5 @@ ...@@ -151,16 +151,5 @@
if starttls or smtps: if starttls or smtps:
ui.note(_(b'(verifying remote certificate)\n')) ui.note(_(b'(verifying remote certificate)\n'))
sslutil.validatesocket(s.sock) sslutil.validatesocket(s.sock)
username = ui.config(b'smtp', b'username')
password = ui.config(b'smtp', b'password')
if username:
if password:
password = encoding.strfromlocal(password)
else:
password = ui.getpass()
if password is not None:
password = encoding.strfromlocal(password)
if username and password:
ui.note(_(b'(authenticating to mail server as %s)\n') % username)
username = encoding.strfromlocal(username)
try: try:
...@@ -166,5 +155,5 @@ ...@@ -166,5 +155,5 @@
try: try:
s.login(username, password) _smtp_login(ui, s, mailhost, mailport)
except smtplib.SMTPException as inst: except smtplib.SMTPException as inst:
raise error.Abort(stringutil.forcebytestr(inst)) raise error.Abort(stringutil.forcebytestr(inst))
...@@ -180,6 +169,29 @@ ...@@ -180,6 +169,29 @@
return send 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. ``smtplib.SMTPException`` is raised on error.
"""
username = ui.config(b'smtp', b'username')
password = ui.config(b'smtp', b'password')
if username:
if password:
password = encoding.strfromlocal(password)
else:
password = ui.getpass()
if password is not None:
password = encoding.strfromlocal(password)
if username and password:
ui.note(_(b'(authenticating to mail server as %s)\n') % username)
username = encoding.strfromlocal(username)
smtp.login(username, password)
def _sendmail(ui, sender, recipients, msg): def _sendmail(ui, sender, recipients, msg):
'''send mail using sendmail.''' '''send mail using sendmail.'''
program = ui.config(b'email', b'method') program = ui.config(b'email', b'method')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment