123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- #!/usr/bin/env python3
- from telnetlib import Telnet
- from collections import defaultdict
- import toml
- from flask import Flask, jsonify, render_template_string
- app = Flask(__name__)
- def parse_ts_response(response):
- # 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
- return [{k: v for k, v in pr} for pr in pairs]
- 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("utf-8"))
- print(tn.read_until(b"\n").decode("utf-8"))
- print("----")
- tn.write(login)
- print("after login")
- print(tn.read_until(b"\n").decode("utf-8"))
- print("----")
- tn.write(b"use 1 -virtual\n")
- print("after use")
- print(tn.read_until(b"\n").decode("utf-8"))
- print("----")
- tn.write(b"channellist\n")
- response = tn.read_until(b"\n").decode("utf-8")
- print("after channellist")
- print(response)
- print(tn.read_until(b"\n").decode("utf-8"))
- print("----")
- channel_maps = parse_ts_response(response)
- # rearrange the maps into one large channel lookup map
- channels = {info["cid"]: info["channel_name"].replace(r"\s", " ") for info in channel_maps}
- tn.write(b"clientlist\n")
- response = tn.read_until(b"\n").decode("utf-8")
- print("after clientlist")
- print(response)
- print(tn.read_until(b"\n").decode("utf-8"))
- print("----")
- entry_maps = parse_ts_response(response)
- # 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("utf-8")
- print(f"after clientinfo for {k}")
- print(response)
- print(tn.read_until(b"\n").decode("utf-8"))
- 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("utf-8"))
- users = []
- channel_users = defaultdict(list)
- 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)
- channel_users[channels[info["cid"]]].append(user_text)
- return sorted(users), {k: sorted(v) for k, v in channel_users.items()}
- @app.route("/")
- def get_status():
- return jsonify({"users": get_users()[0]})
- @app.route("/page")
- def get_status_page():
- # JS adapted from
- # https://stackoverflow.com/questions/6152522/how-can-i-make-a-paragraph-bounce-around-in-a-div
- 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;
- }
- h2 {
- font-size: 18px;
- line-height: 20px;
- margin-top: 0;
- margin-left: 15px;
- margin-bottom: -10px;
- }
- #bounceBox {
- position: absolute;
- top: 0;
- right: 0;
- bottom: 0;
- left: 0;
- }
- #bouncer {
- position: absolute;
- }
- </style>
- <script>
- let vx = 3;
- let vy = 2;
- const buffer = 5;
- const hitLR = (el, bounding) => {
- if (el.offsetLeft <= buffer && vx < 0) {
- vx = -1 * vx;
- }
- if ((el.offsetLeft + el.offsetWidth) >= (bounding.offsetWidth - buffer)) {
- vx = -1 * vx;
- }
- if (el.offsetTop <= buffer && vy < 0) {
- vy = -1 * vy;
- }
- if ((el.offsetTop + el.offsetHeight) >= (bounding.offsetHeight - buffer)) {
- vy = -1 * vy;
- }
- }
- const mover = (el, bounding) => {
- hitLR(el, bounding);
- el.style.left = el.offsetLeft + vx + 'px';
- el.style.top = el.offsetTop + vy + 'px';
- setTimeout(function() {
- mover(el, bounding);
- }, 30);
- }
- setTimeout(() => mover(
- document.getElementById("bouncer"),
- document.getElementById("bounceBox")
- ), 30);
- </script>
- <body>
- <div id="bounceBox">
- <div id="bouncer">
- <h1>TeamSpeak Server Status</h1>
- {% if users|length == 0 %}
- No one in teamspeak!
- {% else %}
- {% for channel, people in users.items() %}
- <h2>{{ channel }}</h2>
- <ul>
- {% for user in people %}
- <li>{{ user }}</li>
- {% endfor %}
- </ul>
- {% endfor %}
- {% endif %}
- </div>
- </div>
- </body>
- """, users=get_users()[1])
- if __name__ == "__main__":
- app.run("0.0.0.0", 5000, debug=True, threaded=True)
|