# HG changeset patch # User Mads Kiilerich # Date 2011-11-23 01:11:24 # Node ID cc58c228503e97dcb405ce0b4496c8b43b59ba1a # Parent 1830d0cc4bc1e5d99ec5ef954878e3ba59604470 mail: mbox handling as a part of mail handling, refactored from patchbomb diff --git a/hgext/patchbomb.py b/hgext/patchbomb.py --- a/hgext/patchbomb.py +++ b/hgext/patchbomb.py @@ -45,7 +45,7 @@ directly from the commandline. See the [ 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 @@ def patchbomb(ui, repo, *revs, **opts): 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) 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, quopri +import os, smtplib, socket, quopri, time import email.Header, email.MIMEText, email.Utils _oldheaderinit = email.Header.Header.__init__ @@ -93,9 +93,23 @@ def _sendmail(ui, sender, recipients, ms 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)