# HG changeset patch # User Mads Kiilerich # Date 1322010684 -3600 # Node ID cc58c228503e97dcb405ce0b4496c8b43b59ba1a # Parent 1830d0cc4bc1e5d99ec5ef954878e3ba59604470 mail: mbox handling as a part of mail handling, refactored from patchbomb diff -r 1830d0cc4bc1 -r cc58c228503e hgext/patchbomb.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) diff -r 1830d0cc4bc1 -r cc58c228503e mercurial/mail.py --- 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)