File: contact.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 (61 lines) | stat: -rw-r--r-- 1,558 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
# =XMPP4R - XMPP Library for Ruby
#
# This file's copyright (c) 2009 by Pablo Lorenzzoni <pablo@propus.com.br>
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://xmpp4r.github.io
#
module Jabber
class Observable
  # Jabber::Observable::Contact - Convenience subclass to deal with Contacts
  class Contact

    # Creates a new Jabber::Observable::Contact
    #
    # jid:: jid to be used
    # observable:: observable to be used
    def initialize(jid, observable)
      @jid = jid.respond_to?(:resource) ? jid : JID.new(jid)
      @observable = observable
    end

    # Returns the stripped version of the JID
    def jid; @jid.strip; end

    def inspect #:nodoc:
      sprintf("#<%s:0x%x @jid=%s>", self.class.name, __id__, @jid.to_s)
    end

    # Are e subscribed?
    def subscribed?
      [:to, :both].include?(subscription)
    end

    # Get the subscription from the roster_item
    def subscription
      items = @observable.roster.items
      return false unless items.include?(jid)
      items[jid].subscription
    end

    # Send a request asking for authorization
    def ask_for_authorization!
      request!(:subscribe)
    end

    # Sends a request asking for unsubscription
    def unsubscribe!
      request!(:unsubscribe)
    end

    private

    # Really send the request.
    def request!(type)
      request = Jabber::Presence.new.set_type(type)
      request.to = jid
      @observable.send!(request)
    end

  end # of class Contact
end # of class Observable
end # of module Jabber