def handle(self, packet_msg): if not self.is_user_auth(): return # Fetch all events. Use this to init client state if self.query == 'fetchall': s = db_session() if self.is_admin(): events = [event.serialize() for event in s.query(Event).all()] else: events = [event.serialize() for event in s.query(Event).filter_by(visible=True).all()] s.close() self.send_message(events) log.info(u"[%s] Events fetched", self.sock.sid[0:6]) return if self.query == 'add' and self.is_admin(): s = db_session() event = Event(name=u'Event {}'.format(generate_session()[:4]), visible=False) s.add(event) s.commit() self.send_message(event.serialize()) s.close() log.info(u"[%s] New event added", self.sock.sid[0:6]) return if self.query == 'edit' and self.is_admin(): event_id = packet_msg.get('id') name = packet_msg.get('name') visible = packet_msg.get('visible') # Get user s = db_session() try: event = s.query(Event).filter_by(id=event_id).one() event.name = name event.visible = visible s.add(event) s.commit() self.send_message(event.serialize()) log.info(u"[%s] Event %d edited", event_id, self.sock.sid[0:6]) except NoResultFound: log.info(u"[%s] error while editing event %d: no event found", event_id, self.sock.sid[0:6]) return finally: s.close()
def handle(self, packet_msg): # If we are already authenticated, don't bother with this if self.sock.authenticated: return username = packet_msg.get('username', '') password = packet_msg.get('password', '') token = packet_msg.get('token') if token: s = db_session() try: player = s.query(Player).filter_by(token=token).one() except NoResultFound: self.send_error('Incorrect token', 401) log.info(u"Invalid token.") return finally: s.close() self.sock.authenticated = True self.sock.client_type = 'token' self.sock.uid = player.id self.sock.level = 0 # Send login success message self.send_message({ 'name': player.name }) log.info(u"[%s] Token logged in", token[0:6]) else: s = db_session() try: user = s.query(User).filter_by(username=username).one() except NoResultFound: self.send_error('Incorrect username or password', 401) log.info(u"Invalid username or password.") return finally: s.close() # If user exists and password matches, pass onwards! if user and pbkdf2_sha256.verify(password, user.password): session_id = generate_session() # Add new session s = db_session() ses = Session(key=session_id, user=user.id) s.add(ses) s.commit() s.close() # Mark connection as authenticated, and save session id self.sock.sid = session_id self.sock.uid = user.id self.sock.authenticated = True self.sock.level = user.level self.sock.client_type = 'user' # Send login success message self.send_message({ 'uid': self.sock.uid, 'sid': self.sock.sid, 'user': user.serialize() }) # Send required skip count self.send_req_skip_count() # Dump out log log.info(u"[%s] Logged in", self.sock.sid[0:6]) else: self.send_error('Incorrect username or password', 401) log.info(u"Invalid username or password.")
s.close() exit(0) if 'create_player' in sys.argv: settings.config_init() db_init(settings.DATABASE_CONFIG) if len(sys.argv) == 4: event_id = int(sys.argv[2]) name = sys.argv[3] else: event_id = int(raw_input("Event ID: ")) name = raw_input("Player name: ") s = db_session() player = Player(event=event_id, name=name, token=generate_session()[:16]) s.add(player) s.commit() s.close() exit(0) if 'list_players' in sys.argv: settings.config_init() db_init(settings.DATABASE_CONFIG) s = db_session() print("Id Ev Name Token") for player in s.query(Player).all(): print("{:3} {:3} {:16} {}".format(player.id, player.event, player.name, player.token)) s.close() exit(0)
def handle(self, packet_msg): if not self.is_user_auth(): return # Add new empty player if self.query == "add" and self.is_admin(): event_id = packet_msg.get("event_id") s = db_session() player = Player( event=event_id, name=u"Player {}".format(generate_session()[:4]), token=generate_session()[:16] ) s.add(player) s.commit() self.send_message(player.serialize(show_token=True)) s.close() log.info(u"[%s] New player added", self.sock.sid[0:6]) return if self.query == "edit" and self.is_admin(): player_id = packet_msg.get("id") name = packet_msg.get("name") # Get user s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() player.name = name s.add(player) s.commit() self.send_message(player.serialize(show_token=True)) log.info(u"[%s] Player %d edited", player_id, self.sock.sid[0:6]) except NoResultFound: log.info(u"[%s] error while editing player %d: no player found", player_id, self.sock.sid[0:6]) return finally: s.close() # Fetch all players. Use this to init client state if self.query == "fetchall": s = db_session() players = [p.serialize(show_token=self.is_admin()) for p in s.query(Player).all()] s.close() self.send_message(players) return if self.query == "now_playing": player_id = packet_msg.get("player_id") # Make sure we have a valid player s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() self.send_message({"state": player.serialize()}, query="change") return # Request skip if self.query == "skip": player_id = packet_msg.get("player_id") # Make sure we have a valid player s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() # Add our own skip s = db_session() try: skip = Skip(user=self.sock.uid, player=player.id, media=player.last) s.add(skip) s.commit() except IntegrityError: log.debug(u"Hissatsu! Double skip!") return finally: s.close() log.info(u"User %s requested skipping media %s on player %s", self.sock.uid, player.last, player.id) # Broadcast skip count skips = s.query(Skip).filter_by(player=player.id, media=player.last).count() if skips >= self.get_req_skip_count(): log.info(u"All skip votes gathered; skipping %d", player.last) PlayerDeviceHandler(self.sock, "playerdev", "status_change").send_source(player) else: self.broadcast( "player", {"count": skips}, query="current_skip_count", req_auth=True, avoid_self=False, client_type="user", ) # That's that. return if self.query == "get_media_skip_count": player_id = packet_msg.get("player_id") # Make sure we have a valid player s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() # Get current skip count for requested player skips = s.query(Skip).filter_by(player=player.id, media=player.last).count() self.send_message({"count": skips}, query="current_skip_count") return if self.query == "req_skip_count": self.send_req_skip_count() return if self.query == "pause" and self.is_admin(): player_id = packet_msg.get("player_id") if player_id: log.info(u"ADMIN: Force status = 2 on player %d", player_id) self._send_message("playerdev", {"status": 2}, query="set_status", target_uid=player_id) return if self.query == "play" and self.is_admin(): player_id = packet_msg.get("player_id") s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() if player.status > 0: log.info(u"ADMIN: Force status = 1 on player %d", player_id) self._send_message("playerdev", {"status": 1}, query="set_status", target_uid=player_id) else: PlayerDeviceHandler(self.sock, "playerdev", "status_change").send_source(player) return if self.query == "force_skip" and self.is_admin(): player_id = packet_msg.get("player_id") s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() log.info(u"ADMIN: Force skipping on player %d", player_id) PlayerDeviceHandler(self.sock, "playerdev", "status_change").send_source(player) return # Maintenance mode, not stop (even if it is named so) if self.query == "stop" and self.is_admin(): player_id = packet_msg.get("player_id") if player_id: log.info(u"ADMIN: Force status = 4 on player %d", player_id) self._send_message("playerdev", {"status": 4}, query="set_status", target_uid=player_id) return
def handle(self, packet_msg): if not self.is_user_auth(): return # Add new empty player if self.query == 'add' and self.is_admin(): event_id = packet_msg.get('event_id') s = db_session() player = Player( event=event_id, name=u'Player {}'.format(generate_session()[:4]), token=generate_session()[:16]) s.add(player) s.commit() self.send_message(player.serialize(show_token=True)) s.close() log.info(u"[%s] New player added", self.sock.sid[0:6]) return if self.query == 'edit' and self.is_admin(): player_id = packet_msg.get('id') name = packet_msg.get('name') # Get user s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() player.name = name s.add(player) s.commit() self.send_message(player.serialize(show_token=True)) log.info(u"[%s] Player %d edited", player_id, self.sock.sid[0:6]) except NoResultFound: log.info(u"[%s] error while editing player %d: no player found", player_id, self.sock.sid[0:6]) return finally: s.close() # Fetch all players. Use this to init client state if self.query == 'fetchall': s = db_session() players = [p.serialize(show_token=self.is_admin()) for p in s.query(Player).all()] s.close() self.send_message(players) return if self.query == 'now_playing': player_id = packet_msg.get('player_id') # Make sure we have a valid player s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() self.send_message({'state': player.serialize()}, query='change') return # Request skip if self.query == 'skip': player_id = packet_msg.get('player_id') # Make sure we have a valid player s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() # Add our own skip s = db_session() try: skip = Skip(user=self.sock.uid, player=player.id, media=player.last) s.add(skip) s.commit() except IntegrityError: log.debug(u"Hissatsu! Double skip!") return finally: s.close() log.info(u"User %s requested skipping media %s on player %s", self.sock.uid, player.last, player.id) # Broadcast skip count skips = s.query(Skip).filter_by(player=player.id, media=player.last).count() if skips >= self.get_req_skip_count(): log.info(u"All skip votes gathered; skipping %d", player.last) PlayerDeviceHandler(self.sock, 'playerdev', 'status_change').send_source(player) else: self.broadcast('player', { 'count': skips, }, query='current_skip_count', req_auth=True, avoid_self=False, client_type='user') # That's that. return if self.query == 'get_media_skip_count': player_id = packet_msg.get('player_id') # Make sure we have a valid player s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() # Get current skip count for requested player skips = s.query(Skip).filter_by(player=player.id, media=player.last).count() self.send_message({'count': skips}, query='current_skip_count') return if self.query == 'req_skip_count': self.send_req_skip_count() return if self.query == 'pause' and self.is_admin(): player_id = packet_msg.get('player_id') if player_id: log.info(u"ADMIN: Force status = 2 on player %d", player_id) self._send_message('playerdev', {'status': 2}, query='set_status', target_uid=player_id) return if self.query == 'play' and self.is_admin(): player_id = packet_msg.get('player_id') s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() if player.status > 0: log.info(u"ADMIN: Force status = 1 on player %d", player_id) self._send_message('playerdev', {'status': 1}, query='set_status', target_uid=player_id) else: PlayerDeviceHandler(self.sock, 'playerdev', 'status_change').send_source(player) return if self.query == 'force_skip' and self.is_admin(): player_id = packet_msg.get('player_id') s = db_session() try: player = s.query(Player).filter_by(id=player_id).one() except NoResultFound: return finally: s.close() log.info(u"ADMIN: Force skipping on player %d", player_id) PlayerDeviceHandler(self.sock, 'playerdev', 'status_change').send_source(player) return # Maintenance mode, not stop (even if it is named so) if self.query == 'stop' and self.is_admin(): player_id = packet_msg.get('player_id') if player_id: log.info(u"ADMIN: Force status = 4 on player %d", player_id) self._send_message('playerdev', {'status': 4}, query='set_status', target_uid=player_id) return