Show More
@@ -1,395 +1,397 | |||||
1 | # mail.py - mail sending bits for mercurial |
|
1 | # mail.py - mail sending bits for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import email |
|
10 | import email | |
11 | import email.charset |
|
11 | import email.charset | |
12 | import email.header |
|
12 | import email.header | |
13 | import email.message |
|
13 | import email.message | |
14 | import email.parser |
|
14 | import email.parser | |
15 | import io |
|
15 | import io | |
16 | import os |
|
16 | import os | |
17 | import smtplib |
|
17 | import smtplib | |
18 | import socket |
|
18 | import socket | |
19 | import time |
|
19 | import time | |
20 |
|
20 | |||
21 | from .i18n import _ |
|
21 | from .i18n import _ | |
22 | from . import ( |
|
22 | from . import ( | |
23 | encoding, |
|
23 | encoding, | |
24 | error, |
|
24 | error, | |
25 | pycompat, |
|
25 | pycompat, | |
26 | sslutil, |
|
26 | sslutil, | |
27 | util, |
|
27 | util, | |
28 | ) |
|
28 | ) | |
29 | from .utils import ( |
|
29 | from .utils import ( | |
30 | procutil, |
|
30 | procutil, | |
31 | stringutil, |
|
31 | stringutil, | |
32 | ) |
|
32 | ) | |
33 |
|
33 | |||
34 | class STARTTLS(smtplib.SMTP): |
|
34 | class STARTTLS(smtplib.SMTP): | |
35 | '''Derived class to verify the peer certificate for STARTTLS. |
|
35 | '''Derived class to verify the peer certificate for STARTTLS. | |
36 |
|
36 | |||
37 | This class allows to pass any keyword arguments to SSL socket creation. |
|
37 | This class allows to pass any keyword arguments to SSL socket creation. | |
38 | ''' |
|
38 | ''' | |
39 | def __init__(self, ui, host=None, **kwargs): |
|
39 | def __init__(self, ui, host=None, **kwargs): | |
40 | smtplib.SMTP.__init__(self, **kwargs) |
|
40 | smtplib.SMTP.__init__(self, **kwargs) | |
41 | self._ui = ui |
|
41 | self._ui = ui | |
42 | self._host = host |
|
42 | self._host = host | |
43 |
|
43 | |||
44 | def starttls(self, keyfile=None, certfile=None): |
|
44 | def starttls(self, keyfile=None, certfile=None): | |
45 | if not self.has_extn("starttls"): |
|
45 | if not self.has_extn("starttls"): | |
46 | msg = "STARTTLS extension not supported by server" |
|
46 | msg = "STARTTLS extension not supported by server" | |
47 | raise smtplib.SMTPException(msg) |
|
47 | raise smtplib.SMTPException(msg) | |
48 | (resp, reply) = self.docmd("STARTTLS") |
|
48 | (resp, reply) = self.docmd("STARTTLS") | |
49 | if resp == 220: |
|
49 | if resp == 220: | |
50 | self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, |
|
50 | self.sock = sslutil.wrapsocket(self.sock, keyfile, certfile, | |
51 | ui=self._ui, |
|
51 | ui=self._ui, | |
52 | serverhostname=self._host) |
|
52 | serverhostname=self._host) | |
53 | self.file = smtplib.SSLFakeFile(self.sock) |
|
53 | self.file = smtplib.SSLFakeFile(self.sock) | |
54 | self.helo_resp = None |
|
54 | self.helo_resp = None | |
55 | self.ehlo_resp = None |
|
55 | self.ehlo_resp = None | |
56 | self.esmtp_features = {} |
|
56 | self.esmtp_features = {} | |
57 | self.does_esmtp = 0 |
|
57 | self.does_esmtp = 0 | |
58 | return (resp, reply) |
|
58 | return (resp, reply) | |
59 |
|
59 | |||
60 | class SMTPS(smtplib.SMTP): |
|
60 | class SMTPS(smtplib.SMTP): | |
61 | '''Derived class to verify the peer certificate for SMTPS. |
|
61 | '''Derived class to verify the peer certificate for SMTPS. | |
62 |
|
62 | |||
63 | This class allows to pass any keyword arguments to SSL socket creation. |
|
63 | This class allows to pass any keyword arguments to SSL socket creation. | |
64 | ''' |
|
64 | ''' | |
65 | def __init__(self, ui, keyfile=None, certfile=None, host=None, |
|
65 | def __init__(self, ui, keyfile=None, certfile=None, host=None, | |
66 | **kwargs): |
|
66 | **kwargs): | |
67 | self.keyfile = keyfile |
|
67 | self.keyfile = keyfile | |
68 | self.certfile = certfile |
|
68 | self.certfile = certfile | |
69 | smtplib.SMTP.__init__(self, **kwargs) |
|
69 | smtplib.SMTP.__init__(self, **kwargs) | |
70 | self._host = host |
|
70 | self._host = host | |
71 | self.default_port = smtplib.SMTP_SSL_PORT |
|
71 | self.default_port = smtplib.SMTP_SSL_PORT | |
72 | self._ui = ui |
|
72 | self._ui = ui | |
73 |
|
73 | |||
74 | def _get_socket(self, host, port, timeout): |
|
74 | def _get_socket(self, host, port, timeout): | |
75 | if self.debuglevel > 0: |
|
75 | if self.debuglevel > 0: | |
76 | self._ui.debug('connect: %r\n' % ((host, port),)) |
|
76 | self._ui.debug('connect: %r\n' % ((host, port),)) | |
77 | new_socket = socket.create_connection((host, port), timeout) |
|
77 | new_socket = socket.create_connection((host, port), timeout) | |
78 | new_socket = sslutil.wrapsocket(new_socket, |
|
78 | new_socket = sslutil.wrapsocket(new_socket, | |
79 | self.keyfile, self.certfile, |
|
79 | self.keyfile, self.certfile, | |
80 | ui=self._ui, |
|
80 | ui=self._ui, | |
81 | serverhostname=self._host) |
|
81 | serverhostname=self._host) | |
82 | self.file = new_socket.makefile(r'rb') |
|
82 | self.file = new_socket.makefile(r'rb') | |
83 | return new_socket |
|
83 | return new_socket | |
84 |
|
84 | |||
85 | def _pyhastls(): |
|
85 | def _pyhastls(): | |
86 | """Returns true iff Python has TLS support, false otherwise.""" |
|
86 | """Returns true iff Python has TLS support, false otherwise.""" | |
87 | try: |
|
87 | try: | |
88 | import ssl |
|
88 | import ssl | |
89 | getattr(ssl, 'HAS_TLS', False) |
|
89 | getattr(ssl, 'HAS_TLS', False) | |
90 | return True |
|
90 | return True | |
91 | except ImportError: |
|
91 | except ImportError: | |
92 | return False |
|
92 | return False | |
93 |
|
93 | |||
94 | def _smtp(ui): |
|
94 | def _smtp(ui): | |
95 | '''build an smtp connection and return a function to send mail''' |
|
95 | '''build an smtp connection and return a function to send mail''' | |
96 | local_hostname = ui.config('smtp', 'local_hostname') |
|
96 | local_hostname = ui.config('smtp', 'local_hostname') | |
97 | tls = ui.config('smtp', 'tls') |
|
97 | tls = ui.config('smtp', 'tls') | |
98 | # backward compatible: when tls = true, we use starttls. |
|
98 | # backward compatible: when tls = true, we use starttls. | |
99 | starttls = tls == 'starttls' or stringutil.parsebool(tls) |
|
99 | starttls = tls == 'starttls' or stringutil.parsebool(tls) | |
100 | smtps = tls == 'smtps' |
|
100 | smtps = tls == 'smtps' | |
101 | if (starttls or smtps) and not _pyhastls(): |
|
101 | if (starttls or smtps) and not _pyhastls(): | |
102 | raise error.Abort(_("can't use TLS: Python SSL support not installed")) |
|
102 | raise error.Abort(_("can't use TLS: Python SSL support not installed")) | |
103 | mailhost = ui.config('smtp', 'host') |
|
103 | mailhost = ui.config('smtp', 'host') | |
104 | if not mailhost: |
|
104 | if not mailhost: | |
105 | raise error.Abort(_('smtp.host not configured - cannot send mail')) |
|
105 | raise error.Abort(_('smtp.host not configured - cannot send mail')) | |
106 | if smtps: |
|
106 | if smtps: | |
107 | ui.note(_('(using smtps)\n')) |
|
107 | ui.note(_('(using smtps)\n')) | |
108 | s = SMTPS(ui, local_hostname=local_hostname, host=mailhost) |
|
108 | s = SMTPS(ui, local_hostname=local_hostname, host=mailhost) | |
109 | elif starttls: |
|
109 | elif starttls: | |
110 | s = STARTTLS(ui, local_hostname=local_hostname, host=mailhost) |
|
110 | s = STARTTLS(ui, local_hostname=local_hostname, host=mailhost) | |
111 | else: |
|
111 | else: | |
112 | s = smtplib.SMTP(local_hostname=local_hostname) |
|
112 | s = smtplib.SMTP(local_hostname=local_hostname) | |
113 | if smtps: |
|
113 | if smtps: | |
114 | defaultport = 465 |
|
114 | defaultport = 465 | |
115 | else: |
|
115 | else: | |
116 | defaultport = 25 |
|
116 | defaultport = 25 | |
117 | mailport = util.getport(ui.config('smtp', 'port', defaultport)) |
|
117 | mailport = util.getport(ui.config('smtp', 'port', defaultport)) | |
118 | ui.note(_('sending mail: smtp host %s, port %d\n') % |
|
118 | ui.note(_('sending mail: smtp host %s, port %d\n') % | |
119 | (mailhost, mailport)) |
|
119 | (mailhost, mailport)) | |
120 | s.connect(host=mailhost, port=mailport) |
|
120 | s.connect(host=mailhost, port=mailport) | |
121 | if starttls: |
|
121 | if starttls: | |
122 | ui.note(_('(using starttls)\n')) |
|
122 | ui.note(_('(using starttls)\n')) | |
123 | s.ehlo() |
|
123 | s.ehlo() | |
124 | s.starttls() |
|
124 | s.starttls() | |
125 | s.ehlo() |
|
125 | s.ehlo() | |
126 | if starttls or smtps: |
|
126 | if starttls or smtps: | |
127 | ui.note(_('(verifying remote certificate)\n')) |
|
127 | ui.note(_('(verifying remote certificate)\n')) | |
128 | sslutil.validatesocket(s.sock) |
|
128 | sslutil.validatesocket(s.sock) | |
129 | username = ui.config('smtp', 'username') |
|
129 | username = ui.config('smtp', 'username') | |
130 | password = ui.config('smtp', 'password') |
|
130 | password = ui.config('smtp', 'password') | |
131 | if username and not password: |
|
131 | if username and not password: | |
132 | password = ui.getpass() |
|
132 | password = ui.getpass() | |
133 | if username and password: |
|
133 | if username and password: | |
134 | ui.note(_('(authenticating to mail server as %s)\n') % |
|
134 | ui.note(_('(authenticating to mail server as %s)\n') % | |
135 | (username)) |
|
135 | (username)) | |
136 | try: |
|
136 | try: | |
137 | s.login(username, password) |
|
137 | s.login(username, password) | |
138 | except smtplib.SMTPException as inst: |
|
138 | except smtplib.SMTPException as inst: | |
139 | raise error.Abort(inst) |
|
139 | raise error.Abort(inst) | |
140 |
|
140 | |||
141 | def send(sender, recipients, msg): |
|
141 | def send(sender, recipients, msg): | |
142 | try: |
|
142 | try: | |
143 | return s.sendmail(sender, recipients, msg) |
|
143 | return s.sendmail(sender, recipients, msg) | |
144 | except smtplib.SMTPRecipientsRefused as inst: |
|
144 | except smtplib.SMTPRecipientsRefused as inst: | |
145 | recipients = [r[1] for r in inst.recipients.values()] |
|
145 | recipients = [r[1] for r in inst.recipients.values()] | |
146 | raise error.Abort('\n' + '\n'.join(recipients)) |
|
146 | raise error.Abort('\n' + '\n'.join(recipients)) | |
147 | except smtplib.SMTPException as inst: |
|
147 | except smtplib.SMTPException as inst: | |
148 | raise error.Abort(inst) |
|
148 | raise error.Abort(inst) | |
149 |
|
149 | |||
150 | return send |
|
150 | return send | |
151 |
|
151 | |||
152 | def _sendmail(ui, sender, recipients, msg): |
|
152 | def _sendmail(ui, sender, recipients, msg): | |
153 | '''send mail using sendmail.''' |
|
153 | '''send mail using sendmail.''' | |
154 | program = ui.config('email', 'method') |
|
154 | program = ui.config('email', 'method') | |
155 | cmdline = '%s -f %s %s' % (program, stringutil.email(sender), |
|
155 | stremail = lambda x: stringutil.email(encoding.strtolocal(x)) | |
156 | ' '.join(map(stringutil.email, recipients))) |
|
156 | cmdline = '%s -f %s %s' % (program, stremail(sender), | |
|
157 | ' '.join(map(stremail, recipients))) | |||
157 | ui.note(_('sending mail: %s\n') % cmdline) |
|
158 | ui.note(_('sending mail: %s\n') % cmdline) | |
158 | fp = procutil.popen(cmdline, 'wb') |
|
159 | fp = procutil.popen(cmdline, 'wb') | |
159 | fp.write(util.tonativeeol(msg)) |
|
160 | fp.write(util.tonativeeol(msg)) | |
160 | ret = fp.close() |
|
161 | ret = fp.close() | |
161 | if ret: |
|
162 | if ret: | |
162 | raise error.Abort('%s %s' % ( |
|
163 | raise error.Abort('%s %s' % ( | |
163 | os.path.basename(program.split(None, 1)[0]), |
|
164 | os.path.basename(program.split(None, 1)[0]), | |
164 | procutil.explainexit(ret))) |
|
165 | procutil.explainexit(ret))) | |
165 |
|
166 | |||
166 | def _mbox(mbox, sender, recipients, msg): |
|
167 | def _mbox(mbox, sender, recipients, msg): | |
167 | '''write mails to mbox''' |
|
168 | '''write mails to mbox''' | |
168 | fp = open(mbox, 'ab+') |
|
169 | fp = open(mbox, 'ab+') | |
169 | # Should be time.asctime(), but Windows prints 2-characters day |
|
170 | # Should be time.asctime(), but Windows prints 2-characters day | |
170 | # of month instead of one. Make them print the same thing. |
|
171 | # of month instead of one. Make them print the same thing. | |
171 | date = time.strftime(r'%a %b %d %H:%M:%S %Y', time.localtime()) |
|
172 | date = time.strftime(r'%a %b %d %H:%M:%S %Y', time.localtime()) | |
172 |
fp.write('From %s %s\n' % (sender, |
|
173 | fp.write('From %s %s\n' % (encoding.strtolocal(sender), | |
|
174 | encoding.strtolocal(date))) | |||
173 | fp.write(msg) |
|
175 | fp.write(msg) | |
174 | fp.write('\n\n') |
|
176 | fp.write('\n\n') | |
175 | fp.close() |
|
177 | fp.close() | |
176 |
|
178 | |||
177 | def connect(ui, mbox=None): |
|
179 | def connect(ui, mbox=None): | |
178 | '''make a mail connection. return a function to send mail. |
|
180 | '''make a mail connection. return a function to send mail. | |
179 | call as sendmail(sender, list-of-recipients, msg).''' |
|
181 | call as sendmail(sender, list-of-recipients, msg).''' | |
180 | if mbox: |
|
182 | if mbox: | |
181 | open(mbox, 'wb').close() |
|
183 | open(mbox, 'wb').close() | |
182 | return lambda s, r, m: _mbox(mbox, s, r, m) |
|
184 | return lambda s, r, m: _mbox(mbox, s, r, m) | |
183 | if ui.config('email', 'method') == 'smtp': |
|
185 | if ui.config('email', 'method') == 'smtp': | |
184 | return _smtp(ui) |
|
186 | return _smtp(ui) | |
185 | return lambda s, r, m: _sendmail(ui, s, r, m) |
|
187 | return lambda s, r, m: _sendmail(ui, s, r, m) | |
186 |
|
188 | |||
187 | def sendmail(ui, sender, recipients, msg, mbox=None): |
|
189 | def sendmail(ui, sender, recipients, msg, mbox=None): | |
188 | send = connect(ui, mbox=mbox) |
|
190 | send = connect(ui, mbox=mbox) | |
189 | return send(sender, recipients, msg) |
|
191 | return send(sender, recipients, msg) | |
190 |
|
192 | |||
191 | def validateconfig(ui): |
|
193 | def validateconfig(ui): | |
192 | '''determine if we have enough config data to try sending email.''' |
|
194 | '''determine if we have enough config data to try sending email.''' | |
193 | method = ui.config('email', 'method') |
|
195 | method = ui.config('email', 'method') | |
194 | if method == 'smtp': |
|
196 | if method == 'smtp': | |
195 | if not ui.config('smtp', 'host'): |
|
197 | if not ui.config('smtp', 'host'): | |
196 | raise error.Abort(_('smtp specified as email transport, ' |
|
198 | raise error.Abort(_('smtp specified as email transport, ' | |
197 | 'but no smtp host configured')) |
|
199 | 'but no smtp host configured')) | |
198 | else: |
|
200 | else: | |
199 | if not procutil.findexe(method): |
|
201 | if not procutil.findexe(method): | |
200 | raise error.Abort(_('%r specified as email transport, ' |
|
202 | raise error.Abort(_('%r specified as email transport, ' | |
201 | 'but not in PATH') % method) |
|
203 | 'but not in PATH') % method) | |
202 |
|
204 | |||
203 | def codec2iana(cs): |
|
205 | def codec2iana(cs): | |
204 | '''''' |
|
206 | '''''' | |
205 | cs = pycompat.sysbytes(email.charset.Charset(cs).input_charset.lower()) |
|
207 | cs = pycompat.sysbytes(email.charset.Charset(cs).input_charset.lower()) | |
206 |
|
208 | |||
207 | # "latin1" normalizes to "iso8859-1", standard calls for "iso-8859-1" |
|
209 | # "latin1" normalizes to "iso8859-1", standard calls for "iso-8859-1" | |
208 | if cs.startswith("iso") and not cs.startswith("iso-"): |
|
210 | if cs.startswith("iso") and not cs.startswith("iso-"): | |
209 | return "iso-" + cs[3:] |
|
211 | return "iso-" + cs[3:] | |
210 | return cs |
|
212 | return cs | |
211 |
|
213 | |||
212 | def mimetextpatch(s, subtype='plain', display=False): |
|
214 | def mimetextpatch(s, subtype='plain', display=False): | |
213 | '''Return MIME message suitable for a patch. |
|
215 | '''Return MIME message suitable for a patch. | |
214 | Charset will be detected by first trying to decode as us-ascii, then utf-8, |
|
216 | Charset will be detected by first trying to decode as us-ascii, then utf-8, | |
215 | and finally the global encodings. If all those fail, fall back to |
|
217 | and finally the global encodings. If all those fail, fall back to | |
216 | ISO-8859-1, an encoding with that allows all byte sequences. |
|
218 | ISO-8859-1, an encoding with that allows all byte sequences. | |
217 | Transfer encodings will be used if necessary.''' |
|
219 | Transfer encodings will be used if necessary.''' | |
218 |
|
220 | |||
219 | cs = ['us-ascii', 'utf-8', encoding.encoding, encoding.fallbackencoding] |
|
221 | cs = ['us-ascii', 'utf-8', encoding.encoding, encoding.fallbackencoding] | |
220 | if display: |
|
222 | if display: | |
221 | return mimetextqp(s, subtype, 'us-ascii') |
|
223 | return mimetextqp(s, subtype, 'us-ascii') | |
222 | for charset in cs: |
|
224 | for charset in cs: | |
223 | try: |
|
225 | try: | |
224 | s.decode(pycompat.sysstr(charset)) |
|
226 | s.decode(pycompat.sysstr(charset)) | |
225 | return mimetextqp(s, subtype, codec2iana(charset)) |
|
227 | return mimetextqp(s, subtype, codec2iana(charset)) | |
226 | except UnicodeDecodeError: |
|
228 | except UnicodeDecodeError: | |
227 | pass |
|
229 | pass | |
228 |
|
230 | |||
229 | return mimetextqp(s, subtype, "iso-8859-1") |
|
231 | return mimetextqp(s, subtype, "iso-8859-1") | |
230 |
|
232 | |||
231 | def mimetextqp(body, subtype, charset): |
|
233 | def mimetextqp(body, subtype, charset): | |
232 | '''Return MIME message. |
|
234 | '''Return MIME message. | |
233 | Quoted-printable transfer encoding will be used if necessary. |
|
235 | Quoted-printable transfer encoding will be used if necessary. | |
234 | ''' |
|
236 | ''' | |
235 | cs = email.charset.Charset(charset) |
|
237 | cs = email.charset.Charset(charset) | |
236 | msg = email.message.Message() |
|
238 | msg = email.message.Message() | |
237 | msg.set_type(pycompat.sysstr('text/' + subtype)) |
|
239 | msg.set_type(pycompat.sysstr('text/' + subtype)) | |
238 |
|
240 | |||
239 | for line in body.splitlines(): |
|
241 | for line in body.splitlines(): | |
240 | if len(line) > 950: |
|
242 | if len(line) > 950: | |
241 | cs.body_encoding = email.charset.QP |
|
243 | cs.body_encoding = email.charset.QP | |
242 | break |
|
244 | break | |
243 |
|
245 | |||
244 | msg.set_payload(body, cs) |
|
246 | msg.set_payload(body, cs) | |
245 |
|
247 | |||
246 | return msg |
|
248 | return msg | |
247 |
|
249 | |||
248 | def _charsets(ui): |
|
250 | def _charsets(ui): | |
249 | '''Obtains charsets to send mail parts not containing patches.''' |
|
251 | '''Obtains charsets to send mail parts not containing patches.''' | |
250 | charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')] |
|
252 | charsets = [cs.lower() for cs in ui.configlist('email', 'charsets')] | |
251 | fallbacks = [encoding.fallbackencoding.lower(), |
|
253 | fallbacks = [encoding.fallbackencoding.lower(), | |
252 | encoding.encoding.lower(), 'utf-8'] |
|
254 | encoding.encoding.lower(), 'utf-8'] | |
253 | for cs in fallbacks: # find unique charsets while keeping order |
|
255 | for cs in fallbacks: # find unique charsets while keeping order | |
254 | if cs not in charsets: |
|
256 | if cs not in charsets: | |
255 | charsets.append(cs) |
|
257 | charsets.append(cs) | |
256 | return [cs for cs in charsets if not cs.endswith('ascii')] |
|
258 | return [cs for cs in charsets if not cs.endswith('ascii')] | |
257 |
|
259 | |||
258 | def _encode(ui, s, charsets): |
|
260 | def _encode(ui, s, charsets): | |
259 | '''Returns (converted) string, charset tuple. |
|
261 | '''Returns (converted) string, charset tuple. | |
260 | Finds out best charset by cycling through sendcharsets in descending |
|
262 | Finds out best charset by cycling through sendcharsets in descending | |
261 | order. Tries both encoding and fallbackencoding for input. Only as |
|
263 | order. Tries both encoding and fallbackencoding for input. Only as | |
262 | last resort send as is in fake ascii. |
|
264 | last resort send as is in fake ascii. | |
263 | Caveat: Do not use for mail parts containing patches!''' |
|
265 | Caveat: Do not use for mail parts containing patches!''' | |
264 | sendcharsets = charsets or _charsets(ui) |
|
266 | sendcharsets = charsets or _charsets(ui) | |
265 | if not isinstance(s, bytes): |
|
267 | if not isinstance(s, bytes): | |
266 | # We have unicode data, which we need to try and encode to |
|
268 | # We have unicode data, which we need to try and encode to | |
267 | # some reasonable-ish encoding. Try the encodings the user |
|
269 | # some reasonable-ish encoding. Try the encodings the user | |
268 | # wants, and fall back to garbage-in-ascii. |
|
270 | # wants, and fall back to garbage-in-ascii. | |
269 | for ocs in sendcharsets: |
|
271 | for ocs in sendcharsets: | |
270 | try: |
|
272 | try: | |
271 | return s.encode(pycompat.sysstr(ocs)), ocs |
|
273 | return s.encode(pycompat.sysstr(ocs)), ocs | |
272 | except UnicodeEncodeError: |
|
274 | except UnicodeEncodeError: | |
273 | pass |
|
275 | pass | |
274 | except LookupError: |
|
276 | except LookupError: | |
275 | ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs) |
|
277 | ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs) | |
276 | else: |
|
278 | else: | |
277 | # Everything failed, ascii-armor what we've got and send it. |
|
279 | # Everything failed, ascii-armor what we've got and send it. | |
278 | return s.encode('ascii', 'backslashreplace') |
|
280 | return s.encode('ascii', 'backslashreplace') | |
279 | # We have a bytes of unknown encoding. We'll try and guess a valid |
|
281 | # We have a bytes of unknown encoding. We'll try and guess a valid | |
280 | # encoding, falling back to pretending we had ascii even though we |
|
282 | # encoding, falling back to pretending we had ascii even though we | |
281 | # know that's wrong. |
|
283 | # know that's wrong. | |
282 | try: |
|
284 | try: | |
283 | s.decode('ascii') |
|
285 | s.decode('ascii') | |
284 | except UnicodeDecodeError: |
|
286 | except UnicodeDecodeError: | |
285 | for ics in (encoding.encoding, encoding.fallbackencoding): |
|
287 | for ics in (encoding.encoding, encoding.fallbackencoding): | |
286 | try: |
|
288 | try: | |
287 | u = s.decode(ics) |
|
289 | u = s.decode(ics) | |
288 | except UnicodeDecodeError: |
|
290 | except UnicodeDecodeError: | |
289 | continue |
|
291 | continue | |
290 | for ocs in sendcharsets: |
|
292 | for ocs in sendcharsets: | |
291 | try: |
|
293 | try: | |
292 | return u.encode(pycompat.sysstr(ocs)), ocs |
|
294 | return u.encode(pycompat.sysstr(ocs)), ocs | |
293 | except UnicodeEncodeError: |
|
295 | except UnicodeEncodeError: | |
294 | pass |
|
296 | pass | |
295 | except LookupError: |
|
297 | except LookupError: | |
296 | ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs) |
|
298 | ui.warn(_('ignoring invalid sendcharset: %s\n') % ocs) | |
297 | # if ascii, or all conversion attempts fail, send (broken) ascii |
|
299 | # if ascii, or all conversion attempts fail, send (broken) ascii | |
298 | return s, 'us-ascii' |
|
300 | return s, 'us-ascii' | |
299 |
|
301 | |||
300 | def headencode(ui, s, charsets=None, display=False): |
|
302 | def headencode(ui, s, charsets=None, display=False): | |
301 | '''Returns RFC-2047 compliant header from given string.''' |
|
303 | '''Returns RFC-2047 compliant header from given string.''' | |
302 | if not display: |
|
304 | if not display: | |
303 | # split into words? |
|
305 | # split into words? | |
304 | s, cs = _encode(ui, s, charsets) |
|
306 | s, cs = _encode(ui, s, charsets) | |
305 | return str(email.header.Header(s, cs)) |
|
307 | return str(email.header.Header(s, cs)) | |
306 | return s |
|
308 | return s | |
307 |
|
309 | |||
308 | def _addressencode(ui, name, addr, charsets=None): |
|
310 | def _addressencode(ui, name, addr, charsets=None): | |
309 | name = headencode(ui, name, charsets) |
|
311 | name = headencode(ui, name, charsets) | |
310 | try: |
|
312 | try: | |
311 | acc, dom = addr.split(r'@') |
|
313 | acc, dom = addr.split(r'@') | |
312 | acc = acc.encode('ascii') |
|
314 | acc = acc.encode('ascii') | |
313 | dom = dom.decode(encoding.encoding).encode('idna') |
|
315 | dom = dom.decode(encoding.encoding).encode('idna') | |
314 | addr = '%s@%s' % (acc, dom) |
|
316 | addr = '%s@%s' % (acc, dom) | |
315 | except UnicodeDecodeError: |
|
317 | except UnicodeDecodeError: | |
316 | raise error.Abort(_('invalid email address: %s') % addr) |
|
318 | raise error.Abort(_('invalid email address: %s') % addr) | |
317 | except ValueError: |
|
319 | except ValueError: | |
318 | try: |
|
320 | try: | |
319 | # too strict? |
|
321 | # too strict? | |
320 | addr = addr.encode('ascii') |
|
322 | addr = addr.encode('ascii') | |
321 | except UnicodeDecodeError: |
|
323 | except UnicodeDecodeError: | |
322 | raise error.Abort(_('invalid local address: %s') % addr) |
|
324 | raise error.Abort(_('invalid local address: %s') % addr) | |
323 | return pycompat.bytesurl( |
|
325 | return pycompat.bytesurl( | |
324 | email.utils.formataddr((name, encoding.strfromlocal(addr)))) |
|
326 | email.utils.formataddr((name, encoding.strfromlocal(addr)))) | |
325 |
|
327 | |||
326 | def addressencode(ui, address, charsets=None, display=False): |
|
328 | def addressencode(ui, address, charsets=None, display=False): | |
327 | '''Turns address into RFC-2047 compliant header.''' |
|
329 | '''Turns address into RFC-2047 compliant header.''' | |
328 | if display or not address: |
|
330 | if display or not address: | |
329 | return address or '' |
|
331 | return address or '' | |
330 | name, addr = email.utils.parseaddr(encoding.strfromlocal(address)) |
|
332 | name, addr = email.utils.parseaddr(encoding.strfromlocal(address)) | |
331 | return _addressencode(ui, name, addr, charsets) |
|
333 | return _addressencode(ui, name, addr, charsets) | |
332 |
|
334 | |||
333 | def addrlistencode(ui, addrs, charsets=None, display=False): |
|
335 | def addrlistencode(ui, addrs, charsets=None, display=False): | |
334 | '''Turns a list of addresses into a list of RFC-2047 compliant headers. |
|
336 | '''Turns a list of addresses into a list of RFC-2047 compliant headers. | |
335 | A single element of input list may contain multiple addresses, but output |
|
337 | A single element of input list may contain multiple addresses, but output | |
336 | always has one address per item''' |
|
338 | always has one address per item''' | |
337 | for a in addrs: |
|
339 | for a in addrs: | |
338 | assert isinstance(a, bytes), (r'%r unexpectedly not a bytestr' % a) |
|
340 | assert isinstance(a, bytes), (r'%r unexpectedly not a bytestr' % a) | |
339 | if display: |
|
341 | if display: | |
340 | return [a.strip() for a in addrs if a.strip()] |
|
342 | return [a.strip() for a in addrs if a.strip()] | |
341 |
|
343 | |||
342 | result = [] |
|
344 | result = [] | |
343 | for name, addr in email.utils.getaddresses( |
|
345 | for name, addr in email.utils.getaddresses( | |
344 | [encoding.strfromlocal(a) for a in addrs]): |
|
346 | [encoding.strfromlocal(a) for a in addrs]): | |
345 | if name or addr: |
|
347 | if name or addr: | |
346 | result.append(_addressencode(ui, name, addr, charsets)) |
|
348 | result.append(_addressencode(ui, name, addr, charsets)) | |
347 | return [pycompat.bytesurl(r) for r in result] |
|
349 | return [pycompat.bytesurl(r) for r in result] | |
348 |
|
350 | |||
349 | def mimeencode(ui, s, charsets=None, display=False): |
|
351 | def mimeencode(ui, s, charsets=None, display=False): | |
350 | '''creates mime text object, encodes it if needed, and sets |
|
352 | '''creates mime text object, encodes it if needed, and sets | |
351 | charset and transfer-encoding accordingly.''' |
|
353 | charset and transfer-encoding accordingly.''' | |
352 | cs = 'us-ascii' |
|
354 | cs = 'us-ascii' | |
353 | if not display: |
|
355 | if not display: | |
354 | s, cs = _encode(ui, s, charsets) |
|
356 | s, cs = _encode(ui, s, charsets) | |
355 | return mimetextqp(s, 'plain', cs) |
|
357 | return mimetextqp(s, 'plain', cs) | |
356 |
|
358 | |||
357 | if pycompat.ispy3: |
|
359 | if pycompat.ispy3: | |
358 | def parse(fp): |
|
360 | def parse(fp): | |
359 | ep = email.parser.Parser() |
|
361 | ep = email.parser.Parser() | |
360 | # disable the "universal newlines" mode, which isn't binary safe. |
|
362 | # disable the "universal newlines" mode, which isn't binary safe. | |
361 | # I have no idea if ascii/surrogateescape is correct, but that's |
|
363 | # I have no idea if ascii/surrogateescape is correct, but that's | |
362 | # what the standard Python email parser does. |
|
364 | # what the standard Python email parser does. | |
363 | fp = io.TextIOWrapper(fp, encoding=r'ascii', |
|
365 | fp = io.TextIOWrapper(fp, encoding=r'ascii', | |
364 | errors=r'surrogateescape', newline=chr(10)) |
|
366 | errors=r'surrogateescape', newline=chr(10)) | |
365 | try: |
|
367 | try: | |
366 | return ep.parse(fp) |
|
368 | return ep.parse(fp) | |
367 | finally: |
|
369 | finally: | |
368 | fp.detach() |
|
370 | fp.detach() | |
369 | else: |
|
371 | else: | |
370 | def parse(fp): |
|
372 | def parse(fp): | |
371 | ep = email.parser.Parser() |
|
373 | ep = email.parser.Parser() | |
372 | return ep.parse(fp) |
|
374 | return ep.parse(fp) | |
373 |
|
375 | |||
374 | def headdecode(s): |
|
376 | def headdecode(s): | |
375 | '''Decodes RFC-2047 header''' |
|
377 | '''Decodes RFC-2047 header''' | |
376 | uparts = [] |
|
378 | uparts = [] | |
377 | for part, charset in email.header.decode_header(s): |
|
379 | for part, charset in email.header.decode_header(s): | |
378 | if charset is not None: |
|
380 | if charset is not None: | |
379 | try: |
|
381 | try: | |
380 | uparts.append(part.decode(charset)) |
|
382 | uparts.append(part.decode(charset)) | |
381 | continue |
|
383 | continue | |
382 | except UnicodeDecodeError: |
|
384 | except UnicodeDecodeError: | |
383 | pass |
|
385 | pass | |
384 | # On Python 3, decode_header() may return either bytes or unicode |
|
386 | # On Python 3, decode_header() may return either bytes or unicode | |
385 | # depending on whether the header has =?<charset>? or not |
|
387 | # depending on whether the header has =?<charset>? or not | |
386 | if isinstance(part, type(u'')): |
|
388 | if isinstance(part, type(u'')): | |
387 | uparts.append(part) |
|
389 | uparts.append(part) | |
388 | continue |
|
390 | continue | |
389 | try: |
|
391 | try: | |
390 | uparts.append(part.decode('UTF-8')) |
|
392 | uparts.append(part.decode('UTF-8')) | |
391 | continue |
|
393 | continue | |
392 | except UnicodeDecodeError: |
|
394 | except UnicodeDecodeError: | |
393 | pass |
|
395 | pass | |
394 | uparts.append(part.decode('ISO-8859-1')) |
|
396 | uparts.append(part.decode('ISO-8859-1')) | |
395 | return encoding.unitolocal(u' '.join(uparts)) |
|
397 | return encoding.unitolocal(u' '.join(uparts)) |
General Comments 0
You need to be logged in to leave comments.
Login now