about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2021-02-24 15:47:20 -0500
committerBen Harris <ben@tilde.team>2021-02-24 15:47:20 -0500
commitfb60e68b3f8d09333bfe495098fef59e01fc81a6 (patch)
tree362def4f5a5eb369c3117c89b319087bae7c4431
parent1e42b6d655b7940afd3ef04e97c018173a1f2cf1 (diff)
tidy up accounts
channel key in account's json will limit that key to that channel
-rw-r--r--account.json.example (renamed from account.json.sample)0
-rw-r--r--tildeverse.json.example8
-rw-r--r--tooter.py77
3 files changed, 41 insertions, 44 deletions
diff --git a/account.json.sample b/account.json.example
index 229c11e..229c11e 100644
--- a/account.json.sample
+++ b/account.json.example
diff --git a/tildeverse.json.example b/tildeverse.json.example
new file mode 100644
index 0000000..278308d
--- /dev/null
+++ b/tildeverse.json.example
@@ -0,0 +1,8 @@
+{
+    "client_id": "myclientid",
+    "client_secret": "myclientsecret",
+    "access_token": "myaccesstoken",
+    "base_url": "https://tilde.zone",
+    "channel": "remove this key if you don't want to limit the bot"
+}
+
diff --git a/tooter.py b/tooter.py
index f34209b..ba80e2e 100644
--- a/tooter.py
+++ b/tooter.py
@@ -10,38 +10,35 @@ import emoji
 import glob
 import json
 import os
-import re
 import sys
 
 HELPTEXT = "helo i can send toots from irc: @tildeverse@tilde.zone - from @tildeteam from the #team channel)"
 
-
-def masto_from_json(conf):
-    conf = json.load(conf)
-    return Mastodon(
-        client_id=conf["client_id"],
-        client_secret=conf["client_secret"],
-        access_token=conf["access_token"],
-        api_base_url=conf["base_url"],
-    )
-
-
 masto = {}
+assigned_channels = {}
 # load masto creds
 for cred in glob.glob("tilde*.json"):
     shortname, _ = os.path.splitext(cred)
     with open(cred, "r") as f:
-        masto[shortname] = masto_from_json(f)
+        conf = json.load(f)
+        if "channel" in conf:
+            assigned_channels[conf["channel"]] = shortname
+        masto[shortname] = Mastodon(
+            client_id=conf["client_id"],
+            client_secret=conf["client_secret"],
+            access_token=conf["access_token"],
+            api_base_url=conf["base_url"],
+        )
+
+if "tildeverse" not in masto:
+    print("you must populate tildeverse.json with mastodon credentials")
+    exit(1)
 
 # do setup
-path = os.path.dirname(os.path.abspath(__file__))
-
-with open(os.path.join(path, "config.json"), "r") as f:
+with open("config.json", "r") as f:
     config = json.load(f)
-
-if os.path.isfile(os.path.join(path, "account.json")):
-    with open(os.path.join(path, "account.json"), "r") as f:
-        account = json.load(f)
+with open("account.json", "r") as f:
+    account = json.load(f)
 
 channels = config["channels"]
 if len(sys.argv) > 1:
@@ -52,25 +49,20 @@ if len(sys.argv) > 1:
 def think(line):
     chan = line.params[0]
     words = line.params[1].split(" ")
-
-    if len(words) > 0:
-        cmd = words[0].lower()
-        if cmd == "!toot":
-            if len(words) >= 2:
-                status = emoji.emojize(" ".join(words[1:]), use_aliases=True)
-                if chan == "#team":
-                    res = masto["tildeteam"].toot(status)
-                elif chan == "#club":
-                    res = masto["tildeclub"].toot(status)
-                else:
-                    res = masto["tildeverse"].toot(status)
-                print(res)
-                return "tooted! {}".format(res["url"])
-            else:
-                return HELPTEXT
-        elif cmd == "!source":
-            return "https://tildegit.org/ben/tooter"
-        elif cmd in ["!botlist", "!toothelp"]:
+    cmd = words[0].lower()
+
+    if cmd == "!source":
+        return "https://tildegit.org/ben/tooter"
+    elif cmd in ["!botlist", "!toothelp"]:
+        return HELPTEXT
+    elif cmd == "!toot":
+        if len(words) >= 2:
+            status = emoji.emojize(" ".join(words[1:]), use_aliases=True)
+            res = masto[
+                assigned_channels[chan] if chan in assigned_channels else "tildeverse"
+            ].toot(status)
+            return "tooted! {}".format(res["url"])
+        else:
             return HELPTEXT
 
 
@@ -81,18 +73,17 @@ class Server(BaseServer):
     async def line_read(self, line: Line):
         print(f"{self.name} < {line.format()}")
         if line.command == "001":
-            print(f"connected to {self.isupport.network}")
             await self.send(build("JOIN", [",".join(channels)]))
             await self.send(build("MODE", [self.nickname, "+B"]))
 
         if line.command == "INVITE":
-            print(f"received invite to {line.params[1:]}")
             await self.send(build("JOIN", line.params[1:]))
 
         if (
             line.command == "PRIVMSG"
             and self.has_channel(line.params[0])
             and self.has_user(line.hostmask.nickname)
+            and len(line.params[1].split(" ")) > 0
             and not line.hostmask is None
             and not self.casefold(line.hostmask.nickname) == self.nickname_lower
             and not ("batch" in line.tags and line.tags["batch"] == "1")
@@ -108,8 +99,6 @@ class Bot(BaseBot):
 
 
 async def main():
-    bot = Bot()
-
     params = ConnectionParams(
         config["botnick"],
         host=config["address"],
@@ -117,7 +106,7 @@ async def main():
         tls=config["tls"],
         sasl=SASLUserPass(account["username"], account["password"]),
     )
-
+    bot = Bot()
     await bot.add_server("tilde", params)
     await bot.run()