forked from seveas/hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutt-keyring
executable file
·50 lines (45 loc) · 1.84 KB
/
mutt-keyring
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
#!/usr/bin/python
#
# 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 gnomekeyring as gk
import glib
import sys
import urllib
import urlparse
from ConfigParser import ConfigParser
import os
KEYRING_NAME = 'login'
def parse_url(url):
url = urlparse.urlsplit(url)
scheme = 'imap' if url.scheme.startswith('imap') else 'smtp'
username = url.username
if ';' in username:
username = username[:username.find(';')]
if '@' in username:
username = urllib.quote(username)
return (scheme, username, url.hostname)
urls = [parse_url(x) for x in sys.argv[1:]]
glib.set_application_name('mutt')
for id in gk.list_item_ids_sync(KEYRING_NAME):
item = gk.item_get_info_sync(KEYRING_NAME, id)
name = item.get_display_name()
url = None
if name.startswith(('imap','smtp')):
url = parse_url(name)
elif name.startswith('Evolution Data Source'):
src = name.rsplit(None,1)[1]
cp = ConfigParser()
cp.read(os.path.join(os.path.expanduser('~'), '.config', 'evolution', 'sources', src + '.source'))
if cp.has_section('Authentication') and cp.has_section('Mail Account'):
url = ('imap', cp.get('Authentication', 'User'), cp.get('Authentication', 'Host'))
elif cp.has_section('Authentication') and cp.has_section('Mail Transport'):
url = ('smtp', cp.get('Authentication', 'User'), cp.get('Authentication', 'Host'))
elif cp.has_section('Authentication') and cp.has_section('Collection'):
url = ('imap', urllib.quote(cp.get('Collection', 'Identity')), 'imap.gmail.com')
if url in urls:
print "set %s_pass=%s" % (url[0], item.get_secret())