Skip to content

Commit

Permalink
[CS] Factor out the common parts of the presence/message MUCUser classes
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Feb 28, 2012
1 parent 4ecd52e commit a301d75
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 80 deletions.
43 changes: 3 additions & 40 deletions lib/blather/stanza/message/muc_user.rb
@@ -1,29 +1,11 @@
require 'blather/stanza/muc/muc_user_base'

module Blather
class Stanza
class Message

class MUCUser < Message
register :muc_user, :x, "http://jabber.org/protocol/muc#user"

def self.new(*args)
new_node = super
new_node.muc_user
new_node
end

def inherit(node)
muc_user.remove
super
self
end

def password
find_password_node && password_node.content
end

def password=(var)
password_node.content = var
end
include Blather::Stanza::MUC::MUCUserBase

def invite?
!!find_invite_node
Expand All @@ -33,25 +15,6 @@ def invite_decline?
!!find_decline_node
end

def muc_user
unless muc_user = find_first('ns:x', :ns => self.class.registered_ns)
self << (muc_user = XMPPNode.new('x', self.document))
muc_user.namespace = self.class.registered_ns
end
muc_user
end

def password_node
unless pw = find_password_node
muc_user << (pw = XMPPNode.new('password', self.document))
end
pw
end

def find_password_node
muc_user.find_first 'ns:password', :ns => self.class.registered_ns
end

def invite
if invite = find_invite_node
Invite.new invite
Expand Down
53 changes: 53 additions & 0 deletions lib/blather/stanza/muc/muc_user_base.rb
@@ -0,0 +1,53 @@
module Blather
class Stanza
class MUC

module MUCUserBase
def self.included(klass)
klass.extend ClassMethods
klass.register :muc_user, :x, "http://jabber.org/protocol/muc#user"
end

module ClassMethods
def new(*args)
super.tap { |e| e.muc_user }
end
end

def inherit(node)
muc_user.remove
super
self
end

def password
find_password_node && password_node.content
end

def password=(var)
password_node.content = var
end

def muc_user
unless muc_user = find_first('ns:x', :ns => self.class.registered_ns)
self << (muc_user = XMPPNode.new('x', self.document))
muc_user.namespace = self.class.registered_ns
end
muc_user
end

def password_node
unless pw = find_password_node
muc_user << (pw = XMPPNode.new('password', self.document))
end
pw
end

def find_password_node
muc_user.find_first 'ns:password', :ns => self.class.registered_ns
end
end # MUCUserBase

end # MUC
end # Stanza
end # Blather
43 changes: 3 additions & 40 deletions lib/blather/stanza/presence/muc_user.rb
@@ -1,21 +1,11 @@
require 'blather/stanza/muc/muc_user_base'

module Blather
class Stanza
class Presence

class MUCUser < Status
register :muc_user, :x, "http://jabber.org/protocol/muc#user"

def self.new(*args)
new_node = super
new_node.muc_user
new_node
end

def inherit(node)
muc_user.remove
super
self
end
include Blather::Stanza::MUC::MUCUserBase

def affiliation
item.affiliation
Expand Down Expand Up @@ -52,22 +42,6 @@ def status_codes=(val)
end
end

def password
find_password_node && password_node.content
end

def password=(var)
password_node.content = var
end

def muc_user
unless muc_user = find_first('ns:x', :ns => self.class.registered_ns)
self << (muc_user = XMPPNode.new('x', self.document))
muc_user.namespace = self.class.registered_ns
end
muc_user
end

def item
if item = muc_user.find_first('ns:item', :ns => self.class.registered_ns)
Item.new item
Expand All @@ -83,17 +57,6 @@ def status
end
end

def password_node
unless pw = find_password_node
muc_user << (pw = XMPPNode.new('password', self.document))
end
pw
end

def find_password_node
muc_user.find_first 'ns:password', :ns => self.class.registered_ns
end

class Item < XMPPNode
def self.new(affiliation = nil, role = nil, jid = nil, document = nil)
new_node = super :item, document
Expand Down

0 comments on commit a301d75

Please sign in to comment.