From ce212e83d2a511e934cd43461a959a376364dd0c Mon Sep 17 00:00:00 2001 From: Open Toontown Date: Fri, 15 Nov 2019 20:33:05 -0500 Subject: [PATCH] login: send proper login response --- otp/login/AstronLoginManager.py | 98 ++++++++++++++++++++++++++++++- otp/login/AstronLoginManagerUD.py | 43 ++++++++++++-- 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/otp/login/AstronLoginManager.py b/otp/login/AstronLoginManager.py index 447d08e..19825dd 100644 --- a/otp/login/AstronLoginManager.py +++ b/otp/login/AstronLoginManager.py @@ -1,5 +1,10 @@ +import json +from datetime import datetime +import time + from direct.distributed.DistributedObjectGlobal import DistributedObjectGlobal from direct.directnotify import DirectNotifyGlobal +from otp.uberdog.AccountDetailRecord import AccountDetailRecord class AstronLoginManager(DistributedObjectGlobal): notify = DirectNotifyGlobal.directNotify.newCategory('AstronLoginManager') @@ -17,5 +22,94 @@ class AstronLoginManager(DistributedObjectGlobal): self.sendUpdate('requestLogin', [playToken]) def loginResponse(self, responseBlob): - # TODO HANDLE THIS PROPERLY - messenger.send(self.doneEvent, [{'mode': 'success'}]) + responseData = json.loads(responseBlob) + now = time.time() + returnCode = responseData.get('returnCode') + respString = responseData.get('respString') + errorString = self.getExtendedErrorMsg(respString) + accountNumber = responseData.get('accountNumber') + self.cr.DISLIdFromLogin = accountNumber + accountDetailRecord = AccountDetailRecord() + self.cr.accountDetailRecord = accountDetailRecord + createFriendsWithChat = responseData.get('createFriendsWithChat') + canChat = createFriendsWithChat == "YES" or createFriendsWithChat == "CODE" + self.cr.secretChatAllowed = canChat + chatCodeCreationRule = responseData.get('chatCodeCreationRule') + self.cr.chatChatCodeCreationRule = chatCodeCreationRule + self.cr.secretChatNeedsParentPassword = chatCodeCreationRule == "PARENT" + serverTime = responseData.get('serverTime') + self.cr.serverTimeUponLogin = serverTime + self.cr.clientTimeUponLogin = now + self.cr.globalClockRealTimeUponLogin = globalClock.getRealTime() + if hasattr(self.cr, "toontownTimeManager"): + self.cr.toontownTimeManager.updateLoginTimes(serverTime, now, self.cr.globalClockRealTimeUponLogin) + serverDelta = serverTime - now + self.cr.setServerDelta(serverDelta) + self.notify.setServerDelta(serverDelta, 28800) + access = responseData.get('access') + isPaid = access == "FULL" + self.cr.parentPasswordSet = isPaid + self.cr.setIsPaid(isPaid) + if isPaid: + launcher.setPaidUserLoggedIn() + WhiteListResponse = responseData.get('WhiteListResponse') + if WhiteListResponse == "YES": + self.cr.whiteListChatEnabled = 1 + else: + self.cr.whiteListChatEnabled = 0 + lastLoggedInStr = responseData.get('lastLoggedInStr') + self.cr.lastLoggedIn = datetime.now() + if hasattr(self.cr, "toontownTimeManager"): + self.cr.lastLoggedIn = self.cr.toontownTimeManager.convertStrToToontownTime(lastLoggedInStr) + accountDaysFromServer = responseData.get('accountDays') + if accountDaysFromServer is not None: + self.cr.accountDays = self.parseAccountDays(accountDaysFromServer) + else: + self.cr.accountDays = 100000 + toonAccountType = responseData.get('toonAccountType') + if toonAccountType == "WITH_PARENT_ACCOUNT": + self.cr.withParentAccount = True + elif toonAccountType == "NO_PARENT_ACCOUNT": + self.cr.withParentAccount = False + else: + self.notify.error("unknown toon account type %s" % toonAccountType) + self.userName = responseData.get('userName') + self.cr.userName = self.userName + self.notify.info("Login response return code %s" % returnCode) + if returnCode == 0: + self.__handleLoginSuccess() + elif returnCode == -13: + self.notify.info("Period Time Expired") + messenger.send(self.doneEvent, [{'mode': 'reject'}]) + else: + self.notify.info("Login failed: %s" % errorString) + messenger.send(self.doneEvent, [{'mode': 'reject'}]) + + def __handleLoginSuccess(self): + self.cr.logAccountInfo() + launcher.setGoUserName(self.userName) + launcher.setLastLogin(self.userName) + launcher.setUserLoggedIn() + if self.cr.loginInterface.freeTimeExpires == -1: + launcher.setPaidUserLoggedIn() + if self.cr.loginInterface.needToSetParentPassword(): + messenger.send(self.doneEvent, [{'mode': 'getChatPassword'}]) + else: + messenger.send(self.doneEvent, [{'mode': 'success'}]) + + def getExtendedErrorMsg(self, errorString): + prefix = 'Bad DC Version Compare' + if len(errorString) < len(prefix): + return errorString + if errorString[:len(prefix)] == prefix: + return '%s%s' % (errorString, ', address=%s' % self.cr.getServerAddress()) + return errorString + + def parseAccountDays(self, accountDays): + result = 100000 + if accountDays >= 0: + result = accountDays + else: + self.notify.warning('account days is negative %s' % accountDays) + self.notify.debug('result=%s' % result) + return result diff --git a/otp/login/AstronLoginManagerUD.py b/otp/login/AstronLoginManagerUD.py index 4e40912..46b5713 100644 --- a/otp/login/AstronLoginManagerUD.py +++ b/otp/login/AstronLoginManagerUD.py @@ -1,6 +1,8 @@ import anydbm import dumbdbm +import json import sys +from datetime import datetime import time from direct.directnotify import DirectNotifyGlobal @@ -145,10 +147,43 @@ class LoginOperation: # set client state to established, thus un-sandboxing the sender self.loginManager.air.setClientState(self.sender, 2) - # send dummy login response - import json - a = json.dumps({}) - self.loginManager.sendUpdateToChannel(self.sender, 'loginResponse', [a]) + responseData = { + 'returnCode': 0, + 'respString': '', + 'accountNumber': self.sender, + 'createFriendsWithChat': 'YES', + 'chatCodeCreationRule': 'YES', + 'access': 'FULL', + 'WhiteListResponse': 'YES', + 'lastLoggedInStr': self.getLastLoggedInStr(), + 'accountDays': self.getAccountDays(), + 'serverTime': int(time.time()), + 'toonAccountType': 'NO_PARENT_ACCOUNT', + 'userName': str(self.databaseId) + } + responseBlob = json.dumps(responseData) + self.loginManager.sendUpdateToChannel(self.sender, 'loginResponse', [responseBlob]) + + def getLastLoggedInStr(self): + return '' # TODO + + def getAccountCreationDate(self): + accountCreationDate = self.account.get('CREATED', '') + try: + accountCreationDate = datetime.fromtimestamp(time.mktime(time.strptime(accountCreationDate))) + except ValueError: + accountCreationDate = '' + + return accountCreationDate + + def getAccountDays(self): + accountCreationDate = self.getAccountCreationDate() + accountDays = -1 + if accountCreationDate: + now = datetime.fromtimestamp(time.mktime(time.strptime(time.ctime()))) + accountDays = abs((now - accountCreationDate).days) + + return accountDays class AstronLoginManagerUD(DistributedObjectGlobalUD):