add reading password from file

Signed-off-by: Florian Brandes <florian.brandes@posteo.de>
This commit is contained in:
2024-07-11 18:53:48 +02:00
parent 14c87f03ce
commit ffeb686fe0
3 changed files with 42 additions and 5 deletions

View File

@@ -107,6 +107,22 @@ class Config:
email_certs[email] = cert
return email_certs
@classmethod
def _read_from_file(cls, file: Union[Path, str]) -> str:
"""Read string from file"""
if len(file) == 0:
# empty string (which is our fallback), so return empty string
return ""
# file is non-empty and should provide a path
file = Path(file)
if not file.exists():
raise OSError("File wasn't found: " + str(file))
with open(file, "r", encoding="utf8") as fp:
line = fp.readline().strip("\n")
if len(line) == 0:
raise OSError("Empty file supplied: " + str(file))
return line
@classmethod
def _from_config(cls, config: configparser.RawConfigParser) -> "Config":
email_certs = cls._get_emails_and_certs(config.items("emails"))
@@ -120,7 +136,9 @@ class Config:
port=config.getint("client", "port"),
sender=config.get("client", "sender"),
username=config.get("client", "username"),
password=config.get("client", "password"),
password=cls._read_from_file(
(config.get("client", "password_file", fallback=""))
),
set_reply_to=config.getboolean(
"client", "set_reply_to", fallback=False
),