diff options
author | Robbie Harwood <rharwood@redhat.com> | 2020-03-10 11:06:52 -0400 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2020-03-19 13:22:59 -0700 |
commit | 1011755b266f69f1fc90d2447737133cbba754de (patch) | |
tree | d77d936243361cc75382f24fe0c179b0c7e580ab /imap-dl | |
parent | 6e26cfb34ae96f2dbef3660afd1857b31bd6ea5c (diff) | |
download | mailscripts-1011755b266f69f1fc90d2447737133cbba754de.tar.gz |
imap-dl: Factor password auth into separate function
Signed-off-by: Robbie Harwood <rharwood@redhat.com>
Acked-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Diffstat (limited to 'imap-dl')
-rwxr-xr-x | imap-dl | 14 |
1 files changed, 9 insertions, 5 deletions
@@ -80,6 +80,14 @@ summary_splitter = Splitter('summary', _summary_re) _fetch_re = rb'^(?P<id>[0-9]+) \(UID (?P<uid>[0-9]+) (FLAGS \([\\A-Za-z ]*\) )?BODY\[\] \{(?P<size>[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)) |