surblclient 0.2.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env python
2
+ """SURBL checker (http://www.surbl.org/)
3
+
4
+ Example usage:
5
+ >>> from surblclient import surbl
6
+ >>> domain = "foo.bar.test.surbl.org"
7
+ >>> domain in surbl
8
+ True
9
+ >>> surbl.lookup(domain)
10
+ ('test.surbl.org', ['ph', 'mw', 'abuse', 'cr'])
11
+ >>> if domain in surbl:
12
+ ... print "%s blacklisted in %s" % surbl.lookup(domain)
13
+ ...
14
+ test.surbl.org blacklisted in ['ph', 'mw', 'abuse', 'cr']
15
+ """
16
+
17
+ # Copyright (c) 2026 Filip Salo
18
+ #
19
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
20
+ # of this software and associated documentation files (the "Software"), to deal
21
+ # in the Software without restriction, including without limitation the rights
22
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23
+ # copies of the Software, and to permit persons to whom the Software is
24
+ # furnished to do so, subject to the following conditions:
25
+ #
26
+ # The above copyright notice and this permission notice shall be included in
27
+ # all copies or substantial portions of the Software.
28
+ #
29
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
35
+ # THE SOFTWARE.
36
+
37
+ from .blacklist import Blacklist # noqa: F401
38
+ from .surbl import SURBL
39
+ from .uribl import URIBL
40
+
41
+ # from .spamhausdbl import SpamhausDBLBlacklist
42
+
43
+ VERSION = "0.2.0a1"
44
+
45
+ surbl = SURBL()
46
+ uribl = URIBL()
47
+ # spamhausdbl = Blacklist("dbl.spamhaus.org")
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # Copyright (c) 2026 Filip Salo
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ """Main class for the blacklists"""
24
+
25
+ import socket
26
+ from typing import Literal
27
+
28
+
29
+ def is_ip_address(domain) -> bool:
30
+ """Return True if `domain` is an IP address"""
31
+ return all(part.isdigit() for part in domain.split("."))
32
+
33
+
34
+ class Blacklist:
35
+ """An RBL blacklist"""
36
+
37
+ domain = ""
38
+ flags = []
39
+
40
+ def __init__(self) -> None:
41
+ self._cache = (None, None)
42
+
43
+ def get_base_domain(self, domain: str) -> str:
44
+ """Return the base domain to use for RBL lookup"""
45
+ return domain
46
+
47
+ def _lookup_exact(
48
+ self, domain: str
49
+ ) -> tuple[str, list[str]] | Literal[False] | None:
50
+ """Like 'lookup', but checks the exact domain name given.
51
+ Not for direct use.
52
+ """
53
+ cached_domain, flags = self._cache
54
+ if cached_domain != domain:
55
+ try:
56
+ lookup_domain = domain
57
+ if is_ip_address(domain):
58
+ lookup_domain = ".".join(reversed(domain.split(".")))
59
+ ip_address = socket.gethostbyname(lookup_domain + "." + self.domain)
60
+ flags = int(ip_address.split(".")[-1])
61
+ except socket.gaierror as err:
62
+ if err.errno in (socket.EAI_NONAME, socket.EAI_NODATA):
63
+ # No record found
64
+ flags = None
65
+ self._cache = (domain, flags)
66
+ return False
67
+ # Unhandled error, pass test for now
68
+ return None
69
+ except OSError:
70
+ # Not sure if this can happen. Timeouts?
71
+ return None
72
+ self._cache = (domain, flags)
73
+ if flags:
74
+ if flags & 1:
75
+ # Blocked from making queries
76
+ return None
77
+ return (domain, [s for (n, s) in self.flags if flags & n])
78
+ return False
79
+
80
+ def lookup(self, domain: str) -> tuple[str, list[str]] | Literal[False] | None:
81
+ """Extract base domain and check it against SURBL.
82
+ Return (basedomain, lists) tuple, where basedomain is the
83
+ base domain and lists is a list of strings indicating which
84
+ blacklists the domain was found in.
85
+ If there was no match, return False.
86
+ If unsure (temporary error), return None.
87
+ """
88
+ # Remove userinfo
89
+ if "@" in domain:
90
+ domain = domain[domain.index("@") + 1 :]
91
+
92
+ # Remove port
93
+ if ":" in domain:
94
+ domain = domain[: domain.index(":")]
95
+
96
+ if not is_ip_address(domain):
97
+ domain = self.get_base_domain(domain)
98
+ return self._lookup_exact(domain)
99
+
100
+ def __contains__(self, domain: str) -> bool:
101
+ """Return True if base domain is listed in this blacklist;
102
+ False otherwise.
103
+ """
104
+ return bool(self.lookup(domain))