123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- #!/usr/bin/env python3
- from telnetlib import Telnet
- import toml
- from flask import Flask, jsonify, render_template_string
- app = Flask(__name__)
- def get_users():
- with open("secret.toml") as infile:
- cfg = toml.load(infile)
- login = ("login %s %s\n" % (cfg["user"], cfg["pass"])).encode("utf-8")
- with Telnet(cfg["host"], cfg["port"], 5) as tn:
- print("connection")
- print(tn.read_until(b"\n").decode("ascii"))
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- tn.write(login)
- print("after login")
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- tn.write(b"use 1 -virtual\n")
- print("after use")
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- tn.write(b"clientlist\n")
- response = tn.read_until(b"\n").decode("ascii")
- print("after clientlist")
- print(response)
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- # entries separated by |'s
- entries = response.split("|")
- # entries contain key=value pairs separated by spaces
- pairs = [[pr.split("=", 1) for pr in ent.split()] for ent in entries]
- # rearrange these into maps for convenience
- entry_maps = [{k: v for k, v in pr} for pr in pairs]
- # combine the maps into one large map, ignoring serveradmin query user
- client_info = {info["client_nickname"]: info for info in entry_maps if "serveradmin" not in info["client_nickname"]}
- for k, v in client_info.items():
- tn.write(f"clientinfo clid={v['clid']}\n".encode("utf-8"))
- response = tn.read_until(b"\n").decode("ascii")
- print(f"after clientinfo for {k}")
- print(response)
- print(tn.read_until(b"\n").decode("ascii"))
- print("----")
- # info is key=value pairs separated by spaces
- pairs = [ent.split("=", 1) for ent in response.split() if "=" in ent]
- # rearrange into a map and put in the client_info
- v["client_info"] = {k: v for k, v in pairs}
- tn.write(b"quit\n")
- print("after quit")
- print(tn.read_until(b"\n").decode("ascii"))
- users = []
- for name, info in client_info.items():
- user_text = name
- audio_status = []
- if info["client_info"].get("client_input_muted", "0") == "1":
- audio_status.append("Mic muted")
- if info["client_info"].get("client_output_muted", "0") == "1":
- audio_status.append("Sound muted")
- if len(audio_status) > 0:
- user_text += f" ({', '.join(audio_status)})"
- users.append(user_text)
- return users
- @app.route("/")
- def get_status():
- return jsonify({"users": get_users()})
- @app.route("/page")
- def get_status_page():
- return render_template_string("""
- <!doctype html>
- <title>Teamspeak Server Status</title>
- <style>
- body {
- margin: 0px 0px 0px 0px;
- padding: 0px 0px 0px 0px;
- font-family: verdana, arial, helvetica, sans-serif;
- color: #ccc;
- background-color: #333;
- }
- h1 {
- font-size: 24px;
- line-height: 44px;
- font-weight: bold;
- margin-top: 0;
- margin-bottom: 0;
- }
- </style>
- {% if scrolling %}
- <marquee direction="up" style="height 600px" behavior="alternate">
- <marquee direction="right" style="width 600px" behavior="alternate">
- {% endif %}
- <div class="page">
- <h1>TeamSpeak Server Status</h1>
- {% if users|length == 0 %}
- No one in teamspeak!
- {% elif users|length == 1 %}
- Only {{ users[0] }}
- {% else %}
- <ul>
- {% for user in users %}
- <li>{{ user }}</li>
- {% endfor %}
- </ul>
- {% endif %}
- </div>
- {% if scrolling %}
- </marquee>
- </marquee>
- {% endif %}
- """, scrolling=True, users=sorted(get_users()))
- if __name__ == "__main__":
- app.run("0.0.0.0", 5000, debug=True, threaded=True)
|