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.

117 lines
3.5 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. parser = OptionParser("""Usage: %prog [options]
  24. This is a minimalistic implementation of a Channel Viewer Protocol provider
  25. using the Flask Python framework and Mumble-Django's MCTL connection library.
  26. """)
  27. parser.add_option( "-c", "--connstring",
  28. help="connection string to use. Default is '%s'." % DEFAULT_CONNSTRING,
  29. default=None
  30. )
  31. parser.add_option( "-i", "--icesecret",
  32. help="Ice secret to use in the connection. Also see --asksecret.",
  33. default=None
  34. )
  35. parser.add_option( "-a", "--asksecret",
  36. help="Ask for the Ice secret on the shell instead of taking it from the command line.",
  37. action="store_true", default=False
  38. )
  39. parser.add_option( "-s", "--slice",
  40. help="path to the slice file. Default is '%s'." % DEFAULT_SLICEFILE,
  41. default=None
  42. )
  43. parser.add_option( "-d", "--debug",
  44. help="Enable error debugging",
  45. default=False, action="store_true" )
  46. parser.add_option( "-H", "--host",
  47. help="The IP to bind to. Default is '127.0.0.1'.",
  48. default="127.0.0.1"
  49. )
  50. parser.add_option( "-p", "--port", type="int",
  51. help="The port number to bind to. Default is 5000.",
  52. default=5000
  53. )
  54. options, progargs = parser.parse_args()
  55. if options.connstring is None:
  56. options.connstring = DEFAULT_CONNSTRING
  57. if options.slice is None:
  58. options.slice = DEFAULT_SLICEFILE
  59. if options.asksecret or options.icesecret == '':
  60. options.icesecret = getpass.getpass( "Ice secret: " )
  61. ctl = MumbleCtlBase.newInstance( options.connstring, options.slice, options.icesecret )
  62. app = Flask(__name__)
  63. def getUser(user):
  64. fields = ["channel", "deaf", "mute", "name", "selfDeaf", "selfMute",
  65. "session", "suppress", "userid", "idlesecs", "recording", "comment",
  66. "prioritySpeaker"]
  67. return dict(zip(fields, [getattr(user, field) for field in fields]))
  68. def getChannel(channel):
  69. fields = ["id", "name", "parent", "links", "description", "temporary", "position"]
  70. data = dict(zip(fields, [getattr(channel.c, field) for field in fields]))
  71. data['channels'] = [ getChannel(subchan) for subchan in channel.children ]
  72. data['users'] = [ getUser(user) for user in channel.users ]
  73. return data
  74. @app.route('/<int:srv_id>')
  75. def getTree(srv_id):
  76. name = ctl.getConf(srv_id, "registername")
  77. tree = ctl.getTree(srv_id)
  78. serv = {
  79. 'id': srv_id,
  80. 'name': name,
  81. 'root': getChannel(tree)
  82. }
  83. return jsonify(serv)
  84. @app.route('/')
  85. def getServers():
  86. return jsonify(servers=ctl.getBootedServers())
  87. if __name__ == '__main__':
  88. app.run(host=options.host, port=options.port, debug=options.debug)