comparison mercurial/mail.py @ 29248:e6de6ef3e426

sslutil: remove ui from sslkwargs (API) Arguments to sslutil.wrapsocket() are partially determined by calling sslutil.sslkwargs(). This function receives a ui and a hostname and determines what settings, if any, need to be applied when the socket is wrapped. Both the ui and hostname are passed into wrapsocket(). The other arguments to wrapsocket() provided by sslkwargs() (ca_certs and cert_reqs) are not looked at or modified anywhere outside of sslutil.py. So, sslkwargs() doesn't need to exist as a separate public API called before wrapsocket(). This commit starts the process of removing external consumers of sslkwargs() by removing the "ui" key/argument from its return. All callers now pass the ui argument explicitly.
author Gregory Szorc <gregory.szorc@gmail.com>
date Wed, 25 May 2016 19:43:22 -0700
parents dffe78d80a6c
children 31acc78c632a
comparison
equal deleted inserted replaced
29247:3e438497edca 29248:e6de6ef3e426
46 class STARTTLS(smtplib.SMTP): 46 class STARTTLS(smtplib.SMTP):
47 '''Derived class to verify the peer certificate for STARTTLS. 47 '''Derived class to verify the peer certificate for STARTTLS.
48 48
49 This class allows to pass any keyword arguments to SSL socket creation. 49 This class allows to pass any keyword arguments to SSL socket creation.
50 ''' 50 '''
51 def __init__(self, sslkwargs, host=None, **kwargs): 51 def __init__(self, ui, sslkwargs, host=None, **kwargs):
52 smtplib.SMTP.__init__(self, **kwargs) 52 smtplib.SMTP.__init__(self, **kwargs)
53 self._ui = ui
53 self._sslkwargs = sslkwargs 54 self._sslkwargs = sslkwargs
54 self._host = host 55 self._host = host
55 56
56 def starttls(self, keyfile=None, certfile=None): 57 def starttls(self, keyfile=None, certfile=None):
57 if not self.has_extn("starttls"): 58 if not self.has_extn("starttls"):
58 msg = "STARTTLS extension not supported by server" 59 msg = "STARTTLS extension not supported by server"
59 raise smtplib.SMTPException(msg) 60 raise smtplib.SMTPException(msg)
60 (resp, reply) = self.docmd("STARTTLS") 61 (resp, reply) = self.docmd("STARTTLS")
61 if resp == 220: 62 if resp == 220:
62 self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, 63 self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile,
64 ui=self._ui,
63 serverhostname=self._host, 65 serverhostname=self._host,
64 **self._sslkwargs) 66 **self._sslkwargs)
65 self.file = smtplib.SSLFakeFile(self.sock) 67 self.file = smtplib.SSLFakeFile(self.sock)
66 self.helo_resp = None 68 self.helo_resp = None
67 self.ehlo_resp = None 69 self.ehlo_resp = None
72 class SMTPS(smtplib.SMTP): 74 class SMTPS(smtplib.SMTP):
73 '''Derived class to verify the peer certificate for SMTPS. 75 '''Derived class to verify the peer certificate for SMTPS.
74 76
75 This class allows to pass any keyword arguments to SSL socket creation. 77 This class allows to pass any keyword arguments to SSL socket creation.
76 ''' 78 '''
77 def __init__(self, sslkwargs, keyfile=None, certfile=None, host=None, 79 def __init__(self, ui, sslkwargs, keyfile=None, certfile=None, host=None,
78 **kwargs): 80 **kwargs):
79 self.keyfile = keyfile 81 self.keyfile = keyfile
80 self.certfile = certfile 82 self.certfile = certfile
81 smtplib.SMTP.__init__(self, **kwargs) 83 smtplib.SMTP.__init__(self, **kwargs)
82 self._host = host 84 self._host = host
83 self.default_port = smtplib.SMTP_SSL_PORT 85 self.default_port = smtplib.SMTP_SSL_PORT
86 self._ui = ui
84 self._sslkwargs = sslkwargs 87 self._sslkwargs = sslkwargs
85 88
86 def _get_socket(self, host, port, timeout): 89 def _get_socket(self, host, port, timeout):
87 if self.debuglevel > 0: 90 if self.debuglevel > 0:
88 print('connect:', (host, port), file=sys.stderr) 91 print('connect:', (host, port), file=sys.stderr)
89 new_socket = socket.create_connection((host, port), timeout) 92 new_socket = socket.create_connection((host, port), timeout)
90 new_socket = sslutil.wrapsocket(new_socket, 93 new_socket = sslutil.wrapsocket(new_socket,
91 self.keyfile, self.certfile, 94 self.keyfile, self.certfile,
95 ui=self._ui,
92 serverhostname=self._host, 96 serverhostname=self._host,
93 **self._sslkwargs) 97 **self._sslkwargs)
94 self.file = smtplib.SSLFakeFile(new_socket) 98 self.file = smtplib.SSLFakeFile(new_socket)
95 return new_socket 99 return new_socket
96 100
113 % (verifycert)) 117 % (verifycert))
114 verifycert = False 118 verifycert = False
115 if (starttls or smtps) and verifycert: 119 if (starttls or smtps) and verifycert:
116 sslkwargs = sslutil.sslkwargs(ui, mailhost) 120 sslkwargs = sslutil.sslkwargs(ui, mailhost)
117 else: 121 else:
118 # 'ui' is required by sslutil.wrapsocket() and set by sslkwargs() 122 sslkwargs = {}
119 sslkwargs = {'ui': ui} 123
120 if smtps: 124 if smtps:
121 ui.note(_('(using smtps)\n')) 125 ui.note(_('(using smtps)\n'))
122 s = SMTPS(sslkwargs, local_hostname=local_hostname, host=mailhost) 126 s = SMTPS(ui, sslkwargs, local_hostname=local_hostname, host=mailhost)
123 elif starttls: 127 elif starttls:
124 s = STARTTLS(sslkwargs, local_hostname=local_hostname, host=mailhost) 128 s = STARTTLS(ui, sslkwargs, local_hostname=local_hostname,
129 host=mailhost)
125 else: 130 else:
126 s = smtplib.SMTP(local_hostname=local_hostname) 131 s = smtplib.SMTP(local_hostname=local_hostname)
127 if smtps: 132 if smtps:
128 defaultport = 465 133 defaultport = 465
129 else: 134 else: