MTPyroger/pyrogram/storage/storage.py

89 lines
2.7 KiB
Python
Raw Normal View History

2020-03-21 14:43:32 +00:00
# Pyrogram - Telegram MTProto API Client Library for Python
2022-01-07 09:23:45 +00:00
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
2019-06-19 14:04:06 +00:00
#
2020-03-21 14:43:32 +00:00
# This file is part of Pyrogram.
2019-06-19 14:04:06 +00:00
#
2020-03-21 14:43:32 +00:00
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2019-06-19 14:04:06 +00:00
#
2020-03-21 14:43:32 +00:00
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
2019-06-19 14:04:06 +00:00
#
2020-03-21 14:43:32 +00:00
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
2019-06-19 14:04:06 +00:00
2019-09-14 17:39:21 +00:00
import base64
import struct
from typing import List, Tuple
2021-12-20 19:34:57 +00:00
from pyrogram import utils
2019-06-19 14:04:06 +00:00
class Storage:
2019-09-14 17:39:21 +00:00
SESSION_STRING_FORMAT = ">B?256sI?"
2021-12-20 19:34:57 +00:00
SESSION_STRING_FORMAT_64 = ">B?256sQ?"
2019-09-14 17:39:21 +00:00
SESSION_STRING_SIZE = 351
2021-12-20 19:34:57 +00:00
SESSION_STRING_SIZE_64 = 356
2019-09-14 17:39:21 +00:00
2019-06-19 14:04:06 +00:00
def __init__(self, name: str):
self.name = name
async def open(self):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def save(self):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def close(self):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def delete(self):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def get_peer_by_id(self, peer_id: int):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def get_peer_by_username(self, username: str):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def get_peer_by_phone_number(self, phone_number: str):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def dc_id(self, value: int = object):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def test_mode(self, value: bool = object):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def auth_key(self, value: bytes = object):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def date(self, value: int = object):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def user_id(self, value: int = object):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def is_bot(self, value: bool = object):
2019-06-19 14:04:06 +00:00
raise NotImplementedError
async def export_session_string(self):
2021-12-20 19:34:57 +00:00
user_id = await self.user_id()
2019-09-14 17:39:21 +00:00
return base64.urlsafe_b64encode(
struct.pack(
2021-12-20 19:34:57 +00:00
self.SESSION_STRING_FORMAT if user_id < utils.MAX_USER_ID_OLD else self.SESSION_STRING_FORMAT_64,
await self.dc_id(),
await self.test_mode(),
await self.auth_key(),
2021-12-20 19:34:57 +00:00
user_id,
await self.is_bot()
2019-09-14 17:39:21 +00:00
)
).decode().rstrip("=")