Class: XMPPNotificator

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc-xmpp-notificator.rb

Constant Summary collapse

@@client =
nil
@@tpl =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ XMPPNotificator



8
9
10
# File 'lib/wcc-xmpp-notificator.rb', line 8

def initialize(opts)
  @jid = Jabber::JID.new(opts)
end

Class Method Details

.get_clientObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/wcc-xmpp-notificator.rb', line 47

def self.get_client
  if @@client.nil? and not WCC::Conf[:xmpp_jid].nil?
    @@client = Jabber::Client.new(WCC::Conf[:xmpp_jid])
    @@client.connect
    begin
      @@client.auth(WCC::Conf[:xmpp_password])
      @@client.send(Jabber::Presence.new.set_status('At your service every night.'))
    rescue Jabber::ClientAuthenticationFailure => ex
      WCC.logger.fatal "Wrong jabber password for #{WCC::Conf[:xmpp_jid]}!"
      @@client.close
      @@client = nil
    end
  end
  @@client
end

.get_tpl(id, name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wcc-xmpp-notificator.rb', line 63

def self.get_tpl(id, name)
  if @@tpl[id].nil?
    @@tpl[id] = WCC::Prog.load_template(name)
    if @@tpl[id].nil?
      src_path = Pathname.new(__FILE__) + "../../assets/template.d/#{name}"
      t = File.open(src_path, 'r') { |f| f.read }
      WCC::Prog.save_template(name, t)
      # retry load
      @@tpl[id] = WCC::Prog.load_template(name)
    end
    if @@tpl[id].nil?
      @@tpl[id] = ERB.new("ERROR LOADING TEMPLATE '#{name}'!", 0, "<>")
    end
  end
  @@tpl[id]
end

.parse_conf(conf) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wcc-xmpp-notificator.rb', line 22

def self.parse_conf(conf)
  if conf.is_a?(Hash)
    if conf['jid'].nil?
      WCC.logger.fatal "Missing jabber ID!"
      return {:xmpp_jid => nil}
    elsif conf['password'].nil?
      WCC.logger.fatal "Missing jabber password!"
    else
      return {
        :xmpp_jid => Jabber::JID.new(conf['jid']),
        :xmpp_password => conf['password']
      }
    end
  end
  # no defaults
  {}
end

.shut_downObject



40
41
42
43
44
45
# File 'lib/wcc-xmpp-notificator.rb', line 40

def self.shut_down
  if not @@client.nil?
    @@client.send(Jabber::Presence.new.set_type(:unavailable))
    @@client.close
  end
end

Instance Method Details

#notify!(data) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/wcc-xmpp-notificator.rb', line 12

def notify!(data)
  # prepare message
  body = self.class.get_tpl(:body, 'xmpp-body.plain.erb').result(binding)
  m = Jabber::Message.new(@jid, body)
  m.type = :normal
  # send it
  c = self.class.get_client
  c.send(m) unless c.nil?
end