- # NOISY-DEBUG: # DEBUG: print(f"DEBUG: registered='{registered}' - EXIT!")
- return registered
-
-def add_instance(domain: str, origin: str, originator: str, path: str = None):
- # DEBUG: print(f"DEBUG: domain={domain},origin={origin},originator={originator},path={path} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
- elif type(origin) != str and origin != None:
- raise ValueError(f"origin[]={type(origin)} is not 'str'")
- elif type(originator) != str:
- raise ValueError(f"originator[]={type(originator)} is not 'str'")
- elif originator == "":
- raise ValueError(f"originator cannot be empty")
- elif not validators.domain(domain.split("/")[0]):
- raise ValueError(f"Bad domain name='{domain}'")
- elif origin is not None and not validators.domain(origin.split("/")[0]):
- raise ValueError(f"Bad origin name='{origin}'")
- elif is_blacklisted(domain):
- raise Exception(f"domain='{domain}' is blacklisted, but method invoked")
-
- # DEBUG: print("DEBUG: domain,origin,originator,path:", domain, origin, originator, path)
- software = determine_software(domain, path)
- # DEBUG: print("DEBUG: Determined software:", software)
-
- print(f"INFO: Adding instance domain='{domain}' (origin='{origin}',software='{software}')")
- try:
- cursor.execute(
- "INSERT INTO instances (domain, origin, originator, hash, software, first_seen) VALUES (?, ?, ?, ?, ?, ?)",
- (
- domain,
- origin,
- originator,
- get_hash(domain),
- software,
- time.time()
- ),
- )
-
- cache.set_cache_key("is_registered", domain, True)
-
- if instances.has_pending_instance_data(domain):
- # DEBUG: print(f"DEBUG: domain='{domain}' has pending nodeinfo being updated ...")
- instances.set_instance_data("last_status_code" , domain, None)
- instances.set_instance_data("last_error_details", domain, None)
- instances.update_instance_data(domain)
- remove_pending_error(domain)
-
- if domain in pending_errors:
- # DEBUG: print("DEBUG: domain has pending error being updated:", domain)
- update_last_error(domain, pending_errors[domain])
- remove_pending_error(domain)
-
- except BaseException as e:
- print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
- else:
- # DEBUG: print("DEBUG: Updating nodeinfo for domain:", domain)
- update_last_nodeinfo(domain)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def send_bot_post(instance: str, blocks: dict):
- # DEBUG: print(f"DEBUG: instance={instance},blocks()={len(blocks)} - CALLED!")
- message = instance + " has blocked the following instances:\n\n"
- truncated = False
-
- if len(blocks) > 20:
- truncated = True
- blocks = blocks[0 : 19]
-
- for block in blocks:
- if block["reason"] == None or block["reason"] == '':
- message = message + block["blocked"] + " with unspecified reason\n"
- else:
- if len(block["reason"]) > 420:
- block["reason"] = block["reason"][0:419] + "[…]"
-
- message = message + block["blocked"] + ' for "' + block["reason"].replace("@", "@\u200b") + '"\n'
-
- if truncated:
- message = message + "(the list has been truncated to the first 20 entries)"
-
- botheaders = {**api_headers, **{"Authorization": "Bearer " + config["bot_token"]}}
-
- req = reqto.post(
- f"{config['bot_instance']}/api/v1/statuses",
- data={
- "status" : message,
- "visibility" : config['bot_visibility'],
- "content_type": "text/plain"
- },
- headers=botheaders,
- timeout=10
- ).json()
-
- return True
-
-def get_mastodon_blocks(domain: str) -> dict:
- # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
-
- # DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
- blocks = {
- "Suspended servers": [],
- "Filtered media" : [],
- "Limited servers" : [],
- "Silenced servers" : [],
- }
-
- try:
- doc = bs4.BeautifulSoup(
- reqto.get(f"https://{domain}/about", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
- "html.parser",
- )
- except BaseException as e:
- print("ERROR: Cannot fetch from domain:", domain, e)
- update_last_error(domain, e)
- return {}
-
- for header in doc.find_all("h3"):
- header_text = tidyup_domain(header.text)
-
- if header_text in language_mapping:
- # DEBUG: print(f"DEBUG: header_text='{header_text}'")
- header_text = language_mapping[header_text]
-
- if header_text in blocks or header_text.lower() in blocks:
- # replaced find_next_siblings with find_all_next to account for instances that e.g. hide lists in dropdown menu
- for line in header.find_all_next("table")[0].find_all("tr")[1:]:
- blocks[header_text].append(
- {
- "domain": tidyup_domain(line.find("span").text),
- "hash" : tidyup_domain(line.find("span")["title"][9:]),
- "reason": tidyup_domain(line.find_all("td")[1].text),
- }
- )
-
- # DEBUG: print("DEBUG: Returning blocks for domain:", domain)
- return {
- "reject" : blocks["Suspended servers"],
- "media_removal" : blocks["Filtered media"],
- "followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
- }
-
-def get_friendica_blocks(domain: str) -> dict:
- # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
-
- # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
- blocks = []
-
- try:
- doc = bs4.BeautifulSoup(
- reqto.get(f"https://{domain}/friendica", headers=headers, timeout=(config["connection_timeout"], config["read_timeout"])).text,
- "html.parser",
- )
- except BaseException as e:
- print("WARNING: Failed to fetch /friendica from domain:", domain, e)
- update_last_error(domain, e)
- return {}
-
- blocklist = doc.find(id="about_blocklist")
-
- # Prevents exceptions:
- if blocklist is None:
- # DEBUG: print("DEBUG: Instance has no block list:", domain)
- return {}
-
- for line in blocklist.find("table").find_all("tr")[1:]:
- # DEBUG: print(f"DEBUG: line='{line}'")
- blocks.append({
- "domain": tidyup_domain(line.find_all("td")[0].text),
- "reason": tidyup_domain(line.find_all("td")[1].text)
- })
-
- # DEBUG: print("DEBUG: Returning blocks() for domain:", domain, len(blocks))
- return {
- "reject": blocks
- }
-
-def get_misskey_blocks(domain: str) -> dict:
- # DEBUG: print(f"DEBUG: domain={domain} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
- elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
-
- # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
- blocks = {
- "suspended": [],
- "blocked" : []
- }
-
- offset = 0
- step = config["misskey_offset"]
- while True:
- # iterating through all "suspended" (follow-only in its terminology)
- # instances page-by-page, since that troonware doesn't support
- # sending them all at once
- try:
- # DEBUG: print(f"DEBUG: Fetching offset='{offset}' from '{domain}' ...")
- if offset == 0:
- # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
- fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
- "sort" : "+pubAt",
- "host" : None,
- "suspended": True,
- "limit" : step
- }), {"Origin": domain})
- else:
- # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
- fetched = post_json_api(domain, "/api/federation/instances", json.dumps({
- "sort" : "+pubAt",
- "host" : None,
- "suspended": True,
- "limit" : step,
- "offset" : offset - 1
- }), {"Origin": domain})
-
- # DEBUG: print("DEBUG: fetched():", len(fetched))
- if len(fetched) == 0:
- # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
- break
- elif len(fetched) != config["misskey_offset"]:
- # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
- offset = offset + (config["misskey_offset"] - len(fetched))
- else:
- # DEBUG: print("DEBUG: Raising offset by step:", step)
- offset = offset + step
-
- for instance in fetched:
- # just in case
- if instance["isSuspended"]:
- blocks["suspended"].append(
- {
- "domain": tidyup_domain(instance["host"]),
- # no reason field, nothing
- "reason": None
- }
- )
-
- except BaseException as e:
- print("WARNING: Caught error, exiting loop:", domain, e)
- update_last_error(domain, e)
- offset = 0
- break
-
- while True:
- # same shit, different asshole ("blocked" aka full suspend)
- try:
- if offset == 0:
- # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
- fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
- "sort" : "+pubAt",
- "host" : None,
- "blocked": True,
- "limit" : step
- }), {"Origin": domain})
- else:
- # DEBUG: print("DEBUG: Sending JSON API request to domain,step,offset:", domain, step, offset)
- fetched = post_json_api(domain,"/api/federation/instances", json.dumps({
- "sort" : "+pubAt",
- "host" : None,
- "blocked": True,
- "limit" : step,
- "offset" : offset-1
- }), {"Origin": domain})
-
- # DEBUG: print("DEBUG: fetched():", len(fetched))
- if len(fetched) == 0:
- # DEBUG: print("DEBUG: Returned zero bytes, exiting loop:", domain)
- break
- elif len(fetched) != config["misskey_offset"]:
- # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config['misskey_offset']}'")
- offset = offset + (config["misskey_offset"] - len(fetched))
- else:
- # DEBUG: print("DEBUG: Raising offset by step:", step)
- offset = offset + step
-
- for instance in fetched:
- if instance["isBlocked"]:
- blocks["blocked"].append({
- "domain": tidyup_domain(instance["host"]),
- "reason": None
- })
-
- except BaseException as e:
- print("ERROR: Exception during POST:", domain, e)
- update_last_error(domain, e)
- offset = 0
- break
-
- # DEBUG: print(f"DEBUG: Updating last_instance_fetch for domain='{domain}' ...")
- update_last_instance_fetch(domain)
-
- # DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocks["blocked"]), len(blocks["suspended"]))
- return {
- "reject" : blocks["blocked"],
- "followers_only": blocks["suspended"]
- }