diff options
author | Jannis M. Hoffmann <jannis@fehcom.de> | 2024-12-09 17:58:17 +0100 |
---|---|---|
committer | Jannis M. Hoffmann <jannis@fehcom.de> | 2024-12-09 17:58:17 +0100 |
commit | 55688b969a645fbc6d94c76f51da3be976c1d098 (patch) | |
tree | b0b72173c041fa1253fba2e3a0b55ce65d0c8006 /src/jwebmail/model/read_mails.py | |
parent | 44b719671fe73b7378789968ecc8d48d7f9c00ca (diff) |
make QMailAuthuser a context manager
Diffstat (limited to 'src/jwebmail/model/read_mails.py')
-rw-r--r-- | src/jwebmail/model/read_mails.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/jwebmail/model/read_mails.py b/src/jwebmail/model/read_mails.py index fc89c8e..534f2f7 100644 --- a/src/jwebmail/model/read_mails.py +++ b/src/jwebmail/model/read_mails.py @@ -12,11 +12,7 @@ class QMAuthError(Exception): class QMailAuthuser: - def __init__( - self, username, password, prog, mailbox_path, virtual_user, authenticator - ): - self._username = username - self._password = password + def __init__(self, prog, mailbox_path, virtual_user, authenticator): self._prog = prog self._mailbox_path = mailbox_path self._virtual_user = virtual_user @@ -179,7 +175,7 @@ class QMailAuthuser: else: assert False - def open(self): + def open(self, username, password): (rp, wp) = os.pipe() (sp, sc) = socketpair() cmdline = [self._authenticator, self._prog] @@ -196,7 +192,7 @@ class QMailAuthuser: assert False sc.close() os.close(rp) - os.write(wp, f"{self._username}\0{self._password}\0\0".encode()) + os.write(wp, f"{username}\0{password}\0\0".encode()) os.close(wp) self._pid = pid @@ -214,11 +210,12 @@ class QMailAuthuser: else: raise - user = self._username[: self._username.index("@")] + user = username[: username.index("@")] self._connection.Init( unix_user=self._virtual_user, mailbox_path=os.path.join(self._mailbox_path, user), ) + return self def close(self): @@ -229,3 +226,15 @@ class QMailAuthuser: rc = os.waitstatus_to_exitcode(status) if rc != 0: raise QMAuthError(rc) + + def __enter__(self): + return self + + def __exit__(self, ex_type, ex_val, ex_tb): + if ex_val is None: + self.close() + elif issubclass(ex_type, BrokenPipeError): + (pid, _status) = os.waitpid(self._pid, 0) + assert pid == self._pid + + return False |