From 1011755b266f69f1fc90d2447737133cbba754de Mon Sep 17 00:00:00 2001 From: Robbie Harwood Date: Tue, 10 Mar 2020 11:06:52 -0400 Subject: imap-dl: Factor password auth into separate function Signed-off-by: Robbie Harwood Acked-by: Daniel Kahn Gillmor --- imap-dl | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'imap-dl') diff --git a/imap-dl b/imap-dl index a1c2329..f1259da 100755 --- a/imap-dl +++ b/imap-dl @@ -80,6 +80,14 @@ summary_splitter = Splitter('summary', _summary_re) _fetch_re = rb'^(?P[0-9]+) \(UID (?P[0-9]+) (FLAGS \([\\A-Za-z ]*\) )?BODY\[\] \{(?P[0-9]+)\}$' fetch_splitter = Splitter('fetch', _fetch_re) +def auth_builtin(username:str, imap:imaplib.IMAP4_SSL, + conf:configparser.ConfigParser, server:str) -> None: + logging.info('Logging in as %s', username) + resp:Tuple[str, List[Union[bytes,Tuple[bytes,bytes]]]] + resp = imap.login(username, conf.get('retriever', 'password')) + if resp[0] != 'OK': + raise Exception(f'login failed with {resp} as user {username} on {server}') + def scan_msgs(configfile:str, verbose:bool) -> None: conf = configparser.ConfigParser() conf.read_file(open(configfile, 'r')) @@ -125,11 +133,7 @@ def scan_msgs(configfile:str, verbose:bool) -> None: port=int(conf.get('retriever', 'port', fallback=993)), ssl_context=ctx) as imap: username:str = conf.get('retriever', 'username') - logging.info('Logging in as %s', username) - resp:Tuple[str, List[Union[bytes,Tuple[bytes,bytes]]]] - resp = imap.login(username, conf.get('retriever', 'password')) - if resp[0] != 'OK': - raise Exception(f'login failed with {resp} as user {username} on {server}') + auth_builtin(username, imap, conf, server) if verbose: # only enable debugging after login to avoid leaking credentials in the log imap.debug = 4 logging.info("capabilities reported: %s", ', '.join(imap.capabilities)) -- cgit v1.2.3