Example #1
0
 def test_decode_incomplete_string_fails(self):
     with self.assertRaises(netstring.IncompleteString):
         netstring.decode('3:abc')
     with self.assertRaises(netstring.IncompleteString):
         netstring.decode('3:ab')
     with self.assertRaises(netstring.IncompleteString):
         netstring.decode('3:')
     with self.assertRaises(netstring.IncompleteString):
         netstring.decode('3')
Example #2
0
 def test_feed_multiple(self):
     sequence = '4:this,2:is,1:a,4:test,'
     self.assertEqual([b'this', b'is', b'a', b'test'],
                      netstring.decode(sequence))
Example #3
0
 def test_decode_nested(self):
     self.assertEqual([b'5:Hello,6:World!,'],
                      netstring.decode('17:5:Hello,6:World!,,'))
Example #4
0
 def test_decode_missing_comma_fails(self):
     with self.assertRaises(netstring.IncompleteString):
         netstring.decode('3:abc_')
Example #5
0
 def test_decode_empty_string(self):
     self.assertEqual([b''], netstring.decode(b'0:,'))
Example #6
0
 def test_accept_leading_zero(self):
     self.assertEqual([b'X'], netstring.decode('01:X,'))
    def handle(self):
        self.data = pynetstring.decode(self.request.recv(1024))[0]
        data_parts = self.data.decode().split(' ')
        if len(data_parts) != 2:
            self.request.sendall(pynetstring.encode('PERM Invalid request'))
            return

        # We don't really care about the name of the request/lookup
        # lookup_name = parts[0]

        email_parts = data_parts[1].split('@')
        if len(email_parts) == 1:
            # Postfix does various lookups, not just with the to-address of the
            # received email. We'll just act like we don't know anything about them.
            self.request.sendall(pynetstring.encode('NOTFOUND '))
            return
        elif len(email_parts) != 2:
            # Either an empty key or several '@'... doesn't seem right.
            self.request.sendall(pynetstring.encode('PERM Invalid request'))
            return

        alias = email_parts[0]

        # Ignore lookup requests for other domains than our own
        if email_parts[1] != django.conf.settings.EMAIL_DOMAIN:
            self.request.sendall(pynetstring.encode('NOTFOUND '))
            return

        # If it's a bounce address, send it to the 'list-bounces' list
        if bounce_pattern.match(alias) is not None:
            alias = 'list-bounces'

        found = False

        try:
            email_list = models.EmailList.objects.get(alias=alias)
            found = True
        except models.EmailList.DoesNotExist:
            found = False

        if found == False:
            # Perhaps it's a username
            try:
                user = User.objects.annotate(
                    username_lower=django.db.models.functions.Lower(
                        'username')).get(username_lower=alias.lower())
                if user.person is not None and user.person.email is not None and user.person.email != '':
                    reply = 'OK {}'.format(user.person.email)
                    self.request.sendall(pynetstring.encode(reply))
                    return
                else:
                    found = False
            except User.DoesNotExist:
                found = False

        if found == False:
            # Perhaps it matches some common (and uncommon) misspellings
            try:
                email_list = models.EmailList.objects.get(
                    alias=spellcorrect(alias))
                found = True
            except models.EmailList.DoesNotExist:
                found = False

        if found == False:
            self.request.sendall(pynetstring.encode('NOTFOUND '))
            return

        addresses = email_list.get_recipients_email()
        if len(addresses) == 0:
            self.request.sendall(
                pynetstring.encode('TEMP No recipients in list'))
            return

        reply = 'OK {}'.format(','.join(addresses))
        self.request.sendall(pynetstring.encode(reply))