File: delay.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 (99 lines) | stat: -rw-r--r-- 2,302 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
# =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'
require 'time'

module Jabber
  module Delay
    ##
    # Implementation of JEP 0091
    # for <x xmlns='jabber:x:delay' stamp='...' .../>
    # applied on <message/> and <presence/> stanzas
    #
    # One may also use XDelay#text for a descriptive reason
    # for the delay.
    #
    # Please note that you must require 'xmpp4r/xdelay' to use
    # this class as it's not required by a basic XMPP implementation.
    # <x/> elements with the specific namespace will then be
    # converted to XDelay automatically.
    class XDelay < X
      name_xmlns 'x', 'jabber:x:delay'

      ##
      # Initialize a new XDelay element
      #
      # insertnow:: [Boolean] Set the stamp to [Time::now]
      def initialize(insertnow=true)
        super()

        if insertnow
          set_stamp(Time.now)
        end
      end

      ##
      # Get the timestamp
      # result:: [Time] or nil
      def stamp
        if attributes['stamp']
          begin
            # Actually this should be Time.xmlschema,
            # but "unfortunately, the 'jabber:x:delay' namespace predates" JEP 0082
            Time.parse("#{attributes['stamp']}Z")
          rescue ArgumentError
            nil
          end
        else
          nil
        end
      end

      ##
      # Set the timestamp
      # t:: [Time] or nil
      def stamp=(t)
        if t.nil?
          attributes['stamp'] = nil
        else
          attributes['stamp'] = t.strftime("%Y%m%dT%H:%M:%S")
        end
      end

      ##
      # Set the timestamp (chaining-friendly)
      def set_stamp(t)
        self.stamp = t
        self
      end

      ##
      # Get the timestamp's origin
      # result:: [JID]
      def from
        if attributes['from']
          JID.new(attributes['from'])
        else
          nil
        end
      end

      ##
      # Set the timestamp's origin
      # jid:: [JID]
      def from=(jid)
        attributes['from'] = jid.nil? ? nil : jid.to_s
      end

      ##
      # Set the timestamp's origin (chaining-friendly)
      def set_from(jid)
        self.from = jid
        self
      end
    end
  end
end