forked from TelegramPlayground/PyroTGFork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
232 lines (193 loc) · 7.39 KB
/
Copy pathstorage.py
File metadata and controls
232 lines (193 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2017-present bakatrouble <https://github.com/bakatrouble>
# Copyright (C) 2017-present cavallium <https://github.com/cavallium>
# Copyright (C) 2017-present andrew-ld <https://github.com/andrew-ld>
# Copyright (C) 2017-present 01101sam <https://github.com/01101sam>
# Copyright (C) 2017-present KurimuzonAkuma <https://github.com/KurimuzonAkuma>
#
# This file is part of Pyrogram.
#
# 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.
#
# 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.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from abc import ABC, abstractmethod
import base64
import struct
from pyrogram import raw
class Storage(ABC):
"""
Abstract class for storage engines.
Parameters:
name (``str``):
The name of the session.
"""
OLD_SESSION_STRING_FORMAT = ">B?256sI?"
OLD_SESSION_STRING_FORMAT_64 = ">B?256sQ?"
SESSION_STRING_SIZE = 351
SESSION_STRING_SIZE_64 = 356
SESSION_STRING_FORMAT = ">BI?256sQ?"
def __init__(self, name: str):
self.name = name
@abstractmethod
async def open(self):
"""Opens the storage engine."""
raise NotImplementedError
@abstractmethod
async def save(self):
"""Saves the current state of the storage engine."""
raise NotImplementedError
@abstractmethod
async def close(self):
"""Closes the storage engine."""
raise NotImplementedError
@abstractmethod
async def delete(self):
"""Deletes the storage file."""
raise NotImplementedError
@abstractmethod
async def update_peers(self, peers: list[tuple[int, int, str, str]]) -> None:
"""
Update the peers table with the provided information.
Parameters:
peers (``list[tuple[int, int, str, str]]``):
A list of tuples containing the
information of the peers to be updated. Each tuple must contain the following
information:
- ``int``: The peer id.
- ``int``: The peer access hash.
- ``str``: The peer type (user, chat or channel).
- ``str``: The peer phone number (if any).
"""
raise NotImplementedError
@abstractmethod
async def update_usernames(self, usernames: list[tuple[int, list[str]]]) -> None:
"""
Update the usernames table with the provided information.
Parameters:
usernames (``list[tuple[int, list[str]]]``):
A list of tuples containing the
information of the usernames to be updated. Each tuple must contain the following
information:
- ``int``: The peer id.
- list of ``str``: The peer username (if any).
"""
raise NotImplementedError
@abstractmethod
async def update_state(self, update_state: tuple[int, int, int, int, int] = object) -> tuple[int, int, int, int, int]:
"""Get or set the update state of the current session.
Parameters:
update_state (``tuple[int, int, int, int, int]``):
A tuple containing the update state to set.
tuple must contain the following information:
- ``int``: The id of the entity.
- ``int``: The pts.
- ``int``: The qts.
- ``int``: The date.
- ``int``: The seq.
"""
raise NotImplementedError
@abstractmethod
async def get_peer_by_id(self, peer_id: int) -> "raw.base.InputPeer":
"""Retrieve a peer by its ID.
Parameters:
peer_id (``int``):
The ID of the peer to retrieve.
"""
raise NotImplementedError
@abstractmethod
async def get_peer_by_username(self, username: str) -> "raw.base.InputPeer":
"""Retrieve a peer by its username.
Parameters:
username (``str``):
The username of the peer to retrieve.
"""
raise NotImplementedError
@abstractmethod
async def get_peer_by_phone_number(self, phone_number: str) -> "raw.base.InputPeer":
"""Retrieve a peer by its phone number.
Parameters:
phone_number (``str``):
The phone number of the peer to retrieve.
"""
raise NotImplementedError
@abstractmethod
async def dc_id(self, value: int = object) -> int:
"""Get or set the DC ID of the current session.
Parameters:
value (``int``, *optional*):
The DC ID to set.
"""
raise NotImplementedError
@abstractmethod
async def api_id(self, value: int = object) -> int:
"""Get or set the API ID of the current session.
Parameters:
value (``int``, *optional*):
The API ID to set.
"""
raise NotImplementedError
@abstractmethod
async def test_mode(self, value: bool = object) -> bool:
"""Get or set the test mode of the current session.
Parameters:
value (``bool``, *optional*):
The test mode to set.
"""
raise NotImplementedError
@abstractmethod
async def auth_key(self, value: bytes = object) -> bytes:
"""Get or set the authorization key of the current session.
Parameters:
value (``bytes``, *optional*):
The authorization key to set.
"""
raise NotImplementedError
@abstractmethod
async def date(self, value: int = object) -> int:
"""Get or set the date of the current session.
Parameters:
value (``int``, *optional*):
The date to set.
"""
raise NotImplementedError
@abstractmethod
async def user_id(self, value: int = object) -> int:
"""Get or set the user ID of the current session.
Parameters:
value (``int``, *optional*):
The user ID to set.
"""
raise NotImplementedError
@abstractmethod
async def is_bot(self, value: bool = object) -> bool:
"""Get or set the bot flag of the current session.
Parameters:
value (``bool``, *optional*):
The bot flag to set.
"""
raise NotImplementedError
async def export_session_string(self) -> str:
"""Exports the session string for the current session.
Returns:
``str``: The session string for the current session.
"""
packed = struct.pack(
self.SESSION_STRING_FORMAT,
await self.dc_id(),
await self.api_id(),
await self.test_mode(),
await self.auth_key(),
await self.user_id(),
await self.is_bot()
)
return base64.urlsafe_b64encode(packed).decode().rstrip("=")