mail: mbox handling as a part of mail handling, refactored from patchbomb
authorMads Kiilerich <mads@kiilerich.com>
Wed, 23 Nov 2011 02:11:24 +0100
changeset 15560 cc58c228503e
parent 15559 1830d0cc4bc1
child 15561 ca572e94d8e7
mail: mbox handling as a part of mail handling, refactored from patchbomb
hgext/patchbomb.py
mercurial/mail.py
--- a/hgext/patchbomb.py	Wed Nov 23 02:09:38 2011 +0100
+++ b/hgext/patchbomb.py	Wed Nov 23 02:11:24 2011 +0100
@@ -45,7 +45,7 @@
 hgrc(5) for details.
 '''
 
-import os, errno, socket, tempfile, cStringIO, time
+import os, errno, socket, tempfile, cStringIO
 import email.MIMEMultipart, email.MIMEBase
 import email.Utils, email.Encoders, email.Generator
 from mercurial import cmdutil, commands, hg, mail, patch, util, discovery
@@ -532,25 +532,14 @@
                     raise
             if fp is not ui:
                 fp.close()
-        elif mbox:
+        else:
+            if not sendmail:
+                sendmail = mail.connect(ui, mbox=mbox)
             ui.status(_('Sending '), subj, ' ...\n')
             ui.progress(_('sending'), i, item=subj, total=len(msgs))
-            fp = open(mbox, i > 0 and 'ab+' or 'wb+')
-            generator = email.Generator.Generator(fp, mangle_from_=True)
-            # Should be time.asctime(), but Windows prints 2-characters day
-            # of month instead of one. Make them print the same thing.
-            date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
-            fp.write('From %s %s\n' % (sender_addr, date))
-            generator.flatten(m, 0)
-            fp.write('\n\n')
-            fp.close()
-        else:
-            if not sendmail:
-                sendmail = mail.connect(ui)
-            ui.status(_('Sending '), subj, ' ...\n')
-            ui.progress(_('sending'), i, item=subj, total=len(msgs))
-            # Exim does not remove the Bcc field
-            del m['Bcc']
+            if not mbox:
+                # Exim does not remove the Bcc field
+                del m['Bcc']
             fp = cStringIO.StringIO()
             generator = email.Generator.Generator(fp, mangle_from_=False)
             generator.flatten(m, 0)
--- a/mercurial/mail.py	Wed Nov 23 02:09:38 2011 +0100
+++ b/mercurial/mail.py	Wed Nov 23 02:11:24 2011 +0100
@@ -7,7 +7,7 @@
 
 from i18n import _
 import util, encoding
-import os, smtplib, socket, quopri
+import os, smtplib, socket, quopri, time
 import email.Header, email.MIMEText, email.Utils
 
 _oldheaderinit = email.Header.Header.__init__
@@ -93,9 +93,23 @@
             os.path.basename(program.split(None, 1)[0]),
             util.explainexit(ret)[0]))
 
-def connect(ui):
+def _mbox(mbox, sender, recipients, msg):
+    '''write mails to mbox'''
+    fp = open(mbox, 'ab+')
+    # Should be time.asctime(), but Windows prints 2-characters day
+    # of month instead of one. Make them print the same thing.
+    date = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
+    fp.write('From %s %s\n' % (sender, date))
+    fp.write(msg)
+    fp.write('\n\n')
+    fp.close()
+
+def connect(ui, mbox=None):
     '''make a mail connection. return a function to send mail.
     call as sendmail(sender, list-of-recipients, msg).'''
+    if mbox:
+        open(mbox, 'wb').close()
+        return lambda s, r, m: _mbox(mbox, s, r, m)
     if ui.config('email', 'method', 'smtp') == 'smtp':
         return _smtp(ui)
     return lambda s, r, m: _sendmail(ui, s, r, m)