On August 25, 2005 at 7:34PM +0900,
tats (at vega.ocn.ne.jp) wrote:

> To fix this bug, Nedko Arnaudov revised the patch, and I sorted out
> and revised it.
>
> I can now recommend the attached patch.
>
> * feedparser.py (_sync_author_detail): Replace '<>' with ''.
> * rss2email.py (header7bit): Use email.Header instead of mimify.
> * rss2email.py (header7bit_ifnonatom): New function.
> * rss2email.py (run): Encode `From:' with header7bit_ifnonatom(), and don't
>   encode `To:'.
> * rss2email.py (run): Insert `Mime-Version:' and `Content-Transfer-Encoding:'.

I've revised the patch.  Please replace it with the attached patch.
(Sorry, the patch of the previous mail is broken.)

The attached patch tries to use us-ascii instead of utf-8 for
header fields and body.

* feedparser.py (_sync_author_detail): Replace '<>' with ''.
* rss2email.py (header7bit): Use email.Header instead of mimify.
* rss2email.py (header7bit_phrase): New function.
* rss2email.py (run): Encode `From:' with header7bit_phrase(), and don't
  encode `To:'.
* rss2email.py (run): Insert `MIME-Version:' and `Content-Transfer-Encoding:'.
* rss2email.py (run): Set charset to us-ascii or utf-8.

--
Tatsuya Kinoshita
--- rss2email-2.55-1/feedparser.py
+++ rss2email-2.55/feedparser.py
@@ -811,6 +811,7 @@
             # probably a better way to do the following, but it passes all the 
tests
             author = author.replace(email, '')
             author = author.replace('()', '')
+            author = author.replace('<>', '')
             author = author.strip()
             if author and (author[0] == '('):
                 author = author[1:]
--- rss2email-2.55-1/rss2email.py
+++ rss2email-2.55/rss2email.py
@@ -107,6 +107,8 @@
 for e in ['error', 'gaierror']:
        if hasattr(socket, e): socket_errors.append(getattr(socket, e))
 import mimify; from StringIO import StringIO as SIO; mimify.CHARSET = 'utf-8'
+from email.Header import Header
+import re
 if SMTP_SEND: import smtplib; smtpserver = smtplib.SMTP(SMTP_SERVER)
 else: smtpserver = None
 
@@ -135,13 +137,27 @@
        """Quote names in email according to RFC822."""
        return '"' + unu(s).replace("\\", "\\\\").replace('"', '\\"') + '"'
 
+nonascii = re.compile('[^\000-\177]')
+nonatom = 
re.compile('[^a-zA-Z0-9\011\012\015\040\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]')
 # ref. RFC2822, atom.  comment is not supported
+
 def header7bit(s):
        """QP_CORRUPT headers."""
-       #return mimify.mime_encode_header(s + ' ')[:-1]
-       # XXX due to mime_encode_header bug
-       import re
-       p = re.compile('=\n([^ \t])');
-       return p.sub(r'\1', mimify.mime_encode_header(s + ' ')[:-1])
+       charset = 'us-ascii'
+       if nonascii.search(s):
+               charset = 'utf-8'
+       h = Header(s, charset, 50)
+       return h.encode()
+
+def header7bit_phrase(s):
+       """QP_CORRUPT headers for phrase."""
+       if nonascii.search(s):
+               charset = 'utf-8'
+       else:
+               charset = 'us-ascii'
+               if nonatom.search(s):
+                       s = quote822(s)
+       h = Header(s, charset, 50)
+       return h.encode()
 
 ### Parsing Utilities ###
 
@@ -405,12 +421,13 @@
                                        from_addr = unu(getEmail(r.feed, entry))
 
                                        message = (
-                                       "From: " + 
quote822(header7bit(getName(r, entry))) + " <"+from_addr+">" +
-                                       "\nTo: " + header7bit(unu(f.to or 
default_to)) + # set a default email!
+                                       "From: " + 
header7bit_phrase(unu(getName(r, entry))) + " <"+from_addr+">" +
+                                       "\nTo: " + unu(f.to or default_to) + # 
set a default email!
                                        "\nSubject: " + header7bit(title) +
                                        "\nDate: " + time.strftime("%a, %d %b 
%Y %H:%M:%S -0000", datetime) +
                                        "\nUser-Agent: rss2email" + # really 
should be X-Mailer 
                                        BONUS_HEADER +
+                                       "\nMIME-Version: 1.0" +
                                        "\nContent-Type: ")         # but 
backwards-compatibility
                                        
                                        if ishtml(content):
@@ -425,7 +442,11 @@
                                                message += "text/plain"
                                                content = unu(content).strip() 
+ "\n\nURL: "+link
                                        
-                                       message += '; charset="utf-8"\n\n' + 
content + "\n"
+                                       if nonascii.search(content):
+                                               message += '; 
charset="utf-8"\nContent-Transfer-Encoding: 8bit'
+                                       else:
+                                               message += '; 
charset="us-ascii"\nContent-Transfer-Encoding: 7bit'
+                                       message += "\n\n" + content + "\n"
 
                                        if QP_REQUIRED:
                                                ins, outs = SIO(message), SIO()

Attachment: pgpjOKeitIYOy.pgp
Description: PGP signature

Reply via email to