Ruby Exchange lookup script for Mutt

Page content

As i use mutt at work i had no way to lookup email address for other people. Well now i have with some nasty hacky ruby. I may clean it up etc when i have time but for now it works.

Simply add this to your mutt config

set query_command = "ruby -W0 {location of below script} '%s'"

The -W0 is needed as there is a bug in the curb gem that i am using.

require "rubygems"
require "uri"
require "curb"
require 'yaml'
require "rexml/document"
c = Curl::Easy.new("https://yourexchangeurl/ews/Exchange.asmx")
c.http_auth_types = :ntlm
c.username = 'yourusername'
c.password = 'yourpassword'
resolve_names_xml = "<?xml version='1.0' encoding='utf-8'?>
        <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns:xsd='http://www.w3.org/2001/XMLSchema'
        xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
        xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types'>
        <soap:Body>
        <ResolveNames xmlns='http://schemas.microsoft.com/exchange/services/2006/messages\'
        xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types\'
        ReturnFullContactData='true'>
        <UnresolvedEntry>#{ARGV[0]}</UnresolvedEntry>
        </ResolveNames>
        </soap:Body>
        </soap:Envelope>"

c.post_body=resolve_names_xml
c.headers = "Content-Type:text/xml"
c.perform

doc = REXML::Document.new(c.body_str)
displayname = ""
email = ""
puts ""
doc.root.each_recursive do |elem|
        if elem.name == "DisplayName"
                displayname = elem.text.to_s
        end
        if elem.name == "EmailAddress"
                email = elem.text.to_s
        end

        if displayname != "" and email != ""
                puts email + "  " + displayname + "     "
                email = ""
                displayname = ""
        end
end