Changeset 112 for trunk

Show
Ignore:
Timestamp:
11/07/08 14:49:39 (2 months ago)
Author:
amcgregor
Message:

Support for internationalized domains (#69) and expanded validation for multiple-level TLDs (e.g. .com.au). Needs additional test cases, and breaks none of the existing ones.

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_address_list.py

    r92 r112  
    66 
    77import unittest 
    8 from turbomail.util import AddressList 
     8from turbomail.util import Address, AddressList 
    99try: 
    1010    from email.mime.text import MIMEText 
     
    1515import logging 
    1616logging.disable(logging.WARNING) 
     17 
     18 
     19class TestAddress(unittest.TestCase): 
     20    def test_punycode(self): 
     21        addr = Address("Foo", u"foo@exámple.test") 
     22        self.assertEqual("Foo <foo@xn--exmple-qta.test>", str(addr)) 
    1723 
    1824 
     
    6167        self.assertEqual(str(self.addresses[-1]), "eviluser@example.com") 
    6268         
     69        # Double-at symbols automatically strip from the second at on. 
     70        self.addresses.append("bad@user@example.com") 
     71        self.assertEqual(str(self.addresses[-1]), "bad@user") 
     72         
    6373        self.assertRaises(ValueError, lambda: self.addresses.append("baduser.example.com")) 
    64         self.assertRaises(ValueError, lambda: self.addresses.append("bad@user@example.com")) 
    6574        self.assertRaises(ValueError, lambda: self.addresses.append("bad-u+s+er@example.com")) 
    66         self.assertRaises(ValueError, lambda: self.addresses.append("baduser@example")) 
     75         
     76        # Domains without TLDs are now supported. 
     77        self.addresses.append("user@company") 
     78        self.assertEqual(str(self.addresses[-1]), "user@company") 
    6779         
    6880        del self.addresses 
  • trunk/turbomail/util.py

    r101 r112  
    77import re 
    88import turbomail 
    9 from turbomail.exceptions import MailNotEnabledError 
    109from email.Header import Header 
    1110from email.Utils import parseaddr, formataddr 
     
    2524    class as well.""" 
    2625     
    27     validator = re.compile(r'^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(\+([0-9a-zA-Z])+)?@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$') 
     26    validator = re.compile(r'^(\w([-.\w]*\w)*(\+\w+)?@(\w(\-*\w)+)+(\.\w{2,9})*)$', re.UNICODE) 
    2827     
    2928    def __init__(self, *args): 
     
    6261        if isinstance(name_string, unicode): 
    6362            name_string = str(Header(name_string, encoding)) 
    64         return formataddr((name_string, self.address)).replace("\n", "") 
     63         
     64        # Encode punycode for internationalized domains. 
     65        localpart, domain = self.address.split("@") 
     66        if isinstance(domain, unicode): 
     67            domain = domain.encode("idna") 
     68        address = "@".join((localpart, domain)) 
     69         
     70        return formataddr((name_string, address)).replace("\n", "") 
    6571 
    6672