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.

109 lines
3.2 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. options, progargs = parser.parse_args()
  47. if options.connstring is None:
  48. options.connstring = DEFAULT_CONNSTRING
  49. if options.slice is None:
  50. options.slice = DEFAULT_SLICEFILE
  51. if options.asksecret or options.icesecret == '':
  52. options.icesecret = getpass.getpass( "Ice secret: " )
  53. ctl = MumbleCtlBase.newInstance( options.connstring, options.slice, options.icesecret )
  54. app = Flask(__name__)
  55. app.debug = options.debug
  56. def getUser(user):
  57. fields = ["channel", "deaf", "mute", "name", "selfDeaf", "selfMute",
  58. "session", "suppress", "userid", "idlesecs", "recording", "comment",
  59. "prioritySpeaker"]
  60. return dict(zip(fields, [getattr(user, field) for field in fields]))
  61. def getChannel(channel):
  62. fields = ["id", "name", "parent", "links", "description", "temporary", "position"]
  63. data = dict(zip(fields, [getattr(channel.c, field) for field in fields]))
  64. data['channels'] = [ getChannel(subchan) for subchan in channel.children ]
  65. data['users'] = [ getUser(user) for user in channel.users ]
  66. return data
  67. @app.route('/<int:srv_id>')
  68. def getTree(srv_id):
  69. name = ctl.getConf(srv_id, "registername")
  70. tree = ctl.getTree(srv_id)
  71. serv = {
  72. 'id': srv_id,
  73. 'name': name,
  74. 'root': getChannel(tree)
  75. }
  76. return jsonify(serv)
  77. @app.route('/')
  78. def getServers():
  79. return jsonify(servers=ctl.getBootedServers())
  80. if __name__ == '__main__':
  81. app.run()