Class: God::Contacts::Jabber

Inherits:
God::Contact show all
Defined in:
lib/god/contacts/jabber.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from God::Contact

#group, #info, #name

Instance Method Summary collapse

Methods inherited from God::Contact

#friendly_name, generate, normalize, valid?

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name, #prepare, #reset

Class Attribute Details

.clientObject

Returns the value of attribute client.



22
23
24
# File 'lib/god/contacts/jabber.rb', line 22

def client
  @client
end

.formatObject

Returns the value of attribute format.



22
23
24
# File 'lib/god/contacts/jabber.rb', line 22

def format
  @format
end

.settingsObject

Returns the value of attribute settings.



22
23
24
# File 'lib/god/contacts/jabber.rb', line 22

def settings
  @settings
end

Instance Attribute Details

#jabber_idObject

Returns the value of attribute jabber_id.



33
34
35
# File 'lib/god/contacts/jabber.rb', line 33

def jabber_id
  @jabber_id
end

Instance Method Details

#clientObject



117
118
119
# File 'lib/god/contacts/jabber.rb', line 117

def client
  Jabber.client
end

#client=(jc) ⇒ Object



121
122
123
# File 'lib/god/contacts/jabber.rb', line 121

def client=(jc)
  Jabber.client = jc
end

#connectObject



79
80
81
# File 'lib/god/contacts/jabber.rb', line 79

def connect
  connect! unless connected?
end

#connect!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/god/contacts/jabber.rb', line 88

def connect!
  disconnect! if connected?

  @connect_mutex ||= Mutex.new
  # don't try to connect if another thread is already connecting.
  return if @connect_mutex.locked?
  @connect_mutex.lock
  
  jabber_id = XMPP4R::JID::new "#{Jabber.settings[:jabber_id]}/God"
  jabber_client = XMPP4R::Client::new jabber_id
  jabber_client.connect Jabber.settings[:host]
  jabber_client.auth Jabber.settings[:password]
  self.client = jabber_client
  
  @connect_mutex.unlock
end

#connected?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/god/contacts/jabber.rb', line 83

def connected?
  connected = client.respond_to?(:is_connected?) && client.is_connected?
  return connected
end

#disconnect!Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/god/contacts/jabber.rb', line 105

def disconnect!
  if client.respond_to?(:is_connected?) && client.is_connected?
    begin
      client.close
    rescue Errno::EPIPE, IOError => e
      self.info "Failed to disconnect: #{e}"
      nil
    end
  end
  client = nil
end

#notify(message, time, priority, category, host) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/god/contacts/jabber.rb', line 39

def notify(message, time, priority, category, host)
  connect
  
  body = Jabber.format.call message, priority, category, host
  
  message = XMPP4R::Message::new self.jabber_id, body
  message.set_type :normal
  message.set_id '1'
  message.set_subject 'God'
  
  self.send!(message)
  
  self.info = "sent jabber message to #{self.jabber_id}"
rescue => e
  puts e.message
  puts e.backtrace.join("\n")
  
  self.info = "failed to send jabber message to #{self.jabber_id}: #{e.message}"
end

#send!(msg) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/god/contacts/jabber.rb', line 59

def send!(msg)
  attempts = 0
  begin
    attempts += 1
    client.send(msg)
  rescue Errno::EPIPE, IOError => e
    sleep 1
    disconnect!
    reconnect!
    retry unless attempts > 3
    raise e
  rescue Errno::ECONNRESET => e
    sleep (attempts^2) * 60 + 60
    disconnect!
    reconnect!
    retry unless attempts > 3
    raise e
  end
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/god/contacts/jabber.rb', line 35

def valid?
  valid = true
end