]> git.mxchange.org Git - fba.git/blobdiff - fba/networks/misskey.py
Continued:
[fba.git] / fba / networks / misskey.py
index 0614a5728a399bfd5175599729843ece1404ea0c..e7c7053c78b988ec0d6f8da31328d86129277433 100644 (file)
 import json
 import logging
 
-from fba import csrf
-from fba import utils
-
+from fba.helpers import blacklist
 from fba.helpers import config
+from fba.helpers import dicts as dict_helper
 from fba.helpers import domain as domain_helper
 from fba.helpers import tidyup
 
+from fba.http import csrf
 from fba.http import network
 
 from fba.models import instances
@@ -35,6 +35,11 @@ def fetch_peers(domain: str) -> list:
     logger.debug("domain='%s' - CALLED!", domain)
     domain_helper.raise_on(domain)
 
+    if blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
+    elif not instances.is_registered(domain):
+        raise Exception(f"domain='{domain}' is not registered but function is invoked.")
+
     logger.debug("domain='%s' is misskey, sending API POST request ...", domain)
     peers  = list()
     offset = 0
@@ -47,8 +52,10 @@ def fetch_peers(domain: str) -> list:
         logger.debug("Checking CSRF for domain='%s'", domain)
         headers = csrf.determine(domain, dict())
     except network.exceptions as exception:
-        logger.warning("Exception '%s' during checking CSRF (fetch_peers,%s) - EXIT!", type(exception), __name__)
+        logger.warning("Exception '%s' during checking CSRF (fetch_peers,%s)", type(exception), __name__)
         instances.set_last_error(domain, exception)
+
+        logger.debug("Returning empty list ... - EXIT!")
         return list()
 
     # iterating through all "suspended" (follow-only in its terminology)
@@ -77,13 +84,13 @@ def fetch_peers(domain: str) -> list:
             instances.set_last_error(domain, fetched)
             break
         elif isinstance(fetched["json"], dict) and "error" in fetched["json"] and "message" in fetched["json"]["error"]:
-            logger.warning("post_json_api() returned error: '%s'", fetched['error']['message'])
+            logger.warning("post_json_api() returned error: '%s'", fetched["json"]["error"]["message"])
             instances.set_last_error(domain, fetched["json"]["error"]["message"])
             break
 
         rows = fetched["json"]
 
-        logger.debug("rows(%d)[]='%s'", len(rows), type(rows))
+        logger.debug("rows(%d)[]='%s',step=%d", len(rows), type(rows), step)
         if len(rows) == 0:
             logger.debug("Returned zero bytes, domain='%s' - BREAK!", domain)
             break
@@ -94,7 +101,7 @@ def fetch_peers(domain: str) -> list:
             logger.debug("Raising offset by step=%d", step)
             offset = offset + step
 
-        already = 0
+        added = 0
         logger.debug("rows(%d))[]='%s'", len(rows), type(rows))
         for row in rows:
             logger.debug("row()=%d", len(row))
@@ -102,21 +109,22 @@ def fetch_peers(domain: str) -> list:
                 logger.warning("row()=%d does not contain key 'host': row='%s',domain='%s' - SKIPPED!", len(row), row, domain)
                 continue
             elif not isinstance(row["host"], str):
-                logger.warning("row[host][]='%s' is not 'str' - SKIPPED!", type(row['host']))
-                continue
-            elif not utils.is_domain_wanted(row["host"]):
-                logger.debug("row[host]='%s' is not wanted, domain='%s' - SKIPPED!", row['host'], domain)
+                logger.warning("row[host][]='%s' is not of type 'str' - SKIPPED!", type(row['host']))
                 continue
             elif row["host"] in peers:
                 logger.debug("Not adding row[host]='%s', already found - SKIPPED!", row['host'])
-                already = already + 1
+                continue
+            elif not domain_helper.is_wanted(row["host"]):
+                logger.debug("row[host]='%s' is not wanted - SKIPPED!", row["host"])
                 continue
 
             logger.debug("Adding peer: row[host]='%s'", row['host'])
+            added = added + 1
             peers.append(row["host"])
 
-        if already == len(rows):
-            logger.debug("Host returned same set of %d instance(s) - BREAK!", already)
+        logger.debug("added=%d,rows()=%d", added, len(rows))
+        if added == 0:
+            logger.debug("Host returned already added (%d) peers - BREAK!", len(rows))
             break
 
     logger.debug("peers()=%d - EXIT!", len(peers))
@@ -126,26 +134,33 @@ def fetch_blocks(domain: str) -> list:
     logger.debug("domain='%s' - CALLED!", domain)
     domain_helper.raise_on(domain)
 
-    logger.debug("Fetching misskey blocks from domain='%s'", domain)
-    blocklist = list()
-
-    offset  = 0
-    step    = config.get("misskey_limit")
+    if blacklist.is_blacklisted(domain):
+        raise Exception(f"domain='{domain}' is blacklisted but function is invoked.")
+    elif not instances.is_registered(domain):
+        raise Exception(f"domain='{domain}' is not registered but function is invoked.")
 
     # No CSRF by default, you don't have to add network.api_headers by yourself here
     headers = tuple()
 
     try:
-        logger.debug("Checking CSRF for domain='%s'", domain)
+        logger.debug("Checking CSRF for domain='%s' ...", domain)
         headers = csrf.determine(domain, dict())
     except network.exceptions as exception:
-        logger.warning("Exception '%s' during checking CSRF (fetch_blocks,%s) - EXIT!", type(exception), __name__)
+        logger.warning("Exception '%s' during checking CSRF (fetch_blocks,%s)", type(exception), __name__)
         instances.set_last_error(domain, exception)
+
+        logger.debug("Returning empty list ... - EXIT!")
         return list()
 
+    blocklist = list()
+    offset    = 0
+    step      = config.get("misskey_limit")
+
     # iterating through all "suspended" (follow-only in its terminology)
     # instances page-by-page since it doesn't support sending them all at once
+    logger.debug("Fetching misskey blocks from domain='%s'", domain)
     while True:
+        logger.debug("offset=%d", offset)
         try:
             logger.debug("Fetching offset=%d from domain='%s' ...", offset, domain)
             if offset == 0:
@@ -172,7 +187,7 @@ def fetch_blocks(domain: str) -> list:
                 instances.set_last_error(domain, fetched)
                 break
             elif isinstance(fetched["json"], dict) and "error" in fetched["json"] and "message" in fetched["json"]["error"]:
-                logger.warning("post_json_api() returned error: '%s'", fetched['error']['message'])
+                logger.warning("post_json_api() returned error: '%s'", fetched["json"]["error"]["message"])
                 instances.set_last_error(domain, fetched["json"]["error"]["message"])
                 break
 
@@ -190,14 +205,33 @@ def fetch_blocks(domain: str) -> list:
                 offset = offset + step
 
             count = 0
+            logger.debug("Checking %d row(s) of instances ...", len(rows))
             for instance in rows:
                 # Is it there?
-                logger.debug("instance[%s]='%s'", type(instance), instance)
-                if "isSuspended" in instance and instance["isSuspended"]:
+                logger.debug("instance[]='%s'", type(instance))
+                if "host" not in instance:
+                    logger.warning("instance(%d)='%s' has no key 'host' - SKIPPED!", len(instance), instance)
+                    continue
+                elif instance["host"] in [None, ""]:
+                    logger.debug("instance[host]='%s' is None or empty - SKIPPED!", instance["host"])
+                    continue
+
+                logger.debug("instance[host]='%s' - BEFORE!", instance["host"])
+                blocked = tidyup.domain(instance["host"])
+                logger.debug("blocked[%s]='%s' - AFTER!", type(blocked), blocked)
+
+                if blocked in [None, ""]:
+                    logger.warning("instance[host]='%s' is None or empty after tidyup.domain() - SKIPPED!", instance["host"])
+                    continue
+                elif not domain_helper.is_wanted(blocked):
+                    logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
+                    continue
+                elif "isSuspended" in instance and instance["isSuspended"] and not dict_helper.has_key(blocklist, "blocked", blocked):
                     count = count + 1
+                    logger.debug("Appending blocker='%s',blocked='%s',block_level='suspended'", domain, blocked)
                     blocklist.append({
                         "blocker"    : domain,
-                        "blocked"    : tidyup.domain(instance["host"]),
+                        "blocked"    : blocked,
                         "reason"     : None,
                         "block_level": "suspended",
                     })
@@ -215,6 +249,7 @@ def fetch_blocks(domain: str) -> list:
 
     while True:
         # Fetch blocked (full suspended) instances
+        logger.debug("offset=%d", offset)
         try:
             if offset == 0:
                 logger.debug("Sending JSON API request to domain='%s',step=%d,offset=%d", domain, step, offset)
@@ -240,7 +275,7 @@ def fetch_blocks(domain: str) -> list:
                 instances.set_last_error(domain, fetched)
                 break
             elif isinstance(fetched["json"], dict) and "error" in fetched["json"] and "message" in fetched["json"]["error"]:
-                logger.warning("post_json_api() returned error: '%s'", fetched['error']['message'])
+                logger.warning("post_json_api() returned error: '%s'", fetched["json"]["error"]["message"])
                 instances.set_last_error(domain, fetched["json"]["error"]["message"])
                 break
 
@@ -258,12 +293,21 @@ def fetch_blocks(domain: str) -> list:
                 offset = offset + step
 
             count = 0
+            logger.debug("Checking %d row(s) of instances ...", len(rows))
             for instance in rows:
                 # Is it there?
-                logger.debug("instance[%s]='%s'", type(instance), instance)
-                if "isBlocked" in instance and instance["isBlocked"]:
+                logger.debug("instance[]='%s'", type(instance))
+                blocked = tidyup.domain(instance["host"]) if instance["host"] != "" else None
+                logger.debug("blocked='%s' - AFTER!", blocked)
+
+                if blocked in [None, ""]:
+                    logger.warning("instance[host]='%s' is None or empty after tidyup.domain() - SKIPPED!", instance["host"])
+                    continue
+                elif not domain_helper.is_wanted(blocked):
+                    logger.debug("blocked='%s' is not wanted - SKIPPED!", blocked)
+                    continue
+                elif "isBlocked" in instance and instance["isBlocked"] and not dict_helper.has_key(blocklist, "blocked", blocked):
                     count = count + 1
-                    blocked = tidyup.domain(instance["host"])
                     logger.debug("Appending blocker='%s',blocked='%s',block_level='reject'", domain, blocked)
                     blocklist.append({
                         "blocker"    : domain,