Name: correlation/client 
1:
#!/usr/bin/ruby
2:
# encoding: UTF-8
3:
require 'rubygems'
4:
require 'highline'
5:
require 'riddl/client'
6:
require 'timeout'
7:
require 'json'
8:
require 'pp'
9:
require 'term/ansicolor'
10:
class String
11:
  include Term::ANSIColor
12:
end
13:
 
14:
H_exit = <<-end #{{{
15:
 
16:
  Get me out of this mess.
17:
end
18:
#}}}
19:
H_clear = <<-end #{{{
20:
 
21:
  Clear screen.
22:
end
23:
#}}}
24:
 
25:
def get_from_server(name,params=[])
26:
  gate = Riddl::Client.interface("xmpp://[email protected]", "correlation.xml", :jid => 'jü[email protected]', :pass => 'mangler')
27:
  begin
28:
    Timeout::timeout(5) do
29:
      para = [
30:
        Riddl::Parameter::Simple.new("command",name)
31:
      ] + params.map { |p| Riddl::Parameter::Simple.new("parameter",p) }
32:
      status, res = gate.put para
33:
      if status == 200
34:
        return JSON.parse(res[0].value.read)
35:
      else
36:
        raise "Server responding with status #{status}"
37:
      end
38:
    end  
39:
  rescue Timeout::Error
40:
    raise "Server has some bad error."
41:
  end  
42:
  gate.close_xmpp
43:
end
44:
 
45:
def send_to_server(name,params)
46:
  begin
47:
    px = get_from_server(name,params)
48:
    type = px.map{|e| e.class}.uniq
49:
    if type.length == 1 && type[0] == Hash
50:
      keys = {}
51:
      px.map{|e| e.keys.each {|k| keys[k] ||= k.to_s.length; keys[k] = e[k].to_s.length if keys[k] < e[k].to_s.length } }.flatten
52:
      keys.each do |k,v|
53:
        print k.ljust(v) + ' | '
54:
      end
55:
      puts if keys.length > 0
56:
      px.each do |e|
57:
        keys.each do |k,v|
58:
          print e[k].to_s.ljust(v) + ' | '
59:
        end
60:
        puts
61:
      end    
62:
    else  
63:
      px.each do |e|
64:
        if e.is_a? String
65:
          puts e
66:
        else  
67:
          pp e
68:
        end  
69:
      end  
70:
    end
71:
  rescue => e
72:
    e.message
73:
  end  
74:
end
75:
 
76:
io = HighLine.new
77:
ICMDS = %w{clear quit exit}
78:
ECMDS = get_from_server('commands')
79:
loop do #{{{
80:
  begin
81:
    cmd = io.ask("<%= color('correlation$ ',BOLD) %>") do |h|
82:
      h.whitespace = :strip_and_collapse
83:
      h.validate = nil
84:
      h.readline = true
85:
      h.completion = ICMDS + ECMDS
86:
    end
87:
    cmd = cmd.scan(/([^"\s]+)|"((?:""|[^"])+)"/).flatten.compact
88:
    case cmd[0]
89:
      when 'help'
90:
        send_to_server(cmd[0],cmd[1..-1])
91:
        rest = ICMDS.flatten.map do |m|
92:
          h = self.class.const_defined?("H_#{m}".to_sym) ? self.class.const_get("H_#{m}".to_sym) : "\n"
93:
          "#{m.to_s.red.bold} #{h}"
94:
        end.join
95:
        print rest
96:
      when 'quit', 'exit'
97:
        break
98:
      when 'clear'  
99:
        print "\e[H\e[2J"
100:
      when nil
101:
      else
102:
        send_to_server(cmd[0],cmd[1..-1])
103:
    end
104:
  rescue EOFError, SignalException
105:
    puts
106:
    break
107:
  end  
108:
end #}}}