sprsquish-blather 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.1 Initial release (birth!)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Jeff Smick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Blather
2
+
3
+ An evented XMPP library
data/blather.gemspec ADDED
@@ -0,0 +1,73 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.rubyforge_project = 'squishtech'
5
+
6
+ s.name = 'blather'
7
+ s.description = 'An evented XMPP library written on EventMachine and libxml-ruby'
8
+ s.summary = 'Evented XMPP library'
9
+ s.version = '0.1'
10
+ s.date = '2008-11-17'
11
+
12
+ s.authors = ['Jeff Smick']
13
+ s.email = 'jeff.smick@squishtech.com'
14
+
15
+ s.files = %w[
16
+ README.rdoc
17
+ CHANGELOG
18
+ blather.gemspec
19
+ examples/echo.rb
20
+ examples/shell_client.rb
21
+ lib/blather/callback.rb
22
+ lib/blather/client.rb
23
+ lib/blather/core/errors.rb
24
+ lib/blather/core/jid.rb
25
+ lib/blather/core/roster.rb
26
+ lib/blather/core/roster_item.rb
27
+ lib/blather/core/stanza.rb
28
+ lib/blather/core/stanzas/error.rb
29
+ lib/blather/core/stanzas/iq.rb
30
+ lib/blather/core/stanzas/iqs/queries/roster.rb
31
+ lib/blather/core/stanzas/iqs/query.rb
32
+ lib/blather/core/stanzas/message.rb
33
+ lib/blather/core/stanzas/presence.rb
34
+ lib/blather/core/stanzas/presences/status.rb
35
+ lib/blather/core/stanzas/presences/subscription.rb
36
+ lib/blather/core/stream.rb
37
+ lib/blather/core/streams/parser.rb
38
+ lib/blather/core/streams/resource.rb
39
+ lib/blather/core/streams/sasl.rb
40
+ lib/blather/core/streams/session.rb
41
+ lib/blather/core/streams/tls.rb
42
+ lib/blather/core/sugar.rb
43
+ lib/blather/core/xmpp_node.rb
44
+ lib/blather/extensions/last_activity.rb
45
+ lib/blather/extensions/version.rb
46
+ lib/blather.rb
47
+ LICENSE
48
+ ]
49
+
50
+ s.test_files = %w[
51
+ lib/autotest/discover.rb
52
+ lib/autotest/spec.rb
53
+ spec/blather/core/jid_spec.rb
54
+ spec/blather/core/roster_item_spec.rb
55
+ spec/blather/core/roster_spec.rb
56
+ spec/blather/core/stanza_spec.rb
57
+ spec/blather/core/stream_spec.rb
58
+ spec/blather/core/xmpp_node_spec.rb
59
+ spec/spec_helper.rb
60
+ ]
61
+
62
+ s.extra_rdoc_files = %w[
63
+ README.rdoc
64
+ CHANGELOG
65
+ LICENSE
66
+ ]
67
+
68
+ s.has_rdoc = true
69
+ s.rdoc_options = %w[--line-numbers --inline-source --title Blather --main README]
70
+
71
+ s.add_dependency('eventmachine', ['> 0.0.0'])
72
+ s.add_dependency('libxml', ['> 0.0.0'])
73
+ end
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { 'spec' }
@@ -0,0 +1,60 @@
1
+ require 'autotest'
2
+
3
+ Autotest.add_hook :initialize do |at|
4
+ at.clear_mappings
5
+ # watch out: Ruby bug (1.8.6):
6
+ # %r(/) != /\//
7
+ at.add_mapping(%r%^spec/.*_spec.rb$%) { |filename, _|
8
+ filename
9
+ }
10
+ at.add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
11
+ ["spec/#{m[1]}_spec.rb"]
12
+ }
13
+ at.add_mapping(%r%^spec/(spec_helper|shared/.*)\.rb$%) {
14
+ at.files_matching %r%^spec/.*_spec\.rb$%
15
+ }
16
+ end
17
+
18
+ BAR = "=" * 78
19
+ REDCODE = 31
20
+ GREENCODE = 32
21
+
22
+ Autotest.add_hook :ran_command do |at|
23
+ at.results.last =~ /^.* (\d+) failures, (\d+) errors/
24
+
25
+ code = ($1 == "0" and $2 == "0") ? GREENCODE : REDCODE
26
+ puts "\e[#{ code }m#{ BAR }\e[0m\n\n"
27
+ end
28
+
29
+ class Autotest::Spec < Autotest
30
+ def path_to_classname(s)
31
+ sep = File::SEPARATOR
32
+ f = s.sub(/spec#{sep}/, '').sub(/(spec)?\.rb$/, '').split(sep)
33
+ f = f.map { |path| path.split(/_|(\d+)/).map { |seg| seg.capitalize }.join }
34
+ f = f.delete_if { |path| path == 'Core' }
35
+ f.join
36
+ end
37
+
38
+ ##
39
+ # Returns a hash mapping a file name to the known failures for that
40
+ # file.
41
+
42
+ def consolidate_failures(failed)
43
+ filters = new_hash_of_arrays
44
+
45
+ class_map = Hash[*self.find_order.grep(/^spec/).map { |f| # TODO: ugly
46
+ [path_to_classname(f), f]
47
+ }.flatten]
48
+ class_map.merge!(self.extra_class_map)
49
+
50
+ failed.each do |method, klass|
51
+ if class_map.has_key? klass then
52
+ filters[class_map[klass]] << method
53
+ else
54
+ output.puts "Unable to map class #{klass} to a file"
55
+ end
56
+ end
57
+
58
+ return filters
59
+ end
60
+ end
@@ -0,0 +1,24 @@
1
+ module Blather
2
+
3
+ class Callback
4
+ include Comparable
5
+
6
+ attr_accessor :priority
7
+
8
+ def initialize(priority = 0, &callback)
9
+ @priority = priority
10
+ @callback = callback
11
+ end
12
+
13
+ def call(*args)
14
+ @callback.call(*args)
15
+ end
16
+
17
+ # Favor higher numbers
18
+ def <=>(o)
19
+ self.priority <=> o.priority
20
+ end
21
+
22
+ end #Callback
23
+
24
+ end
@@ -0,0 +1,81 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. blather])
2
+
3
+ module Blather
4
+
5
+ class Client
6
+ @@callbacks = {}
7
+ @@status = nil
8
+
9
+ attr_accessor :jid,
10
+ :roster
11
+
12
+ def send_data(data)
13
+ @stream.send data
14
+ end
15
+
16
+ def status
17
+ @@status
18
+ end
19
+
20
+ def set_status(state = nil, msg = nil, to = nil)
21
+ status = Presence::Status.new state, msg
22
+ status.to = to
23
+ @@status = status unless to
24
+
25
+ send_data status
26
+ end
27
+
28
+ def stream_started(stream)
29
+ @stream = stream
30
+ retreive_roster
31
+ end
32
+
33
+ def call(stanza)
34
+ stanza.callback_heirarchy.each { |type| break if callback(type, stanza) }
35
+ end
36
+
37
+ # Default response to an Iq 'get' or 'set' is 'service-unavailable'/'cancel'
38
+ def receive_iq(iq)
39
+ send_data(ErrorStanza.new_from(iq, 'service-unavailable', 'cancel').reply!) if [:set, :get].include?(iq.type)
40
+ end
41
+
42
+ def receive_roster(node)
43
+ if !@roster && node.type == :result
44
+ self.roster = Roster.new(@stream, node)
45
+ register_callback(:status, -128) { |_, status| roster[status.from].status = status if roster[status.from]; false }
46
+ set_status
47
+ elsif node.type == :set
48
+ roster.process node
49
+ end
50
+ end
51
+
52
+ def self.register_callback(type, priority = 0, &callback)
53
+ @@callbacks[type] ||= []
54
+ @@callbacks[type] << Callback.new(priority, &callback)
55
+ @@callbacks[type].sort!
56
+ end
57
+
58
+ def register_callback(type, priority = 0, &callback)
59
+ self.class.register_callback(type, priority = 0, &callback)
60
+ end
61
+
62
+ def self.status(state = nil, msg = nil)
63
+ @@status = Presence::Status.new state, msg
64
+ end
65
+
66
+ def callback(type, stanza)
67
+ complete = false
68
+ (@@callbacks[type] || []).each { |callback| break if complete = callback.call(self, stanza) }
69
+
70
+ method = "receive_#{type}"
71
+ complete = __send__(method, stanza) if !complete && respond_to?(method)
72
+ complete
73
+ end
74
+
75
+ def retreive_roster
76
+ send_data Iq::Roster.new
77
+ end
78
+
79
+ end #Client
80
+
81
+ end
@@ -0,0 +1,24 @@
1
+ module Blather
2
+ # Main error class
3
+ class BlatherError < StandardError; end
4
+
5
+ # Stream errors
6
+ class StreamError < BlatherError
7
+ attr_accessor :type, :text
8
+
9
+ def initialize(node)
10
+ @type = node.detect { |n| n.name != 'text' && n['xmlns'] == 'urn:ietf:params:xml:ns:xmpp-streams' }
11
+ @text = node.detect { |n| n.name == 'text' }
12
+
13
+ @extra = node.detect { |n| n['xmlns'] != 'urn:ietf:params:xml:ns:xmpp-streams' }
14
+ end
15
+
16
+ def to_s
17
+ "Stream Error (#{type.name}) #{"[#{@extra.name}]" if @extra}: #{text.content if text}"
18
+ end
19
+ end
20
+
21
+ # Stanza errors
22
+ class StanzaError < StandardError; end
23
+ class ArgumentError < StanzaError; end
24
+ end
@@ -0,0 +1,105 @@
1
+ # =XMPP4R - XMPP Library for Ruby
2
+ # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
3
+ # Website::http://home.gna.org/xmpp4r/
4
+
5
+ module Blather
6
+ ##
7
+ # The JID class represents a Jabber Identifier as described by
8
+ # RFC3920 section 3.1.
9
+ #
10
+ # Note that you can use JIDs also for Sorting, Hash keys, ...
11
+ class JID
12
+ include Comparable
13
+
14
+ PATTERN = /^(?:([^@]*)@)??([^@\/]*)(?:\/(.*?))?$/
15
+
16
+ begin
17
+ require 'idn'
18
+ USE_STRINGPREP = true
19
+ rescue LoadError
20
+ USE_STRINGPREP = false
21
+ end
22
+
23
+ # Get the JID's node
24
+ attr_reader :node
25
+
26
+ # Get the JID's domain
27
+ attr_reader :domain
28
+
29
+ # Get the JID's resource
30
+ attr_reader :resource
31
+
32
+ def self.new(node, domain = nil, resource = nil)
33
+ node.is_a?(JID) ? node : super
34
+ end
35
+
36
+ ##
37
+ # Create a new JID. If called as new('a@b/c'), parse the string and
38
+ # split (node, domain, resource)
39
+ def initialize(node, domain = nil, resource = nil)
40
+ @resource = resource
41
+ @domain = domain
42
+ @node = node
43
+
44
+ if @domain.nil? && @resource.nil?
45
+ @node, @domain, @resource = @node.to_s.scan(PATTERN).first
46
+ end
47
+
48
+ if USE_STRINGPREP
49
+ @node = IDN::Stringprep.nodeprep(@node) if @node
50
+ @domain = IDN::Stringprep.nameprep(@domain) if @domain
51
+ @resource = IDN::Stringprep.resourceprep(@resource) if @resource
52
+ else
53
+ @node.downcase! if @node
54
+ @domain.downcase! if @domain
55
+ end
56
+
57
+ raise ArgumentError, 'Node too long' if (@node || '').length > 1023
58
+ raise ArgumentError, 'Domain too long' if (@domain || '').length > 1023
59
+ raise ArgumentError, 'Resource too long' if (@resource || '').length > 1023
60
+ end
61
+
62
+ ##
63
+ # Returns a string representation of the JID
64
+ # * ""
65
+ # * "domain"
66
+ # * "node@domain"
67
+ # * "domain/resource"
68
+ # * "node@domain/resource"
69
+ def to_s
70
+ s = @domain
71
+ s = "#{@node}@#{s}" if @node
72
+ s = "#{s}/#{@resource}" if @resource
73
+ return s
74
+ end
75
+
76
+ ##
77
+ # Returns a new JID with resource removed.
78
+ # return:: [JID]
79
+ def stripped
80
+ self.class.new @node, @domain
81
+ end
82
+
83
+ ##
84
+ # Removes the resource (sets it to nil)
85
+ # return:: [JID] self
86
+ def strip!
87
+ @resource = nil
88
+ self
89
+ end
90
+
91
+ ##
92
+ # Compare two JIDs,
93
+ # helpful for sorting etc.
94
+ #
95
+ # String representations are compared, see JID#to_s
96
+ def <=>(o)
97
+ to_s <=> o.to_s
98
+ end
99
+
100
+ # Test id jid is strepped
101
+ def stripped?
102
+ @resource.nil?
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,62 @@
1
+ module Blather
2
+
3
+ class Roster
4
+ include Enumerable
5
+
6
+ def initialize(stream, stanza = nil)
7
+ @stream = stream
8
+ @items = {}
9
+ stanza.items.each { |i| push i, false } if stanza
10
+ end
11
+
12
+ def process(stanza)
13
+ stanza.items.each do |i|
14
+ case i.subscription
15
+ when :remove then @items.delete(key(i.jid))
16
+ else @items[key(i.jid)] = RosterItem.new(i)
17
+ end
18
+ end
19
+ end
20
+
21
+ def <<(elem)
22
+ push elem
23
+ self
24
+ end
25
+
26
+ def push(elem, send = true)
27
+ jid = elem.respond_to?(:jid) ? elem.jid : JID.new(elem)
28
+ @items[key(jid)] = node = RosterItem.new(elem)
29
+
30
+ @stream.send_data(node.to_stanza(:set)) if send
31
+ end
32
+ alias_method :add, :push
33
+
34
+ def delete(jid)
35
+ @items.delete key(jid)
36
+ @stream.send_data Stanza::Iq::Roster.new(:set, Stanza::Iq::Roster::RosterItem.new(jid, nil, :remove))
37
+ end
38
+ alias_method :remove, :delete
39
+
40
+ def [](jid)
41
+ items[key(jid)]
42
+ end
43
+
44
+ def each(&block)
45
+ items.each &block
46
+ end
47
+
48
+ def items
49
+ @items.dup
50
+ end
51
+
52
+ private
53
+ def self.key(jid)
54
+ JID.new(jid).stripped.to_s
55
+ end
56
+
57
+ def key(jid)
58
+ self.class.key(jid)
59
+ end
60
+ end #Roster
61
+
62
+ end
@@ -0,0 +1,64 @@
1
+ module Blather
2
+
3
+ class RosterItem
4
+ attr_reader :jid,
5
+ :ask,
6
+ :statuses
7
+
8
+ attr_accessor :name,
9
+ :groups
10
+
11
+ def initialize(item)
12
+ @statuses = []
13
+
14
+ if item.is_a?(JID)
15
+ self.jid = item.stripped
16
+ elsif item.is_a?(XMPPNode)
17
+ self.jid = JID.new(item['jid']).stripped
18
+ self.name = item['name']
19
+ self.subscription = item['subscription']
20
+ self.ask = item['ask']
21
+ item.groups.each { |g| self.groups << g }
22
+ end
23
+ end
24
+
25
+ def jid=(jid)
26
+ @jid = JID.new(jid).stripped
27
+ end
28
+
29
+ VALID_SUBSCRIPTION_TYPES = [:both, :from, :none, :remove, :to].freeze
30
+ def subscription=(sub)
31
+ raise ArgumentError, "Invalid Type (#{sub}), use: #{VALID_SUBSCRIPTION_TYPES*' '}" if
32
+ sub && !VALID_SUBSCRIPTION_TYPES.include?(sub = sub.to_sym)
33
+ @subscription = sub ? sub : :none
34
+ end
35
+
36
+ def subscription
37
+ @subscription || :none
38
+ end
39
+
40
+ def ask=(ask)
41
+ raise ArgumentError, "Invalid Type (#{ask}), use: #{VALID_SUBSCRIPTION_TYPES*' '}" if ask && (ask = ask.to_sym) != :subscribe
42
+ @ask = ask ? ask : nil
43
+ end
44
+
45
+ def status=(presence)
46
+ @statuses.delete_if { |s| s.from == presence.from }
47
+ @statuses << presence
48
+ @statuses.sort!
49
+ end
50
+
51
+ def status(resource = nil)
52
+ top = resource ? @statuses.detect { |s| s.from.resource == resource } : @statuses.first
53
+ end
54
+
55
+ def to_stanza(type = nil)
56
+ r = Stanza::Iq::Roster.new type
57
+ n = Stanza::Iq::Roster::RosterItem.new jid, name, subscription, ask
58
+ r.query << n
59
+ n.groups = groups
60
+ r
61
+ end
62
+ end #RosterItem
63
+
64
+ end
@@ -0,0 +1,90 @@
1
+ module Blather
2
+ class Stanza < XMPPNode
3
+ @@registered_callbacks = []
4
+
5
+ def self.registered_callbacks
6
+ @@registered_callbacks
7
+ end
8
+
9
+ class_inheritable_array :callback_heirarchy
10
+
11
+ def self.register(callback_type, name = nil, xmlns = nil)
12
+ @@registered_callbacks << callback_type
13
+
14
+ self.callback_heirarchy ||= []
15
+ self.callback_heirarchy.unshift callback_type
16
+
17
+ name = name || self.name || callback_type
18
+ super name, xmlns
19
+ end
20
+
21
+ def self.next_id
22
+ @@last_id ||= 0
23
+ @@last_id += 1
24
+ 'blather%04x' % @@last_id
25
+ end
26
+
27
+ def self.import(node)
28
+ self.new(node.element_name).inherit(node)
29
+ end
30
+
31
+ def self.new(elem_name = nil)
32
+ elem = super
33
+ elem.id = next_id
34
+ XML::Document.new.root = elem
35
+ elem
36
+ end
37
+
38
+ def error?
39
+ self.type == :error
40
+ end
41
+
42
+ def reply
43
+ elem = self.copy(true)
44
+ elem.to, elem.from = self.from, self.to
45
+ elem
46
+ end
47
+
48
+ def reply!
49
+ self.to, self.from = self.from, self.to
50
+ self
51
+ end
52
+
53
+ def id=(id)
54
+ attributes.remove :id
55
+ self['id'] = id if id
56
+ end
57
+
58
+ def id
59
+ self['id']
60
+ end
61
+
62
+ def to=(to)
63
+ attributes.remove :to
64
+ self['to'] = to.to_s if to
65
+ end
66
+
67
+ def to
68
+ JID.new(self['to']) if self['to']
69
+ end
70
+
71
+ def from=(from)
72
+ attributes.remove :from
73
+ self['from'] = from.to_s if from
74
+ end
75
+
76
+ def from
77
+ JID.new(self['from']) if self['from']
78
+ end
79
+
80
+ def type=(type)
81
+ attributes.remove :type
82
+ self['type'] = type.to_s
83
+ end
84
+
85
+ def type
86
+ self['type'].to_sym if self['type']
87
+ end
88
+
89
+ end
90
+ end