From 0bdca607a1dd48db00e9cd2cc482f03d76e8c16d Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 20 Jan 2013 13:26:59 -0500 Subject: [PATCH] mutt-ldap.py: Pulled out into its own repository --- posts/LDAP.mdwn | 6 +-- posts/LDAP/mutt-ldap.py | 116 ---------------------------------------- posts/mutt-ldap.mdwn | 8 +++ 3 files changed, 10 insertions(+), 120 deletions(-) delete mode 100755 posts/LDAP/mutt-ldap.py create mode 100644 posts/mutt-ldap.mdwn diff --git a/posts/LDAP.mdwn b/posts/LDAP.mdwn index b423f50..52f8a35 100644 --- a/posts/LDAP.mdwn +++ b/posts/LDAP.mdwn @@ -242,10 +242,8 @@ Mutt If you use the [[Mutt]] email client (or just want a simple way to query email addresses from the command line) there are a [number of scripts][mutts] available. Pick whichever sounds most appealing to -you. I wrote up [[mutt-ldap.py]], which lets you configuration the -connection details via a config file (`~/.mutt-ldap.rc`) rather than -editing the script itself. Usage details are available in the -docstring. +you. I wrote up [[mutt-ldap.py|mutt-ldap]], which has since seen +contributions from others and been pulled out into its own repository. Apple Address Book ------------------ diff --git a/posts/LDAP/mutt-ldap.py b/posts/LDAP/mutt-ldap.py deleted file mode 100755 index d75f51b..0000000 --- a/posts/LDAP/mutt-ldap.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env python2 -# -# Copyright (C) 2008-2012 W. Trevor King -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -"""LDAP address searches for Mutt. - -Add :file:`mutt-ldap.py` to your ``PATH`` and add the following line -to your :file:`.muttrc`:: - - set query_command = "mutt-ldap.py '%s'" - -Search for addresses with `^t`, optionally after typing part of the -name. Configure your connection by creating :file:`~/.mutt-ldap.py` -contaning something like:: - - [connection] - server = myserver.example.net - basedn = ou=people,dc=example,dc=net - -See the `CONFIG` options for other available settings. -""" - -import email.utils -import itertools -import os.path -import ConfigParser - -import ldap -import ldap.sasl - - -CONFIG = ConfigParser.SafeConfigParser() -CONFIG.add_section('connection') -CONFIG.set('connection', 'server', 'domaincontroller.yourdomain.com') -CONFIG.set('connection', 'port', '389') # set to 636 for default over SSL -CONFIG.set('connection', 'ssl', 'no') -CONFIG.set('connection', 'starttls', 'no') -CONFIG.set('connection', 'basedn', 'ou=x co.,dc=example,dc=net') -CONFIG.add_section('auth') -CONFIG.set('auth', 'user', '') -CONFIG.set('auth', 'password', '') -CONFIG.set('auth', 'gssapi', 'no') -CONFIG.read(os.path.expanduser('~/.mutt-ldap.rc')) - -def connect(): - protocol = 'ldap' - if CONFIG.getboolean('connection', 'ssl'): - protocol = 'ldaps' - url = '{}://{}:{}'.format( - protocol, - CONFIG.get('connection', 'server'), - CONFIG.get('connection', 'port')) - connection = ldap.initialize(url) - if CONFIG.getboolean('connection', 'starttls') and protocol == 'ldap': - connection.start_tls_s() - if CONFIG.getboolean('auth', 'gssapi'): - sasl = ldap.sasl.gssapi() - connection.sasl_interactive_bind_s('', sasl) - else: - connection.bind( - CONFIG.get('auth', 'user'), - CONFIG.get('auth', 'password'), - ldap.AUTH_SIMPLE) - return connection - -def search(query, connection=None): - local_connection = False - try: - if not connection: - local_connection = True - connection = connect() - post = '' - if query: - post = '*' - filterstr = u'(|{})'.format( - u' '.join([u'({}=*{}{})'.format(field, query, post) - for field in ['cn', 'displayName', 'uid', 'mail']])) - r = connection.search_s( - CONFIG.get('connection', 'basedn'), - ldap.SCOPE_SUBTREE, - filterstr.encode('utf-8')) - finally: - if local_connection and connection: - connection.unbind() - return r - -def format_entry(entry): - cn,data = entry - if 'mail' in data: - name = data.get('displayName', data['cn'])[-1] - for m in data['mail']: - yield email.utils.formataddr((name, m)) - - -if __name__ == '__main__': - import sys - - query = unicode(' '.join(sys.argv[1:]), 'utf-8') - entries = search(query) - addresses = list(itertools.chain( - *[format_entry(e) for e in sorted(entries)])) - print('{} addresses found:'.format(len(addresses))) - print('\n'.join(addresses)) diff --git a/posts/mutt-ldap.mdwn b/posts/mutt-ldap.mdwn new file mode 100644 index 0000000..1c6ba09 --- /dev/null +++ b/posts/mutt-ldap.mdwn @@ -0,0 +1,8 @@ +[[!template id=gitrepo repo=mutt-ldap]] + +I wrote this [[Python]] script to query an [[LDAP]] server for +addresses from [[Mutt]]. Anything interesting enough for others to +hack on deserves it's own repository, so I pulled it out of my blog +repository (linked above, and mirrored on [GitHub][]). + +[GitHub]: https://github.com/wking/mutt-ldap -- 2.26.2