pop refactor done?

This commit is contained in:
Balakrishnan Balasubramanian 2023-06-03 21:55:11 -04:00
parent 5cdf36fe32
commit 203c78b97b

View File

@ -85,6 +85,7 @@ async def auth_stage():
f"User: {username} already has an active session") f"User: {username} already has an active session")
raise AuthError("Already logged in") raise AuthError("Already logged in")
else: else:
config().loggedin_users.add(username)
write(ok("Login successful")) write(ok("Login successful"))
except AuthError as ae: except AuthError as ae:
write(err(f"Auth Failed: {ae}")) write(err(f"Auth Failed: {ae}"))
@ -206,40 +207,32 @@ def save_deleted_items(deleted_items_path: Path, deleted_items: Set):
f.writelines(f"{did}\n" for did in deleted_items) f.writelines(f"{did}\n" for did in deleted_items)
async def transaction_stage(existing_deleted_items: Set): async def transaction_stage():
deleted_items_path = config().mails_path / state().mbox / state().username
existing_deleted_items: set[str] = get_deleted_items(deleted_items_path)
mails_list = [ mails_list = [
entry for entry in get_mails_list(config().mails_path / 'new') entry
for entry in get_mails_list(config().mails_path / state().mbox / 'new')
if entry.uid not in existing_deleted_items if entry.uid not in existing_deleted_items
] ]
new_deleted_items: Set = await process_transactions(mails_list) new_deleted_items: Set = await process_transactions(mails_list)
return new_deleted_items logging.info(f"completed transactions. Deleted:{len(new_deleted_items)}")
if new_deleted_items:
save_deleted_items(deleted_items_path,
existing_deleted_items.union(new_deleted_items))
logging.info(f"Saved deleted items")
async def start_session(): async def start_session():
logging.info("New session started") logging.info("New session started")
try: try:
await auth_stage() await auth_stage()
assert username is not None assert state().username
config().loggedin_users.add(username) assert state().mbox
_, mbox = config().users[username] await transaction_stage()
deleted_items_path = config().mails_path / mbox / username logging.info(f"User:{state().username} done")
logging.info(f"User:{username} logged in successfully")
existing_deleted_items: Set = get_deleted_items(deleted_items_path)
new_deleted_items: Set = await transaction_stage(existing_deleted_items
)
logging.info(
f"{username=} completed transactions. Deleted:{len(new_deleted_items)}"
)
if new_deleted_items:
save_deleted_items(deleted_items_path,
existing_deleted_items.union(new_deleted_items))
logging.info(f"User:{username} Saved deleted items")
except ClientError as c: except ClientError as c:
write(err("Something went wrong")) write(err("Something went wrong"))
logging.error(f"Unexpected client error: {c}") logging.error(f"Unexpected client error: {c}")
@ -247,8 +240,8 @@ async def start_session():
logging.error(f"Serious client error: {e}") logging.error(f"Serious client error: {e}")
raise raise
finally: finally:
if username: if state().username:
config().loggedin_users.remove(username) config().loggedin_users.remove(state().username)
def parse_users(users: list[User]): def parse_users(users: list[User]):
@ -292,9 +285,9 @@ def state() -> State:
return c_state.get() return c_state.get()
def make_pop_server_callback(dirpath: Path, users: list[User], def make_pop_server_callback(mails_path: Path, users: list[User],
timeout_seconds: int): timeout_seconds: int):
config = Config(mails_path=dirpath, users=parse_users(users)) config = Config(mails_path=mails_path, users=parse_users(users))
async def session_cb(reader: StreamReader, writer: StreamWriter): async def session_cb(reader: StreamReader, writer: StreamWriter):
c_config.set(config) c_config.set(config)
@ -314,13 +307,13 @@ async def create_pop_server(host: str,
ssl_context: ssl.SSLContext = None, ssl_context: ssl.SSLContext = None,
timeout_seconds: int = 60): timeout_seconds: int = 60):
logging.info( logging.info(
f"Starting POP3 server {dirpath=}, {host=}, {port=}, {timeout_seconds=}, ssl={context != None}" f"Starting POP3 server {host=}, {port=}, {mails_path=}, {len(users)=}, {ssl_context != None=}, {timeout_seconds=}"
) )
return await asyncio.start_server(make_pop_server_callback( return await asyncio.start_server(make_pop_server_callback(
dirpath, users, timeout_seconds), mails_path, users, timeout_seconds),
host=host, host=host,
port=port, port=port,
ssl=context) ssl=ssl_context)
async def a_main(*args, **kwargs): async def a_main(*args, **kwargs):