From d46d5d7fc289579592dc4260369729279c97bbdd Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Sat, 25 Feb 2012 19:39:09 +0000 Subject: [PATCH] [FEATURE] Add support for invitation elements to MUCUser presence --- lib/blather/stanza/presence/muc_user.rb | 67 +++++++++++++++++++ spec/blather/stanza/presence/muc_user_spec.rb | 56 ++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/lib/blather/stanza/presence/muc_user.rb b/lib/blather/stanza/presence/muc_user.rb index b51bb267..35da4ba3 100644 --- a/lib/blather/stanza/presence/muc_user.rb +++ b/lib/blather/stanza/presence/muc_user.rb @@ -60,6 +60,10 @@ def password=(var) password_node.content = var end + def invite? + !!invite + end + def muc_user unless muc_user = find_first('ns:x', :ns => self.class.registered_ns) self << (muc_user = XMPPNode.new('x', self.document)) @@ -94,6 +98,15 @@ def find_password_node muc_user.find_first 'ns:password', :ns => self.class.registered_ns end + def invite + if invite = muc_user.find_first('ns:invite', :ns => self.class.registered_ns) + Invite.new invite + else + muc_user << (invite = Invite.new nil, nil, nil, self.document) + invite + end + end + class Item < XMPPNode def self.new(affiliation = nil, role = nil, jid = nil, document = nil) new_node = super :item, document @@ -166,6 +179,60 @@ def code=(val) write_attr :code, val end end + + class Invite < XMPPNode + def self.new(to = nil, from = nil, reason = nil, document = nil) + new_node = super :invite, document + + case to + when self + to.document ||= document + return to + when Nokogiri::XML::Node + new_node.inherit to + when Hash + new_node.to = to[:to] + new_node.from = to[:from] + new_node.reason = to[:reason] + else + new_node.to = to + new_node.from = from + new_node.reason = reason + end + new_node + end + + def to + read_attr :to + end + + def to=(val) + write_attr :to, val + end + + def from + read_attr :from + end + + def from=(val) + write_attr :from, val + end + + def reason + reason_node.content.strip + end + + def reason=(val) + reason_node.content = val + end + + def reason_node + unless reason = find_first('ns:reason', :ns => MUCUser.registered_ns) + self << (reason = XMPPNode.new('reason', self.document)) + end + reason + end + end end # MUC end # Presence diff --git a/spec/blather/stanza/presence/muc_user_spec.rb b/spec/blather/stanza/presence/muc_user_spec.rb index 3a05f6ec..e08f614c 100644 --- a/spec/blather/stanza/presence/muc_user_spec.rb +++ b/spec/blather/stanza/presence/muc_user_spec.rb @@ -17,6 +17,23 @@ def muc_user_xml XML end +def muc_invite_xml + <<-XML + + + + + Hey Hecate, this is the place for all good witches! + + + + + XML +end + describe 'Blather::Stanza::Presence::MUCUser' do it 'registers itself' do Blather::XMPPNode.class_from_registration(:x, 'http://jabber.org/protocol/muc#user' ).must_equal Blather::Stanza::Presence::MUCUser @@ -84,4 +101,43 @@ def muc_user_xml muc_user.password = 'hello_world' muc_user.password.must_equal 'hello_world' end + + describe "with an invite element" do + it "should be an #invite?" do + muc_user = Blather::XMPPNode.import(parse_stanza(muc_invite_xml).root) + muc_user.invite?.must_equal true + end + + it "should know the invite attributes properly" do + muc_user = Blather::XMPPNode.import(parse_stanza(muc_invite_xml).root) + invite = muc_user.invite + invite.to.must_equal 'hecate@shakespeare.lit' + invite.from.must_equal 'crone1@shakespeare.lit/desktop' + invite.reason.must_equal 'Hey Hecate, this is the place for all good witches!' + end + + it "must be able to set the to jid" do + muc_user = Blather::Stanza::Presence::MUCUser.new + invite = muc_user.invite + invite.to.must_equal nil + invite.to = 'foo@bar.com' + invite.to.must_equal 'foo@bar.com' + end + + it "must be able to set the from jid" do + muc_user = Blather::Stanza::Presence::MUCUser.new + invite = muc_user.invite + invite.from.must_equal nil + invite.from = 'foo@bar.com' + invite.from.must_equal 'foo@bar.com' + end + + it "must be able to set the reason" do + muc_user = Blather::Stanza::Presence::MUCUser.new + invite = muc_user.invite + invite.reason.must_equal '' + invite.reason = 'Please join' + invite.reason.must_equal 'Please join' + end + end end