It's a Linux solution, but it can easily be ported to Windows using
AutoHotkey.
My aim was to create a hotkey which allows me to look up LDAP mail
addresses from the adress field. Binding the hokey was no problem, I bound
Ctrl+L (l for LDAP) into the global Application context which called an
external script with Execute Command.
Next, I needed to pass the typed in substring to the script somehow. I did
it by putting the substring to the clipboard and the script can fetch it
from there. Putting the string to the clipboard was done by sending the
key sequence Ctrl+Shift+Left to Opera using the xautomation package.
The script fetches the string, looks it up in LDAP and shows a listbox
with the results. The user can select an item with Enter or dismiss the
dialog with Esc. The selected entry then sent back to Opera using
xautomation.
It works pretty good, the only problem left to solve is that accented
characters are
problematic to sent back properly, so they should be replaced with the
appropriate X
keysyms. The perfect solution would be putting the completed address to
the clipboard and paste it to the address field, but I couldn't do it with
Qt yet (it supports clipboard manipulation, but I could not paste the
string to Opera).
For Enlightenment a few extra lines are added which restore the focus to
the Opera window when the dialog is closed. These parts can be removed
for other window managers.
Enough talking. Here are the scripts. ldap.sh is invoked by Opera and it
is responsible for sending the simulated keycodes. ldapquery.py is invoked
by ldap.sh. It does the LDAP lookup and the GUI stuff. It can be run
separately while one tunes the script for his LDAP server.
Enjoy.
--
ldap.sh:
#!/bin/bash
# Enlightenment specific section
# save window id of current window
title=`eesh -ewait "win_op current title ?" | sed "s/^window title: *//"`
window_id=`eesh -ewait "window_list" | egrep "^ +[0-9a-f]+ +: +$title$" |
head - | sed "s/
^ *\([0-9a-f]*\) *:.*/\1/"`
# end of Enlightenment specific section
xte "keydown Control_L"
xte "keydown Shift_L"
xte "key Left"
xte "keyup Control_L"
xte "keyup Shift_L"
basedir=`dirname $0`
result=`$basedir/ldapquery.py`
# Enlightenment specific section
# restore focus of Opera window
eesh -e "win_op $window_id focus"
# end of Enlightenment specific section
if [ ! "$result" ]; then
exit
fi
# for some reason sending the result string works only character by
character
length=${#result}
index=0
while [ $index -lt $length ]; do
xte "key ${result:$index:1}"
index=$(($index + 1))
# a bit of sleep is needed between characters, otherwise
# the characters will mix up sometimes at those names
# for which M2 offers completions
python -c "import time; time.sleep(0.001)"done
--
ldapquery.sh:
#! /usr/bin/python
import sys
from qt import *
import ldap
# fetch the selection from the clipboard
app = QApplication(sys.argv)
selection = app.clipboard().text(QClipboard.Selection)
if not selection:
sys.exit(0)
# lookup LDAP macthes
l = ldap.initialize("ldap://<your LDAP server>")
l.simple_bind_s("","")
try:
results = l.search_s("o=<your organization>",
ldap.SCOPE_SUBTREE,
"(&(objectclass=*)(sn=" + str(selection) + "*))",
["cn", "mail"])
except ldap.ADMINLIMIT_EXCEEDED:
QMessageBox.warning(None, "LDAP search result",
"There are too many matches for search string: "
+ str(selection) +
"\nAdd some more characters to the search.",
QMessageBox.Ok)
sys.exit(0)
if not results:
QMessageBox.warning(None, "LDAP search result",
"There are no matches for search string: "
+ str(selection),
QMessageBox.Ok)
sys.exit(0)
# show the selection dialog
class LDAPDialog(QDialog):
def accept(self):
print listbox.currentText().ascii()
QDialog.accept(self)
dialog = LDAPDialog()
dialog.resize(400, 300)
layout = QHBoxLayout(dialog)
layout.setAutoAdd(1)
listbox = QListBox(dialog)
for result in results:
listbox.insertItem(QString.fromUtf8(result[1]["cn"][0]).append(" <")
.append(result[1]["mail"][0]).append(">"))
listbox.sort()
listbox.setSelectionMode(QListBox.Single)
listbox.setSelected(0, 1)
QObject.connect(listbox, SIGNAL("selected(int)"), dialog, SLOT("accept()"))
app.setMainWidget(dialog)
dialog.exec_loop()
--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/