Ticket #69: idna.patch

File idna.patch, 2.4 kB (added by fs, 8 months ago)

proposed patch which still has some problems as it breaks some validity tests

  • tests/test_address_list.py

    diff -r 0725f5b9ebc7 tests/test_address_list.py
    a b  
    55 
    66 
    77import unittest 
    8 from turbomail.util import AddressList 
     8from turbomail.util import Address, AddressList 
    99try: 
    1010    from email.mime.text import MIMEText 
    1111except ImportError: 
     
    1515import logging 
    1616logging.disable(logging.WARNING) 
    1717 
     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-cua.test>", str(addr)) 
    1823 
    1924class TestAddressList(unittest.TestCase): 
    2025    """Test the AddressList helper class.""" 
  • turbomail/util.py

    diff -r 0725f5b9ebc7 turbomail/util.py
    a b  
    2626     
    2727    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})$') 
    2828     
     29    def _assert_valid_emailaddress(self, address): 
     30        mailparts = address.split("@") 
     31        if len(mailparts) != 2: 
     32            raise ValueError, 'An e-mail address must contain a single "@" character!' 
     33        localpart, domain = mailparts 
     34        if len(localpart) == 0: 
     35            raise ValueError, 'local part is empty!' 
     36        if len(domain) == 0: 
     37            raise ValueError, 'The domain must not be empty!' 
     38     
     39    def _convert_punycode(self, address): 
     40        localpart, domain = address.split("@") 
     41        if isinstance(domain, unicode): 
     42            domain = domain.encode("idna") 
     43        return "%s@%s" % (localpart, domain) 
     44     
    2945    def __init__(self, *args): 
    3046        assert len(args) < 3 and len(args) > 0, "You must specify an address or a name and address." 
    31          
    3247        if len(args) == 1 and isinstance(args[0], basestring): 
    3348            self.name, self.address = parseaddr(args[0]) 
    3449             
     
    3853        else: 
    3954            self.name, self.address = args 
    4055         
    41         if not self.validator.match(self.address): 
    42             raise ValueError, 'Invalid e-mail address.' 
    43          
     56        self._assert_valid_emailaddress(self.address) 
     57        self.address = self._convert_punycode(self.address) 
    4458        super(Address, self).__init__() 
    4559     
    4660    def __cmp__(self, other):