[PATCH 2 of 6] patchbomb: use mailcharset to encode headers and parts not containing patches

Christian Ebert blacktrash at gmx.net
Wed Jul 9 11:33:43 CDT 2008


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1205141667 -3600
# Node ID 340fd1f2ca254b3bd4007014345eb150b30ebf94
# Parent  1e5ec68b102fc39ba77d3761548c1e766ae37389
patchbomb: use mailcharset to encode headers and parts not containing patches

Preserve subject for display.

diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py
--- a/hgext/patchbomb.py
+++ b/hgext/patchbomb.py
@@ -64,7 +64,7 @@
 import os, errno, socket, tempfile, cStringIO
 import email.MIMEMultipart, email.MIMEText, email.MIMEBase
 import email.Utils, email.Encoders, email.Generator
-from mercurial import cmdutil, commands, hg, mail, patch, util
+from mercurial import cmdutil, commands, hg, mail, mailcharset, patch, util
 from mercurial.i18n import _
 from mercurial.node import bin
 
@@ -110,6 +110,8 @@
     Before using this command, you will need to enable email in your hgrc.
     See the [email] section in hgrc(5) for details.
     '''
+
+    mconv = mailcharset.converter(ui)
 
     def prompt(prompt, default = None, rest = ': ', empty_ok = False):
         if not ui.interactive:
@@ -176,7 +178,7 @@
         if opts.get('attach') or opts.get('inline'):
             msg = email.MIMEMultipart.MIMEMultipart()
             if body:
-                msg.attach(email.MIMEText.MIMEText(body, 'plain'))
+                msg.attach(mconv.mimeencode(body), **opts)
             p = email.MIMEText.MIMEText('\n'.join(patch), 'x-patch')
             binnode = bin(node)
             # if node is mq patch, it will have patch file name as tag
@@ -204,9 +206,9 @@
         else:
             tlen = len(str(total))
             subj = '[PATCH %0*d of %d] %s' % (tlen, idx, total, subj)
-        msg['Subject'] = subj
+        msg['Subject'] = mconv.headencode(subj, **opts)
         msg['X-Mercurial-Node'] = node
-        return msg
+        return msg, subj
 
     def outgoing(dest, revs):
         '''Return the revisions present locally but not in dest'''
@@ -328,10 +330,10 @@
                     body = '\n' + d
 
             body = getdescription(body, sender)
-            msg = email.MIMEText.MIMEText(body)
-            msg['Subject'] = subj
+            msg = mconv.mimeencode(body, **opts)
+            msg['Subject'] = mconv.headencode(subj, **opts)
 
-            msgs.insert(0, msg)
+            msgs.insert(0, (msg, subj))
         return msgs
 
     def getbundlemsgs(bundle):
@@ -341,15 +343,15 @@
         body = getdescription('', sender)
         msg = email.MIMEMultipart.MIMEMultipart()
         if body:
-            msg.attach(email.MIMEText.MIMEText(body, 'plain'))
+            msg.attach(mconv.mimeencode(body, **opts))
         datapart = email.MIMEBase.MIMEBase('application', 'x-mercurial-bundle')
         datapart.set_payload(bundle)
         datapart.add_header('Content-Disposition', 'attachment',
                             filename='bundle.hg')
         email.Encoders.encode_base64(datapart)
         msg.attach(datapart)
-        msg['Subject'] = subj
-        return [msg]
+        msg['Subject'] = mconv.headencode(subj, **opts)
+        return [(msg, subj)]
 
     sender = (opts.get('from') or ui.config('email', 'from') or
               ui.config('patchbomb', 'from') or
@@ -364,22 +366,24 @@
         addrs = opts.get(opt) or (ui.config('email', opt) or
                                   ui.config('patchbomb', opt) or
                                   prompt(prpt, default = default)).split(',')
-        return [a.strip() for a in addrs if a.strip()]
+        return [mconv.addressencode(a.strip(), **opts)
+                for a in addrs if a.strip()]
 
     to = getaddrs('to', 'To')
     cc = getaddrs('cc', 'Cc', '')
 
     bcc = opts.get('bcc') or (ui.config('email', 'bcc') or
                           ui.config('patchbomb', 'bcc') or '').split(',')
-    bcc = [a.strip() for a in bcc if a.strip()]
+    bcc = [mconv.addressencode(a.strip(), **opts) for a in bcc if a.strip()]
 
     ui.write('\n')
 
     parent = None
 
     sender_addr = email.Utils.parseaddr(sender)[1]
+    sender = mconv.addressencode(sender, **opts)
     sendmail = None
-    for m in msgs:
+    for m, subj in msgs:
         try:
             m['Message-Id'] = genmsgid(m['X-Mercurial-Node'])
         except TypeError:
@@ -398,7 +402,7 @@
         if bcc:
             m['Bcc'] = ', '.join(bcc)
         if opts.get('test'):
-            ui.status('Displaying ', m['Subject'], ' ...\n')
+            ui.status('Displaying ', subj, ' ...\n')
             ui.flush()
             if 'PAGER' in os.environ:
                 fp = util.popen(os.environ['PAGER'], 'w')
@@ -414,7 +418,7 @@
             if fp is not ui:
                 fp.close()
         elif opts.get('mbox'):
-            ui.status('Writing ', m['Subject'], ' ...\n')
+            ui.status('Writing ', subj, ' ...\n')
             fp = open(opts.get('mbox'), 'In-Reply-To' in m and 'ab+' or 'wb+')
             generator = email.Generator.Generator(fp, mangle_from_=True)
             date = util.datestr(start_time, '%a %b %d %H:%M:%S %Y')
@@ -425,7 +429,7 @@
         else:
             if not sendmail:
                 sendmail = mail.connect(ui)
-            ui.status('Sending ', m['Subject'], ' ...\n')
+            ui.status('Sending ', subj, ' ...\n')
             # Exim does not remove the Bcc field
             del m['Bcc']
             fp = cStringIO.StringIO()


More information about the Mercurial-devel mailing list