Class: XMPPAgent

Inherits:
Object
  • Object
show all
Includes:
AppRoutes
Defined in:
lib/xmpp-agent.rb

Instance Method Summary collapse

Constructor Details

#initializeXMPPAgent

Returns a new instance of XMPPAgent.



11
12
13
# File 'lib/xmpp-agent.rb', line 11

def initialize()        
  @route = {}; @params = {}
end

Instance Method Details

#routes(params, messenger, msg) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/xmpp-agent.rb', line 26

def routes(params, messenger, msg)

  get %r{(send_to|send2)\s+([^\s]+)\s+(.*)} do
    user, message = params[:captures].values_at 1,2
    messenger.deliver(user, message)
  end

  get 'help' do
    messenger.deliver(msg.from, "available commands: help, send_to")
  end

  get '*' do
    messenger.deliver(msg.from, "need some help? type help")
  end

end

#run(user, password) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/xmpp-agent.rb', line 15

def run(user, password)

  begin
    xmpp_connect(user, password)
  rescue
    puts ($!).to_s
    sleep 5
    retry
  end
end

#xmpp_connect(user, password) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xmpp-agent.rb', line 43

def xmpp_connect(user, password)

  puts "connecting to jabber server.."  
  messenger = Jabber::Simple.new(user,password)  
  puts "connected."  

  while true
    messenger.received_messages do |msg|  
      routes(@params, messenger, msg)
      run_route msg.body.strip
    end  
    sleep 1
  end
end