def handle_del_buddy(self, request_data): response = {"success": False} success = False from_profile = Profile.get( (Profile.id == request_data["from_profileid"]) & (Profile.deleted == False)) to_profile = Profile.get((Profile.id == request_data["to_profileid"]) & (Profile.deleted == False)) send_revoke = "send_revoke" in request_data and request_data[ "send_revoke"] count = Buddy.delete().where((Buddy.from_profile == from_profile) & ( Buddy.to_profile == to_profile)).execute() if count > 0: response["success"] = True if send_revoke: status_key = "status_{}".format(to_profile) if self.redis_presence_ctx.exists( status_key): #check if online #send revoke to GP publish_data = "\\type\\del_buddy\\from_profileid\\{}\\to_profileid\\{}".format( request_data["from_profileid"], request_data["to_profileid"]) self.redis_presence_ctx.publish( self.redis_presence_channel, publish_data) else: revoke_list_key = "revokes_{}".format( request_data["to_profileid"]) timestamp = int(time.time()) self.redis_presence_ctx.hset( revoke_list_key, request_data["from_profileid"], timestamp) return success
def handle_authorize_buddy_add(self, request_data): success = False redis_key = "add_req_{}".format(request_data["from_profileid"]) if self.redis_presence_ctx.exists(redis_key): if self.redis_presence_ctx.hexists(redis_key, request_data["to_profileid"]): if self.redis_presence_ctx.hdel(redis_key, request_data["to_profileid"]): success = True to_profile_model = Profile.get( (Profile.id == request_data["to_profileid"])) from_profile_model = Profile.get( (Profile.id == request_data["from_profileid"])) Buddy.insert(from_profile=to_profile_model, to_profile=from_profile_model).execute() Buddy.insert(to_profile=to_profile_model, from_profile=from_profile_model).execute() publish_data = "\\type\\authorize_add\\from_profileid\\{}\\to_profileid\\{}".format( request_data["from_profileid"], request_data["to_profileid"]) self.redis_presence_ctx.publish( self.redis_presence_channel, publish_data) publish_data = "\\type\\authorize_add\\from_profileid\\{}\\to_profileid\\{}\\silent\\1".format( request_data["to_profileid"], request_data["from_profileid"]) self.redis_presence_ctx.publish( self.redis_presence_channel, publish_data) return {"success": success}
def handle_create_profile(self, data): profile_data = data["profile"] user_data = None if "user" in data: user_data = data["user"] if "namespaceid" in profile_data: namespaceid = profile_data["namespaceid"] else: namespaceid = 0 if "uniquenick" in profile_data: if not self.is_name_valid(profile_data["uniquenick"]): raise OS_Profile_UniquenickInvalid() nick_available = self.check_uniquenick_available( profile_data["uniquenick"], namespaceid) if nick_available["exists"]: raise OS_Profile_UniquenickInUse(nick_available["profile"]) user = User.get((User.id == user_data["id"])) if "nick" in profile_data: if not self.is_name_valid(profile_data["nick"]): raise OS_Profile_NickInvalid() profile_data["user"] = user profile_pk = Profile.insert(**profile_data).execute() profile = Profile.get((Profile.id == profile_pk)) profile = model_to_dict(profile) del profile["user"] user = model_to_dict(user) return {"user": user, "profile": profile, "success": True}
def send_buddy_message(self, request_data): response = {"success": False} try: from_profile = Profile.get( (Profile.id == request_data["from_profileid"]) & (Profile.deleted == False)) to_profile = Profile.get( (Profile.id == request_data["to_profileid"]) & (Profile.deleted == False)) status_key = "status_{}".format(to_profile.id) if self.redis_presence_ctx.exists(status_key): #check if online publish_data = "\\type\\buddy_message\\from_profileid\\{}\\to_profileid\\{}\\msg_type\\{}\\message\\{}".format( from_profile.id, to_profile.id, request_data["type"], request_data["message"]) self.redis_presence_ctx.publish(self.redis_presence_channel, publish_data) else: msg_id = self.redis_presence_ctx.incr("MSG_CNT") redis_key = "msg_{}_{}".format(to_profile.id, msg_id) self.redis_presence_ctx.hset(redis_key, "message", request_data["message"]) self.redis_presence_ctx.hset(redis_key, "timestamp", int(time.time())) self.redis_presence_ctx.hset(redis_key, "type", request_data["type"]) self.redis_presence_ctx.hset(redis_key, "from", from_profile.id) response["success"] = True except Profile.DoesNotExist: pass return response
def handle_update_profile(self, data): if "profile" not in data: data = {'profile': data} if "profileid" in data["profile"]: data["profile"]["id"] = data["profile"]["profileid"] del data["profile"]["profileid"] if "nick" in data["profile"]: data["profile"]["nick"] = data["profile"]["nick"] del data["profile"]["nick"] profile_model = Profile.get((Profile.id == data['profile']['id'])) namespaceid = 0 if "namespaceid" in data['profile']: namespaceid = data['profile']['namespaceid'] if "nick" in data['profile']: if not Profile.is_nick_valid(data['profile']['nick'], namespaceid): raise OS_Profile_NickInvalid() if "uniquenick" in data['profile']: if not Profile.is_nick_valid(data['profile']['uniquenick'], namespaceid): raise OS_Profile_UniquenickInvalid() nick_available = self.check_uniquenick_available( data['profile']['uniquenick'], namespaceid) if nick_available["exists"]: raise OS_Profile_UniquenickInUse(nick_available["profile"]) for key in data['profile']: if key != "id": setattr(profile_model, key, data['profile'][key]) profile_model.save() return {"success": True}
def get_profile_by_id(self, profileid): try: return Profile.select().join( User).where((Profile.id == profileid) & (User.deleted == 0) & (Profile.deleted == 0)).get() except Profile.DoesNotExist: return None
def get_profile_by_uniquenick(self, uniquenick, namespaceid, partnercode): try: if namespaceid == 0: the_uniquenick = Profile.select().join(User).where( (Profile.uniquenick == uniquenick) & (User.partnercode == partnercode) & (User.deleted == 0) & (Profile.deleted == 0)).get() else: the_uniquenick = Profile.select().join(User).where( (Profile.uniquenick == uniquenick) & (Profile.namespaceid == namespaceid) & (User.partnercode == partnercode) & (User.deleted == 0) & (Profile.deleted == 0)).get() return the_uniquenick except Profile.DoesNotExist: return None
def get_keyed_data(self, persist_data, key): ret = {} profile = None game = None try: profile = Profile.select().where( (Profile.id == persist_data["profileid"])).get() game = Game.select().where( (Game.id == persist_data["game_id"])).get() except (PersistKeyedData.DoesNotExist, Profile.DoesNotExist) as e: pass try: where = (PersistKeyedData.key_name == key) & ( PersistKeyedData.data_index == persist_data["data_index"]) & ( PersistKeyedData.persist_type == persist_data["data_type"] ) & (PersistKeyedData.profileid == profile.id) & (PersistKeyedData.gameid == game.id) if "modified_since" in persist_data: where = (where) & (PersistKeyedData.modified > persist_data["modified_since"]) data_entry = PersistKeyedData.select().where(where).get() ret = model_to_dict(data_entry) ret["modified"] = calendar.timegm(ret["modified"].utctimetuple()) del ret["profile"] except (PersistKeyedData.DoesNotExist) as e: pass return ret
def set_persist_raw_data(self, persist_data): profile = None game = None try: profile = Profile.select().where( (Profile.id == persist_data["profileid"])).get() game = Game.select().where( (Game.id == persist_data["game_id"])).get() except (PersistKeyedData.DoesNotExist, Profile.DoesNotExist) as e: pass try: data_entry = PersistData.select().where( (PersistData.data_index == persist_data["data_index"]) & (PersistData.persist_type == persist_data["data_type"]) & (PersistData.profileid == profile.id) & (PersistData.gameid == game.id)).get() data_entry.base64Data = persist_data["data"] data_entry.modified = datetime.utcnow() data_entry.save() except (PersistData.DoesNotExist) as e: data_entry = PersistData.create( base64Data=persist_data["data"], data_index=persist_data["data_index"], persist_type=persist_data["data_type"], modified=datetime.utcnow(), profileid=persist_data["profileid"], gameid=persist_data["game_id"]) ret = model_to_dict(data_entry) del ret["profile"] ret["modified"] = calendar.timegm(ret["modified"].utctimetuple()) return ret
def get_profile_by_nick_email(self, nick, namespaceid, email, partnercode): try: return Profile.select().join(User).where( (Profile.nick == nick) & (Profile.namespaceid == namespaceid) & (User.email == email) & (User.deleted == 0) & (Profile.deleted == 0)).get() except Profile.DoesNotExist: return None
def handle_del_block_buddy(self, request_data): response = {"success": False} try: to_profile_model = Profile.get( (Profile.id == request_data["to_profileid"])) from_profile_model = Profile.get( (Profile.id == request_data["from_profileid"])) count = Block.delete().where( (Block.from_profile == from_profile_model) & (Block.to_profile == to_profile_model)).execute() if count > 0: publish_data = "\\type\\del_block_buddy\\from_profileid\\{}\\to_profileid\\{}\\".format( from_profile_model.id, to_profile_model.id) self.redis_presence_ctx.publish(self.redis_presence_channel, publish_data) response["success"] = True except Profile.DoesNotExist: pass return response
def handle_block_buddy(self, request_data): response = {"success": False} try: to_profile_model = Profile.get( (Profile.id == request_data["to_profileid"])) from_profile_model = Profile.get( (Profile.id == request_data["from_profileid"])) success = Block.insert( to_profile=to_profile_model, from_profile=from_profile_model).execute() != 0 if success: publish_data = "\\type\\block_buddy\\from_profileid\\{}\\to_profileid\\{}\\".format( from_profile_model.id, to_profile_model.id) self.redis_presence_ctx.publish(self.redis_presence_channel, publish_data) response["success"] = True except Profile.DoesNotExist: pass return response
def handle_get_blocks_status(self, request_data): response = {"profiles": []} try: profile = Profile.get(Profile.id == request_data["profileid"]) blocks = Block.select().where((Block.from_profile == profile)) for block in blocks: status_data = self.get_gp_status(block.to_profile.id) status_data['profileid'] = block.to_profile.id response["profiles"].append(status_data) except (Block.DoesNotExist, Profile.DoesNotExist) as e: pass return response
def test_session_by_profileid(self, params): if "profileid" not in params or "session_key" not in params: return {'valid': False} try: profile = Profile.get((Profile.id == params["profileid"])) test_params = { 'session_key': params["session_key"], 'userid': profile.userid } return self.test_session(test_params) except Profile.DoesNotExist: return {'valid': False}
def handle_get_profile(self, data): profile = None response = {} try: if "profileid" in data: profile = Profile.get((Profile.id == data["profileid"])) elif "uniquenick" in data: if "namespaceid" in data: profile = Profile.get( (Profile.uniquenick == data["uniquenick"]) & (Profile.namespaceid == data["namespaceid"])) else: profile = Profile.get( (Profile.uniquenick == data["uniquenick"]) & (Profile.namespaceid == 0)) if profile: response["profile"] = model_to_dict(profile) response["success"] = True except Profile.DoesNotExist: pass return response
def handle_delete_profile(self, data): profile_data = data["profile"] response = {"success": False} try: profile = Profile.get((Profile.id == profile_data["id"])) if profile.deleted: return response profile.deleted = True profile.save() response["success"] = True except Profile.DoesNotExist: pass return response
def handle_get_buddies_status(self, request_data): response = {"statuses": [], "success": False} try: profile = Profile.get(Profile.id == request_data["profileid"]) buddies = Buddy.select().where((Buddy.from_profile == profile)) for buddy in buddies: status_data = self.get_gp_status(buddy.to_profile.id) status_data['profileid'] = buddy.to_profile.id response["statuses"].append(status_data) response["success"] = True except (Buddy.DoesNotExist, Profile.DoesNotExist) as e: pass return response
def handle_get_profiles(self, data): response = {"success": False} profiles = [] try: for profile in Profile.select().where( (Profile.userid == data["userid"]) & (Profile.deleted == False)): profile_dict = model_to_dict(profile) profiles.append(profile_dict) response["success"] = True except Profile.DoesNotExist: raise OS_Auth_NoSuchUser() response["profiles"] = profiles return response
def check_uniquenick_available(self, uniquenick, namespaceid): try: if namespaceid != 0: profile = Profile.get((Profile.uniquenick == uniquenick) & (Profile.namespaceid == namespaceid) & (Profile.deleted == False)) return {"exists": True, "profile": profile} else: #profile = Profile.get((Profile.uniquenick == uniquenick) & (Profile.deleted == False)) #profile = model_to_dict(profile) #del profile["user"]["password"] return {"exists": False} except Profile.DoesNotExist: return {"exists": False}
def handle_delete_session(self, params): userid = None session_key = None if "profileid" in params: profile = Profile.get((Profile.id == params["profileid"])) userid = profile.userid elif "userid" in params: userid = params["userid"] if "session_key" in params: session_key = params["session_key"] if userid != None: redis_key = "{}:{}".format(int(userid), session_key) if self.redis_ctx.exists(redis_key): self.redis_ctx.delete(redis_key) return {'deleted': True} return {'deleted': False}
def validate_request(self, xml_tree): request_info = {"success": False} game_id = xml_tree.find("{http://gamespy.net/sake}gameid").text secretkey = xml_tree.find("{http://gamespy.net/sake}secretKey").text loginTicket = xml_tree.find( "{http://gamespy.net/sake}loginTicket").text #gameid = xml_tree.find() #print("GameID: {}\n".format(game_id)) game = Game.select().where(Game.id == game_id).get() profile = Profile.select().where(Profile.id == 10000).get() #print("Profile: {}\nGame: {}\n{} - {}\n".format(profile,game, game.secretkey, secretkey)) if game.secretkey != secretkey: return request_info request_info["success"] = True request_info["game"] = game request_info["profile"] = profile return request_info
def handle_blocks_search(self, request_data): response = {"profiles": [], "success": False} profile_data = request_data["profile"] if "id" not in profile_data: return False profile = Profile.get(Profile.id == profile_data["id"]) buddies = Block.select().where((Block.from_profile == profile)) try: for buddy in buddies: model = model_to_dict(buddy) to_profile = model['to_profile'] del to_profile['user']['password'] response["profiles"].append(to_profile) response["success"] = True except (Buddy.DoesNotExist, Profile.DoesNotExist) as e: pass return response
def update_or_create_keyed_data(self, persist_data, key, value): profile = None game = None try: profile = Profile.select().where( (Profile.id == persist_data["profileid"])).get() game = Game.select().where( (Game.id == persist_data["game_id"])).get() except (Profile.DoesNotExist, Game.DoesNotExist) as e: pass try: data_entry = PersistKeyedData.select().where( (PersistKeyedData.key_name == key) & (PersistKeyedData.data_index == persist_data["data_index"]) & (PersistKeyedData.persist_type == persist_data["data_type"]) & (PersistKeyedData.profileid == profile.id) & (PersistKeyedData.gameid == game.id)).get() data_entry.key_value = value data_entry.modified = datetime.utcnow() data_entry.save() except (PersistKeyedData.DoesNotExist) as e: data_entry = PersistKeyedData.create( key_name=key, key_value=value, data_index=persist_data["data_index"], persist_type=persist_data["data_type"], modified=datetime.utcnow(), profileid=persist_data["profileid"], gameid=persist_data["game_id"]) ret = model_to_dict(data_entry) del ret["profile"] ret["modified"] = calendar.timegm(ret["modified"].utctimetuple()) return ret
def auth_or_create_profile(self, request_body, account_data): #{u'profilenick': u'sctest01', u'save_session': True, u'set_context': u'profile', u'hash_type': u'auth_or_create_profile', u'namespaceid': 1, #u'uniquenick': u'', u'partnercode': 0, u'password': u'gspy', u'email': u'*****@*****.**'} user_where = (User.deleted == False) user_data = request_body["user"] if "email" in user_data: #if User.is_email_valid(user_data["email"]): # raise OS_InvalidParam("email") user_where = (user_where) & (User.email == user_data["email"]) if "partnercode" in user_data: user_data['partnercode'] = user_data["partnercode"] partnercode = user_data["partnercode"] else: partnercode = 0 auth_create_auth_token = False if "use_auth_token" in request_body: auth_create_auth_token = request_body["use_auth_token"] user_where = ((user_where) & (User.partnercode == partnercode)) try: user = User.get(user_where) user = model_to_dict(user) if ("password" not in user_data or user['password'] != user_data["password"]) and not auth_create_auth_token: raise OS_Auth_InvalidCredentials() except User.DoesNotExist: register_svc = RegistrationService() user = register_svc.try_register_user(user_data) user = user["user"] profile_data = request_body["profile"] #create data profile_where = (Profile.deleted == False) force_unique = False if "uniquenick" in profile_data: if "namespaceid" in profile_data: if profile_data["namespaceid"] > 0: force_unique = True else: force_unique = True profile_where = (profile_where) & (Profile.uniquenick == profile_data["uniquenick"]) if "nick" in profile_data: profile_where = (profile_where) & (Profile.nick == profile_data['nick']) if "namespaceid" in profile_data: namespaceid = profile_data["namespaceid"] else: namespaceid = 0 profile_where = (profile_where) & (Profile.namespaceid == namespaceid) ret = {} try: if not force_unique: profile_where = (profile_where) & (Profile.userid == user["id"]) profile = Profile.get(profile_where) profile = model_to_dict(profile) del profile['user']['password'] ret["new_profile"] = False ret["success"] = True if user["id"] != profile["user"]["id"]: raise OS_InvalidParam("uniquenick") except Profile.DoesNotExist: user_profile_srv = UserProfileMgrService() #this should raise an exception instead of return an error object... profile = user_profile_srv.handle_create_profile({ 'profile': profile_data, 'user': user }) if "error" in profile: ret["error"] = profile["error"] return ret ret["new_profile"] = True ret["success"] = True profile = profile["profile"] ret["profile"] = profile ret["user"] = user if auth_create_auth_token: auth_token_data = self.handle_make_auth_ticket( {"profileid": profile["id"]}) ret["auth_token"] = auth_token_data return ret
def handle_send_presence_login_messages(self, request_data): profile = Profile.get((Profile.id == request_data["profileid"]) & (Profile.deleted == False)) #send revokes revoke_list_key = "revokes_{}".format(profile.id) cursor = 0 while True: resp = self.redis_presence_ctx.hscan(revoke_list_key, cursor) cursor = resp[0] for pid, time in resp[1].items(): publish_data = "\\type\\del_buddy\\from_profileid\\{}\\to_profileid\\{}".format( pid, profile.id) self.redis_presence_ctx.publish(self.redis_presence_channel, publish_data) self.redis_presence_ctx.delete(revoke_list_key) if cursor == 0: break #send add requests add_req_key = "add_req_{}".format(profile.id) cursor = 0 while True: resp = self.redis_presence_ctx.hscan(add_req_key, cursor) cursor = resp[0] for pid, reason in resp[1].items(): publish_data = "\\type\\add_request\\from_profileid\\{}\\to_profileid\\{}\\reason\\{}".format( int(pid.decode("utf-8")), profile.id, reason.decode("utf-8")) self.redis_presence_ctx.publish(self.redis_presence_channel, publish_data) if cursor == 0: break #send pending msgs msg_scan_key = "msg_{}_*".format(profile.id) cursor = 0 while True: resp = self.redis_presence_ctx.scan(cursor, msg_scan_key) cursor = resp[0] for key in resp[1]: msg_key = key message = self.redis_presence_ctx.hget( msg_key, "message").decode("utf-8") timestamp = int( self.redis_presence_ctx.hget(msg_key, "timestamp").decode("utf-8")) msg_type = int( self.redis_presence_ctx.hget(msg_key, "type").decode("utf-8")) msg_from = int( self.redis_presence_ctx.hget(msg_key, "from").decode("utf-8")) self.redis_presence_ctx.delete(msg_key) publish_data = "\\type\\buddy_message\\from_profileid\\{}\\to_profileid\\{}\\msg_type\\{}\\message\\{}".format( msg_from, profile.id, msg_type, message) self.redis_presence_ctx.publish(self.redis_presence_channel, publish_data) if cursor == 0: break return {"success": True}
def handle_profile_search(self, search_data): #{u'chc': 0, u'ooc': 0, u'i1': 0, u'pic': 0, u'lon': 0.0, u'mar': 0, u'namespaceids': [1], u'lat': 0.0, #u'birthday': 0, u'mode': u'profile_search', u'partnercode': 0, u'ind': 0, u'sex': 0, u'email': u'*****@*****.**'} profile_search_data = search_data["profile"] user_seach_data = search_data["user"] where_expression = ((Profile.deleted == False) & (User.deleted == False)) #user search if "userid" in profile_search_data: where_expression = ((where_expression) & (User.id == profile_search_data["userid"])) elif "id" in user_seach_data: where_expression = ((where_expression) & (User.id == user_seach_data["id"])) if "email" in user_seach_data: where_expression = ((where_expression) & (User.email == user_seach_data["email"])) if "partnercode" in profile_search_data: where_expression = ( (where_expression) & (User.partnercode == profile_search_data["partnercode"])) #profile search if "id" in profile_search_data: where_expression = ((where_expression) & (Profile.id == profile_search_data["id"])) if "nick" in profile_search_data: where_expression = ((where_expression) & (Profile.nick == profile_search_data["nick"])) if "uniquenick" in profile_search_data: where_expression = ( (where_expression) & (Profile.uniquenick == profile_search_data["uniquenick"])) if "firstname" in profile_search_data: where_expression = ( (where_expression) & (Profile.firstname == profile_search_data["firstname"])) if "lastname" in profile_search_data: where_expression = ( (where_expression) & (Profile.firstname == profile_search_data["lastname"])) if "icquin" in profile_search_data: where_expression = ( (where_expression) & (Profile.icquin == profile_search_data["icquin"])) if "namespaceids" in profile_search_data: namespace_expression = None for namespaceid in profile_search_data["namespaceids"]: if namespace_expression == None: namespace_expression = (Profile.namespaceid == namespaceid) else: namespace_expression = ((Profile.namespaceid == namespaceid) | (namespace_expression)) if namespace_expression != None: where_expression = (where_expression) & (namespace_expression) profiles = Profile.select().join(User).where(where_expression) ret_profiles = [] for profile in profiles: append_profile = model_to_dict(profile) append_profile['userid'] = append_profile['user']['id'] del append_profile['user']['password'] ret_profiles.append(append_profile) return {"profiles": ret_profiles, "success": True}