Remove useless scripts
[dotfiles/scripts.git] / imap-passwords.py
1 #!/usr/bin/python
2
3 #import gtk
4 import gnomekeyring as gkey
5 import getpass
6
7 class Keyring(object):
8     def __init__(self, name, server, protocol):
9         self._name = name
10         self._server = server
11         self._protocol = protocol
12         self._keyring = gkey.get_default_keyring_sync()
13
14     def has_credentials(self):
15         try:
16             attrs = {"server": self._server, "protocol": self._protocol}
17             items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
18             return len(items) > 0
19         except gkey.DeniedError:
20             return False
21
22     def get_credentials(self):
23         attrs = {"server": self._server, "protocol": self._protocol}
24         items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs)
25         return (items[0].attributes["user"], items[0].secret)
26
27     def set_credentials(self, (user, pw)):
28         attrs = {
29                 "user": user,
30                 "server": self._server,
31                 "protocol": self._protocol,
32             }
33         gkey.item_create_sync(gkey.get_default_keyring_sync(),
34                 gkey.ITEM_NETWORK_PASSWORD, self._name, attrs, pw, True)
35
36 server = raw_input("Server: ")
37 username = raw_input("Username: ")
38 password = getpass.getpass(prompt="Password: ")
39 print
40
41 keyring = Keyring("offlineimap", server, "imap")
42
43 keyring.set_credentials((username, password))