On 11/09/2012 09:31 AM, C. Michael Pilato wrote: > On 09/04/2012 12:58 PM, Igor Galić wrote: >> Hey folks, >> >> I've patched up my local mailer.py[1] to correctly encode >> "From: " Headers. >> >> The attached patch *works* but it's not pretty. >> >> Someone who actually knows Python might want to encapsulate >> my changes in a function. >> >> n.b.: I am not subscribed to this ML, if you want notify me >> of updates, please CC me. >> >> So long, >> >> i >> >> [1] >> http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/mailer/mailer.py >> > > Igor, I took a look at your patch. I was confused by one aspect: why split > the from_addr into words an encode each one individually, rather than > encoding the whole header value like we do for the Subject: header? I dug > around in the related RFCs, and if I'm reading the RFCs correctly, doing > per-word encoding is actually the right thing to do. Should we, then, do > the same for the Subject: header?
Attaching my current modification of your original patch. -- C. Michael Pilato <cmpil...@collab.net> CollabNet <> www.collab.net <> Enterprise Cloud Development
* tools/hook-scripts/mailer/mailer.py (MailedOutput.mail_headers): Modularize the code which encodes header value tokens, and use it for encoding both the Subject: and From: lines as necessary. Index: tools/hook-scripts/mailer/mailer.py =================================================================== --- tools/hook-scripts/mailer/mailer.py (revision 1407458) +++ tools/hook-scripts/mailer/mailer.py (working copy) @@ -228,12 +228,18 @@ self.reply_to = self.reply_to[3:] def mail_headers(self, group, params): - subject = self.make_subject(group, params) - try: - subject.encode('ascii') - except UnicodeError: - from email.Header import Header - subject = Header(subject, 'utf-8').encode() + + def _maybe_encode_header(hdr): + try: + hdr.encode('ascii') + return hdr + except UnicodeError: + from email.Header import Header + return Header(hdr, 'utf-8').encode() + + subject = ' '.join(map(_maybe_encode_header, + self.make_subject(group, params).split())) + from_hdr = ' '.join(map(_maybe_encode_header, self.from_addr.split())) hdrs = 'From: %s\n' \ 'To: %s\n' \ 'Subject: %s\n' \ @@ -244,7 +250,7 @@ 'X-Svn-Commit-Author: %s\n' \ 'X-Svn-Commit-Revision: %d\n' \ 'X-Svn-Commit-Repository: %s\n' \ - % (self.from_addr, ', '.join(self.to_addrs), subject, + % (from_hdr, ', '.join(self.to_addrs), subject, group, self.repos.author or 'no_author', self.repos.rev, os.path.basename(self.repos.repos_dir)) if self.reply_to:
signature.asc
Description: OpenPGP digital signature