Schurger.org

10 mars 2010

Filed under: Code,Gnome,Python — Jean Schurger @ 1:59

Tired of typing your #freenode password when ERC is connecting ? You don't want to write you passwords as clear text in your .emacs files ?

Lets see how to store, passwords in the Gnome Keyring, and access it from Emacs. It only needs Pymacs and gnome-keyring python bindings.

  • Write a little module to read and write passwords. This module will be loaded my pymacs and its functions will be available from emacs-lisp. You may name your module 'gnome-keyring.py' and store it somewhere like '~/.emacs.d/pymacs/'
def get_password(name):
    import gnomekeyring as gk
    try:
        items = gk.find_items_sync(gk.ITEM_GENERIC_SECRET,
                                   dict(variable_name=name))
    except gk.NoMatchError:
        return None
    return items[0].secret
get_password.interaction = ""

def set_password(name, password):
    import gnomekeyring as gk
    gk.item_create_sync(gk.get_default_keyring_sync(),
                        gk.ITEM_GENERIC_SECRET,
                        "Emacs password", dict(variable_name=name),
                        password, True),
set_password.interaction = ""

  • Load the module using pymacs
(require 'pymacs)
(add-to-list '
pymacs-load-path "~/.emacs.d/pymacs/")
(pymacs-load "gnome-keyring" "gnome-keyring-")

  • Trying from the interactive emacs lisp mode (M-x ielm)
ELISP> (gnome-keyring-set-password "my-password" "p4$$w0rD")
nil
ELISP> (gnome-keyring-get-password "my-password")
"p4$$w0rD"

  • And now, an example for ERC
(setq erc-password (gnome-keyring-get-password "my-password"))
(setq erc-prompt-for-password nil)

Too easy ! :D

Powered by WordPress