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)
25 if not isinstance(string, str):
26 raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
29 string = string.strip()
31 logger.debug("string='%s' - EXIT!", string)
34 def domain(string: str) -> str:
35 logger.debug("string='%s' - CALLED!", string)
37 if not isinstance(string, str):
38 raise ValueError(f"Parameter string[]='{type(string)}' is not of type 'str'")
40 raise ValueError("Parameter string is empty")
42 # All lower-case and strip spaces out + last dot
43 string = string.lower().strip().rstrip(".").replace("..", ".")
44 logger.debug("string='%s' - #1", string)
47 string = re.sub(r"\:\d+$", "", string)
48 logger.debug("string='%s' - #2", string)
50 # No protocol, sometimes without the slashes
51 string = re.sub(r"^https?\:(\/*)", "", string)
52 logger.debug("string='%s' - #3", string)
55 string = re.sub(r"\/$", "", string)
56 logger.debug("string='%s' - #4", string)
59 string = re.sub(r"^\@", "", string)
60 string = string.split(":")[0]
61 logger.debug("string='%s' - #5", string)
63 # Try to "detect" user profiles, not wanted here. Don't block single users
64 # in an instance block list! Everything personal can be solved in a
66 string = re.sub(r"(.+)\@", "", string)
67 logger.debug("string='%s' - #6", string)
69 if string.find("/profile/"):
70 string = string.split("/profile/")[0]
71 elif string.find("/users/"):
72 string = string.split("/users/")[0]
73 elif string.find("/tag/"):
74 string = string.split("/tag/")[0]
76 # Some people have TLDs with this word on the end
77 logger.debug("string='%s' - #7", string)
78 if string.endswith("silence"):
79 string = string.split("silence")[0]
81 logger.debug("string='%s' - EXIT!", string)