Class: Jabber::MUC::MUCClient

Inherits:
Object
  • Object
show all
Defined in:
lib/hipchat_bot/muc_client.rb

Instance Method Summary collapse

Instance Method Details

#join(jid, password = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# File 'lib/hipchat_bot/muc_client.rb', line 8

def join(jid, password=nil)
  if active?
    raise "MUCClient already active"
  end

  @jid = (jid.kind_of?(JID) ? jid : JID.new(jid))
  activate

  # Joining
  pres = Presence.new
  pres.to = @jid
  pres.from = @my_jid
  xmuc = XMUC.new
  xmuc.password = password
  pres.add(xmuc)

  # NOTE: Adding 'maxstanzas="0"' to 'history' subelement of xmuc nixes
  # the history being sent to us when we join.
  history = XMPPElement.new('history')
  history.add_attributes({'maxstanzas' => '0'})
  xmuc.add(history)

  # We don't use Stream#send_with_id here as it's unknown
  # if the MUC component *always* uses our stanza id.
  error = nil
  @stream.send(pres) { |r|
    if from_room?(r.from) and r.kind_of?(Presence) and r.type == :error
      # Error from room
      error = r.error
      true
      # type='unavailable' may occur when the MUC kills our previous instance,
      # but all join-failures should be type='error'
    elsif r.from == jid and r.kind_of?(Presence) and r.type != :unavailable
      # Our own presence reflected back - success
      if r.x(XMUCUser) and (i = r.x(XMUCUser).items.first)
        @affiliation = i.affiliation  # we're interested in if it's :owner
        @role = i.role                # :moderator ?
      end

      handle_presence(r, false)
      true
    else
      # Everything else
      false
    end
  }

  if error
    deactivate
    raise ServerError.new(error)
  end

  self
end