# HG changeset patch # User Rocco Rutte # Date 2009-05-08 16:30:44 # Node ID 3e544c07445935c6920ab864dfd2fa394c731a2b # Parent 03cfc6ea93dffd2f4c530c31eb75045aa29b519e patchbomb: quoted-printable encode overly long lines RfC2822 mandates a line length limit of 998 byte + CRLF. Python mail tools break lines at 990 byte. To prevent that, we quoted-printable encode overly long lines. diff --git a/mercurial/mail.py b/mercurial/mail.py --- a/mercurial/mail.py +++ b/mercurial/mail.py @@ -7,7 +7,7 @@ from i18n import _ import util, encoding -import os, smtplib, socket +import os, smtplib, socket, quopri import email.Header, email.MIMEText, email.Utils def _smtp(ui): @@ -88,14 +88,31 @@ def validateconfig(ui): def mimetextpatch(s, subtype='plain', display=False): '''If patch in utf-8 transfer-encode it.''' + + enc = None + for line in s.splitlines(): + if len(line) > 950: + s = quopri.encodestring(s) + enc = "quoted-printable" + break + + cs = 'us-ascii' if not display: - for cs in ('us-ascii', 'utf-8'): + try: + s.decode('us-ascii') + except UnicodeDecodeError: try: - s.decode(cs) - return email.MIMEText.MIMEText(s, subtype, cs) + s.decode('utf-8') + cs = 'utf-8' except UnicodeDecodeError: + # We'll go with us-ascii as a fallback. pass - return email.MIMEText.MIMEText(s, subtype) + + msg = email.MIMEText.MIMEText(s, subtype, cs) + if enc: + del msg['Content-Transfer-Encoding'] + msg['Content-Transfer-Encoding'] = enc + return msg def _charsets(ui): '''Obtains charsets to send mail parts not containing patches.''' diff --git a/tests/test-patchbomb b/tests/test-patchbomb --- a/tests/test-patchbomb +++ b/tests/test-patchbomb @@ -58,7 +58,23 @@ echo "% md5sum of 8-bit output" $TESTDIR/md5sum.py mailtest rm mailtest -echo "% mime encoded mbox" +echo "% mime encoded mbox (base64)" +hg email --date '1970-1-1 0:4' -f quux -t foo -c bar -r tip -m mbox +cat mbox | fixheaders +rm mbox + +echo "% mime encoded mbox (quoted-printable)" +python -c 'fp = open("qp", "wb"); fp.write("%s\nfoo\n\nbar\n" % \ + ("x" * 1024)); fp.close();' +hg commit -A -d '4 0' -m 'charset=utf-8; content-transfer-encoding: quoted-printable' + +echo "% no mime encoding for email --test" +hg email --date '1970-1-1 0:4' -f quux -t foo -c bar -r tip -n | fixheaders > mailtest +echo "% md5sum of qp output" +$TESTDIR/md5sum.py mailtest +rm mailtest + +echo "% mime encoded mbox (quoted-printable)" hg email --date '1970-1-1 0:4' -f quux -t foo -c bar -r tip -m mbox cat mbox | fixheaders rm mbox @@ -85,17 +101,25 @@ echo "% test inline for single patch" hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -i 2 | \ fixheaders +echo "% test inline for single patch (quoted-printable)" +hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -i 4 | \ + fixheaders + echo "% test inline for multiple patches" -hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -i 0:1 | \ - fixheaders +hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -i \ + -r 0:1 -r 4 | fixheaders echo "% test attach for single patch" hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -a 2 | \ fixheaders +echo "% test attach for single patch (quoted-printable)" +hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -a 4 | \ + fixheaders + echo "% test attach for multiple patches" -hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -a 0:1 | \ - fixheaders +hg email --date '1970-1-1 0:1' -n -f quux -t foo -c bar -s test -a \ + -r 0:1 -r 4 | fixheaders echo "% test intro for single patch" hg email --date '1970-1-1 0:1' -n --intro -f quux -t foo -c bar -s test 2 | \ diff --git a/tests/test-patchbomb.out b/tests/test-patchbomb.out --- a/tests/test-patchbomb.out +++ b/tests/test-patchbomb.out @@ -158,7 +158,7 @@ adding utf % no mime encoding for email --test % md5sum of 8-bit output e726c29b3008e77994c7572563e57c34 mailtest -% mime encoded mbox +% mime encoded mbox (base64) This patch series consists of 1 patches. @@ -188,6 +188,59 @@ MCAxOTcwICswMDAwCisrKyBiL3V0ZglUaHUgSmFu LTAsMCArMSwxIEBACitow7ZtbWEhCg== +% mime encoded mbox (quoted-printable) +adding qp +% no mime encoding for email --test +% md5sum of qp output +0402c7d033e04044e423bb04816f9dae mailtest +% mime encoded mbox (quoted-printable) +This patch series consists of 1 patches. + + +Writing [PATCH] charset=utf-8; content-transfer-encoding: quoted-printable ... +From quux Thu Jan 01 00:04:01 1970 +Content-Type: text/plain; charset="us-ascii" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: [PATCH] charset=utf-8; content-transfer-encoding: quoted-printable +X-Mercurial-Node: c655633f8c87700bb38cc6a59a2753bdc5a6c376 +Message-Id: