Thatbytes

Mostly Interesting

Ruby Exchange Lookup Script for Mutt

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

1
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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

Comments