Pārlūkot izejas kodu

Added teamspeak plugin

Kirk Trombley 6 gadi atpakaļ
vecāks
revīzija
3aa89bbeee
2 mainītis faili ar 46 papildinājumiem un 0 dzēšanām
  1. 1 0
      config/config.toml
  2. 45 0
      src/plugins/teamspeak.py

+ 1 - 0
config/config.toml

@@ -1,5 +1,6 @@
 [plugins]
 echo_plugin = [ "echo" ]
+teamspeak = [ "teamspeak" ]
 
 [postgres]
 host = "db"

+ 45 - 0
src/plugins/teamspeak.py

@@ -0,0 +1,45 @@
+from telnetlib import Telnet
+
+from command_system import RollbotResponse, RollbotFailure, as_plugin
+from config import get_config, get_secret
+
+TS3_HOST = get_config("teamspeak.host")
+_TS3_USER = get_config("teamspeak.user")
+_TS3_PASS = get_secret("teamspeak.pass")
+TS3_LOGIN = ("login %s %s\n" % (_TS3_USER, _TS3_PASS)).encode("utf-8")
+
+@as_plugin
+def teamspeak(db, message):
+    try:
+        with Telnet(TS3_HOST, 10011, 5) as tn:
+            tn.write(TS3_LOGIN)
+            tn.write(b"use 1 -virtual\n")
+            tn.write(b"clientlist\n")
+            tn.write(b"whoami\n")
+            tn.write(b"quit\n")
+            response = tn.read_until(b"virtualserver_status").decode("ascii")
+    except Exception as e:
+        return RollbotResponse(
+            message,
+            failure=RollbotFailure.SERVICE_DOWN,
+            debugging={
+                "explain": "Encountered failure during Telnet calls.",
+                "exception": e
+            }
+        )
+
+    if "banned" in response:
+        return RollbotResponse(
+            message,
+            failure=RollbotFailure.SERVICE_DOWN,
+            debugging={ "explain": "Rate limiting issue." }
+        )
+
+    nicks = response.split()
+    nicks = [x.split("=")[1] for x in nicks if x.startswith("client_nickname")]
+    nicks = [y for y in nicks if "serveradmin" not in y]
+    if len(nicks) == 0:
+        return RollbotResponse(message, txt="No one in teamspeak!")
+    if len(nicks) == 1:
+        return RollbotResponse(message, txt="Only "+nicks[0])
+    return RollbotResponse(message, txt="I see the following people: "+", ".join(nicks))