Class Jabber::MUC::SimpleMUCClient
In: lib/xmpp4r/muc/helper/simplemucclient.rb
Parent: MUCClient

This class attempts to implement a lot of complexity of the Multi-User Chat protocol. If you want to implement JEP0045 yourself, use Jabber::MUC::MUCClient for some minor abstraction.

Minor flexibility penalty: the on_* callbacks are no CallbackLists and may therefore only used once. A second invocation will overwrite the previous set up block.

*Hint:* the parameter time may be nil if the server didn‘t send it.

Example usage:

  my_muc = Jabber::MUC::SimpleMUCClient.new(my_client)
  my_muc.on_message { |time,nick,text|
    puts (time || Time.new).strftime('%I:%M') + " <#{nick}> #{text}"
  }
  my_muc.join(Jabber::JID.new('jdev@conference.jabber.org/XMPP4R-Bot'))

Please take a look at Jabber::MUC::MUCClient for derived methods, such as MUCClient#join, MUCClient#exit, …

Methods

Public Class methods

Initialize a SimpleMUCClient

stream:[Stream] to operate on
jid:[JID] room@component/nick
password:[String] Optional password

[Source]

    # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 39
39:       def initialize(stream)
40:         super
41: 
42:         @room_message_block = nil
43:         @message_block = nil
44:         @private_message_block = nil
45:         @subject_block = nil
46: 
47:         @subject = nil
48: 
49:         @join_block = nil
50:         add_join_callback(999) { |pres|
51:           # Presence time
52:           time = nil
53:           pres.each_element('x') { |x|
54:             if x.kind_of?(Delay::XDelay)
55:               time = x.stamp
56:             end
57:           }
58: 
59:           # Invoke...
60:           @join_block.call(time, pres.from.resource) if @join_block
61:           false
62:         }
63: 
64:         @leave_block = nil
65:         @self_leave_block = nil
66:         add_leave_callback(999) { |pres|
67:           # Presence time
68:           time = nil
69:           pres.each_element('x') { |x|
70:             if x.kind_of?(Delay::XDelay)
71:               time = x.stamp
72:             end
73:           }
74: 
75:           # Invoke...
76:           if pres.from == jid
77:             @self_leave_block.call(time) if @self_leave_block
78:           else
79:             @leave_block.call(time, pres.from.resource) if @leave_block
80:           end
81:           false
82:         }
83:       end

Public Instance methods

Request the MUC to invite users to this room

Sample usage:

  my_muc.invite( {'wiccarocks@shakespeare.lit/laptop' => 'This coven needs both wiccarocks and hag66.',
                  'hag66@shakespeare.lit' => 'This coven needs both hag66 and wiccarocks.'} )
recipients:[Hash] of [JID] => [String] Reason

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 156
156:       def invite(recipients)
157:         msg = Message.new
158:         x = msg.add(XMucUser.new)
159:         recipients.each { |jid,reason|
160:           x.add(XMucUserInvite.new(jid, reason))
161:         }
162:         send(msg)
163:       end

Block to be called when somebody enters the room

If there is a non-nil time passed to the block, chances are great that this is initial presence from a participant after you have joined the room.

block:Takes two arguments: time, nickname

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 205
205:       def on_join(&block)
206:         @join_block = block
207:       end

Block to be called when somebody leaves the room

block:Takes two arguments: time, nickname

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 212
212:       def on_leave(&block)
213:         @leave_block = block
214:       end

Block to be invoked when a message from a participant to the whole room arrives

block:Takes three arguments: time, sender nickname, text

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 179
179:       def on_message(&block)
180:         @message_block = block
181:       end

Block to be invoked when a private message from a participant to you arrives.

block:Takes three arguments: time, sender nickname, text

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 187
187:       def on_private_message(&block)
188:         @private_message_block = block
189:       end

Block to be invoked when a message from the room arrives

Example:

  Astro has joined this session
block:Takes two arguments: time, text

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 171
171:       def on_room_message(&block)
172:         @room_message_block = block
173:       end

Block to be called when you leave the room

Deactivation occurs afterwards.

block:Takes one argument: time

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 221
221:       def on_self_leave(&block)
222:         @self_leave_block = block
223:       end

Block to be invoked when somebody sets a new room subject

block:Takes three arguments: time, nickname, new subject

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 194
194:       def on_subject(&block)
195:         @subject_block = block
196:       end

Send a simple text message

text:[String] Message body
to:[String] Optional nick if directed to specific user

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 145
145:       def say(text, to=nil)
146:         send(Message.new(nil, text), to)
147:       end

Room subject/topic

result:[String] The subject

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 125
125:       def subject
126:         @subject
127:       end

Change the room‘s subject/topic

This will not be reflected by SimpleMUCClient#subject immediately, wait for SimpleMUCClient#on_subject

s:[String] New subject

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 135
135:       def subject=(s)
136:         msg = Message.new
137:         msg.subject = s
138:         send(msg)
139:       end

Private Instance methods

[Source]

     # File lib/xmpp4r/muc/helper/simplemucclient.rb, line 87
 87:       def handle_message(msg)
 88:         super
 89: 
 90:         # Message time (e.g. history)
 91:         time = nil
 92:         msg.each_element('x') { |x|
 93:           if x.kind_of?(Delay::XDelay)
 94:             time = x.stamp
 95:           end
 96:         }
 97:         sender_nick = msg.from.resource
 98: 
 99: 
100:         if msg.subject
101:           @subject = msg.subject
102:           @subject_block.call(time, sender_nick, @subject) if @subject_block
103:         end
104:         
105:         if msg.body
106:           if sender_nick.nil?
107:             @room_message_block.call(time, msg.body) if @room_message_block
108:           else
109:             if msg.type == :chat
110:               @private_message_block.call(time, msg.from.resource, msg.body) if @private_message_block
111:             elsif msg.type == :groupchat
112:               @message_block.call(time, msg.from.resource, msg.body) if @message_block
113:             else
114:               # ...?
115:             end
116:           end
117:         end
118:       end

[Validate]