Class: JabberChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/cirrocumulus/channels/jabber.rb

Constant Summary collapse

@@jabber_clients =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = nil, conference = nil) ⇒ JabberChannel

Returns a new instance of JabberChannel.



118
119
120
121
122
123
124
125
126
# File 'lib/cirrocumulus/channels/jabber.rb', line 118

def initialize(server = nil, conference = nil)
  @jabber = nil
  @server = server || @@server
  @conference = conference || @@conference
  @send_q = Queue.new
  @recv_q = Queue.new
  
  @@jabber_clients << self
end

Instance Attribute Details

#conferenceObject (readonly)

Returns the value of attribute conference.



116
117
118
# File 'lib/cirrocumulus/channels/jabber.rb', line 116

def conference
  @conference
end

#jidObject (readonly)

Returns the value of attribute jid.



115
116
117
# File 'lib/cirrocumulus/channels/jabber.rb', line 115

def jid
  @jid
end

Class Method Details

.conference(conf) ⇒ Object



110
111
112
# File 'lib/cirrocumulus/channels/jabber.rb', line 110

def conference(conf)
  @@conference = conf
end

.password(pass) ⇒ Object



106
107
108
# File 'lib/cirrocumulus/channels/jabber.rb', line 106

def password(pass)
  @@password = pass
end

.query_client(jid) ⇒ Object



98
99
100
# File 'lib/cirrocumulus/channels/jabber.rb', line 98

def query_client(jid)
  @@jabber_clients.find {|c| c.jid == jid}
end

.server(server) ⇒ Object



102
103
104
# File 'lib/cirrocumulus/channels/jabber.rb', line 102

def server(server)
  @@server = server
end

Instance Method Details

#connect(jid) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cirrocumulus/channels/jabber.rb', line 132

def connect(jid)
  @full_jid = "%s@%s" % [jid, @server]
  @jid = jid

  Log4r::Logger['channels::jabber'].info "Server: #{@server}"
  Log4r::Logger['channels::jabber'].info "JID: '#{@jid}'"
  
  begin
    @jabber = Jabber::Simple.new(@full_jid, @@password)
  rescue Jabber::ClientAuthenticationFailure => ex
    Log4r::Logger['channels::jabber'].debug('Received Jabber::ClientAuthenticationFailure, registering new account')
    client = Jabber::Client.new(@full_jid)
    client.connect()
    client.register(@@password)
    client.close()
    @jabber = Jabber::Simple.new(@full_jid, @@password)
  rescue Exception => ex
    Log4r::Logger['channels::jabber'].fatal('Failed to register new account or connect.')
    Log4r::Logger['channels::jabber'].fatal("Received exception: #{ex.to_s}")
    return false
  end
  
  join_conference(@conference) if connected?
  connected?
end

#connected?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/cirrocumulus/channels/jabber.rb', line 128

def connected?
  @jabber && @jabber.connected?
end

#disconnectObject



158
159
160
# File 'lib/cirrocumulus/channels/jabber.rb', line 158

def disconnect()
  @jabber.disconnect()
end

#queue(msg) ⇒ Object



162
163
164
# File 'lib/cirrocumulus/channels/jabber.rb', line 162

def queue(msg)
  @send_q << msg
end

#received_messagesObject



166
167
168
# File 'lib/cirrocumulus/channels/jabber.rb', line 166

def received_messages
  @recv_q
end

#tickObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cirrocumulus/channels/jabber.rb', line 170

def tick()
  return if !connected?

  @jabber.received_messages do |msg|
    next unless msg.x('jabber:x:delay').nil?
    @recv_q << msg
  end
  
  while true do
    to_send = @send_q.pop(true) rescue nil
    break if to_send.nil?

    @jabber.send!('<message type="groupchat" to="%s" id="%s"><body>%s</body></message>' % [
      "%s@conference.%s" % [@conference, @server], Guid.new.to_s.gsub('-', ''),                                                                                         
      to_send.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;').gsub('"', '&quot;')                                     
    ]) 
  end
rescue Exception => ex
  Log4r::Logger['channels::jabber'].warn(ex.to_s)
end