Class: Cuttlebone::Drivers::XMPP

Inherits:
Base
  • Object
show all
Defined in:
lib/cuttlebone/drivers/xmpp.rb

Constant Summary collapse

@@jid =
'cuttlebone@localhost'
@@password =
'cuttlebone'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #sessions

Constructor Details

This class inherits a constructor from Cuttlebone::Drivers::Base

Class Method Details

.jid=(jid) ⇒ Object



9
10
11
# File 'lib/cuttlebone/drivers/xmpp.rb', line 9

def self.jid= jid
  @@jid = jid
end

.password=(password) ⇒ Object



13
14
15
# File 'lib/cuttlebone/drivers/xmpp.rb', line 13

def self.password= password
  @@password = password
end

Instance Method Details

#runObject



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
# File 'lib/cuttlebone/drivers/xmpp.rb', line 17

def run
  client = Jabber::Client.new(@@jid)
  client.connect()
  client.auth(@@password)
  client.send(Jabber::Presence.new.set_type(:available))

  roster = Jabber::Roster::Helper.new(client)

  client.add_message_callback do |message|
    unless message.composing? or message.body.nil?
      begin
        session = sessions[message.from]
      rescue Cuttlebone::Session::NotFound
        session = sessions.create(:id => message.from)
      end

      begin
        _, _, output, error = session.call(message.body.force_encoding("UTF-8"))
      rescue
        output, error = nil, $!.message
      end

      result          = Jabber::Message.new
      result.to       = message.from
      result.body     = output.join("\n") if output
      result.body    += %{<<#{error}>>} if error
      client.send(result)
    end
  end

  roster.add_subscription_request_callback do |item,presence|
    roster.accept_subscription(presence.from)
    client.send(Jabber::Presence.new.set_type(:subscribe).set_to(presence.from))
    
    m      = Jabber::Message.new
    m.to   = presence.from
    m.body = "Registered!"
    client.send(m)
  end

  Thread.stop

  client.close
end