Class: MUC

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb

Instance Method Summary collapse

Constructor Details

#initialize(jid, secret, addr, port = 5347) ⇒ MUC

Returns a new instance of MUC.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 128

def initialize(jid, secret, addr, port=5347)
  @rooms = {}

  @component = Jabber::Component.new(jid, addr, port)
  @component.connect
  @component.auth(secret)

  @component.add_iq_callback { |iq|
    handle_iq(iq)
  }
  @component.add_presence_callback { |pres|
    handle_presence(pres)
  }
  @component.add_message_callback { |msg|
    handle_message(msg)
  }
end

Instance Method Details

#handle_disco_info(iq) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 203

def handle_disco_info(iq)
  if (iq.type == :get)
    iq.type = :result
    iq.query = Jabber::IqQueryDiscoInfo.new
    if iq.to.node == nil
      iq.query.add(Jabber::DiscoIdentity.new('conference', 'Minimal Multi-User Chat', 'text'))
    else
      room = @rooms[iq.to]
      if room.nil?
        iq.type = :error
        iq.add_element(Jabber::ErrorResponse.new('item-not-found'))
      else
        iq.query.add(Jabber::DiscoIdentity.new('conference', "#{iq.to.node} (#{room.num_users})", 'text'))
      end
    end
    [Jabber::IqQueryDiscoInfo.new.namespace,
     Jabber::IqQueryDiscoItems.new.namespace,
     Jabber::XMUC.new.namespace,
     Jabber::XMUCUser.new.namespace].each { |ns|
        iq.query.add(Jabber::DiscoFeature.new(ns))
    }
  else
    iq.type = :error
    iq.add_element(Jabber::ErrorResponse.new('bad-request'))
  end
  iq.to, iq.from = iq.from, iq.to
  @component.send(iq)
end

#handle_disco_items(iq) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 232

def handle_disco_items(iq)
  if (iq.type == :get)
    iq.type = :result
    if iq.to.node == nil
      @rooms.each { |name,room|
        iq.query.add(Jabber::DiscoItem.new(Jabber::JID.new(name, @component.jid.domain), name))
      }
    elsif iq.to.resource == nil
      room = @rooms[iq.to.strip]
      unless room.nil?
        room.each_user { |nick,jid|
          iq.query.add(Jabber::DiscoItem.new(jid, nick))
        }
      else
        iq.type = :error
        iq.add_element(Jabber::ErrorResponse.new('item-not-found'))
      end
    end
  else
    iq.type = :error
    iq.add_element(Jabber::ErrorResponse.new('bad-request'))
  end
  iq.to, iq.from = iq.from, iq.to
  @component.send(iq)
end

#handle_iq(iq) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 146

def handle_iq(iq)
  puts "#{iq.from} #{iq.queryns} to #{iq.to}"
  if iq.query.kind_of?(Jabber::IqQueryDiscoInfo)
    handle_disco_info(iq)
    true
  elsif iq.query.kind_of?(Jabber::IqQueryDiscoItems)
    handle_disco_items(iq)
    true
  else
    false
  end
end

#handle_message(msg) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 188

def handle_message(msg)
  puts "Message from #{msg.from} to #{msg.to} (#{msg.type}): #{msg.body.inspect}"
  return if msg.type == :error

  room = @rooms[msg.to]
  unless room.nil?
    room.handle_message(msg)
  else
    msg.to, msg.from = msg.from, msg.to
    msg.type = :error
    msg.add(Jabber::ErrorResponse.new('item-not-found', 'The chatroom you are trying to reach is currently not available.'))
    @component.send(msg)
  end
end

#handle_presence(pres) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 159

def handle_presence(pres)
  room = nil
  @rooms.each { |name,r|
    room = r if name == pres.to.strip
  }

  if room.nil? && pres.type.nil? && (pres.to.node.to_s != '')
    room = Room.new(@component, pres.to.strip)
    @rooms[pres.to.strip] = room
  elsif room.nil? && pres.type != :error
    pres.to, pres.from = pres.from, pres.to
    pres.type = :error
    pres.add(Jabber::ErrorResponse.new('item-not-found'))
    @component.send(pres)
    return(true)
  end

  begin
    room.handle_presence(pres)
  rescue Room::RoomException => e
    pres.to, pres.from = pres.from, pres.to
    pres.type = :error
    pres.add(e.error)
    @component.send(pres)
    return(true)
  end
  return(false)
end