[PATCH 3 of 5] notify: send mime messages with mailutil

Christian Ebert blacktrash at gmx.net
Tue Feb 19 13:16:54 CST 2008


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1203448082 -3600
# Node ID 6f75570a97dbc60c98da98214978ec5553f16399
# Parent  21c697fe1dee7f6714c7956d1d00fc50392ff7ea
notify: send mime messages with mailutil

- addresses will be properly encoded
- message bodies will also be encoded as we are not sending
  patches that are meant to be applied
- update test output
- adapt test-keyword to ignore the new headers

diff --git a/hgext/notify.py b/hgext/notify.py
--- a/hgext/notify.py
+++ b/hgext/notify.py
@@ -68,6 +68,7 @@
 from mercurial.i18n import _
 from mercurial.node import *
 from mercurial import patch, cmdutil, templater, util, mail
+from hgext import mailutil
 import email.Parser, fnmatch, socket, time
 
 # template for single changeset can include email headers.
@@ -106,8 +107,8 @@
         self.stripcount = int(self.ui.config('notify', 'strip', 0))
         self.root = self.strip(self.repo.root)
         self.domain = self.ui.config('notify', 'domain')
+        self.mconv = mailutil.converter(self.ui)
         self.subs = self.subscribers()
-
         mapfile = self.ui.config('notify', 'style')
         template = (self.ui.config('notify', hooktype) or
                     self.ui.config('notify', 'template'))
@@ -156,7 +157,7 @@
             if fnmatch.fnmatch(self.repo.root, pat):
                 for user in users.split(','):
                     subs[self.fixmail(user)] = 1
-        subs = subs.keys()
+        subs = [self.mconv.addressencode(a) for a in subs.keys()]
         subs.sort()
         return subs
 
@@ -182,10 +183,16 @@
         p = email.Parser.Parser()
         msg = p.parsestr(data)
 
-        def fix_subject():
+        # store sender and subject
+        sender, subject = msg['From'], msg['Subject']
+        # create fresh mime message from msg body
+        text = msg.get_payload()
+        # for notification prefer readability over data precision
+        msg = self.mconv.mimeencode(text)
+
+        def fix_subject(subject):
             '''try to make subject line exist and be useful.'''
 
-            subject = msg['Subject']
             if not subject:
                 if count > 1:
                     subject = _('%s: %d new changesets') % (self.root, count)
@@ -196,25 +203,22 @@
             maxsubject = int(self.ui.config('notify', 'maxsubject', 67))
             if maxsubject and len(subject) > maxsubject:
                 subject = subject[:maxsubject-3] + '...'
-            del msg['Subject']
-            msg['Subject'] = subject
+            msg['Subject'] = self.mconv.headencode(subject)
 
-        def fix_sender():
+        def fix_sender(sender):
             '''try to make message have proper sender.'''
 
-            sender = msg['From']
             if not sender:
                 sender = self.ui.config('email', 'from') or self.ui.username()
             if '@' not in sender or '@localhost' in sender:
                 sender = self.fixmail(sender)
-            del msg['From']
-            msg['From'] = sender
+            msg['From'] = self.mconv.addressencode(sender)
 
         msg['Date'] = util.datestr(date=util.makedate(),
                                    format="%a, %d %b %Y %H:%M:%S",
                                    timezone=True)
-        fix_subject()
-        fix_sender()
+        fix_subject(subject)
+        fix_sender(sender)
 
         msg['X-Hg-Notification'] = 'changeset ' + short(node)
         if not msg['Message-Id']:
diff --git a/tests/test-keyword b/tests/test-keyword
--- a/tests/test-keyword
+++ b/tests/test-keyword
@@ -103,7 +103,7 @@
 EOF
 
 echo % pull from bundle
-hg pull -u ../kw.hg 2>&1 | sed -e '/^Date:/,/^diffs (/ d'
+hg pull -u ../kw.hg 2>&1 | sed -e '/^Content-Type:/,/^diffs (/ d'
 
 echo % remove notify config
 sed -e '/\[hooks\]/,$ d' $HGRCPATH > $HGRCPATH.nonotify
diff --git a/tests/test-notify.out b/tests/test-notify.out
--- a/tests/test-notify.out
+++ b/tests/test-notify.out
@@ -13,6 +13,9 @@
 adding manifests
 adding file changes
 added 1 changesets with 1 changes to 1 files
+Content-Type: text/plain; charset="us-ascii"
+MIME-Version: 1.0
+Content-Transfer-Encoding: 7bit
 Date:
 Subject: changeset in test-notify/b: b
 From: test
@@ -45,6 +48,9 @@
 adding manifests
 adding file changes
 added 1 changesets with 1 changes to 1 files
+Content-Type: text/plain; charset="us-ascii"
+MIME-Version: 1.0
+Content-Transfer-Encoding: 7bit
 Date:
 Subject: b
 From: test at test.com


More information about the Mercurial-devel mailing list