Skip to content

Instantly share code, notes, and snippets.

@ikaros
Created July 5, 2012 12:22
Show Gist options
  • Save ikaros/3053399 to your computer and use it in GitHub Desktop.
Save ikaros/3053399 to your computer and use it in GitHub Desktop.
LDAP Addressbook for mutt commandline email client
#!/usr/bin/env ruby
require "rubygems"
require "yaml"
require "net/ldap" or raise "Please install 'net-ldap'"
class LBook
DEFAULT_ATTRIBUTES = ['mail', 'displayName', 'description']
def initialize(args)
@base_dn = args['base_dn'] or raise ArgumentError.new("Need :base_dn")
@attributes = args['attributes'] || DEFAULT_ATTRIBUTES
@connection ||= Net::LDAP.new(
host: args['host'],
port: args['port'],
encryption: args['encryption'],
auth: { method: :simple,
username: args['username'] || username,
password: args['password'] || password }
)
end
def find_by_mail(mail)
find Net::LDAP::Filter.contains('mail', mail)
end
def find(filter)
@connection.search(base:@base_dn, filter:filter, attributes:@attributes)
end
end
unless ARGV.first
puts 'Please specify a search string'
exit 1
end
# # ~/.lbook.yml
# connection:
# host: 'ldap_srv'
# port: 389
# username: 'ldap_usr'
# password: 'ldap_pw'
# base_dn: 'DC=whatever,DC=local'
config = YAML.load_file File.expand_path('~/.lbook.yml')
lbook = LBook.new config['connection']
# Print for mutt
print "\n"
lbook.find_by_mail(ARGV.first).each do |e|
puts "#{ e.mail.first }\t#{ e.displayName.first }\t "
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment