1 # Copyright (C) 2023 Free Software Foundation
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Affero General Public License as published
5 # by the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU Affero General Public License for more details.
13 # You should have received a copy of the GNU Affero General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 logging.basicConfig(level=logging.INFO)
20 logger = logging.getLogger(__name__)
22 def reason(string: str) -> str:
23 logger.debug("string='%s' - CALLED!", string)
24 if not isinstance(string, str):
25 raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
28 string = string.strip()
30 logger.debug("string='%s' - EXIT!", string)
33 def domain(string: str) -> str:
34 logger.debug("string='%s' - CALLED!", string)
35 if not isinstance(string, str):
36 raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
38 # All lower-case and strip spaces out + last dot
39 string = string.lower().strip().rstrip(".")
40 logger.debug("string='%s' - #1", string)
43 string = re.sub(r"\:\d+$", "", string)
44 logger.debug("string='%s' - #2", string)
46 # No protocol, sometimes without the slashes
47 string = re.sub(r"^https?\:(\/*)", "", string)
48 logger.debug("string='%s' - #3", string)
51 string = re.sub(r"\/$", "", string)
52 logger.debug("string='%s' - #4", string)
55 string = re.sub(r"^\@", "", string)
56 string = string.split(":")[0]
57 logger.debug("string='%s' - #5", string)
59 # Try to "detect" user profiles, not wanted here. Don't block single users
60 # in an instance block list! Everything personal can be solved in a
62 string = re.sub(r"(.+)\@", "", string)
63 logger.debug("string='%s' - #6", string)
65 if string.find("/profile/"):
66 string = string.split("/profile/")[0]
67 elif string.find("/users/"):
68 string = string.split("/users/")[0]
69 elif string.find("/tag/"):
70 string = string.split("/tag/")[0]
72 # Some people have TLDs with this word on the end
73 logger.debug("string='%s' - #7", string)
74 if string.endswith("silence"):
75 string = string.split("silence")[0]
77 logger.debug("string='%s' - EXIT!", string)