| 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, …
Initialize a SimpleMUCClient
| stream: | [Stream] to operate on |
| jid: | [JID] room@component/nick |
| password: | [String] Optional password |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 35
35: def initialize(stream)
36: super
37:
38: @room_message_block = nil
39: @message_block = nil
40: @private_message_block = nil
41: @subject_block = nil
42:
43: @subject = nil
44:
45: @join_block = nil
46: add_join_callback(999) { |pres|
47: # Presence time
48: time = nil
49: pres.each_element('x') { |x|
50: if x.kind_of?(Delay::XDelay)
51: time = x.stamp
52: end
53: }
54:
55: # Invoke...
56: @join_block.call(time, pres.from.resource) if @join_block
57: false
58: }
59:
60: @leave_block = nil
61: @self_leave_block = nil
62: add_leave_callback(999) { |pres|
63: # Presence time
64: time = nil
65: pres.each_element('x') { |x|
66: if x.kind_of?(Delay::XDelay)
67: time = x.stamp
68: end
69: }
70:
71: # Invoke...
72: if pres.from == jid
73: @self_leave_block.call(time) if @self_leave_block
74: else
75: @leave_block.call(time, pres.from.resource) if @leave_block
76: end
77: false
78: }
79: end
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 |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 152
152: def invite(recipients)
153: msg = Message.new
154: x = msg.add(XMucUser.new)
155: recipients.each { |jid,reason|
156: x.add(XMucUserInvite.new(jid, reason))
157: }
158: send(msg)
159: 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 |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 201
201: def on_join(&block)
202: @join_block = block
203: end
Block to be called when somebody leaves the room
| block: | Takes two arguments: time, nickname |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 208
208: def on_leave(&block)
209: @leave_block = block
210: end
Block to be invoked when a message from a participant to the whole room arrives
| block: | Takes three arguments: time, sender nickname, text |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 175
175: def on_message(&block)
176: @message_block = block
177: end
Block to be invoked when a private message from a participant to you arrives.
| block: | Takes three arguments: time, sender nickname, text |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 183
183: def on_private_message(&block)
184: @private_message_block = block
185: end
Block to be invoked when a message from the room arrives
Example:
Astro has joined this session
| block: | Takes two arguments: time, text |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 167
167: def on_room_message(&block)
168: @room_message_block = block
169: end
Block to be called when you leave the room
Deactivation occurs afterwards.
| block: | Takes one argument: time |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 217
217: def on_self_leave(&block)
218: @self_leave_block = block
219: 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 |
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 131
131: def subject=(s)
132: msg = Message.new
133: msg.subject = s
134: send(msg)
135: end
# File lib/xmpp4r/muc/helper/simplemucclient.rb, line 83
83: def handle_message(msg)
84: super
85:
86: # Message time (e.g. history)
87: time = nil
88: msg.each_element('x') { |x|
89: if x.kind_of?(Delay::XDelay)
90: time = x.stamp
91: end
92: }
93: sender_nick = msg.from.resource
94:
95:
96: if msg.subject
97: @subject = msg.subject
98: @subject_block.call(time, sender_nick, @subject) if @subject_block
99: end
100:
101: if msg.body
102: if sender_nick.nil?
103: @room_message_block.call(time, msg.body) if @room_message_block
104: else
105: if msg.type == :chat
106: @private_message_block.call(time, msg.from.resource, msg.body) if @private_message_block
107: elsif msg.type == :groupchat
108: @message_block.call(time, msg.from.resource, msg.body) if @message_block
109: else
110: # ...?
111: end
112: end
113: end
114: end