Class: Jabber::MUC::SimpleMUCClient

Inherits:
MUCClient
  • Object
show all
Defined in:
lib/xmpp4r/muc/helper/simplemucclient.rb

Overview

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, …

Instance Attribute Summary

Attributes inherited from MUCClient

#jid, #my_jid, #roster

Instance Method Summary collapse

Methods inherited from MUCClient

#active?, #add_join_callback, #add_leave_callback, #add_message_callback, #add_presence_callback, #add_private_message_callback, #exit, #from_room?, #join, #nick, #nick=, #room, #send

Constructor Details

#initialize(stream) ⇒ SimpleMUCClient

Initialize a SimpleMUCClient

stream
Stream

to operate on

jid
JID

room@component/nick

password
String

Optional password



39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 39

def initialize(stream)
  super

  @room_message_block = nil
  @message_block = nil
  @private_message_block = nil
  @subject_block = nil

  @subject = nil

  @join_block = nil
  add_join_callback(999) { |pres|
    # Presence time
    time = nil
    pres.each_element('x') { |x|
      if x.kind_of?(Delay::XDelay)
        time = x.stamp
      end
    }

    # Invoke...
    @join_block.call(time, pres.from.resource) if @join_block
    false
  }

  @leave_block = nil
  @self_leave_block = nil
  add_leave_callback(999) { |pres|
    # Presence time
    time = nil
    pres.each_element('x') { |x|
      if x.kind_of?(Delay::XDelay)
        time = x.stamp
      end
    }

    # Invoke...
    if pres.from == jid
      @self_leave_block.call(time) if @self_leave_block
    else
      @leave_block.call(time, pres.from.resource) if @leave_block
    end
    false
  }
end

Instance Method Details

#invite(recipients) ⇒ Object

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



156
157
158
159
160
161
162
163
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 156

def invite(recipients)
  msg = Message.new
  x = msg.add(XMucUser.new)
  recipients.each { |jid,reason|
    x.add(XMucUserInvite.new(jid, reason))
  }
  send(msg)
end

#on_join(&block) ⇒ Object

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



205
206
207
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 205

def on_join(&block)
  @join_block = block
end

#on_leave(&block) ⇒ Object

Block to be called when somebody leaves the room

block

Takes two arguments: time, nickname



212
213
214
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 212

def on_leave(&block)
  @leave_block = block
end

#on_message(&block) ⇒ Object

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

block

Takes three arguments: time, sender nickname, text



179
180
181
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 179

def on_message(&block)
  @message_block = block
end

#on_private_message(&block) ⇒ Object

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

block

Takes three arguments: time, sender nickname, text



187
188
189
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 187

def on_private_message(&block)
  @private_message_block = block
end

#on_room_message(&block) ⇒ Object

Block to be invoked when a message from the room arrives

Example:

Astro has joined this session
block

Takes two arguments: time, text



171
172
173
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 171

def on_room_message(&block)
  @room_message_block = block
end

#on_self_leave(&block) ⇒ Object

Block to be called when you leave the room

Deactivation occurs afterwards.

block

Takes one argument: time



221
222
223
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 221

def on_self_leave(&block)
  @self_leave_block = block
end

#on_subject(&block) ⇒ Object

Block to be invoked when somebody sets a new room subject

block

Takes three arguments: time, nickname, new subject



194
195
196
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 194

def on_subject(&block)
  @subject_block = block
end

#say(text, to = nil) ⇒ Object

Send a simple text message

text
String

Message body

to
String

Optional nick if directed to specific user



145
146
147
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 145

def say(text, to=nil)
  send(Message.new(nil, text), to)
end

#subjectObject

Room subject/topic

result
String

The subject



125
126
127
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 125

def subject
  @subject
end

#subject=(s) ⇒ Object

Change the room's subject/topic

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

s
String

New subject



135
136
137
138
139
# File 'lib/xmpp4r/muc/helper/simplemucclient.rb', line 135

def subject=(s)
  msg = Message.new
  msg.subject = s
  send(msg)
end