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

Christian Ebert blacktrash at gmx.net
Sat Mar 8 10:01:50 CST 2008


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1204991652 -3600
# Node ID a246888643b4462a49e022418f2b3dcaed621a0f
# Parent  7e43019601e5f4b597e91aaa6498ef15ac14462f
patchbomb: use mailutil 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
@@ -70,6 +70,7 @@
 from mercurial import cmdutil, commands, hg, mail, patch, util
 from mercurial.i18n import _
 from mercurial.node import bin
+from hgext import mailutil
 
 def patchbomb(ui, repo, *revs, **opts):
     '''send changesets by email
@@ -113,6 +114,8 @@
     Before using this command, you will need to enable email in your hgrc.
     See the [email] section in hgrc(5) for details.
     '''
+
+    mconv = mailutil.converter(ui)
 
     def prompt(prompt, default = None, rest = ': ', empty_ok = False):
         if not ui.interactive:
@@ -179,7 +182,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))
             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
@@ -207,9 +210,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)
         msg['X-Mercurial-Node'] = node
-        return msg
+        return msg, subj
 
     def outgoing(dest, revs):
         '''Return the revisions present locally but not in dest'''
@@ -331,10 +334,10 @@
                     body = '\n' + d
 
             body = getdescription(body, sender)
-            msg = email.MIMEText.MIMEText(body)
-            msg['Subject'] = subj
+            msg = mconv.mimeencode(body)
+            msg['Subject'] = mconv.headencode(subj)
 
-            msgs.insert(0, msg)
+            msgs.insert(0, (msg, subj))
         return msgs
 
     def getbundlemsgs(bundle):
@@ -344,15 +347,15 @@
         body = getdescription('', sender)
         msg = email.MIMEMultipart.MIMEMultipart()
         if body:
-            msg.attach(email.MIMEText.MIMEText(body, 'plain'))
+            msg.attach(mconv.mimeencode(body))
         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)
+        return [(msg, subj)]
 
     sender = (opts.get('from') or ui.config('email', 'from') or
               ui.config('patchbomb', 'from') or
@@ -367,7 +370,8 @@
         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()]
+        addrs = [a.strip() for a in addrs if a.strip()]
+        return [mconv.addressencode(a) for a in addrs]
 
     to = getaddrs('to', 'To')
     cc = getaddrs('cc', 'Cc', '')
@@ -375,14 +379,16 @@
     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) for a in bcc]
 
     ui.write('\n')
 
     parent = None
 
     sender_addr = email.Utils.parseaddr(sender)[1]
+    sender = mconv.addressencode(sender)
     sendmail = None
-    for m in msgs:
+    for m, subj in msgs:
         try:
             m['Message-Id'] = genmsgid(m['X-Mercurial-Node'])
         except TypeError:
@@ -402,7 +408,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 = os.popen(os.environ['PAGER'], 'w')
@@ -417,7 +423,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+')
             date = util.datestr(date=start_time,
                                 format='%a %b %d %H:%M:%S %Y', timezone=False)
@@ -428,7 +434,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']
             sendmail(sender, to + bcc + cc, m.as_string(0))


More information about the Mercurial-devel mailing list