em-xmpp 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  $LOAD_PATH.unshift './lib'
2
- require 'em-xmpp'
2
+ require 'em-xmpp/connection'
3
3
  require 'em-xmpp/helpers'
4
4
 
5
5
  if ARGV.empty?
@@ -9,11 +9,14 @@ end
9
9
 
10
10
  jid = ARGV.first
11
11
  pass = ARGV[1]
12
- certdir = ARGV[2]
12
+ server = ARGV[2]
13
+ port = ARGV[3]
14
+ certdir = ARGV[4]
13
15
 
14
16
  module RosterClient
15
17
  attr_reader :roster
16
18
 
19
+ include EM::Xmpp::XmlParser::Nokogiri
17
20
  include EM::Xmpp::Helpers
18
21
 
19
22
  def ready
@@ -84,5 +87,5 @@ end
84
87
  cfg = {:certificates => certdir}
85
88
 
86
89
  EM.run do
87
- conn = EM::Xmpp::Connection.start(jid, pass, RosterClient, cfg)
90
+ conn = EM::Xmpp::Connection.start(jid, pass, RosterClient, cfg, server, port)
88
91
  end
@@ -0,0 +1,90 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'em-xmpp/non-em'
3
+ require 'em-xmpp/helpers'
4
+
5
+ if ARGV.empty?
6
+ puts "usage: #{__FILE__} <jid> [<pass>] [certificates-dir]"
7
+ exit 0
8
+ end
9
+
10
+ jid = ARGV.first
11
+ pass = ARGV[1]
12
+ server = ARGV[2]
13
+ port = ARGV[3]
14
+ certdir = ARGV[4]
15
+
16
+ module RosterClient
17
+ attr_reader :roster
18
+
19
+ include EM::Xmpp::XmlParser::Nokogiri
20
+ include EM::Xmpp::Helpers
21
+
22
+ def ready
23
+ super #setup helpers
24
+ puts "***** #{@jid} ready"
25
+
26
+ on_presence do |ctx|
27
+ presence = ctx.bit(:presence)
28
+
29
+ if presence.subscription_request?
30
+ puts "**** accepting subscription from #{presence.from}"
31
+ send_stanza presence.reply('type'=>'subscribed')
32
+ presence.from.subscribe
33
+ presence.from.add_to_roster
34
+ else
35
+ puts "**** #{presence.from} is present"
36
+ end
37
+
38
+ ctx #returns a ctx for subsequent handlers if any
39
+ end
40
+
41
+ on_message do |ctx|
42
+ msg = ctx.bit :message
43
+
44
+ puts "**** message from #{msg.from}"
45
+
46
+ key = msg.from.to_s
47
+
48
+ conv = conversation(key)
49
+
50
+ if conv
51
+ conv.resume ctx
52
+ else
53
+ x = rand(300)
54
+ y = rand(300)
55
+ start_conversation(ctx, key) do |c|
56
+ rep = c.send_stanza(msg.reply{|xml| xml.body("how much is #{x} - #{y} ?")}, 5)
57
+ greeting = if rep.interrupted?
58
+ if rep.ctx.bit(:message).body == (x - y).to_s
59
+ "great!"
60
+ else
61
+ "wrong: #{x - y}"
62
+ end
63
+ else
64
+ "too slow, laggard"
65
+ end
66
+ self.send_stanza(msg.reply{|xml| xml.body(greeting)})
67
+ end
68
+ end
69
+
70
+ ctx #returns a ctx for subsequent handlers if any
71
+ end
72
+
73
+ on_exception(:anything) do |ctx|
74
+ p "rescued error"
75
+ raise ctx.env['error']
76
+ ctx
77
+ end
78
+
79
+ puts "***** friends list"
80
+ subscriptions = get_roster
81
+ subscriptions.each do |sub|
82
+ p sub.to_a
83
+ end
84
+ end
85
+ end
86
+
87
+ cfg = {:certificates => certdir}
88
+
89
+ bot = EM::Xmpp::NonEM::Connection.start(jid, pass, RosterClient, cfg, server, port)
90
+ bot.event_loop
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-xmpp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-26 00:00:00.000000000Z
12
+ date: 2013-09-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
16
- requirement: &2151825840 !ruby/object:Gem::Requirement
16
+ requirement: &2153173140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2151825840
24
+ version_requirements: *2153173140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &2151814740 !ruby/object:Gem::Requirement
27
+ requirement: &2153172720 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2151814740
35
+ version_requirements: *2153172720
36
+ - !ruby/object:Gem::Dependency
37
+ name: ox
38
+ requirement: &2153172300 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2153172300
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: ruby-sasl
38
- requirement: &2151812280 !ruby/object:Gem::Requirement
49
+ requirement: &2153171880 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,7 +54,7 @@ dependencies:
43
54
  version: '0'
44
55
  type: :runtime
45
56
  prerelease: false
46
- version_requirements: *2151812280
57
+ version_requirements: *2153171880
47
58
  description: XMPP client for event machine
48
59
  email:
49
60
  - crapooze@gmail.com
@@ -61,21 +72,26 @@ files:
61
72
  - em-xmpp.gemspec
62
73
  - lib/em-xmpp.rb
63
74
  - lib/em-xmpp/cert_store.rb
75
+ - lib/em-xmpp/component.rb
64
76
  - lib/em-xmpp/connection.rb
65
- - lib/em-xmpp/connector.rb
66
77
  - lib/em-xmpp/context.rb
67
78
  - lib/em-xmpp/conversation.rb
68
79
  - lib/em-xmpp/entity.rb
80
+ - lib/em-xmpp/evented.rb
69
81
  - lib/em-xmpp/handler.rb
70
82
  - lib/em-xmpp/helpers.rb
71
83
  - lib/em-xmpp/jid.rb
72
84
  - lib/em-xmpp/namespaces.rb
73
85
  - lib/em-xmpp/nodes.rb
86
+ - lib/em-xmpp/non-em.rb
74
87
  - lib/em-xmpp/resolver.rb
75
88
  - lib/em-xmpp/stanza_handler.rb
76
89
  - lib/em-xmpp/stanza_matcher.rb
77
90
  - lib/em-xmpp/version.rb
91
+ - lib/em-xmpp/xml_builder.rb
92
+ - lib/em-xmpp/xml_parser.rb
78
93
  - samples/hello.rb
94
+ - samples/non-em-hello.rb
79
95
  homepage: https://github.com/crapooze/em-xmpp
80
96
  licenses: []
81
97
  post_install_message:
@@ -1,244 +0,0 @@
1
-
2
- require 'nokogiri'
3
- require 'eventmachine'
4
- require 'em-xmpp/context'
5
- require 'em-xmpp/namespaces'
6
- require 'em-xmpp/resolver'
7
-
8
- module EM::Xmpp
9
- module Connector
10
- include Namespaces
11
-
12
- #XML SAX document which delegates its method to a recipient object
13
- class ForwardingDocument < Nokogiri::XML::SAX::Document
14
- attr_accessor :recipient
15
- %w{xmldecl start_document end_document start_element_namespace end_element characters
16
- comment warning error cdata_block}.each do |meth|
17
- meth2 = "xml_#{meth}"
18
- define_method(meth) do |*args|
19
- recipient.send(meth2, *args) if recipient
20
- end
21
- end
22
- end
23
-
24
- def self.included(obj)
25
- obj.extend ClassMethods
26
- end
27
-
28
- module ClassMethods
29
- def start(jid, pass=nil, mod=nil, cfg={}, server=nil, port=5222, &blk)
30
- jid = JID.parse jid
31
- if server.nil?
32
- record = Resolver.resolve jid.domain
33
- if record
34
- server = record.target.to_s
35
- port = record.port
36
- else
37
- server = jid.domain
38
- end
39
- end
40
-
41
- EM.connect(server, port, self, jid, pass, mod, cfg, &blk)
42
- end
43
- end
44
-
45
- extend ClassMethods
46
-
47
- def send_raw(data)
48
- puts ">> out\n#{data}\n" if $DEBUG
49
- send_data data
50
- end
51
-
52
- def send_xml(&blk)
53
- data = build_xml(&blk)
54
- send_raw data
55
- end
56
-
57
- def restart_xml_stream
58
- @xml_parser.document.recipient = nil
59
- post_init
60
- end
61
-
62
- def post_init
63
- doc = ForwardingDocument.new
64
- doc.recipient = self
65
- @xml_parser = Nokogiri::XML::SAX::PushParser.new doc
66
- @stack = []
67
- @stanza = nil
68
- @streamdoc = nil
69
-
70
- open_xml_stream
71
- end
72
-
73
- def receive_data(dat)
74
- puts "<< in\n#{dat}\n" if $DEBUG
75
- @xml_parser << dat
76
- end
77
-
78
- def unbind
79
- puts "**** unbound ****" if $DEBUG
80
- end
81
-
82
- def build_xml(&blk)
83
- n = Nokogiri::XML::Builder.new(&blk)
84
- n.doc.root.to_xml
85
- end
86
-
87
- private
88
-
89
- def open_xml_stream_tag
90
- domain = @jid.domain
91
- version = '1.0'
92
- lang = 'en'
93
- start_stream = <<-STREAM
94
- <stream:stream
95
- to='#{domain}'
96
- version='#{version}'
97
- xml:lang='#{lang}'
98
- xmlns='#{Client}'
99
- xmlns:stream='#{Stream}'
100
- >
101
- STREAM
102
- end
103
-
104
- def close_xml_stream_tag
105
- '</stream:stream>'
106
- end
107
-
108
- def open_xml_stream
109
- send_raw open_xml_stream_tag
110
- end
111
-
112
- def close_xml_stream
113
- send_raw close_xml_stream_tag
114
- end
115
-
116
- ### XML world
117
-
118
- def xml_xmldecl(version,encoding,standalone)
119
- end
120
-
121
- def xml_start_document
122
- #XXX set namespaces and stream prefix
123
- # namespace may depend on the type of connection ('jabber:client' or
124
- # 'jabber:server')
125
- # currently we do not set any stream's namespace, hence when builidng stanza,
126
- # we must explicitely avoid writing the namespace of iq/presence/message XML nodes
127
- @streamdoc = Nokogiri::XML::Document.new
128
- end
129
-
130
- def xml_end_document
131
- @streamdoc = @stanza = @stack = @xml_parser = nil
132
- end
133
-
134
- def xml_start_element_namespace(name, attrs=[],prefix=nil,uri=nil,ns=[])
135
- node = Nokogiri::XML::Node.new(name, @streamdoc)
136
- attrs.each do |attr|
137
- #attr is a Struct with members localname/prefix/uri/value
138
- node[attr.localname] = attr.value
139
- end
140
- #XXX - if prefix is there maybe we do not want to set uri as default
141
- node.default_namespace = uri if uri
142
-
143
- ns.each do |pfx,href|
144
- node.add_namespace_definition pfx, href
145
- end
146
-
147
- # puts "starting: #{name}, stack:#{@stack.size}" if $DEBUG
148
- case @stack.size
149
- when 0 #the streaming tag starts
150
- stream_support(node)
151
- when 1 #a stanza starts
152
- set_current_stanza!(node)
153
- stanza_start node
154
- else
155
- @stack.last.add_child node
156
- end
157
-
158
- @stack << node
159
- end
160
-
161
- def xml_end_element(name)
162
- node = @stack.pop
163
- #puts "ending: #{name}, stack:#{@stack.size}" if $DEBUG
164
-
165
- case @stack.size
166
- when 0 #i.e., the stream support ends
167
- xml_stream_closing
168
- when 1 #i.e., we've finished a stanza
169
- raise RuntimeError, "should end on a stanza" unless node == @stanza
170
- stanza_end node
171
- else
172
- #the stanza keeps growing
173
- end
174
- end
175
-
176
- def xml_characters(txt)
177
- @stack.last << Nokogiri::XML::Text.new(txt, @streamdoc)
178
- end
179
-
180
- def xml_error(err)
181
- #raise RuntimeError, err
182
- end
183
-
184
- def xml_stream_closing
185
- close_xml_stream
186
- close_connection
187
- end
188
-
189
- def xml_comment(comm)
190
- raise NotImplementedError
191
- end
192
-
193
- def xml_warning(warn)
194
- raise NotImplementedError
195
- end
196
-
197
- def xml_cdata_block(data)
198
- raise NotImplementedError
199
- end
200
-
201
- ### XMPP World
202
-
203
- def stream_support(node)
204
- @stanza = Nokogiri::XML::Node.new('dummy', @streamdoc)
205
- node << @stanza
206
-
207
- @streamdoc.root = node
208
- end
209
-
210
- def set_current_stanza!(node)
211
- @stanza.remove
212
-
213
- @stanza = node
214
- @streamdoc.root << @stanza
215
- end
216
-
217
- def stanza_start(node)
218
- raise NotImplementedError
219
- end
220
-
221
- def stanza_end(node)
222
- raise NotImplementedError
223
- end
224
-
225
- public
226
-
227
- ### TLS World
228
-
229
- def ask_for_tls
230
- send_xml do |x|
231
- x.starttls(:xmlns => TLS)
232
- end
233
- end
234
-
235
- def start_using_tls_and_reset_stream
236
- start_tls(:verify_peer => false)
237
- restart_xml_stream
238
- end
239
-
240
- def ssl_verify_peer(pem)
241
- raise NotImplementedError
242
- end
243
- end
244
- end