Class: Vines::Agent::Connection

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/vines/agent/connection.rb

Overview

Connects the agent process to the chat server and provides service discovery and command execution abilities. Users are authorized against an access control list before being allowed to run commands.

Constant Summary collapse

NS =
'http://getvines.com/protocol'.freeze
SYSTEMS =
'http://getvines.com/protocol/systems'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vines/agent/connection.rb', line 15

def initialize(options)
  domain, password, host, port, download, conf =
    *options.values_at(:domain, :password, :host, :port, :download, :conf)

  certs = File.expand_path('certs', conf)
  @permissions, @services, @sessions, @component = {}, {}, {}, nil
  @ready = false

  jid = Blather::JID.new(fqdn, domain, 'vines')
  @stream = Blather::Client.setup(jid, password, host, port, certs)

  @stream.register_handler(:stream_error) do |e|
    log.error(e.message)
    true # prevent EM.stop
  end

  @stream.register_handler(:disconnected) do
    log.info("Stream disconnected, reconnecting . . .")
    EM::Timer.new(rand(16) + 5) do
      self.class.new(options).start
    end
    true # prevent EM.stop
  end

  @stream.register_handler(:ready) do
    # prevent handler called twice
    unless @ready
      log.info("Connected #{@stream.jid} agent to #{host}:#{port}")
      log.warn("Agent must run as root user to allow user switching") unless root?
      @ready = true
      startup
    end
  end

  @stream.register_handler(:subscription, :request?) do |node|
    # ignore, rather than refuse, requests from users lacking
    # permissions, so agents are invisible to them
    @stream.write(node.approve!) if valid_user?(node.from)
  end

  @stream.register_handler(:file_transfer) do |iq|
    transfer_file(iq)
  end

  @stream.register_handler(:disco_info, :get?) do |node|
    disco_info(node)
  end

  @stream.register_handler(:iq, '/iq[@type="set"]/ns:query', :ns => SYSTEMS) do |node|
    update_permissions(node)
  end

  @stream.register_handler(:iq, '/iq[@type="get"]/ns:query', :ns => 'jabber:iq:version') do |node|
    version(node)
  end

  @stream.register_handler(:message, :chat?, :body) do |msg|
    process_message(msg)
  end
end

Instance Method Details

#startObject



76
77
78
# File 'lib/vines/agent/connection.rb', line 76

def start
  @stream.run
end