summaryrefslogtreecommitdiff
path: root/src/jwebmail/model/read_mails.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/jwebmail/model/read_mails.py')
-rw-r--r--src/jwebmail/model/read_mails.py25
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