]> git.mxchange.org Git - fba.git/commitdiff
Report findings to a bot
authorMint <>
Sun, 12 Mar 2023 21:21:46 +0000 (00:21 +0300)
committerMint <>
Sun, 12 Mar 2023 21:21:46 +0000 (00:21 +0300)
config.defaults.json
fetch_blocks.py

index 97a5258ec484d96f3b240dea8455a0aadf604b47..06a32301ea0617461b63d2f7a01014e1d5880edf 100644 (file)
@@ -1,5 +1,9 @@
 {
     "base_url": "",
     "port": 8069,
-    "useragent": "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0"
+    "useragent": "Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0",
+    "bot_enabled": false,
+    "bot_instance": "https://example.com",
+    "bot_token": "",
+    "bot_visibility": "unlisted"
 }
index 8b39eedf348c94079c99636d950db80c8e4df70f..f79a206d830214cec6f273f0b0d1716d99d7eacd 100644 (file)
@@ -7,6 +7,7 @@ from json import dumps
 from json import loads
 import re
 from time import time
+import itertools
 
 with open("config.json") as f:
     config = loads(f.read())
@@ -15,6 +16,26 @@ headers = {
     "user-agent": config["useragent"]
 }
 
+def send_bot_post(instance: str, blocks: dict):
+    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:
+            message = message + block["blocked"] + ' for "' + block["reason"] + '"\n'
+    if truncated:
+        message = message + "(the list has been truncated to the first 20 entries)"
+
+    botheaders = {**headers, **{"Authorization": "Bearer " + config["bot_token"]}}
+    req = post(f"{config['bot_instance']}/api/v1/statuses",
+        data={"status":message, "visibility":config['bot_visibility'], "content_type":"text/plain"},
+        headers=botheaders, timeout=10).json()
+    print(req)
+    return True
 
 def get_mastodon_blocks(domain: str) -> dict:
     blocks = {
@@ -205,10 +226,12 @@ conn = sqlite3.connect("blocks.db")
 c = conn.cursor()
 
 c.execute(
-    "select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
+    #"select domain, software from instances where software in ('pleroma', 'mastodon', 'friendica', 'misskey', 'gotosocial')"
+    "select domain, software from instances where domain = 'mstdn.social'"
 )
 
 for blocker, software in c.fetchall():
+    blockdict = []
     blocker = tidyup(blocker)
     if software == "pleroma":
         print(blocker)
@@ -253,6 +276,12 @@ for blocker, software in c.fetchall():
                                 "insert into blocks select ?, ?, '', ?, ?, ?",
                                 (blocker, blocked, block_level, timestamp, timestamp),
                             )
+                            if block_level == "reject":
+                                blockdict.append(
+                                    {
+                                        "blocked": blocked,
+                                        "reason": None
+                                    })
                         else:
                             c.execute(
                                 "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
@@ -283,6 +312,10 @@ for blocker, software in c.fetchall():
                             "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
                             (reason["reason"], blocker, blocked, block_level),
                         )
+                        for entry in blockdict:
+                            if entry["blocked"] == blocked:
+                                entry["reason"] = reason["reason"]
+
             conn.commit()
         except Exception as e:
             print("error:", e, blocker)
@@ -364,6 +397,12 @@ for blocker, software in c.fetchall():
                                 timestamp,
                             ),
                         )
+                        if block_level == "reject":
+                            blockdict.append(
+                                {
+                                    "blocked": blocked,
+                                    "reason": reason
+                                })
                     else:
                         c.execute(
                             "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
@@ -433,6 +472,12 @@ for blocker, software in c.fetchall():
                                 timestamp
                             ),
                         )
+                        if block_level == "reject":
+                            blockdict.append(
+                                {
+                                    "blocked": blocked,
+                                    "reason": reason
+                                })
                     else:
                         c.execute(
                             "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
@@ -483,6 +528,11 @@ for blocker, software in c.fetchall():
                         "insert into blocks select ?, ?, ?, ?, ?, ?",
                            (blocker, blocked, "", "reject", timestamp, timestamp),
                     )
+                    blockdict.append(
+                        {
+                            "blocked": blocked,
+                            "reason": None
+                        })
                 else:
                     c.execute(
                         "update blocks set last_seen = ? where blocker = ? and blocked = ? and block_level = ?",
@@ -494,7 +544,15 @@ for blocker, software in c.fetchall():
                         "update blocks set reason = ? where blocker = ? and blocked = ? and block_level = ? and reason = ''",
                         (reason, blocker, blocked, "reject"),
                     )
+                    for entry in blockdict:
+                        if entry["blocked"] == blocked:
+                            entry["reason"] = reason
             conn.commit()
         except Exception as e:
             print("error:", e, blocker)
+
+    if config["bot_enabled"] and len(blockdict) > 0:
+        send_bot_post(blocker, blockdict)
+    blockdict = []
+
 conn.close()