Class: Room

Inherits:
Object show all
Defined in:
lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb

Overview

Jabber::debug = true

Defined Under Namespace

Classes: RoomException

Instance Method Summary collapse

Constructor Details

#initialize(stream, name) ⇒ Room

Returns a new instance of Room.



21
22
23
24
25
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 21

def initialize(stream, name)
  @users = {}   # nick => jid
  @stream = stream
  @name = name
end

Instance Method Details

#broadcast(stanza) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 118

def broadcast(stanza)
  x = stanza.class::import(stanza)
  @users.each { |nick,jid|
    x.to = jid
    @stream.send(x)
  }
end

#each_user(&block) ⇒ Object



31
32
33
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 31

def each_user(&block)
  @users.each { |n,j| yield(n, j) }
end

#handle_message(msg) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 35

def handle_message(msg)
  origin = msg.from
  msg.from = nil
  @users.each { |nick,jid|
    if jid == origin
      msg.from = Jabber::JID.new(@name.node, @name.domain, nick)
    end
  }
  unless msg.from.nil?
    broadcast(msg)
  end

  true
end

#handle_presence(pres) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 50

def handle_presence(pres)
  reason = nil

  # Check if nick already registered
  @users.each { |nick,jid|
    if pres.from != jid && pres.to.resource == nick
      puts "#{pres.from} tried to use already active #{pres.to}"
      raise RoomException.new('conflict', "Nick already used")
    end
  }
  # New user?
  unless @users.has_key?(pres.to.resource)
    # Check nick length
    if pres.to.resource.size < 1
      puts "#{pres.from} tried to use empty nick"
      raise RoomException.new('not-acceptable', "Nick must contain characters")
    end

    # Push user-list
    userinfo = Jabber::Presence.import(pres)
    userinfo.to = pres.from
    userinfo.add(Jabber::XMUCUser.new).add(Jabber::XMUCUserItem.new(:none, :participant))
    print "Sending all users for #{pres.to} to #{pres.from}:"
    @users.each { |nick,jid|
      userinfo.from = Jabber::JID.new(@name.node, @name.domain, nick)
      print " #{nick} (#{jid})"
      @stream.send(userinfo)
    }
    puts " Ok"

    # Add the new user
    puts "Adding #{pres.from} as #{pres.to}"
    @users[pres.to.resource] = pres.from

    send_message("#{pres.to.resource} joins #{@name.node}")
    reason = "#{pres.to.resource} joins #{@name.node}"
  end

  # Remove dead rats
  if pres.type == :error || pres.type == :unavailable
    was_nick = nil
    @users.delete_if { |nick,jid|
      was_nick = nick
      jid == pres.from
    }
    unless was_nick.nil?
      send_message("#{was_nick} has left #{@name.node}")
      reason = "#{was_nick} has left #{@name.node}"
    end
  end

  # Advertise users presence to all
  puts "Advertising user to all"
  x = Jabber::XMUCUserItem.new(:none, :participant, pres.from)
  x.reason = reason
  pres.add(Jabber::XMUCUser.new).add(x)
  pres.from = pres.to
  broadcast(pres)
end

#num_usersObject



27
28
29
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 27

def num_users
  @users.size
end

#send_message(body) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/gems/xmpp4r-0.4/data/doc/xmpp4r/examples/advanced/minimuc.rb', line 110

def send_message(body)
  msg = Jabber::Message.new
  msg.from = Jabber::JID.new(@name.node, @name.domain, "")
  msg.type = :groupchat
  msg.body = body
  broadcast(msg)
end