em-xmpp 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -104,7 +104,7 @@ module EM::Xmpp
104
104
  def send_stanza(stanza)
105
105
  send_raw stanza.xml
106
106
  if block_given?
107
- on(:anything) do |ctx|
107
+ upon(:anything) do |ctx|
108
108
  if ctx.id == stanza.params['id']
109
109
  yield ctx
110
110
  ctx.delete_xpath_handler!
@@ -115,7 +115,7 @@ module EM::Xmpp
115
115
  end
116
116
  end
117
117
 
118
- %w{on on_exception on_presence on_iq on_message}.each do |meth|
118
+ %w{upon on on_exception on_presence on_iq on_message on_decorator on_iq_decorator on_presence_decorator on_message_decorator}.each do |meth|
119
119
  define_method(meth) do |*args,&blk|
120
120
  @handler.send meth, *args, &blk
121
121
  end
@@ -30,7 +30,14 @@ module EM::Xmpp
30
30
  jid.to_s
31
31
  end
32
32
 
33
- #TODO: say, pub, sub, etc.
33
+ #TODO: pub, sub, etc.
34
+
35
+ def say(body)
36
+ msg = connection.message_stanza(:to => jid) do |x|
37
+ x.body body
38
+ end
39
+ connection.send_stanza msg
40
+ end
34
41
 
35
42
  def subscribe
36
43
  pres = connection.presence_stanza('to'=>jid.bare, 'type' => 'subscribe')
@@ -14,73 +14,81 @@ module EM::Xmpp
14
14
  def initialize(conn)
15
15
  @connection = conn
16
16
  @handlers = []
17
+ @decorator_handlers = []
17
18
  @exception_handlers = []
18
19
 
19
20
  stack_decorators
20
21
  end
21
22
 
22
- def on_presence(&blk)
23
- on('//xmlns:presence', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
24
- end
25
-
26
- def on_message(&blk)
27
- on('//xmlns:message', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
23
+ # wraps the stanza in a context and calls handle_context
24
+ def handle(stanza)
25
+ handle_context Context.new(@connection, stanza)
28
26
  end
29
27
 
30
- def on_iq(&blk)
31
- on('//xmlns:iq', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
32
- end
28
+ private
33
29
 
34
30
  def stack_decorators
35
- on_presence do |ctx|
31
+ on_presence_decorator do |ctx|
36
32
  ctx = ctx.with(:presence)
37
33
  ctx = ctx.with(:error) if ctx.error?
38
34
  ctx
39
35
  end
40
- on_message do |ctx|
36
+ on_message_decorator do |ctx|
41
37
  ctx = ctx.with(:message)
42
38
  ctx = ctx.with(:error) if ctx.error?
43
39
  ctx
44
40
  end
45
- on_iq do |ctx|
41
+ on_iq_decorator do |ctx|
46
42
  ctx = ctx.with(:iq)
47
43
  ctx = ctx.with(:error) if ctx.error?
48
44
  ctx
49
45
  end
50
- on('//xmlns:delay', 'xmlns' => Delay) do |ctx|
46
+ on_decorator('//xmlns:delay', 'xmlns' => Delay) do |ctx|
51
47
  ctx.with(:delay)
52
48
  end
53
- on('//xmlns:query', 'xmlns' => DiscoverInfos) do |ctx|
49
+ on_decorator('//xmlns:query', 'xmlns' => DiscoverInfos) do |ctx|
54
50
  ctx.with(:discoinfos)
55
51
  end
56
- on('//xmlns:query', 'xmlns' => DiscoverItems) do |ctx|
52
+ on_decorator('//xmlns:query', 'xmlns' => DiscoverItems) do |ctx|
57
53
  ctx.with(:discoitems)
58
54
  end
59
- on('//xmlns:query', 'xmlns' => Roster) do |ctx|
55
+ on_decorator('//xmlns:query', 'xmlns' => Roster) do |ctx|
60
56
  ctx.with(:roster)
61
57
  end
62
- on('//xmlns:command', 'xmlns' => Commands) do |ctx|
58
+ on_decorator('//xmlns:command', 'xmlns' => Commands) do |ctx|
63
59
  ctx.with(:command)
64
60
  end
65
- on('//xmlns:x', 'xmlns' => DataForms) do |ctx|
61
+ on_decorator('//xmlns:x', 'xmlns' => DataForms) do |ctx|
66
62
  ctx.with(:dataforms)
67
63
  end
68
- on('//xmlns:nick', 'xmlns' => Nick) do |ctx|
64
+ on_decorator('//xmlns:nick', 'xmlns' => Nick) do |ctx|
69
65
  ctx.with(:nickname)
70
66
  end
71
- on('//xmlns:x', 'xmlns' => MucUser) do |ctx|
67
+ on_decorator('//xmlns:x', 'xmlns' => MucUser) do |ctx|
72
68
  ctx.with(:mucuser)
73
69
  end
74
70
  end
75
71
 
72
+ def add_decorator_handler(handler)
73
+ @decorator_handlers << handler
74
+ end
75
+
76
76
  def add_handler(handler)
77
77
  @handlers << handler
78
78
  end
79
79
 
80
+ def add_handler_before_the_other_handlers(handler)
81
+ @handlers.unshift handler
82
+ end
83
+
80
84
  def add_exception_handler(handler)
81
85
  @exception_handlers << handler
82
86
  end
83
87
 
88
+ def remove_decorator_handler(handler)
89
+ @decorator_handlers.delete handler
90
+ end
91
+
84
92
  def remove_handler(handler)
85
93
  @handlers.delete handler
86
94
  end
@@ -94,6 +102,44 @@ module EM::Xmpp
94
102
  handler = StanzaHandler.new(matcher, blk)
95
103
  end
96
104
 
105
+ public
106
+
107
+ def on_presence(&blk)
108
+ on('//xmlns:presence', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
109
+ end
110
+
111
+ def on_message(&blk)
112
+ on('//xmlns:message', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
113
+ end
114
+
115
+ def on_iq(&blk)
116
+ on('//xmlns:iq', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
117
+ end
118
+
119
+ def on_presence_decorator(&blk)
120
+ on_decorator('//xmlns:presence', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
121
+ end
122
+
123
+ def on_message_decorator(&blk)
124
+ on_decorator('//xmlns:message', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
125
+ end
126
+
127
+ def on_iq_decorator(&blk)
128
+ on_decorator('//xmlns:iq', 'xmlns' => EM::Xmpp::Namespaces::Client, &blk)
129
+ end
130
+
131
+ def on_decorator(path, args={}, &blk)
132
+ handler = handler_for path, args, &blk
133
+ add_decorator_handler handler
134
+ handler
135
+ end
136
+
137
+ def upon(path, args={}, &blk)
138
+ handler = handler_for path, args, &blk
139
+ add_handler_before_the_other_handlers handler
140
+ handler
141
+ end
142
+
97
143
  def on(path, args={}, &blk)
98
144
  handler = handler_for path, args, &blk
99
145
  add_handler handler
@@ -106,11 +152,9 @@ module EM::Xmpp
106
152
  handler
107
153
  end
108
154
 
109
- # wraps the stanza in a context and calls handle_context
110
- def handle(stanza)
111
- handle_context Context.new(@connection, stanza)
112
- end
155
+ private
113
156
 
157
+ # runs all decorator_handlers against the stanza context so that the context has all needed methods
114
158
  # runs all handlers against the stanza context
115
159
  # catches all exception (in which case, the context gets passed to all
116
160
  # exception_handlers)
@@ -122,14 +166,15 @@ module EM::Xmpp
122
166
  # handlers such as request/responses
123
167
  def handle_context(ctx)
124
168
  catch :halt do
169
+ run_xpath_handlers ctx, @decorator_handlers.dup, :remove_decorator_handler
125
170
  run_xpath_handlers ctx, @handlers.dup, :remove_handler
126
171
  end
127
172
  rescue => err
128
173
  ctx['error'] = err
129
- run_xpath_handlers ctx, @exception_handlers, :remove_exception_handler
174
+ run_xpath_handlers ctx, @exception_handlers.dup, :remove_exception_handler
130
175
  end
131
176
 
132
- # runs all handlers and returns a list of handlers for the next stanza
177
+ # runs all handlers, calls the remover method if a handler should be removed
133
178
  def run_xpath_handlers(ctx, handlers, remover)
134
179
  handlers.each do |h|
135
180
  if (not ctx.done?) and (h.match?(ctx.stanza))
@@ -143,9 +188,10 @@ module EM::Xmpp
143
188
  end
144
189
 
145
190
  class XmppSASL < ::SASL::Preferences
146
- attr_accessor :handler
191
+ attr_accessor :handler, :authzid
147
192
  def initialize(handler)
148
193
  @handler = handler
194
+ @authzid = handler.jid.bare.to_s
149
195
  end
150
196
  def realm
151
197
  handler.jid.domain
@@ -162,6 +208,9 @@ module EM::Xmpp
162
208
  def password
163
209
  ret = handler.pass
164
210
  end
211
+ def allow_plaintext?
212
+ true
213
+ end
165
214
  end
166
215
 
167
216
  class Routine < Handler
@@ -252,10 +301,12 @@ module EM::Xmpp
252
301
  end
253
302
 
254
303
  on(:anything) do |ctx|
255
- @connection.unhandled ctx.stanza
304
+ @connection.unhandled_stanza ctx.stanza
256
305
  end
257
306
  end
258
307
 
308
+ private
309
+
259
310
  def extract_jid(stanza)
260
311
  jid = stanza.xpath('//bind:jid', {'bind' => Bind})
261
312
  jid.text if jid.any?
@@ -270,15 +321,26 @@ module EM::Xmpp
270
321
  end
271
322
 
272
323
  def start_session
273
- c.send_stanza(c.iq_stanza('type' => 'set', 'to' => jid.domain) do |x|
324
+ session_request = c.iq_stanza('type' => 'set', 'to' => jid.domain) do |x|
274
325
  x.session('xmlns' => Session)
275
- end)
326
+ end
327
+
328
+ c.send_stanza(session_request) do |ctx|
329
+ if ctx.type == 'result'
330
+ @connection.negotiation_finished
331
+ ctx.delete_xpath_handler!.done!
332
+ else
333
+ @connection.negotiation_failed(ctx)
334
+ end
335
+ end
276
336
  end
277
337
 
278
338
  def start_sasl(methods)
279
339
  @sasl = ::SASL.new(methods, XmppSASL.new(self))
280
340
  msg,val = sasl.start
281
341
  mech = sasl.mechanism
342
+ @sasl.preferences.authzid = nil if mech == "DIGEST-MD5"
343
+ val = Base64.strict_encode64(val) if val
282
344
  reply_sasl(msg,val,mech)
283
345
  end
284
346
 
@@ -1,5 +1,5 @@
1
1
  module Em
2
2
  module Xmpp
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
5
5
  end
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.7
4
+ version: 0.0.8
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: 2012-08-25 00:00:00.000000000Z
12
+ date: 2012-09-22 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
16
- requirement: &2155062040 !ruby/object:Gem::Requirement
16
+ requirement: &2157880740 !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: *2155062040
24
+ version_requirements: *2157880740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &2155061620 !ruby/object:Gem::Requirement
27
+ requirement: &2157880320 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2155061620
35
+ version_requirements: *2157880320
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ruby-sasl
38
- requirement: &2155061200 !ruby/object:Gem::Requirement
38
+ requirement: &2157879900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2155061200
46
+ version_requirements: *2157879900
47
47
  description: XMPP client for event machine
48
48
  email:
49
49
  - crapooze@gmail.com