em-xmpp 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -105,7 +105,7 @@ module EM::Xmpp
105
105
  send_raw stanza.xml
106
106
  if block_given?
107
107
  upon(:anything) do |ctx|
108
- if ctx.id == stanza.params['id']
108
+ if ctx.bit!(:stanza).id == stanza.params['id']
109
109
  yield ctx
110
110
  ctx.delete_xpath_handler!
111
111
  else
@@ -7,7 +7,7 @@ require 'ostruct'
7
7
 
8
8
  module EM::Xmpp
9
9
  class Context
10
- attr_reader :connection, :stanza, :env
10
+ attr_reader :connection, :stanza, :env, :bits
11
11
 
12
12
  def []key
13
13
  env[key]
@@ -40,6 +40,7 @@ module EM::Xmpp
40
40
  end
41
41
 
42
42
  def initialize(conn, stanza, env={})
43
+ @bits = {}
43
44
  @connection = conn
44
45
  @stanza = stanza
45
46
  @env = default_env.merge env
@@ -65,10 +66,18 @@ module EM::Xmpp
65
66
  end
66
67
 
67
68
  def with(modname)
69
+ if $DEBUG
70
+ $stderr.puts "using outdated <with> which is slow"
71
+ end
72
+ bit! modname
73
+ slow_with modname
74
+ end
75
+
76
+ def slow_with(modname)
68
77
  mod = if modname.is_a?(Module)
69
78
  modname
70
79
  else
71
- self.class.const_get(modname.to_s.capitalize)
80
+ Contexts.const_get(modname.to_s.capitalize)
72
81
  end
73
82
  env['modules'] << mod
74
83
  obj = self
@@ -76,429 +85,465 @@ module EM::Xmpp
76
85
  obj
77
86
  end
78
87
 
79
- module IncomingStanza
80
- include Namespaces
81
- %w{type id lang}.each do |w|
82
- define_method w do
83
- read_attr stanza, w
84
- end
85
- end
86
- def to
87
- read_attr(stanza, 'to'){|j| @connection.entity(j)}
88
- end
89
- def from
90
- read_attr(stanza, 'from'){|j| @connection.entity(j)}
91
- end
92
- def error?
93
- type == 'error'
94
- end
95
- def delay?
96
- false
97
- end
88
+ def bit(klassname)
89
+ bits[bit_klass_name(klassname)]
98
90
  end
99
91
 
100
- module Error
101
- def error_node
102
- xpath('//xmlns:error',{'xmlns' => Client}).first
103
- end
104
-
105
- def error_code
106
- n = error_node
107
- read_attr(n, 'code') if n
108
- end
92
+ def bit!(klassname)
93
+ ret = bit klassname
94
+ unless ret
95
+ klass = if klassname.is_a?(Class)
96
+ klassname
97
+ else
98
+ Bits.const_get(klassname.to_s.capitalize)
99
+ end
100
+ ret = bit_from_klass klass
101
+ end
102
+ ret
103
+ end
109
104
 
110
- def error_type
111
- n = error_node
112
- read_attr(n, 'type') if n
113
- end
105
+ def bit?(klassname)
106
+ bits.has_key? bit_klass_name(klassname)
107
+ end
114
108
 
115
- def error_condition_node
116
- n = error_node
117
- n.children.first if n
118
- end
109
+ private
119
110
 
120
- def error_condition
121
- n = error_condition_node
122
- n.name if n
123
- end
111
+ def bit_klass_name(obj)
112
+ obj.to_s.split('::').last.capitalize
124
113
  end
125
114
 
126
- module Presence
127
- include IncomingStanza
128
- def reply_default_params
129
- jid = @connection.jid.full
130
- {'from' => jid, 'to' => from, 'id' => id}
131
- end
132
- def reply(args={},&blk)
133
- args = reply_default_params.merge args
134
- @connection.presence_stanza(args,&blk)
135
- end
136
- def priority_node
137
- xpath('//xmlns:priority',{'xmlns' => Client}).first
138
- end
139
- def status_node
140
- xpath('//xmlns:status',{'xmlns' => Client}).first
141
- end
142
- def show_node
143
- xpath('//xmlns:show',{'xmlns' => Client}).first
144
- end
145
- def priority
146
- node = priority_node
147
- node.content if node
148
- end
149
- def status
150
- node = status_node
151
- node.content if node
152
- end
153
- def show
154
- node = show_node
155
- node.content if node
156
- end
157
- def subscription_request?
158
- type == 'subscribe'
159
- end
160
- def entity_left?
161
- type == 'unavailable'
162
- end
115
+ def bit_from_klass(klass)
116
+ obj = klass.new(self)
117
+ bits[bit_klass_name(klass)] = obj
118
+ obj
163
119
  end
164
120
 
165
- module Message
166
- include IncomingStanza
167
- def subject_node
168
- xpath('//xmlns:subject',{'xmlns' => Client}).first
169
- end
121
+ public
170
122
 
171
- def subject
172
- node = subject_node
173
- node.text if node
123
+ module Contexts
124
+ module IncomingStanza
125
+ include Namespaces
126
+ %w{type id lang}.each do |w|
127
+ define_method w do
128
+ read_attr stanza, w
129
+ end
130
+ end
131
+ def to
132
+ read_attr(stanza, 'to'){|j| connection.entity(j)}
133
+ end
134
+ def from
135
+ read_attr(stanza, 'from'){|j| connection.entity(j)}
136
+ end
137
+ def error?
138
+ type == 'error'
139
+ end
140
+ def delay?
141
+ false
142
+ end
174
143
  end
175
144
 
176
- def body_node
177
- xpath('//xmlns:body',{'xmlns' => Client}).first
178
- end
145
+ module Error
146
+ def error_node
147
+ xpath('//xmlns:error',{'xmlns' => Client}).first
148
+ end
179
149
 
180
- def body
181
- node = body_node
182
- node.text if node
183
- end
150
+ def error_code
151
+ n = error_node
152
+ read_attr(n, 'code') if n
153
+ end
184
154
 
185
- def reply_default_params
186
- h = {'to' => from, 'type' => type}
187
- h['id'] = id.succ if id
188
- h
189
- end
155
+ def error_type
156
+ n = error_node
157
+ read_attr(n, 'type') if n
158
+ end
190
159
 
191
- def reply(args={},&blk)
192
- args = reply_default_params.merge args
193
- @connection.message_stanza(args,&blk)
194
- end
160
+ def error_condition_node
161
+ n = error_node
162
+ n.children.first if n
163
+ end
195
164
 
196
- def groupchat?
197
- type == 'groupchat'
165
+ def error_condition
166
+ n = error_condition_node
167
+ n.name if n
168
+ end
198
169
  end
199
- end
200
170
 
201
- module Iq
202
- include IncomingStanza
203
- def reply_default_params
204
- jid = @connection.jid.full
205
- {'from' => jid, 'to' => from, 'type' => 'result', 'id' => id}
171
+ module Presence
172
+ include IncomingStanza
173
+ def reply_default_params
174
+ jid = connection.jid.full
175
+ {'from' => jid, 'to' => from, 'id' => id}
176
+ end
177
+ def reply(args={},&blk)
178
+ args = reply_default_params.merge args
179
+ connection.presence_stanza(args,&blk)
180
+ end
181
+ def priority_node
182
+ xpath('//xmlns:priority',{'xmlns' => Client}).first
183
+ end
184
+ def status_node
185
+ xpath('//xmlns:status',{'xmlns' => Client}).first
186
+ end
187
+ def show_node
188
+ xpath('//xmlns:show',{'xmlns' => Client}).first
189
+ end
190
+ def priority
191
+ node = priority_node
192
+ node.content if node
193
+ end
194
+ def status
195
+ node = status_node
196
+ node.content if node
197
+ end
198
+ def show
199
+ node = show_node
200
+ node.content if node
201
+ end
202
+ def subscription_request?
203
+ type == 'subscribe'
204
+ end
205
+ def entity_left?
206
+ type == 'unavailable'
207
+ end
206
208
  end
207
209
 
208
- def reply(args={},&blk)
209
- args = reply_default_params.merge args
210
- @connection.iq_stanza(args,&blk)
211
- end
212
- end
210
+ module Message
211
+ include IncomingStanza
212
+ def subject_node
213
+ xpath('//xmlns:subject',{'xmlns' => Client}).first
214
+ end
213
215
 
214
- module Delay
215
- def delay?
216
- true
217
- end
218
- #does not handle legacy delay
219
- def delay_node
220
- xpath('//xmlns:delay',{'xmlns' => EM::Xmpp::Namespaces::Delay}).first
221
- end
222
- def stamp
223
- n = delay_node
224
- Time.parse read_attr(n, 'stamp') if n
216
+ def subject
217
+ node = subject_node
218
+ node.text if node
219
+ end
220
+
221
+ def body_node
222
+ xpath('//xmlns:body',{'xmlns' => Client}).first
223
+ end
224
+
225
+ def body
226
+ node = body_node
227
+ node.text if node
228
+ end
229
+
230
+ def reply_default_params
231
+ h = {'to' => from, 'type' => type}
232
+ h['id'] = id.succ if id
233
+ h
234
+ end
235
+
236
+ def reply(args={},&blk)
237
+ args = reply_default_params.merge args
238
+ connection.message_stanza(args,&blk)
239
+ end
240
+
241
+ def groupchat?
242
+ type == 'groupchat'
243
+ end
225
244
  end
226
- end
227
245
 
228
- module Discoveries
229
- include Iq
230
- %w{node}.each do |word|
231
- define_method word do
232
- n = query_node
233
- read_attr(n, word) if n
246
+ module Iq
247
+ include IncomingStanza
248
+ def reply_default_params
249
+ jid = connection.jid.full
250
+ {'from' => jid, 'to' => from, 'type' => 'result', 'id' => id}
251
+ end
252
+
253
+ def reply(args={},&blk)
254
+ args = reply_default_params.merge args
255
+ connection.iq_stanza(args,&blk)
234
256
  end
235
257
  end
236
- end
237
258
 
238
- module Discoinfos
239
- include Discoveries
240
- Identity = Struct.new(:name, :category, :type)
241
- Feature = Struct.new(:var)
242
- def query_node
243
- xpath('//xmlns:query',{'xmlns' => DiscoverInfos}).first
244
- end
245
- def identities
246
- n = query_node
247
- if n
248
- n.xpath('xmlns:identity',{'xmlns' => DiscoverInfos}).map do |x|
249
- cat = read_attr(x, 'category')
250
- type = read_attr(x, 'type')
251
- name = read_attr(x, 'name')
252
- Identity.new name, cat, type
253
- end
259
+ module Delay
260
+ def delay?
261
+ true
262
+ end
263
+ #does not handle legacy delay
264
+ def delay_node
265
+ xpath('//xmlns:delay',{'xmlns' => EM::Xmpp::Namespaces::Delay}).first
266
+ end
267
+ def stamp
268
+ n = delay_node
269
+ Time.parse read_attr(n, 'stamp') if n
254
270
  end
255
271
  end
256
- def features
257
- n = query_node
258
- if n
259
- n.xpath('xmlns:feature',{'xmlns' => DiscoverInfos}).map do |x|
260
- var = read_attr(x, 'var')
261
- Feature.new var
272
+
273
+ module Discoveries
274
+ include Iq
275
+ %w{node}.each do |word|
276
+ define_method word do
277
+ n = query_node
278
+ read_attr(n, word) if n
262
279
  end
263
280
  end
264
281
  end
265
- end
266
282
 
267
- module Discoitems
268
- include Discoveries
269
- Item = Struct.new(:entity, :node, :name)
270
- def query_node
271
- xpath('//xmlns:query',{'xmlns' => DiscoverItems}).first
272
- end
273
- def item_nodes
274
- xpath('//xmlns:item',{'xmlns' => DiscoverItems})
275
- end
276
- def items
277
- item_nodes.map do |n|
278
- entity = read_attr(n, 'jid'){|x| @connection.entity(x)}
279
- node = read_attr(n, 'node')
280
- name = read_attr(n, 'name')
281
- Item.new(entity, node, name)
283
+ module Discoinfos
284
+ include Discoveries
285
+ Identity = Struct.new(:name, :category, :type)
286
+ Feature = Struct.new(:var)
287
+ def query_node
288
+ xpath('//xmlns:query',{'xmlns' => DiscoverInfos}).first
289
+ end
290
+ def identities
291
+ n = query_node
292
+ if n
293
+ n.xpath('xmlns:identity',{'xmlns' => DiscoverInfos}).map do |x|
294
+ cat = read_attr(x, 'category')
295
+ type = read_attr(x, 'type')
296
+ name = read_attr(x, 'name')
297
+ Identity.new name, cat, type
298
+ end
299
+ end
300
+ end
301
+ def features
302
+ n = query_node
303
+ if n
304
+ n.xpath('xmlns:feature',{'xmlns' => DiscoverInfos}).map do |x|
305
+ var = read_attr(x, 'var')
306
+ Feature.new var
307
+ end
308
+ end
282
309
  end
283
310
  end
284
- end
285
311
 
286
- module Command
287
- def command_node
288
- xpath('//xmlns:command',{'xmlns' => Commands}).first
312
+ module Discoitems
313
+ include Discoveries
314
+ Item = Struct.new(:entity, :node, :name)
315
+ def query_node
316
+ xpath('//xmlns:query',{'xmlns' => DiscoverItems}).first
317
+ end
318
+ def item_nodes
319
+ xpath('//xmlns:item',{'xmlns' => DiscoverItems})
320
+ end
321
+ def items
322
+ item_nodes.map do |n|
323
+ entity = read_attr(n, 'jid'){|x| connection.entity(x)}
324
+ node = read_attr(n, 'node')
325
+ name = read_attr(n, 'name')
326
+ Item.new(entity, node, name)
327
+ end
328
+ end
289
329
  end
290
330
 
291
- %w{node sessionid action}.each do |word|
292
- define_method word do
293
- n = command_node
294
- read_attr(n, word) if n
331
+ module Command
332
+ def command_node
333
+ xpath('//xmlns:command',{'xmlns' => Commands}).first
295
334
  end
296
- end
297
335
 
298
- def previous?
299
- action == 'prev'
300
- end
301
- end
336
+ %w{node sessionid action}.each do |word|
337
+ define_method word do
338
+ n = command_node
339
+ read_attr(n, word) if n
340
+ end
341
+ end
302
342
 
303
- module Dataforms
304
- Form = Struct.new(:type, :fields)
305
- Field = Struct.new(:var, :type, :values) do
306
- def value
307
- values.first
343
+ def previous?
344
+ action == 'prev'
308
345
  end
309
346
  end
310
347
 
311
- def x_form_nodes
312
- xpath('//xmlns:x',{'xmlns' => Namespaces::DataForms})
313
- end
348
+ module Dataforms
349
+ Form = Struct.new(:type, :fields)
350
+ Field = Struct.new(:var, :type, :values) do
351
+ def value
352
+ values.first
353
+ end
354
+ end
314
355
 
315
- def x_forms
316
- x_form_nodes.map do |form|
317
- form_type = read_attr(form, 'type')
318
- field_nodes = form.xpath('xmlns:field',{'xmlns' => Namespaces::DataForms})
319
- fields = field_nodes.map do |field|
320
- var = read_attr(field, 'var')
321
- type = read_attr(field, 'type')
322
- value_nodes = field.xpath('xmlns:value',{'xmlns' => Namespaces::DataForms})
323
- values = value_nodes.map(&:content)
356
+ def x_form_nodes
357
+ xpath('//xmlns:x',{'xmlns' => Namespaces::DataForms})
358
+ end
324
359
 
325
- Field.new(var,type,values)
360
+ def x_forms
361
+ x_form_nodes.map do |form|
362
+ form_type = read_attr(form, 'type')
363
+ field_nodes = form.xpath('xmlns:field',{'xmlns' => Namespaces::DataForms})
364
+ fields = field_nodes.map do |field|
365
+ var = read_attr(field, 'var')
366
+ type = read_attr(field, 'type')
367
+ value_nodes = field.xpath('//xmlns:value',{'xmlns' => Namespaces::DataForms})
368
+ values = value_nodes.map(&:content)
369
+
370
+ Field.new(var,type,values)
371
+ end
372
+ Form.new form_type, fields
326
373
  end
327
- Form.new form_type, fields
328
374
  end
329
375
  end
330
- end
331
376
 
332
- module Capabilities
333
- def c_node
334
- xpath('//xmlns:c',{'xmlns' => EM::Xmpp::Namespaces::Capabilities}).first
335
- end
377
+ module Capabilities
378
+ def c_node
379
+ xpath('//xmlns:c',{'xmlns' => EM::Xmpp::Namespaces::Capabilities}).first
380
+ end
336
381
 
337
- %w{node ver ext}.each do |word|
338
- define_method word do
339
- n = c_node
340
- read_attr(n, word) if n
382
+ %w{node ver ext}.each do |word|
383
+ define_method word do
384
+ n = c_node
385
+ read_attr(n, word) if n
386
+ end
341
387
  end
342
388
  end
343
- end
344
389
 
345
- module Roster
346
- def query_node
347
- xpath('//xmlns:query',{'xmlns' => EM::Xmpp::Namespaces::Roster}).first
348
- end
390
+ module Roster
391
+ def query_node
392
+ xpath('//xmlns:query',{'xmlns' => EM::Xmpp::Namespaces::Roster}).first
393
+ end
349
394
 
350
- def items
351
- n = query_node
352
- if n
353
- n.children.map do |xml|
354
- new_subscription(xml)
395
+ def items
396
+ n = query_node
397
+ if n
398
+ n.children.map do |xml|
399
+ new_subscription(xml)
400
+ end
355
401
  end
356
402
  end
357
- end
358
403
 
359
- private
404
+ private
360
405
 
361
- Subscription = Struct.new(:type, :jid, :name, :groups)
406
+ Subscription = Struct.new(:type, :jid, :name, :groups)
362
407
 
363
- def new_subscription(n)
364
- type = read_attr(n,'subscription')
365
- jid = read_attr(n,'jid') {|x| @connection.entity x}
366
- name = read_attr(n,'name')
367
- groups = n.xpath('xmlns:group', 'xmlns' => EM::Xmpp::Namespaces::Roster).map{|n| n.content}
368
- Subscription.new(type, jid, name, groups)
408
+ def new_subscription(n)
409
+ type = read_attr(n,'subscription')
410
+ jid = read_attr(n,'jid') {|x| connection.entity x}
411
+ name = read_attr(n,'name')
412
+ groups = n.xpath('xmlns:group', 'xmlns' => EM::Xmpp::Namespaces::Roster).map{|n| n.content}
413
+ Subscription.new(type, jid, name, groups)
414
+ end
369
415
  end
370
- end
371
416
 
372
- module Tune
373
- def tune_node
374
- xpath('//xmlns:tune',{'xmlns' => Namespaces::Tune}).first
375
- end
417
+ module Tune
418
+ def tune_node
419
+ xpath('//xmlns:tune',{'xmlns' => Namespaces::Tune}).first
420
+ end
376
421
 
377
- DecimalFields = %w{length rating}.freeze
378
- StringFields = %w{artist source title track uri}.freeze
422
+ DecimalFields = %w{length rating}.freeze
423
+ StringFields = %w{artist source title track uri}.freeze
379
424
 
380
- DecimalFields.each do |decimal|
381
- define_method(decimal) do
382
- n = tune_node
383
- if n
384
- d = n.children.find{|c| c.name == decimal}
385
- d.content.to_i if d
425
+ DecimalFields.each do |decimal|
426
+ define_method(decimal) do
427
+ n = tune_node
428
+ if n
429
+ d = n.children.find{|c| c.name == decimal}
430
+ d.content.to_i if d
431
+ end
386
432
  end
387
433
  end
388
- end
389
434
 
390
435
 
391
- StringFields.each do |str|
392
- define_method(str) do
393
- n = tune_node
394
- if n
395
- d = n.children.find{|c| c.name == str}
396
- d.content
436
+ StringFields.each do |str|
437
+ define_method(str) do
438
+ n = tune_node
439
+ if n
440
+ d = n.children.find{|c| c.name == str}
441
+ d.content
442
+ end
397
443
  end
398
444
  end
399
- end
400
445
 
401
- def tune
402
- ostruct = OpenStruct.new
403
- (StringFields + DecimalFields).each do |field|
404
- val = send field
405
- ostruct.send("#{field}=", val)
446
+ def tune
447
+ ostruct = OpenStruct.new
448
+ (StringFields + DecimalFields).each do |field|
449
+ val = send field
450
+ ostruct.send("#{field}=", val)
451
+ end
452
+ ostruct
406
453
  end
407
- ostruct
408
454
  end
409
- end
410
455
 
411
- module Nickname
412
- def nickname_node
413
- xpath('//xmlns:nick',{'xmlns' => Nick}).first
414
- end
415
- def nickname
416
- n = nickname_node
417
- n.content if n
456
+ module Nickname
457
+ def nickname_node
458
+ xpath('//xmlns:nick',{'xmlns' => Nick}).first
459
+ end
460
+ def nickname
461
+ n = nickname_node
462
+ n.content if n
463
+ end
418
464
  end
419
- end
420
465
 
421
- module Geolocation
422
- def geoloc_node
423
- xpath('//xmlns:nick',{'xmlns' => Geoloc}).first
424
- end
466
+ module Geolocation
467
+ def geoloc_node
468
+ xpath('//xmlns:nick',{'xmlns' => Geoloc}).first
469
+ end
425
470
 
426
- def geoloc
427
- ostruct = OpenStruct.newt
428
- (StringFields + DecimalFields + %w{timestamp}).each do |field|
429
- val = send field
430
- ostruct.send("#{field}=", val)
471
+ def geoloc
472
+ ostruct = OpenStruct.newt
473
+ (StringFields + DecimalFields + %w{timestamp}).each do |field|
474
+ val = send field
475
+ ostruct.send("#{field}=", val)
476
+ end
477
+ ostruct
431
478
  end
432
- ostruct
433
- end
434
479
 
435
- StringFields = %w{area building country countrycode datum description
480
+ StringFields = %w{area building country countrycode datum description
436
481
  floor locality postalcode region room street text uri}.freeze
437
- DecimalFields = %w{accuracy error alt lat lon bearing speed}.freeze
482
+ DecimalFields = %w{accuracy error alt lat lon bearing speed}.freeze
483
+
484
+ DecimalFields.each do |decimal|
485
+ define_method(decimal) do
486
+ n = geoloc_node
487
+ if n
488
+ d = n.children.find{|c| c.name == decimal}
489
+ d.content.to_i if d
490
+ end
491
+ end
492
+ end
438
493
 
439
- DecimalFields.each do |decimal|
440
- define_method(decimal) do
441
- n = geoloc_node
442
- if n
443
- d = n.children.find{|c| c.name == decimal}
444
- d.content.to_i if d
494
+ StringFields.each do |str|
495
+ define_method(str) do
496
+ n = geoloc_node
497
+ if n
498
+ d = n.children.find{|c| c.name == str}
499
+ d.content
500
+ end
445
501
  end
446
502
  end
447
- end
448
503
 
449
- StringFields.each do |str|
450
- define_method(str) do
504
+ def timestamp
451
505
  n = geoloc_node
452
506
  if n
453
- d = n.children.find{|c| c.name == str}
454
- d.content
507
+ d = n.children.find{|c| c.name == 'timestamp'}
508
+ Time.parse d.content if d
455
509
  end
456
510
  end
457
511
  end
458
512
 
459
- def timestamp
460
- n = geoloc_node
461
- if n
462
- d = n.children.find{|c| c.name == 'timestamp'}
463
- Time.parse d.content if d
513
+ module Useractivity
514
+ def activity_node
515
+ xpath('//xmlns:mood',{'xmlns' => Namespaces::Activity}).first
464
516
  end
465
- end
466
- end
467
-
468
- module Useractivity
469
- def activity_node
470
- xpath('//xmlns:mood',{'xmlns' => Namespaces::Activity}).first
471
- end
472
517
 
473
- def activity_category_node
474
- n = activity_node
475
- n.children.first if n
476
- end
518
+ def activity_category_node
519
+ n = activity_node
520
+ n.children.first if n
521
+ end
477
522
 
478
- def activity_text_node
479
- n = activity_node
480
- n.children.find{|n| n.name == 'text'} if n
481
- end
523
+ def activity_text_node
524
+ n = activity_node
525
+ n.children.find{|n| n.name == 'text'} if n
526
+ end
482
527
 
483
- def activity
484
- ret = []
485
- n = activity_category_node
486
- if n
487
- ret << n.name
488
- detail = n.children.first
489
- ret << detail.name if detail
528
+ def activity
529
+ ret = []
530
+ n = activity_category_node
531
+ if n
532
+ ret << n.name
533
+ detail = n.children.first
534
+ ret << detail.name if detail
535
+ end
536
+ ret
490
537
  end
491
- ret
492
- end
493
538
 
494
- def activity_text
495
- n = activity_text_node
496
- n.content if n
539
+ def activity_text
540
+ n = activity_text_node
541
+ n.content if n
542
+ end
497
543
  end
498
- end
499
544
 
500
- module Mood
501
- DefinedMoods = %w{afraid amazed angry amorous annoyed anxious aroused
545
+ module Mood
546
+ DefinedMoods = %w{afraid amazed angry amorous annoyed anxious aroused
502
547
  ashamed bored brave calm cautious cold confident confused contemplative
503
548
  contented cranky crazy creative curious dejected depressed disappointed
504
549
  disgusted dismayed distracted embarrassed envious excited flirtatious
@@ -509,60 +554,253 @@ relieved remorseful restless sad sarcastic serious shocked shy sick sleepy
509
554
  spontaneous stressed strong surprised thankful thirsty tired undefined weak
510
555
  worried}.freeze
511
556
 
512
- def mood_node
513
- xpath('//xmlns:mood',{'xmlns' => Namespaces::Mood}).first
514
- end
515
- def mood_name_node
516
- n = mood_node
517
- n.children.find{|c| DefinedMoods.include?(c.name)} if n
557
+ def mood_node
558
+ xpath('//xmlns:mood',{'xmlns' => Namespaces::Mood}).first
559
+ end
560
+ def mood_name_node
561
+ n = mood_node
562
+ n.children.find{|c| DefinedMoods.include?(c.name)} if n
563
+ end
564
+ def mood_text_node
565
+ n = mood_node
566
+ n.children.find{|c| c.name == 'text'}
567
+ end
568
+ def mood
569
+ n = mood_name_node
570
+ n.name if n
571
+ end
572
+ def mood
573
+ n = mood_text_node
574
+ n.content if n
575
+ end
518
576
  end
519
- def mood_text_node
520
- n = mood_node
521
- n.children.find{|c| c.name == 'text'}
577
+
578
+ module Bytestreams
579
+ include Iq
580
+ StreamHost = Struct.new(:host, :port, :jid)
581
+ def query_node
582
+ xpath('//xmlns:query',{'xmlns' => Namespaces::ByteStreams}).first
583
+ end
584
+ def transport_mode
585
+ n = query_node
586
+ read_attr(n,'mode') if n
587
+ end
588
+ def sid
589
+ n = query_node
590
+ read_attr(n,'sid') if n
591
+ end
592
+ def stream_hosts
593
+ n = query_node
594
+ if n
595
+ n.children.select{|c| c.name == 'streamhost'}.map do |c|
596
+ host = read_attr(c,'host')
597
+ port = read_attr(c,'port',:to_i)
598
+ jid = read_attr(c,'jid')
599
+ StreamHost.new(host, port, jid)
600
+ end
601
+ else
602
+ []
603
+ end
604
+ end
605
+ def used_stream_hosts
606
+ n = query_node
607
+ if n
608
+ n.children.select{|c| c.name == 'streamhost-used'}.map do |c|
609
+ host = read_attr(c,'host')
610
+ port = read_attr(c,'port',:to_i)
611
+ jid = read_attr(c,'jid')
612
+ StreamHost.new(host, port, jid)
613
+ end
614
+ else
615
+ []
616
+ end
617
+ end
618
+ def support_fast_mode?
619
+ xpath('//xmnls:fast',{'xmlns'=> Namespaces::FastByteStreams}).any?
620
+ end
522
621
  end
523
- def mood
524
- n = mood_name_node
525
- n.name if n
622
+
623
+ module Streaminitiation
624
+ include Iq
625
+ include Dataforms
626
+ def si_node
627
+ xpath('//xmlns:si',{'xmlns' => Namespaces::StreamInitiation}).first
628
+ end
629
+ def file_node
630
+ xpath('//xmlns:file',{'xmlns' => Namespaces::FileTransfer}).first
631
+ end
632
+ def mime_type
633
+ n = si_node
634
+ read_attr(n, 'mime-type') if n
635
+ end
636
+ def profile
637
+ n = si_node
638
+ read_attr(n, 'profile') if n
639
+ end
640
+ def file_name
641
+ n = file_node
642
+ read_attr(n, 'name') if n
643
+ end
644
+ def file_size
645
+ n = file_node
646
+ read_attr(n, 'size') if n
647
+ end
648
+ def file_date
649
+ n = file_node
650
+ read_attr(n, 'date') if n
651
+ end
652
+ def file_md5
653
+ n = file_node
654
+ read_attr(n, 'hash') if n
655
+ end
656
+ def can_do_range?
657
+ n = file_node
658
+ n.children.find{|n| n.name == 'range'} if n
659
+ end
660
+ # TODO: range on requests
661
+ def description
662
+ n = file_node
663
+ if n
664
+ node = n.children.find{|n| n.name == 'desc'}
665
+ node.content if node
666
+ end
667
+ end
668
+ def stream_methods
669
+ form = x_forms.first
670
+ if form
671
+ field = form.fields.first
672
+ field.values if field
673
+ end
674
+ end
526
675
  end
527
- def mood
528
- n = mood_text_node
529
- n.content if n
676
+
677
+ module Mucuser
678
+ def x_nodstreaminitiation
679
+ xpath('//xmlns:x',{'xmlns' => EM::Xmpp::Namespaces::MucUser}).first
680
+ end
681
+
682
+ def item_node
683
+ xpath('//xmlns:item',{'xmlns' => EM::Xmpp::Namespaces::MucUser}).first
684
+ end
685
+
686
+ def status_node
687
+ xpath('//xmlns:status',{'xmlns' => EM::Xmpp::Namespaces::MucUser}).first
688
+ end
689
+
690
+ def status
691
+ n = status_node
692
+ read_attr(n, 'code') if n
693
+ end
694
+
695
+ def jid
696
+ n = item_node
697
+ jid_str = read_attr(n, 'jid') if n
698
+ connection.entity jid_str if jid_str
699
+ end
700
+
701
+ def affiliation
702
+ n = item_node
703
+ read_attr(n, 'affiliation') if n
704
+ end
705
+
706
+ def role
707
+ n = item_node
708
+ read_attr(n, 'role') if n
709
+ end
530
710
  end
531
711
  end
532
712
 
533
- module Mucuser
534
- def x_node
535
- xpath('//xmlns:x',{'xmlns' => EM::Xmpp::Namespaces::MucUser}).first
713
+ class Bit
714
+ include Namespaces
715
+ attr_reader :ctx
716
+
717
+ def initialize(ctx)
718
+ @ctx = ctx
536
719
  end
537
720
 
538
- def item_node
539
- xpath('//xmlns:item',{'xmlns' => EM::Xmpp::Namespaces::MucUser}).first
721
+ def connection
722
+ ctx.connection
540
723
  end
541
724
 
542
- def status_node
543
- xpath('//xmlns:status',{'xmlns' => EM::Xmpp::Namespaces::MucUser}).first
725
+ def stanza
726
+ ctx.stanza
544
727
  end
545
728
 
546
- def status
547
- n = status_node
548
- read_attr(n, 'code') if n
729
+ def read_attr(*args,&blk)
730
+ ctx.read_attr(*args,&blk)
549
731
  end
550
732
 
551
- def jid
552
- n = item_node
553
- jid_str = read_attr(n, 'jid') if n
554
- @connection.entity jid_str if jid_str
733
+ def xpath(*args)
734
+ ctx.xpath(*args)
555
735
  end
556
736
 
557
- def affiliation
558
- n = item_node
559
- read_attr(n, 'affiliation') if n
737
+ def xpath?(*args)
738
+ ctx.xpath(*args)
560
739
  end
740
+ end
561
741
 
562
- def role
563
- n = item_node
564
- read_attr(n, 'role') if n
742
+ module Bits
743
+ class Stanza < Bit
744
+ include Contexts::IncomingStanza
745
+ end
746
+ class Error < Bit
747
+ include Contexts::Error
748
+ end
749
+ class Presence < Bit
750
+ include Contexts::Presence
751
+ end
752
+ class Message < Bit
753
+ include Contexts::Message
754
+ end
755
+ class Iq < Bit
756
+ include Contexts::Iq
757
+ end
758
+ class Delay < Bit
759
+ include Contexts::Delay
760
+ end
761
+ class Discoinfos < Bit
762
+ include Contexts::Discoinfos
763
+ end
764
+ class Discoitems < Bit
765
+ include Contexts::Discoitems
766
+ end
767
+ class Command < Bit
768
+ include Contexts::Command
769
+ end
770
+ class Dataforms < Bit
771
+ include Contexts::Dataforms
772
+ end
773
+ class Capabilities < Bit
774
+ include Contexts::Capabilities
775
+ end
776
+ class Roster < Bit
777
+ include Contexts::Roster
778
+ end
779
+ class Tune < Bit
780
+ include Contexts::Tune
781
+ end
782
+ class Nickname < Bit
783
+ include Contexts::Nickname
784
+ end
785
+ class Geolocation < Bit
786
+ include Contexts::Geolocation
787
+ end
788
+ class Useractivity < Bit
789
+ include Contexts::Useractivity
790
+ end
791
+ class Mood < Bit
792
+ include Contexts::Mood
793
+ end
794
+ class Bytestreams < Bit
795
+ include Contexts::Bytestreams
796
+ end
797
+ class Streaminitiation < Bit
798
+ include Contexts::Streaminitiation
799
+ end
800
+ class Mucuser < Bit
801
+ include Contexts::Mucuser
565
802
  end
566
803
  end
804
+
567
805
  end
568
806
  end