dotfiles/bin/exchangebook
2015-04-21 09:49:36 -04:00

57 lines
1.7 KiB
Bash
Executable File

#!/bin/sh
# Crappy shell script to parse WPI's address book. Designed for mutt.
# Uses LDAP over TLS to query information.
#
# To use in mutt, add this to your .muttrc
# set query_command="/path/to/this/script '%s'"
#
# To combine with goobook, you can just do this:
# set query_command="goobook query '%s'; /path/to/this/script '%s'"
# pass all arguments to LDAP search
query=$@
# authentication
username=ianaval
password=$(pass WPI_UNIX)
# sketchy, but we don't require verification of TLS certs
# at least we're not transmitting plaintext passwords
export LDAPTLS_REQCERT=never
# Use WPI's PAM binddn to get the user's binddn
binddn=$(ldapsearch -ZZ -LLL -x -b dc=wpi,dc=edu -H ldap://ldapv2back.wpi.edu/ -D cn=pam,ou=access,dc=wpi,dc=edu -w $(pass ldapv2back) "(|(uid=$username))" dn | awk '{print $2}' 2>/dev/null)
# Makes names look like "Naval, Ian A."
parse_name() {
case $# in
2) echo "$2, $1" ;;
3) echo "$3, $1 ${2:0:1}." ;;
*) echo "$@" ;;
esac
}
# loop line by line
OIFS="${IFS}"
NIFS=$'\n'
IFS="${NIFS}"
for line in $(ldapsearch -ZZ -LLL -x -H ldap://ldapv2back.wpi.edu/ -D $binddn -w $password -b "dc=wpi,dc=edu" "(|(uid=$query)(cn=*$query*)(displayName=*$query*))" displayName mail 2>/dev/null); do
IFS="${OIFS}"
# parse displayName (might not be in the right order)
if [[ $line =~ displayName* ]]; then
displayName=$(parse_name $(echo "$line" | sed -e 's/displayName: //'))
fi
# parse email
if [[ $line =~ mail* ]]; then
mail=$(echo "$line" | sed -e 's/mail: //')
fi
# print address book record with the group being 'none'
if [[ -n $displayName ]] && [[ -n $mail ]]; then
printf "$mail\t$displayName\tnone\n"
displayName=""
mail=""
fi
IFS="${NIFS}"
done
IFS="${OIFS}"