def set_conf(self, id): """ Change a configuration variable on a server. """ key = request.form.get('key') value = request.form.get('value') if key and value: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 server.setConf(key, value) return jsonify(message="Configuration updated.") else: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 count = 0 for key, val in list(request.form.items()): count += 1 server.setConf(key, val) if count > 0: return jsonify(message="Configuration updated: %d values." % count) else: return jsonify(message="Configuration key and value required.")
def set_conf(self, id): """ Change a configuration variable on a server. """ key = request.form.get('key') value = request.form.get('value') if key and value: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 server.setConf(key, value) return jsonify(message="Configuration updated.") else: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 count = 0 for key, val in request.form.items(): count += 1 server.setConf(key, val) if count > 0: return jsonify(message="Configuration updated: %d values." % count) else: return jsonify(message="Configuration key and value required.")
def user_update_user(self, id, userid): """ Updates a user by ID """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 user = self.get_user(server, userid) if user is None: return jsonify(message="User Not Found"), 404 username = request.form.get('username') if username is None: return jsonify(message="Username required"), 400 updated_user = {Murmur.UserInfo.UserName: username} server.updateRegistration(user.userid, updated_user) json_data = { "user_id": user.userid, "username": username, "updated": "Success" } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def channel_acl(self, id, channel_id): """ Gets all channel ACLs in server """ server = meta.getServer(id) data = obj_to_dict(server.getACL(channel_id)) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def cvp(self, id): server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 allowed = bool(get_server_conf(meta, server, 'x_cvp')) if not allowed: return jsonify(message="CVP Disabled"), 403 # Fetch tree from server tree = server.getTree() # Get server properties relevant to CVP rname = get_server_conf(meta, server, 'registername') rhost = get_server_conf(meta, server, 'registerhostname') port = get_server_port(meta, server) # Build the CVP object cvp = { "root": cvp_chan_to_dict(tree), "name": rname if rname != '' else 'Root', "x_uptime": server.getUptime(), "id": server.id() } if rhost != '': cvp['x_connecturl'] = "mumble://%s:%d/?version=1.2.0" % (rhost, port) return Response(json.dumps(cvp, sort_keys=True, indent=4), mimetype='application/json')
def get(self, id): """ Lists server details """ id = long(id) s = meta.getServer(id) tree = obj_to_dict(s.getTree()) json_data = { 'id': s.id(), 'name': get_server_conf(meta, s, 'registername'), 'host': get_server_conf(meta, s, 'host'), 'port': get_server_port(meta, s), 'address': '%s:%s' % ( get_server_conf(meta, s, 'host'), get_server_port(meta, s), ), 'password': get_server_conf(meta, s, 'password'), 'welcometext': get_server_conf(meta, s, 'welcometext'), 'user_count': (s.isRunning() and len(s.getUsers())) or 0, 'maxusers': get_server_conf(meta, s, 'users') or 0, 'uptime': s.getUptime() if s.isRunning() else 0, 'humanize_uptime': str( timedelta(seconds=s.getUptime()) if s.isRunning() else '' ), 'parent_channel': tree['c'], 'sub_channels': tree['children'], 'users': tree['users'], 'registered_users': s.getRegisteredUsers(''), 'log_length': s.getLogLen() } return jsonify(json_data)
def user_update_user(self, id, userid): """ Updates a user by ID """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 user = self.get_user(server, userid) if user is None: return jsonify(message="User Not Found"), 404 username = request.form.get('username') if username is None: return jsonify(message="Username required"), 400 updated_user = { Murmur.UserInfo.UserName: username } server.updateRegistration(user.userid, updated_user) json_data = { "user_id": user.userid, "username": username, "updated": "Success" } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def user_new_user(self, id): """ Creates user """ server = meta.getServer(int(id)) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 username = request.form.get('username') password = request.form.get('password') new_user = { Murmur.UserInfo.UserName: username, Murmur.UserInfo.UserPassword: password } added = server.registerUser(new_user) data = obj_to_dict(server.getRegistration(added)) json_data = { "user_id": added, "username": data['UserName'], "last_active": data['UserLastActive'] } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def user_new_user(self, id): """ Creates user """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 username = request.form.get('username') password = request.form.get('password') new_user = { Murmur.UserInfo.UserName: username, Murmur.UserInfo.UserPassword: password } added = server.registerUser(new_user) data = obj_to_dict(server.getRegistration(added)) json_data = { "user_id": added, "username": data['UserName'], "last_active": data['UserLastActive'] } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def bans(self, id): """ Gets all banned IPs in server """ server = meta.getServer(id) data = obj_to_dict(server.getBans()) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def kick_user(self, id): """ Kicks user from server. """ user_session = int( request.form.get("usersession")) # Session ID of user reason = request.form.get( "reason", "Reason not defined.") # Reason messaged for being kicked. if user_session: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 try: server.kickUser(user_session, reason) return jsonify(message="User kicked from server.") except Murmur.InvalidSessionException: return jsonify(message="Not a valid session ID.") else: return jsonify(message="User session required.")
def conf(self, id): """ Gets all configuration in server """ server = meta.getServer(id) data = obj_to_dict(server.getAllConf()) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def get(self, id): """ Lists server details """ id = int(id) s = meta.getServer(id) # Return 404 if not found if s is None: return jsonify(message="Not Found"), 404 tree = obj_to_dict(s.getTree()) if s.isRunning() else None json_data = { 'id': s.id(), 'host': get_server_conf(meta, s, 'host'), 'port': get_server_port(meta, s), 'address': '%s:%s' % ( get_server_conf(meta, s, 'host'), get_server_port(meta, s), ), 'welcometext': get_server_conf(meta, s, 'welcometext'), 'user_count': (s.isRunning() and len(s.getUsers())) or 0, 'maxusers': get_server_conf(meta, s, 'users') or 0, 'running': s.isRunning(), 'uptime': s.getUptime() if s.isRunning() else 0, 'humanize_uptime': str( timedelta(seconds=s.getUptime()) if s.isRunning() else '' ), 'users': [s.getUsers()[uid].name for uid in s.getUsers().keys()] if s.isRunning() else None, } return jsonify(json_data)
def delete(self, id): """ Shuts down and deletes a server """ server = meta.getServer(int(id)) server.stop() server.delete() return jsonify(message="Server deleted")
def get(self, id): """ Lists server details """ id = long(id) s = meta.getServer(id) # Return 404 if not found if s is None: return jsonify(message="Not Found"), 404 tree = obj_to_dict(s.getTree()) if s.isRunning() else None json_data = { 'id': s.id(), 'name': get_server_conf(meta, s, 'registername'), 'host': get_server_conf(meta, s, 'host'), 'port': get_server_port(meta, s), 'address': '%s:%s' % ( get_server_conf(meta, s, 'host'), get_server_port(meta, s), ), 'password': get_server_conf(meta, s, 'password'), 'welcometext': get_server_conf(meta, s, 'welcometext'), 'user_count': (s.isRunning() and len(s.getUsers())) or 0, 'maxusers': get_server_conf(meta, s, 'users') or 0, 'running': s.isRunning(), 'uptime': s.getUptime() if s.isRunning() else 0, 'humanize_uptime': str(timedelta(seconds=s.getUptime()) if s.isRunning() else ''), 'parent_channel': tree['c'] if s.isRunning() else None, 'sub_channels': tree['children'] if s.isRunning() else None, 'users': tree['users'] if s.isRunning() else None, 'registered_users': s.getRegisteredUsers('') if s.isRunning() else None, 'log_length': s.getLogLen(), 'bans': s.getBans() if s.isRunning() else 0 } return jsonify(json_data)
def start(self, id): """ Starts the server matching server ID """ server = meta.getServer(int(id)) if server.isRunning(): return jsonify(message="Server is already started") else: server.start() return jsonify(message="Server started")
def channel_acl(self, id, channel_id): """ Gets all channel ACLs in server """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 data = obj_to_dict(server.getACL(channel_id)) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def set_superuser_pw(self, id): """ Sets SuperUser password for server id """ password = request.form.get('password') if password: server = meta.getServer(id) server.setSuperuserPassword(password) return jsonify(message="Superuser password set.") else: return jsonify(message="Password required.")
def send_message(self, id): """ Sends a message to all channels in a server """ message = request.form.get('message') if message: server = meta.getServer(id) server.sendMessageChannel(0, True, message) return jsonify(message="Message sent.") else: return jsonify(message="Message required.")
def logs(self, id): """ Gets all server logs by server ID """ server = meta.getServer(int(id)) logs = [] for l in server.getLog(0, -1): logs.append({ "message": l.txt, "timestamp": l.timestamp, }) return Response(json.dumps(logs, sort_keys=True, indent=4), mimetype='application/json')
def register_user(self, id, user): """ Gets registered user by ID """ server = meta.getServer(int(id)) data = obj_to_dict(server.getRegistration(user)) json_data = { "user_id": user, "username": data['UserName'], "last_active": data['UserLastActive'] } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def channel(self, id, channel_id): """ Gets a specific channel from a server """ server = meta.getServer(int(id)) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 data = obj_to_dict(server.getChannelState(int(channel_id))) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def channel_del_channel(self, id, channel): """ Deletes channel """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="No Server Found for ID " + str(id)), 500 server.removeChannel(int(channel)) json_data = {"channel_id": channel, "deleted": 'Success'} return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def channel_del_channel(self, id, channel): """ Deletes channel """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="No Server Found for ID " + str(id)), 500 server.removeChannel(int(channel)) json_data = { "channel_id": channel, "deleted": 'Success' } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def send_message(self, id): """ Sends a message to all channels in a server """ message = request.form.get('message') if message: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 server.sendMessageChannel(0, True, message) return jsonify(message="Message sent.") else: return jsonify(message="Message required.")
def set_superuser_pw(self, id): """ Sets SuperUser password for server id """ password = request.form.get('password') if password: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 server.setSuperuserPassword(password) return jsonify(message="Superuser password set.") else: return jsonify(message="Password required.")
def start(self, id): """ Starts server """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 # Message if server is already running if server.isRunning(): return jsonify(message="Server already running.") # Start server instance server.start() return jsonify(message="Server started.")
def stop(self, id): """ Stops server """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 # Stop server first if it is running if not server.isRunning(): return jsonify(message="Server already stopped.") # Stop server instance server.stop() return jsonify(message="Server stopped.")
def logs(self, id): """ Gets all server logs by server ID """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 logs = [] for l in server.getLog(0, -1): logs.append({ "message": l.txt, "timestamp": l.timestamp, }) return Response(json.dumps(logs, sort_keys=True, indent=4), mimetype='application/json')
def channel_new_channel(self, id): """ Creates channel """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 name = request.form.get('name') parent = request.form.get('parent') added = server.addChannel(name, int(parent)) data = obj_to_dict(server.getChannelState(int(added))) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def register_user(self, id, user): """ Gets registered user by ID """ server = meta.getServer(int(id)) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 data = obj_to_dict(server.getRegistration(int(user))) json_data = { "user_id": user, "username": data['UserName'], "last_active": data['UserLastActive'] } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def delete(self, id): """ Shuts down and deletes a server """ server = meta.getServer(int(id)) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 # Stop server first if it is running if server.isRunning(): server.stop() # Delete server instance server.delete() return jsonify(message="Server deleted")
def logs(self, id): """ Gets all server logs by server ID """ server = meta.getServer(int(id)) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 logs = [] for l in server.getLog(0, -1): logs.append({ "message": l.txt, "timestamp": l.timestamp, }) return Response(json.dumps(logs, sort_keys=True, indent=4), mimetype='application/json')
def set_conf(self, id): """ Sends a message to all channels in a server """ key = request.form.get('key') value = request.form.get('value') if key and value: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 server.setConf(key, value) return jsonify(message="Configuration updated.") else: return jsonify(message="Configuration key and value required.")
def register_user(self, id, user): """ Gets registered user by ID """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 data = obj_to_dict(server.getRegistration(int(user))) json_data = { "user_id": user, "username": data['UserName'], "last_active": data['UserLastActive'] } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def update_channel(self, id, channel_id): """ Update specific channel attributes """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 # Return 404 if not found channel = server.getChannelState(channel_id) if channel is None: return jsonify(message="Not Found"), 404 name = request.form.get('name') parent = request.form.get('parent') description = request.form.get('description') links = request.form.getlist('links[]') description = request.form.get('description') position = request.form.get('position') temporary = request.form.get('temporary') if name is not None: channel.name = str(name) if parent is not None: channel.parent = int(parent) if description is not None: channel.description = str(description) if links is not None: for idx in range(len(links)): links[idx] = int(links[idx]) channel.links = links if position is not None: channel.position = int(position) if temporary is not None: channel.temporary = bool(temporary) server.setChannelState(channel) data = obj_to_dict(server.getChannelState(channel_id)) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def update_channel_acl(self, id, channel_id): """ Update specific channel ACL """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 origin_acl = server.getACL(channel_id) update_acls = origin_acl[0] update_groups = origin_acl[1] update_inherit = origin_acl[2] params = request.get_json() if "acls" in params and params['acls'] is not None: new_acls = [] for props in params['acls']: new_acls.append( Murmur.ACL(props['applyHere'], props['applySubs'], props['inherited'], props['userid'], props['group'], props['allow'], props['deny'])) update_acls = new_acls if "groups" in params and params['groups'] is not None: new_groups = [] for props in params['groups']: new_groups.append( Murmur.Group(props['name'], props['inherited'], props['inherit'], props['inheritable'], props['add'], props['remove'])) update_groups = new_groups if "inherit" in params and params['inherit'] is not None: update_inherit = bool(params['inherit']) server.setACL(channel_id, update_acls, update_groups, update_inherit) data = obj_to_dict(server.getACL(channel_id)) return Response(json.dumps(data, sort_keys=True, indent=4), mimetype='application/json')
def user_del_user(self, id, user): """ Deletes user """ server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="No Server Found for ID " + str(id)), 500 olduser = server.getRegistration(int(user)) if olduser is None: return jsonify(message="No User Found for ID " + str(user)), 500 server.unregisterUser(int(user)) json_data = {"user_id": user, "deleted": 'Success'} return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def user_unmute_user(self, id, userid): """ Unmutes a user """ server = meta.getServer(id) if server is None: return jsonify(message="Server Not Found"), 404 user = self.get_user(server, userid) if user is None: return jsonify(message="User Not Found"), 404 state = server.getState(user.session) state.mute = False state.suppress = False server.setState(state) json_data = {"user_id": user.userid, "unmuted": 'Success'} return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def user_del_user(self, id, user): """ Deletes user """ server = meta.getServer(int(id)) # Return 404 if not found if server is None: return jsonify(message="No Server Found for ID "+str(id)), 500 olduser = server.getRegistration(int(user)) if olduser is None: return jsonify(message="No User Found for ID "+str(user)), 500 server.unregisterUser(int(user)) json_data = { "user_id": user, "deleted": 'Success' } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def kick_user(self, id): """ Kicks user from server. """ user_session = int(request.form.get("usersession")) # Session ID of user reason = request.form.get("reason", "Reason not defined.") # Reason messaged for being kicked. if user_session: server = meta.getServer(id) # Return 404 if not found if server is None: return jsonify(message="Not Found"), 404 try: server.kickUser(user_session, reason) return jsonify(message="User kicked from server.") except Murmur.InvalidSessionException: return jsonify(message="Not a valid session ID.") else: return jsonify(message="User session required.")
def delete_multiple(self): """ Delete multiple servers. """ id = request.args.get('id') if not id: return jsonify(message="No servers to delete.") ids = list(map(int, id.split(","))) # Delete each server. for i in ids: server = meta.getServer(i) if not server: continue if server.isRunning(): server.stop() server.delete() return jsonify(message="Deleting servers.", ids=ids)
def user_unmute_user(self, id, userid): """ Unmutes a user """ server = meta.getServer(id) if server is None: return jsonify(message="Server Not Found"), 404 user = self.get_user(server, userid) if user is None: return jsonify(message="User Not Found"), 404 state = server.getState(user.session) state.mute = False state.suppress = False server.setState(state) json_data = { "user_id": user.userid, "unmuted": 'Success' } return Response(json.dumps(json_data, sort_keys=True, indent=4), mimetype='application/json')
def delete_multiple(self): """ Delete multiple servers. """ id = request.args.get('id') if not id: return jsonify(message="No servers to delete.") ids = map(int, id.split(",")) # Delete each server. for i in ids: server = meta.getServer(i) if not server: continue if server.isRunning(): server.stop() server.delete() return jsonify(message="Deleting servers.", ids=ids)