Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.8 KiB

13 years ago
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # kate: space-indent on; indent-width 4; replace-tabs on;
  4. """
  5. * Copyright (C) 2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  6. *
  7. * Mumble-Django is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This package is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. """
  17. import getpass
  18. from optparse import OptionParser
  19. from flask import Flask, jsonify
  20. from mumble.mctl import MumbleCtlBase
  21. DEFAULT_CONNSTRING = 'Meta:tcp -h 127.0.0.1 -p 6502'
  22. DEFAULT_SLICEFILE = '/usr/share/slice/Murmur.ice'
  23. DEFAULT_ICESECRET = None
  24. if __name__ == '__main__':
  25. parser = OptionParser("""Usage: %prog [options]
  26. This is a minimalistic implementation of a Channel Viewer Protocol provider
  27. using the Flask Python framework and Mumble-Django's MCTL connection library.
  28. """)
  29. parser.add_option( "-c", "--connstring",
  30. help="connection string to use. Default is '%s'." % DEFAULT_CONNSTRING,
  31. default=None
  32. )
  33. parser.add_option( "-i", "--icesecret",
  34. help="Ice secret to use in the connection. Also see --asksecret.",
  35. default=DEFAULT_ICESECRET
  36. )
  37. parser.add_option( "-a", "--asksecret",
  38. help="Ask for the Ice secret on the shell instead of taking it from the command line.",
  39. action="store_true", default=False
  40. )
  41. parser.add_option( "-s", "--slice",
  42. help="path to the slice file. Default is '%s'." % DEFAULT_SLICEFILE,
  43. default=None
  44. )
  45. parser.add_option( "-d", "--debug",
  46. help="Enable error debugging",
  47. default=False, action="store_true" )
  48. parser.add_option( "-H", "--host",
  49. help="The IP to bind to. Default is '127.0.0.1'.",
  50. default="127.0.0.1"
  51. )
  52. parser.add_option( "-p", "--port", type="int",
  53. help="The port number to bind to. Default is 5000.",
  54. default=5000
  55. )
  56. options, progargs = parser.parse_args()
  57. if options.connstring is None:
  58. options.connstring = DEFAULT_CONNSTRING
  59. if options.slice is None:
  60. options.slice = DEFAULT_SLICEFILE
  61. if options.asksecret:
  62. options.icesecret = getpass.getpass( "Ice secret: " )
  63. else:
  64. class options:
  65. connstring = DEFAULT_CONNSTRING
  66. slice = DEFAULT_SLICEFILE
  67. icesecret = DEFAULT_ICESECRET
  68. ctl = MumbleCtlBase.newInstance( options.connstring, options.slice, options.icesecret )
  69. app = Flask(__name__)
  70. def getUser(user):
  71. fields = ["channel", "deaf", "mute", "name", "selfDeaf", "selfMute",
  72. "session", "suppress", "userid", "idlesecs", "recording", "comment",
  73. "prioritySpeaker"]
  74. return dict(zip(fields, [getattr(user, field) for field in fields]))
  75. def getChannel(channel):
  76. fields = ["id", "name", "parent", "links", "description", "temporary", "position"]
  77. data = dict(zip(fields, [getattr(channel.c, field) for field in fields]))
  78. data['channels'] = [ getChannel(subchan) for subchan in channel.children ]
  79. data['users'] = [ getUser(user) for user in channel.users ]
  80. return data
  81. @app.route('/<int:srv_id>')
  82. def getTree(srv_id):
  83. name = ctl.getConf(srv_id, "registername")
  84. tree = ctl.getTree(srv_id)
  85. serv = {
  86. 'id': srv_id,
  87. 'name': name,
  88. 'root': getChannel(tree)
  89. }
  90. return jsonify(serv)
  91. @app.route('/')
  92. def getServers():
  93. return jsonify(servers=ctl.getBootedServers())
  94. if __name__ == '__main__':
  95. app.run(host=options.host, port=options.port, debug=options.debug)