-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmutt-keyring
More file actions
executable file
·62 lines (56 loc) · 2.08 KB
/
mutt-keyring
File metadata and controls
executable file
·62 lines (56 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python3
#
# Fetch passwords for imap/smtp accounts from gnome-keyring
# Adding passwords to gnome-keyring is up to yourself. Passwords added
# by evolution will be picked up, which may help.
#
# Usage in .muttrc:
# source "~/bin/mutt-keyring $folder $smtp_url|"
import configparser
import glob
import os
import secretstorage
import sys
import urllib.parse
def main():
urls = [parse_url(x) for x in sys.argv[1:]]
bus = secretstorage.dbus_init()
collection = secretstorage.get_default_collection(bus)
root = os.path.join(os.path.expanduser('~'), '.config', 'evolution', 'sources')
for source in glob.glob(os.path.join(root, '*.source')):
uid = os.path.splitext(os.path.basename(source))[0]
proto, user, host, parent = read_source(source)
if (proto, user, host) in urls:
secretd = {
'xdg:schema': 'org.gnome.Evolution.Data.Source',
'e-source-uid': uid,
}
secrets = list(collection.search_items(secretd))
if not secrets and parent:
secretd['e-source-uid'] = parent
secrets = list(collection.search_items(secretd))
if secrets:
print("set %s_pass='%s'" % (proto, secrets[0].get_secret().decode('utf-8')))
def read_source(file):
source = configparser.ConfigParser()
source.read(file)
user = host = proto = None
parent = source['Data Source']['parent']
if 'Authentication' in source:
user = source['Authentication']['user']
host = source['Authentication']['host']
for key in ('Mail Account', 'Mail Transport'):
if key in source:
proto = source[key]['backendname']
if proto == 'imapx':
proto = 'imap'
return (proto, user, host, parent)
def parse_url(url):
url = urllib.parse.urlsplit(url)
scheme = 'imap' if url.scheme.startswith('imap') else 'smtp'
username = url.username
if ';' in username:
username = username[:username.find(';')]
return (scheme, username, url.hostname)
if __name__ == '__main__':
main()