app.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. from telnetlib import Telnet
  3. import toml
  4. from flask import Flask, jsonify, render_template_string
  5. app = Flask(__name__)
  6. def get_users():
  7. with open("secret.toml") as infile:
  8. cfg = toml.load(infile)
  9. login = ("login %s %s\n" % (cfg["user"], cfg["pass"])).encode("utf-8")
  10. with Telnet(cfg["host"], cfg["port"], 5) as tn:
  11. print("connection")
  12. print(tn.read_until(b"\n").decode("ascii"))
  13. print(tn.read_until(b"\n").decode("ascii"))
  14. print("----")
  15. tn.write(login)
  16. print("after login")
  17. print(tn.read_until(b"\n").decode("ascii"))
  18. print("----")
  19. tn.write(b"use 1 -virtual\n")
  20. print("after use")
  21. print(tn.read_until(b"\n").decode("ascii"))
  22. print("----")
  23. tn.write(b"clientlist\n")
  24. response = tn.read_until(b"\n").decode("ascii")
  25. print("after clientlist")
  26. print(response)
  27. print(tn.read_until(b"\n").decode("ascii"))
  28. print("----")
  29. # TODO clean this nicely
  30. client_info = {info["client_nickname"]: info for info in [{k: v for k, v in [i.split("=", 1) for i in ent.split()]} for ent in response.split("|")] if "serveradmin" not in info["client_nickname"]}
  31. for k, v in client_info.items():
  32. tn.write(f"clientinfo clid={v['clid']}\n".encode("utf-8"))
  33. response = tn.read_until(b"\n").decode("ascii")
  34. print(f"after clientinfo for {k}")
  35. print(response)
  36. print(tn.read_until(b"\n").decode("ascii"))
  37. print("----")
  38. v["client_info"] = {k: v for k, v in [ent.split("=", 1) for ent in response.split() if "=" in ent]}
  39. tn.write(b"quit\n")
  40. print("after quit")
  41. print(tn.read_until(b"\n").decode("ascii"))
  42. import pprint
  43. pprint.pprint(client_info)
  44. return client_info
  45. @app.route("/")
  46. def get_status():
  47. return jsonify({"users": list(get_users().keys())})
  48. @app.route("/page")
  49. def get_status_page():
  50. info = get_users()
  51. users = list(info.keys())
  52. if len(users) == 0:
  53. text = "No one in teamspeak!"
  54. elif len(users) == 1:
  55. text = f"Only {users[0]}"
  56. else:
  57. text = "I see the following people: " + ", ".join(users)
  58. return render_template_string("""
  59. <!doctype html>
  60. <title>Teamspeak Server Status</title>
  61. <style>
  62. body {
  63. margin: 0px 0px 0px 0px;
  64. padding: 0px 0px 0px 0px;
  65. font-family: verdana, arial, helvetica, sans-serif;
  66. color: #ccc;
  67. background-color: #333;
  68. }
  69. h1 {
  70. font-size: 24px;
  71. line-height: 44px;
  72. font-weight: bold;
  73. margin-top: 0;
  74. margin-bottom: 0;
  75. }
  76. </style>
  77. {% if scrolling %}
  78. <marquee direction="up" style="height 600px" behavior="alternate">
  79. <marquee direction="right" style="width 600px" behavior="alternate">
  80. {% endif %}
  81. <div class="page">
  82. <h1>TeamSpeak Server Status</h1>
  83. {{ r }}
  84. </div>
  85. {% if scrolling %}
  86. </marquee>
  87. </marquee>
  88. {% endif %}
  89. """, scrolling=True, r=text)
  90. if __name__ == "__main__":
  91. print(get_users())
  92. # app.run("0.0.0.0", 5000, debug=True, threaded=True)