@ -1,11 +1,13 @@
import asyncio
import functools
import gc
from concurrent . futures import ProcessPoolExecutor
import telebot
from sqlalchemy . future import Engine
from telebot . async_telebot import AsyncTeleBot
from . . import d ata base , encryption , generate_password
from . . import d b, encryption , generate_password
from . . account_checks import (
check_account ,
check_account_name ,
@ -18,7 +20,6 @@ from . import markups
from . helper_functions import (
base_handler ,
delete_message ,
escape ,
get_state ,
register_state ,
send_deleteable_message ,
@ -34,7 +35,7 @@ async def get_accounts(
mes : Message ,
) - > None :
await base_handler ( bot , mes )
accounts = d ata base . get . get_account s(
accounts = d b. get . get_account _name s(
engine ,
mes . from_user . id ,
to_sort = True ,
@ -71,8 +72,8 @@ async def _delete_all2(
await base_handler ( bot , mes , prev_mes )
text = mes . text . strip ( )
if text == " YES " :
d ata base . delete . purge_accounts ( engine , mes . from_user . id )
d ata base . delete . delete_master_pass ( engine , mes . from_user . id )
d b. delete . purge_accounts ( engine , mes . from_user . id )
d b. delete . delete_master_pass ( engine , mes . from_user . id )
await send_tmp_message (
bot ,
mes . chat . id ,
@ -93,7 +94,7 @@ async def set_master_password(
mes : Message ,
) - > None :
await base_handler ( bot , mes , None )
if d ata base . get . get_master_pass ( engine , mes . from_user . id ) is not None :
if d b. get . get_master_pass ( engine , mes . from_user . id ) is not None :
return await send_tmp_message (
bot ,
mes . chat . id ,
@ -118,7 +119,7 @@ async def _set_master_pass2(
mes . from_user . id ,
text ,
)
d ata base . add . add_master_pass ( engine , master_password )
d b. add . add_master_pass ( engine , master_password )
await send_tmp_message ( bot , mes . chat . id , " Успех " )
del mes , text
@ -132,7 +133,7 @@ async def reset_master_pass(
) - > None :
await base_handler ( bot , mes )
if d ata base . get . get_master_pass ( engine , mes . from_user . id ) is None :
if d b. get . get_master_pass ( engine , mes . from_user . id ) is None :
return await send_tmp_message (
bot ,
mes . chat . id ,
@ -163,8 +164,8 @@ async def _reset_master_pass2(
mes . from_user . id ,
text ,
)
d ata base . delete . purge_accounts ( engine , mes . from_user . id )
d ata base . change . change_master_pass ( engine , master_password )
d b. delete . purge_accounts ( engine , mes . from_user . id )
d b. change . change_master_pass ( engine , master_password )
await send_tmp_message (
bot , mes . chat . id , " В с е ваши аккаунты удалены, а мастер пароль изменён"
@ -176,7 +177,7 @@ async def _reset_master_pass2(
async def add_account ( bot : AsyncTeleBot , engine : Engine , mes : Message ) - > None :
await base_handler ( bot , mes )
master_password_from_db = d ata base . get . get_master_pass (
master_password_from_db = d b. get . get_master_pass (
engine ,
mes . from_user . id ,
)
@ -208,7 +209,7 @@ async def _add_account2(
mes . chat . id ,
" Н е корректное название аккаунта" ,
)
if text in d ata base . get . get_account s( engine , mes . from_user . id ) :
if text in d b. get . get_account _name s( engine , mes . from_user . id ) :
return await send_tmp_message (
bot , mes . chat . id , " Аккаунт с таким именем уже существует "
)
@ -289,7 +290,7 @@ async def _add_account5(
if text == " /cancel " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
master_password = d ata base . get . get_master_pass ( engine , mes . from_user . id )
master_password = d b. get . get_master_pass ( engine , mes . from_user . id )
if not encryption . master_pass . check_master_pass ( text , master_password ) :
return await send_tmp_message (
bot ,
@ -305,12 +306,9 @@ async def _add_account5(
password = data [ " passwd " ] ,
)
encrypted_account = encryption . other_accounts . encrypt (
account ,
text ,
)
encrypted_account = encryption . accounts . encrypt ( account , text )
result = d ata base . add . add_account (
result = db . add . add_account (
engine ,
encrypted_account ,
)
@ -329,11 +327,11 @@ async def _add_account5(
async def get_account ( bot : AsyncTeleBot , engine : Engine , mes : Message ) - > None :
await base_handler ( bot , mes )
master_pass = d ata base . get . get_master_pass ( engine , mes . from_user . id )
master_pass = d b. get . get_master_pass ( engine , mes . from_user . id )
if master_pass is None :
return await send_tmp_message ( bot , mes . chat . id , " Нет мастер пароля " )
accounts = d ata base . get . get_account s(
accounts = d b. get . get_account _name s(
engine ,
mes . from_user . id ,
to_sort = True ,
@ -356,7 +354,7 @@ async def _get_account2(
if text == " /cancel " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
if text not in d ata base . get . get_account s( engine , mes . from_user . id ) :
if text not in d b. get . get_account _name s( engine , mes . from_user . id ) :
return await send_tmp_message ( bot , mes . chat . id , " Нет такого аккаунта " )
bot_mes = await bot . send_message ( mes . chat . id , " Отправьте мастер пароль " )
@ -378,7 +376,7 @@ async def _get_account3(
if text == " /cancel " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
master_password = d ata base . get . get_master_pass (
master_password = d b. get . get_master_pass (
engine ,
mes . from_user . id ,
)
@ -390,17 +388,14 @@ async def _get_account3(
" Н е подходит мастер пароль" ,
)
account = database . get . get_account_info ( engine , mes . from_user . id , name )
account = encryption . other_accounts . decrypt (
account ,
text ,
)
account = db . get . get_account_info ( engine , mes . from_user . id , name )
account = encryption . accounts . decrypt ( account , text )
await send_deleteable_message (
bot ,
mes . chat . id ,
f " Название: \n { escape ( account . name ) } \n "
f " Название: \n ` { account . name } ` \n "
f " Логин: \n ` { account . login } ` \n Пароль: \n ` { account . password } ` \n Нажмите "
" на логин или пароль, чтобы скопировать" ,
" на название, логин или пароль, чтобы скопировать" ,
)
del text , mes
@ -414,11 +409,11 @@ async def delete_account(
) - > None :
await base_handler ( bot , mes )
master_pass = d ata base . get . get_master_pass ( engine , mes . from_user . id )
master_pass = d b. get . get_master_pass ( engine , mes . from_user . id )
if master_pass is None :
return await send_tmp_message ( bot , mes . chat . id , " Нет мастер пароля " )
accounts = d ata base . get . get_account s(
accounts = d b. get . get_account _name s(
engine ,
mes . from_user . id ,
to_sort = True ,
@ -447,7 +442,7 @@ async def _delete_account2(
if text == " /cancel " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
if text not in d ata base . get . get_account s( engine , mes . from_user . id ) :
if text not in d b. get . get_account _name s( engine , mes . from_user . id ) :
return await send_tmp_message ( bot , mes . chat . id , " Нет такого аккаунта " )
bot_mes = await bot . send_message (
@ -474,7 +469,7 @@ async def _delete_account3(
if text != " YES " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
d ata base . delete . delete_account ( engine , mes . from_user . id , account_name )
d b. delete . delete_account ( engine , mes . from_user . id , account_name )
await send_tmp_message ( bot , mes . chat . id , " Аккаунт удалён " )
@ -498,7 +493,7 @@ async def help_command(bot: AsyncTeleBot, mes: Message) -> None:
async def export ( bot : AsyncTeleBot , engine : Engine , mes : Message ) - > None :
await base_handler ( bot , mes )
master_password_from_db = d ata base . get . get_master_pass (
master_password_from_db = d b. get . get_master_pass (
engine ,
mes . from_user . id ,
)
@ -506,7 +501,7 @@ async def export(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None:
if master_password_from_db is None :
return await send_tmp_message ( bot , mes . chat . id , " Нет мастер пароля " )
if not d ata base . get . get_account s( engine , mes . from_user . id ) :
if not d b. get . get_account _name s( engine , mes . from_user . id ) :
return await send_tmp_message ( bot , mes . chat . id , " Нет аккаунтов " )
bot_mes = await bot . send_message ( mes . chat . id , " Отправьте мастер пароль " )
@ -522,7 +517,7 @@ async def _export2(
if text == " /cancel " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
master_password = d ata base . get . get_master_pass (
master_password = d b. get . get_master_pass (
engine ,
mes . from_user . id ,
)
@ -533,8 +528,19 @@ async def _export2(
" Н е подходит мастер пароль" ,
)
accounts = database . get . get_all_accounts ( engine , mes . from_user . id )
accounts = encryption . other_accounts . decrypt_multiple ( accounts , text )
accounts = db . get . get_accounts ( engine , mes . from_user . id )
with ProcessPoolExecutor ( ) as pool :
loop = asyncio . get_running_loop ( )
tasks = [ ]
for account in accounts :
function = functools . partial (
encryption . accounts . decrypt ,
account ,
text ,
)
tasks . append ( loop . run_in_executor ( pool , function ) )
accounts = await asyncio . gather ( * tasks )
accounts . sort ( key = lambda account : account . name )
json_io = accounts_to_json ( accounts )
await bot . send_document (
mes . chat . id ,
@ -552,7 +558,7 @@ async def import_accounts(
mes : Message ,
) - > None :
await base_handler ( bot , mes )
master_password_from_db = d ata base . get . get_master_pass (
master_password_from_db = d b. get . get_master_pass (
engine ,
mes . from_user . id ,
)
@ -620,7 +626,7 @@ async def _import3(
if text == " /cancel " :
return await send_tmp_message ( bot , mes . chat . id , " Успешная отмена " )
master_password = d ata base . get . get_master_pass (
master_password = d b. get . get_master_pass (
engine ,
mes . from_user . id ,
)
@ -638,11 +644,8 @@ async def _import3(
if not check_account ( account ) :
failed . append ( account . name )
continue
account = encryption . other_accounts . encrypt (
account ,
text ,
)
result = database . add . add_account ( engine , account )
account = encryption . accounts . encrypt ( account , text )
result = db . add . add_account ( engine , account )
if not result :
failed . append ( account . name )