Module: Mack::Notifier::DeliveryHandlers::XmppTransport

Defined in:
lib/mack-notifier/delivery_handlers/xmpp_transport.rb

Overview

Delivers Mack::Notifier objects using XMPP (Jabber)

Class Method Summary collapse

Class Method Details

.check_availability(client, recipients) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mack-notifier/delivery_handlers/xmpp_transport.rb', line 7

def self.check_availability(client, recipients)
  buddies = {}
  recipients.each do |rcpt|
    buddies[rcpt] = :offline
  end

  # try to get the presence update
  sleep(1)
  client.presence_updates do |update|
    from = update[0].to_s
    presence = update[1].to_s
    if buddies.has_key?(from)
      buddies[from] = presence
    end
    Mack.logger.info "#{from} --> #{presence}"
  end
  return buddies
end

.deliver(xmpp_msg) ⇒ Object

Raises:

  • (@ex)


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/mack-notifier/delivery_handlers/xmpp_transport.rb', line 36

def self.deliver(xmpp_msg)
  @ex ||= Mack::Errors::XmppError.new("xmpp error")
  xmpp_settings = configatron.mack.notifier.xmpp
  jid_str = xmpp_settings.jid
  jid_str += ("/" + xmpp_settings.jid_resource) if !jid_str.index("/")
  password = xmpp_settings.password
  client = Jabber::Simple.new(jid_str, password)
  thr = Thread.new {
    begin
      xmpp_msg = xmpp_msg.deliverable
      Timeout::timeout(20) do
        buddies = check_availability(client, xmpp_msg.recipients)

        online_buddies = buddies.keys.reject { |x| buddies[x].to_sym == :offline }
        offline_buddies = buddies.keys.reject { |x| buddies[x].to_sym == :online }

        if !offline_buddies.empty?
          # there are offline buddies, log the errors
          offline_buddies.each do |buddy|
            err = Mack::Errors::XmppUserNotOnline.new(buddy)
            @ex.add_error(:offline, err)
          end
        end
        
        online_buddies.each do |buddy|
          deliver_message(client, xmpp_msg.find_message_by_recipient(buddy), xmpp_settings.wait_for_response, @ex)
        end
        
        raise @ex if !@ex.empty?
      end # timeout
    rescue Timeout::Error => ex
      @error = ex
    rescue Exception => ex
    end
  }
  
  thr.join
  raise @ex if !@ex.empty?
end

.deliver_message(client, xmpp_msg, wait_for_response, xmpp_errors) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mack-notifier/delivery_handlers/xmpp_transport.rb', line 26

def self.deliver_message(client, xmpp_msg, wait_for_response, xmpp_errors)
  Mack.logger.info "#{xmpp_msg.to} is online, delivering message"
  client.deliver(xmpp_msg.to, xmpp_msg)
  loop do
    client.received_messages { |ret_msg| @msg_received = true if ret_msg.body == "ack" }
    break if @msg_received or @error
  end if wait_for_response
  return xmpp_errors
end