File: roster.rb

package info (click to toggle)
ruby-xmpp4r 0.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,384 kB
  • sloc: ruby: 17,382; xml: 74; sh: 12; makefile: 4
file content (138 lines) | stat: -rwxr-xr-x 3,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# =XMPP4R - XMPP Library for Ruby
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://xmpp4r.github.io

require 'xmpp4r/x'
require 'xmpp4r/jid'

module Jabber
  module Roster
  ##
  # Implementation of JEP-0144
  # for <tt><x xmlns='http://jabber.org/protocol/rosterx'/></tt>
  # attached to <tt><message/></tt> stanzas
  #
  # Should be backwards compatible to JEP-0093,
  # as only action attribute of roster items are missing there.
  # Pay attention to the namespace which is <tt>jabber:x:roster</tt>
  # for JEP-0093!
    class XRoster < X
      name_xmlns 'x', 'jabber:x:roster'
    end #Class XRoster

    class RosterX < XRoster
      name_xmlns 'x', 'http://jabber.org/protocol/rosterx'
    end

    ##
    # Class containing an <item/> element
    #
    # The 'name' attribute has been renamed to 'iname' here
    # as 'name' is already used by REXML::Element for the
    # element's name. It's still name='...' in XML.
    #
    # This is all a bit analoguous to Jabber::RosterItem, used by
    # Jabber::IqQueryRoster. But this class lacks the subscription and
    # ask attributes.
    class XRosterItem < XMPPElement
      name_xmlns 'item', 'jabber:x:roster'

      ##
      # Construct a new roster item
      # jid:: [JID] Jabber ID
      # iname:: [String] Name in the roster
      def initialize(jid=nil, iname=nil)
        super()
        self.jid = jid
        self.iname = iname
      end

      ##
      # Get name of roster item
      #
      # names can be set by the roster's owner himself
      # return:: [String]
      def iname
        attributes['name']
      end

      ##
      # Set name of roster item
      # val:: [String] Name for this item
      def iname=(val)
        attributes['name'] = val
      end

      ##
      # Get JID of roster item
      # Resource of the JID will _not_ be stripped
      # return:: [JID]
      def jid
        JID.new(attributes['jid'])
      end

      ##
      # Set JID of roster item
      # val:: [JID] or nil
      def jid=(val)
        attributes['jid'] = val.nil? ? nil : val.to_s
      end

      ##
      # Get action for this roster item
      # * :add
      # * :modify
      # * :delete
      # result:: [Symbol] (defaults to :add according to JEP-0144)
      def action
        case attributes['action']
          when 'modify' then :modify
          when 'delete' then :delete
          else :add
        end
      end

      ##
      # Set action for this roster item
      # (see action)
      def action=(a)
        case a
          when :modify then attributes['action'] = 'modify'
          when :delete then attributes['action'] = 'delete'
          else attributes['action'] = 'add'
        end
      end

      ##
      # Get groups the item belongs to
      # result:: [Array] of [String] The groups
      def groups
        result = []
        each_element('group') { |group|
          result.push(group.text)
        }
        result
      end

      ##
      # Set groups the item belongs to,
      # deletes old groups first.
      #
      # See JEP 0083 for nested groups
      # ary:: [Array] New groups, duplicate values will be removed
      def groups=(ary)
        # Delete old group elements
        delete_elements('group')

        # Add new group elements
        ary.uniq.each { |group|
          add_element('group').text = group
        }
      end
    end #Class XRosterItem

    class RosterXItem < XRosterItem
      name_xmlns 'item', 'http://jabber.org/protocol/rosterx'
    end
  end #Module Roster
end #Module Jabber