dotfiles/mail/.offlineimap.py

40 lines
1.3 KiB
Python

# GPG password extraction adapted from stackexchange
# https://unix.stackexchange.com/questions/44214
import os, subprocess, re
def decrypt(path):
args = ["gpg", "--use-agent", "--quiet", "--batch", "-d", path]
try:
return subprocess.check_output(args).strip()
except BaseException as e:
print("[ERROR] %s" % e)
return ""
# Extract account information stored in key/value paris
def get_account(acct, key):
acct = os.path.basename(acct)
path = os.path.expanduser("~/.passwd/%s.gpg" % str(acct))
o = decrypt(path).splitlines()
values = {k.strip():v.strip() for k, v in (l.split('=') for l in o)}
return values[key]
default_folders = ['INBOX', 'Archive', 'Sent', 'Trash']
def folder_filter(name):
# Mail provider has a folder with my mail folders called `Folders`.
# Anything inside `Folders` should be synced: match the first
# part of the path to allow them, otherwise only sync the inbox
# and other default folders.
return name[:7] == "Folders" or name in default_folders
def folder_rename(name):
# Nested folders have `Folders.` as a prefix to indicate nesting.
# Remove the prefix so names match, unless doing so would clash
# with the default folders
stripped = re.sub('^Folders.', '', name);
if stripped not in default_folders:
return stripped
else:
return name