Mercurial > public > mercurial-scm > hg
comparison mercurial/mail.py @ 29251:31acc78c632a
mail: remove use of sslkwargs
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 25 May 2016 19:56:20 -0700 |
parents | e6de6ef3e426 |
children | 63a3749147af |
comparison
equal
deleted
inserted
replaced
29250:d6b9468eebee | 29251:31acc78c632a |
---|---|
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, ui, sslkwargs, host=None, **kwargs): | 51 def __init__(self, ui, host=None, **kwargs): |
52 smtplib.SMTP.__init__(self, **kwargs) | 52 smtplib.SMTP.__init__(self, **kwargs) |
53 self._ui = ui | 53 self._ui = ui |
54 self._sslkwargs = sslkwargs | |
55 self._host = host | 54 self._host = host |
56 | 55 |
57 def starttls(self, keyfile=None, certfile=None): | 56 def starttls(self, keyfile=None, certfile=None): |
58 if not self.has_extn("starttls"): | 57 if not self.has_extn("starttls"): |
59 msg = "STARTTLS extension not supported by server" | 58 msg = "STARTTLS extension not supported by server" |
60 raise smtplib.SMTPException(msg) | 59 raise smtplib.SMTPException(msg) |
61 (resp, reply) = self.docmd("STARTTLS") | 60 (resp, reply) = self.docmd("STARTTLS") |
62 if resp == 220: | 61 if resp == 220: |
63 self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, | 62 self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, |
64 ui=self._ui, | 63 ui=self._ui, |
65 serverhostname=self._host, | 64 serverhostname=self._host) |
66 **self._sslkwargs) | |
67 self.file = smtplib.SSLFakeFile(self.sock) | 65 self.file = smtplib.SSLFakeFile(self.sock) |
68 self.helo_resp = None | 66 self.helo_resp = None |
69 self.ehlo_resp = None | 67 self.ehlo_resp = None |
70 self.esmtp_features = {} | 68 self.esmtp_features = {} |
71 self.does_esmtp = 0 | 69 self.does_esmtp = 0 |
74 class SMTPS(smtplib.SMTP): | 72 class SMTPS(smtplib.SMTP): |
75 '''Derived class to verify the peer certificate for SMTPS. | 73 '''Derived class to verify the peer certificate for SMTPS. |
76 | 74 |
77 This class allows to pass any keyword arguments to SSL socket creation. | 75 This class allows to pass any keyword arguments to SSL socket creation. |
78 ''' | 76 ''' |
79 def __init__(self, ui, sslkwargs, keyfile=None, certfile=None, host=None, | 77 def __init__(self, ui, keyfile=None, certfile=None, host=None, |
80 **kwargs): | 78 **kwargs): |
81 self.keyfile = keyfile | 79 self.keyfile = keyfile |
82 self.certfile = certfile | 80 self.certfile = certfile |
83 smtplib.SMTP.__init__(self, **kwargs) | 81 smtplib.SMTP.__init__(self, **kwargs) |
84 self._host = host | 82 self._host = host |
85 self.default_port = smtplib.SMTP_SSL_PORT | 83 self.default_port = smtplib.SMTP_SSL_PORT |
86 self._ui = ui | 84 self._ui = ui |
87 self._sslkwargs = sslkwargs | |
88 | 85 |
89 def _get_socket(self, host, port, timeout): | 86 def _get_socket(self, host, port, timeout): |
90 if self.debuglevel > 0: | 87 if self.debuglevel > 0: |
91 print('connect:', (host, port), file=sys.stderr) | 88 print('connect:', (host, port), file=sys.stderr) |
92 new_socket = socket.create_connection((host, port), timeout) | 89 new_socket = socket.create_connection((host, port), timeout) |
93 new_socket = sslutil.wrapsocket(new_socket, | 90 new_socket = sslutil.wrapsocket(new_socket, |
94 self.keyfile, self.certfile, | 91 self.keyfile, self.certfile, |
95 ui=self._ui, | 92 ui=self._ui, |
96 serverhostname=self._host, | 93 serverhostname=self._host) |
97 **self._sslkwargs) | |
98 self.file = smtplib.SSLFakeFile(new_socket) | 94 self.file = smtplib.SSLFakeFile(new_socket) |
99 return new_socket | 95 return new_socket |
100 | 96 |
101 def _smtp(ui): | 97 def _smtp(ui): |
102 '''build an smtp connection and return a function to send mail''' | 98 '''build an smtp connection and return a function to send mail''' |
114 if verifycert not in ['strict', 'loose']: | 110 if verifycert not in ['strict', 'loose']: |
115 if util.parsebool(verifycert) is not False: | 111 if util.parsebool(verifycert) is not False: |
116 raise error.Abort(_('invalid smtp.verifycert configuration: %s') | 112 raise error.Abort(_('invalid smtp.verifycert configuration: %s') |
117 % (verifycert)) | 113 % (verifycert)) |
118 verifycert = False | 114 verifycert = False |
119 if (starttls or smtps) and verifycert: | |
120 sslkwargs = sslutil.sslkwargs(ui, mailhost) | |
121 else: | |
122 sslkwargs = {} | |
123 | 115 |
124 if smtps: | 116 if smtps: |
125 ui.note(_('(using smtps)\n')) | 117 ui.note(_('(using smtps)\n')) |
126 s = SMTPS(ui, sslkwargs, local_hostname=local_hostname, host=mailhost) | 118 s = SMTPS(ui, local_hostname=local_hostname, host=mailhost) |
127 elif starttls: | 119 elif starttls: |
128 s = STARTTLS(ui, sslkwargs, local_hostname=local_hostname, | 120 s = STARTTLS(ui, local_hostname=local_hostname, host=mailhost) |
129 host=mailhost) | |
130 else: | 121 else: |
131 s = smtplib.SMTP(local_hostname=local_hostname) | 122 s = smtplib.SMTP(local_hostname=local_hostname) |
132 if smtps: | 123 if smtps: |
133 defaultport = 465 | 124 defaultport = 465 |
134 else: | 125 else: |