-def is_instance_registered(domain: str) -> bool:
- # 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' is empty")
-
- # DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
- if not cache.key_exists("is_registered"):
- # DEBUG: print(f"DEBUG: Cache for 'is_registered' not initialized, fetching all rows ...")
- try:
- cursor.execute("SELECT domain FROM instances")
-
- # Check Set all
- cache.set_all("is_registered", cursor.fetchall(), True)
- except BaseException as e:
- print(f"ERROR: failed SQL query: domain='{domain}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # Is cache found?
- registered = cache.sub_key_exists("is_registered", domain)
-
- # 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' is 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 blacklist.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_sub_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("last_status_code" , domain, None)
- instances.set("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, blocklist: dict):
- # DEBUG: print(f"DEBUG: instance={instance},blocklist()={len(blocklist)} - CALLED!")
- if type(domain) != str:
- raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
- elif domain == "":
- raise ValueError("Parameter 'domain' is empty")
- elif type(blocklist) != dict:
- raise ValueError(f"Parameter blocklist[]='{type(blocklist)}' is not 'dict'")
-
- message = instance + " has blocked the following instances:\n\n"
- truncated = False
-
- if len(blocklist) > 20:
- truncated = True
- blocklist = blocklist[0 : 19]
-
- # DEBUG: print(f"DEBUG: blocklist()={len(blocklist)}")
- for block in blocklist:
- # DEBUG: print(f"DEBUG: block['{type(block)}']={block}")
- 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.get("bot_token")}}
-
- req = reqto.post(
- f"{config.get('bot_instance')}/api/v1/statuses",
- data={
- "status" : message,
- "visibility" : config.get('bot_visibility'),
- "content_type": "text/plain"
- },
- headers=botheaders,
- timeout=10
- ).json()
-
- return True
-
-def fetch_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' is empty")
-
- # DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
- blocked = list()
-
- try:
- doc = bs4.BeautifulSoup(
- get_response(domain, "/friendica", headers, (config.get("connection_timeout"), config.get("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 {}
-
- table = blocklist.find("table")
-
- # DEBUG: print(f"DEBUG: table[]='{type(table)}'")
- if table.find("tbody"):
- rows = table.find("tbody").find_all("tr")
- else:
- rows = table.find_all("tr")
-
- # DEBUG: print(f"DEBUG: Found rows()={len(rows)}")
- for line in rows:
- # DEBUG: print(f"DEBUG: line='{line}'")
- blocked.append({
- "domain": tidyup_domain(line.find_all("td")[0].text),
- "reason": tidyup_reason(line.find_all("td")[1].text)
- })
- # DEBUG: print("DEBUG: Next!")
-
- # DEBUG: print("DEBUG: Returning blocklist() for domain:", domain, len(blocklist))
- return {
- "reject": blocked
- }
-
-def fetch_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' is empty")
-
- # DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
- blocklist = {
- "suspended": [],
- "blocked" : []
- }
-
- offset = 0
- step = config.get("misskey_limit")
- 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.get("misskey_limit"):
- # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
- offset = offset + (config.get("misskey_limit") - len(fetched))
- else:
- # DEBUG: print("DEBUG: Raising offset by step:", step)
- offset = offset + step
-
- count = 0
- for instance in fetched:
- # Is it there?
- if instance["isSuspended"] and not has_key(blocklist["suspended"], "domain", instance):
- count = count + 1
- blocklist["suspended"].append(
- {
- "domain": tidyup_domain(instance["host"]),
- # no reason field, nothing
- "reason": None
- }
- )
-
- # DEBUG: print(f"DEBUG: count={count}")
- if count == 0:
- # DEBUG: print(f"DEBUG: API is no more returning new instances, aborting loop!")
- break
-
- 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.get("misskey_limit"):
- # DEBUG: print(f"DEBUG: Fetched '{len(fetched)}' row(s) but expected: '{config.get('misskey_limit')}'")
- offset = offset + (config.get("misskey_limit") - len(fetched))
- else:
- # DEBUG: print("DEBUG: Raising offset by step:", step)
- offset = offset + step
-
- count = 0
- for instance in fetched:
- # Is it there?
- if instance["isBlocked"] and not has_key(blocklist["blocked"], "domain", instance):
- count = count + 1
- blocklist["blocked"].append({
- "domain": tidyup_domain(instance["host"]),
- "reason": None
- })
-
- # DEBUG: print(f"DEBUG: count={count}")
- if count == 0:
- # DEBUG: print(f"DEBUG: API is no more returning new instances, aborting loop!")
- break
-
- 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}' ...")
- instances.update_last_instance_fetch(domain)
-
- # DEBUG: print("DEBUG: Returning for domain,blocked(),suspended():", domain, len(blocklist["blocked"]), len(blocklist["suspended"]))
- return {
- "reject" : blocklist["blocked"],
- "followers_only": blocklist["suspended"]
- }
-