This commit is contained in:
@@ -62,6 +62,7 @@ class MatrixBot:
|
|||||||
Returns:
|
Returns:
|
||||||
bool: True if file was saved
|
bool: True if file was saved
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
session = {
|
session = {
|
||||||
'access_token': self.session.access_token,
|
'access_token': self.session.access_token,
|
||||||
'device_id': self.session.device_id,
|
'device_id': self.session.device_id,
|
||||||
@@ -71,22 +72,37 @@ class MatrixBot:
|
|||||||
with open(self.session_file, 'w') as f:
|
with open(self.session_file, 'w') as f:
|
||||||
yaml.dump(session, f)
|
yaml.dump(session, f)
|
||||||
return True
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f'Failed to save session to {self.session_file}: {str(e)}')
|
||||||
|
return False
|
||||||
|
|
||||||
def _load_session(self) -> bool:
|
def _load_session(self) -> bool:
|
||||||
"""Load session date from file.
|
"""Load session data from file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if file was found and loaded
|
bool: True if file was found and loaded
|
||||||
"""
|
"""
|
||||||
if not os.path.exists(self.session_file):
|
if not os.path.exists(self.session_file):
|
||||||
|
logging.info(f'No session file found at {self.session_file}')
|
||||||
return False
|
return False
|
||||||
|
try:
|
||||||
logging.info(f'restore session from file {self.session_file}')
|
logging.info(f'restore session from file {self.session_file}')
|
||||||
with open(self.session_file, 'r') as f:
|
with open(self.session_file, 'r') as f:
|
||||||
session = yaml.safe_load(f)
|
session = yaml.safe_load(f)
|
||||||
|
|
||||||
|
# Extended validation of session data
|
||||||
|
required_fields = ['access_token', 'device_id', 'next_batch']
|
||||||
|
if not all(field in session for field in required_fields):
|
||||||
|
logging.warning('Session file exists but is incomplete')
|
||||||
|
return False
|
||||||
|
|
||||||
self.client.access_token = session['access_token']
|
self.client.access_token = session['access_token']
|
||||||
self.client.device_id = session['device_id']
|
self.client.device_id = session['device_id']
|
||||||
self.client.next_batch = session['next_batch']
|
self.client.next_batch = session['next_batch']
|
||||||
return True
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f'Failed to load session from {self.session_file}: {str(e)}')
|
||||||
|
return False
|
||||||
|
|
||||||
async def _login(self) -> bool:
|
async def _login(self) -> bool:
|
||||||
"""Restore login with session file or run full login process.
|
"""Restore login with session file or run full login process.
|
||||||
@@ -100,15 +116,21 @@ class MatrixBot:
|
|||||||
logging.debug('restore session successfully')
|
logging.debug('restore session successfully')
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logging.warning('no session file was found or file is broken')
|
logging.warning('no session file was found or file is broken - will attempt full login')
|
||||||
|
|
||||||
logging.debug(f'try login with account {self.username}')
|
logging.debug(f'try login with account {self.username}')
|
||||||
|
try:
|
||||||
self.session = await self.client.login(self.__password)
|
self.session = await self.client.login(self.__password)
|
||||||
if isinstance(self.session, LoginResponse):
|
if isinstance(self.session, LoginResponse):
|
||||||
logging.info(f'login successfull with account {self.username} on server {self.homeserver}')
|
logging.info(f'login successful with account {self.username} on server {self.homeserver}')
|
||||||
logging.info(self.session)
|
logging.info(self.session)
|
||||||
self._save_session()
|
self._save_session()
|
||||||
return True
|
return True
|
||||||
|
else:
|
||||||
|
logging.error(f"Login failed for user {self.username}: Unexpected response type {type(self.session)}")
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Login attempt failed for user {self.username}: {str(e)}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _find_skills(self) -> None:
|
def _find_skills(self) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user