troystribling-agent_xmpp 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/README.rdoc +397 -2
  2. data/Rakefile +33 -21
  3. data/VERSION +1 -1
  4. data/agent_xmpp.gemspec +78 -33
  5. data/lib/agent_xmpp.rb +11 -7
  6. data/lib/agent_xmpp/app.rb +0 -5
  7. data/lib/agent_xmpp/app/boot.rb +47 -33
  8. data/lib/agent_xmpp/app/controller.rb +135 -19
  9. data/lib/agent_xmpp/client.rb +6 -1
  10. data/lib/agent_xmpp/client/client.rb +22 -155
  11. data/lib/agent_xmpp/client/connection.rb +41 -250
  12. data/lib/agent_xmpp/client/message_delegate.rb +706 -0
  13. data/lib/agent_xmpp/client/message_pipe.rb +176 -0
  14. data/lib/agent_xmpp/client/publish_model.rb +65 -0
  15. data/lib/agent_xmpp/{app/format.rb → client/response.rb} +19 -24
  16. data/lib/agent_xmpp/client/roster_model.rb +139 -0
  17. data/lib/agent_xmpp/client/services_model.rb +97 -0
  18. data/lib/agent_xmpp/config.rb +45 -0
  19. data/lib/agent_xmpp/main.rb +126 -0
  20. data/lib/agent_xmpp/patches.rb +6 -2
  21. data/lib/agent_xmpp/patches/{standard_library_patches/array.rb → array.rb} +14 -7
  22. data/lib/agent_xmpp/patches/{standard_library_patches/float.rb → float.rb} +0 -0
  23. data/lib/agent_xmpp/patches/{standard_library_patches/hash.rb → hash.rb} +3 -3
  24. data/lib/agent_xmpp/patches/{standard_library_patches/object.rb → object.rb} +3 -3
  25. data/lib/agent_xmpp/patches/rexml.rb +69 -0
  26. data/lib/agent_xmpp/patches/{standard_library_patches/string.rb → string.rb} +5 -0
  27. data/lib/agent_xmpp/xmpp.rb +17 -0
  28. data/lib/agent_xmpp/xmpp/element.rb +159 -0
  29. data/lib/agent_xmpp/xmpp/error_response.rb +179 -0
  30. data/lib/agent_xmpp/xmpp/iq.rb +90 -0
  31. data/lib/agent_xmpp/xmpp/iq_command.rb +47 -0
  32. data/lib/agent_xmpp/xmpp/iq_disco.rb +205 -0
  33. data/lib/agent_xmpp/xmpp/iq_pubsub.rb +268 -0
  34. data/lib/agent_xmpp/xmpp/iq_roster.rb +183 -0
  35. data/lib/agent_xmpp/xmpp/iq_version.rb +89 -0
  36. data/lib/agent_xmpp/xmpp/jid.rb +150 -0
  37. data/lib/agent_xmpp/xmpp/message.rb +82 -0
  38. data/lib/agent_xmpp/xmpp/presence.rb +127 -0
  39. data/lib/agent_xmpp/xmpp/sasl.rb +241 -0
  40. data/lib/agent_xmpp/xmpp/stanza.rb +107 -0
  41. data/lib/agent_xmpp/xmpp/x_data.rb +269 -0
  42. data/test/app/app.rb +134 -0
  43. data/test/cases/test_application_message_processing.rb +65 -0
  44. data/test/cases/test_errors.rb +24 -0
  45. data/test/cases/test_presence_management.rb +139 -0
  46. data/test/cases/test_roster_management.rb +214 -0
  47. data/test/cases/test_service_discovery.rb +168 -0
  48. data/test/cases/test_session_management.rb +120 -0
  49. data/test/cases/test_version_discovery.rb +67 -0
  50. data/test/helpers/matchers.rb +23 -0
  51. data/test/helpers/mocks.rb +82 -0
  52. data/test/helpers/test_case_extensions.rb +45 -0
  53. data/test/helpers/test_client.rb +44 -0
  54. data/test/helpers/test_delegate.rb +61 -0
  55. data/test/helpers/test_helper.rb +91 -0
  56. data/test/messages/application_messages.rb +206 -0
  57. data/test/messages/error_messages.rb +35 -0
  58. data/test/messages/presence_messages.rb +66 -0
  59. data/test/messages/roster_messages.rb +126 -0
  60. data/test/messages/service_discovery_messages.rb +201 -0
  61. data/test/messages/session_messages.rb +158 -0
  62. data/test/messages/version_discovery_messages.rb +69 -0
  63. data/test/peer/peer.rb +36 -0
  64. data/test/xml_messages.txt +197 -0
  65. metadata +82 -42
  66. data/bin/agent_xmpp +0 -56
  67. data/lib/agent_xmpp/app/chat_message_body_controller.rb +0 -16
  68. data/lib/agent_xmpp/app/map.rb +0 -45
  69. data/lib/agent_xmpp/app/routes.rb +0 -77
  70. data/lib/agent_xmpp/app/view.rb +0 -49
  71. data/lib/agent_xmpp/client/parser.rb +0 -71
  72. data/lib/agent_xmpp/patches/standard_library_patches.rb +0 -5
  73. data/lib/agent_xmpp/patches/xmpp4r_patches.rb +0 -3
  74. data/lib/agent_xmpp/patches/xmpp4r_patches/command.rb +0 -26
  75. data/lib/agent_xmpp/patches/xmpp4r_patches/iq.rb +0 -26
  76. data/lib/agent_xmpp/patches/xmpp4r_patches/x_data.rb +0 -116
  77. data/lib/agent_xmpp/utils.rb +0 -2
  78. data/lib/agent_xmpp/utils/logger.rb +0 -24
  79. data/lib/agent_xmpp/utils/roster.rb +0 -23
  80. data/lib/agent_xmpp/version.rb +0 -6
  81. data/test/agent_xmpp_test.rb +0 -7
  82. data/test/test_helper.rb +0 -10
@@ -0,0 +1,268 @@
1
+ # Original from XMPP4R - XMPP Library for Ruby Website::http://home.gna.org/xmpp4r/
2
+ ##############################################################################################################
3
+ module AgentXmpp
4
+
5
+ #####-------------------------------------------------------------------------------------------------------
6
+ module Xmpp
7
+
8
+ #####-------------------------------------------------------------------------------------------------------
9
+ class IqPubSub < Element
10
+
11
+ #.......................................................................................................
12
+ name_xmlns 'pubsub', 'http://jabber.org/protocol/pubsub'
13
+
14
+ #####-------------------------------------------------------------------------------------------------------
15
+ class << self
16
+
17
+ #.........................................................................................................
18
+ def create_node(pipe, to, node)
19
+ iq = Xmpp::Iq.new(:set, to)
20
+ create = REXML::Element.new('create')
21
+ create.add_attribute('node', node)
22
+ iq.pubsub = IqPubSub.new << create
23
+ configure = REXML::Element.new('configure')
24
+ user_config = AgentXmpp.published.find_by_node(node)
25
+ if user_config
26
+ form = Xmpp::XData.new(:submit)
27
+ form.add_field_with_value('FORM_TYPE', 'http://jabber.org/protocol/pubsub#node_config', :hidden)
28
+ configure << AgentXmpp::DEFAULT_PUBSUB_CONFIG.inject(form) do |f, (var, val)|
29
+ f.add_field_with_value("pubsub##{var.to_s}", user_config.send(var) || val)
30
+ end
31
+ end
32
+ iq.pubsub << configure
33
+ Send(iq) do |r|
34
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
35
+ pipe.broadcast_to_delegates(:did_receive_pubsub_create_node_result, pipe, r, node)
36
+ elsif r.type.eql?(:error)
37
+ pipe.broadcast_to_delegates(:did_receive_pubsub_create_node_error, pipe, r, node)
38
+ end
39
+ end
40
+ end
41
+
42
+ #.........................................................................................................
43
+ def subscriptions(pipe, to)
44
+ iq = Xmpp::Iq.new(:get, to)
45
+ iq.pubsub = IqPubSub.new << REXML::Element.new('subscriptions')
46
+ Send(iq) do |r|
47
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
48
+ pipe.broadcast_to_delegates(:did_receive_pubsub_subscriptions_result, pipe, r)
49
+ elsif r.type.eql?(:error)
50
+ pipe.broadcast_to_delegates(:did_receive_pubsub_subscriptions_error, pipe, r)
51
+ end
52
+ end
53
+ end
54
+
55
+ #.........................................................................................................
56
+ def subscribe(pipe, to, node)
57
+ iq = Xmpp::Iq.new(:set, to)
58
+ subscribe = REXML::Element.new('subscribe')
59
+ subscribe.add_attribute('node', node)
60
+ subscribe.add_attribute('jid', AgentXmpp.jid.bare.to_s)
61
+ iq.pubsub = IqPubSub.new << subscribe
62
+ Send(iq) do |r|
63
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
64
+ pipe.broadcast_to_delegates(:did_receive_pubsub_subscribe_result, pipe, r, node)
65
+ elsif r.type.eql?(:error)
66
+ pipe.broadcast_to_delegates(:did_receive_pubsub_subscribe_error, pipe, r, node)
67
+ end
68
+ end
69
+ end
70
+
71
+ #.........................................................................................................
72
+ def unsubscribe(pipe, to, node)
73
+ iq = Xmpp::Iq.new(:set, to)
74
+ unsubscribe = REXML::Element.new('unsubscribe')
75
+ unsubscribe.add_attribute('node', node)
76
+ unsubscribe.add_attribute('jid', AgentXmpp.jid.bare.to_s)
77
+ iq.pubsub = IqPubSub.new << unsubscribe
78
+ Send(iq) do |r|
79
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
80
+ pipe.broadcast_to_delegates(:did_receive_pubsub_unsubscribe_result, pipe, r, node)
81
+ elsif r.type.eql?(:error)
82
+ pipe.broadcast_to_delegates(:did_receive_pubsub_unsubscribe_error, pipe, r, node)
83
+ end
84
+ end
85
+ end
86
+
87
+ #.........................................................................................................
88
+ def affiliations(pipe, to)
89
+ iq = Xmpp::Iq.new(:get, to)
90
+ iq.pubsub = IqPubSub.new << REXML::Element.new('affiliations')
91
+ Send(iq) do |r|
92
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
93
+ pipe.broadcast_to_delegates(:did_receive_pubsub_affiliations_result, pipe, r)
94
+ elsif r.type.eql?(:error)
95
+ pipe.broadcast_to_delegates(:did_receive_pubsub_affiliations_error, pipe, r)
96
+ end
97
+ end
98
+ end
99
+
100
+ #### self
101
+ end
102
+
103
+ #.........................................................................................................
104
+ def subscriptions
105
+ first_element('subscriptions').elements.to_a('subscription')
106
+ end
107
+
108
+ #### IqPubSub
109
+ end
110
+
111
+ #####--------------------------------------------------------------------------------------------------------
112
+ class IqPubSubOwner < Element
113
+
114
+ #..........................................................................................................
115
+ name_xmlns 'pubsub', 'http://jabber.org/protocol/pubsub' + '#owner'
116
+
117
+ #####-------------------------------------------------------------------------------------------------------
118
+ class << self
119
+
120
+ #.........................................................................................................
121
+ def delete_node(pipe, to, node)
122
+ iq = Xmpp::Iq.new(:set, to)
123
+ delete = REXML::Element.new('delete')
124
+ delete.add_attribute('node', node)
125
+ iq.pubsub = IqPubSubOwner.new << delete
126
+ Send(iq) do |r|
127
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
128
+ pipe.broadcast_to_delegates(:did_receive_pubsub_delete_node_result, pipe, r, node)
129
+ elsif r.type.eql?(:error)
130
+ pipe.broadcast_to_delegates(:did_receive_pubsub_delete_node_error, pipe, r, node)
131
+ end
132
+ end
133
+ end
134
+
135
+ #### self
136
+ end
137
+
138
+ end
139
+
140
+ #####-------------------------------------------------------------------------------------------------------
141
+ class IqPublish < Element
142
+
143
+ #.........................................................................................................
144
+ name_xmlns 'publish', 'http://jabber.org/protocol/pubsub'
145
+ xmpp_attribute :node
146
+
147
+ #####-------------------------------------------------------------------------------------------------------
148
+ class << self
149
+
150
+ #.........................................................................................................
151
+ def set(pipe, args)
152
+ iq = Xmpp::Iq.new(:set, args[:to])
153
+ item = Item.new << args[:payload]
154
+ pub = IqPublish.new("#{AgentXmpp.user_pubsub_root}/#{args[:node]}") << item
155
+ iq.pubsub = IqPubSub.new << pub
156
+ Send(iq) do |r|
157
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
158
+ pipe.broadcast_to_delegates(:did_receive_publish_result, pipe, r, args[:node])
159
+ elsif r.type.eql?(:error)
160
+ pipe.broadcast_to_delegates(:did_receive_publish_error, pipe, r, args[:node])
161
+ end
162
+ end
163
+ end
164
+
165
+ #### self
166
+ end
167
+
168
+ #.........................................................................................................
169
+ def initialize(node=nil)
170
+ super()
171
+ self.node = node if node
172
+ end
173
+ end
174
+
175
+ #####-------------------------------------------------------------------------------------------------------
176
+ class Items < Element
177
+ name_xmlns 'items', 'http://jabber.org/protocol/pubsub'
178
+ xmpp_attribute :node, :subid, :max_items
179
+ end
180
+
181
+ #####-------------------------------------------------------------------------------------------------------
182
+ class Item < Element
183
+ name_xmlns 'item', 'http://jabber.org/protocol/pubsub'
184
+ xmpp_attribute :id
185
+ def initialize(id=nil)
186
+ super()
187
+ self.id = id if id
188
+ end
189
+ end
190
+
191
+ #####-------------------------------------------------------------------------------------------------------
192
+ class Event < Element
193
+ name_xmlns 'event', 'http://jabber.org/protocol/pubsub' + "#event"
194
+ def items
195
+ elements.to_a('items')
196
+ end
197
+ end
198
+
199
+ #####-------------------------------------------------------------------------------------------------------
200
+ class EventItems < Items
201
+ name_xmlns 'items', 'http://jabber.org/protocol/pubsub' + "#event"
202
+ #.........................................................................................................
203
+ def item
204
+ elements.to_a('item')
205
+ end
206
+ end
207
+
208
+ #####-------------------------------------------------------------------------------------------------------
209
+ class EventItem < Item
210
+ name_xmlns 'item', 'http://jabber.org/protocol/pubsub' + "#event"
211
+ xmpp_child :x
212
+ end
213
+
214
+ #####-------------------------------------------------------------------------------------------------------
215
+ class Subscriptions < Element
216
+ name_xmlns 'subscriptions', 'http://jabber.org/protocol/pubsub'
217
+ end
218
+
219
+ #####-------------------------------------------------------------------------------------------------------
220
+ class Subscription < Element
221
+
222
+ #.........................................................................................................
223
+ name_xmlns 'subscription', 'http://jabber.org/protocol/pubsub'
224
+ xmpp_attribute :node, :subid
225
+
226
+ #.........................................................................................................
227
+ def initialize(jid=nil, node=nil, subid=nil, subscription=nil)
228
+ super()
229
+ self.jid = jid if jid
230
+ self.node = node if node
231
+ self.subid = subid if subid
232
+ self.state = subscription if subscription
233
+ end
234
+
235
+ #.........................................................................................................
236
+ def jid
237
+ (a = attribute('jid')).nil? ? a : Jid.new(a.value)
238
+ end
239
+
240
+ #.........................................................................................................
241
+ def jid=(myjid)
242
+ add_attribute('jid', myjid ? myjid.to_s : nil)
243
+ end
244
+
245
+ #.........................................................................................................
246
+ def state
247
+ sub = attributes['subscription']
248
+ ['none', 'pending', 'subscribed', 'unconfigured'].include?(sub) ? sub.to_sym : nil
249
+ end
250
+
251
+ #.........................................................................................................
252
+ def state=(mystate)
253
+ attributes['subscription'] = mystate
254
+ end
255
+
256
+ #.........................................................................................................
257
+ def need_approval?
258
+ state == :pending
259
+ end
260
+
261
+ #### Subscription
262
+ end
263
+
264
+ #### Xmpp
265
+ end
266
+
267
+ #### AgentXmpp
268
+ end
@@ -0,0 +1,183 @@
1
+ # Original from XMPP4R - XMPP Library for Ruby Website::http://home.gna.org/xmpp4r/
2
+ ##############################################################################################################
3
+ module AgentXmpp
4
+
5
+ #####-------------------------------------------------------------------------------------------------------
6
+ module Xmpp
7
+
8
+ #####-------------------------------------------------------------------------------------------------------
9
+ class IqRoster < IqQuery
10
+
11
+ #.......................................................................................................
12
+ name_xmlns 'query', 'jabber:iq:roster'
13
+
14
+ #####-------------------------------------------------------------------------------------------------------
15
+ class << self
16
+
17
+ #.........................................................................................................
18
+ def get(pipe)
19
+ Send(new_rosterget) do |r|
20
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
21
+ pipe.broadcast_to_delegates(:did_receive_roster_result, pipe, r)
22
+ elsif r.type.eql?(:error)
23
+ raise AgentXmppError, "roster request failed"
24
+ end
25
+ end
26
+ end
27
+
28
+ #.........................................................................................................
29
+ def update(pipe, roster_item_jid, groups=nil)
30
+ request = new_rosterset
31
+ item = Xmpp::RosterItem.new(roster_item_jid)
32
+ item.groups = groups unless groups.nil?
33
+ request.query.add(item)
34
+ Send(request) do |r|
35
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
36
+ pipe.broadcast_to_delegates(:did_receive_update_roster_item_result, pipe, r)
37
+ elsif r.type.eql?(:error)
38
+ pipe.broadcast_to_delegates(:did_receive_update_roster_item_error, pipe, roster_item_jid)
39
+ end
40
+ end
41
+ end
42
+
43
+ #.........................................................................................................
44
+ def remove(pipe, roster_item_jid)
45
+ request = new_rosterset
46
+ request.query.add(Xmpp::RosterItem.new(roster_item_jid, nil, :remove))
47
+ Send(request) do |r|
48
+ if r.type == :result and r.kind_of?(Xmpp::Iq)
49
+ pipe.broadcast_to_delegates(:did_receive_remove_roster_item_result, pipe, r)
50
+ elsif r.type.eql?(:error)
51
+ pipe.broadcast_to_delegates(:did_receive_remove_roster_item_error, pipe, roster_item_jid)
52
+ end
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ #.......................................................................................................
59
+ def new_rosterget
60
+ iq = Iq.new(:get)
61
+ query = IqQuery.new
62
+ query.add_namespace('jabber:iq:roster')
63
+ iq.add(query)
64
+ iq
65
+ end
66
+
67
+ #.......................................................................................................
68
+ def new_rosterset
69
+ iq = Iq.new(:set)
70
+ query = IqQuery.new
71
+ query.add_namespace('jabber:iq:roster')
72
+ iq.add(query)
73
+ iq
74
+ end
75
+
76
+ #### self
77
+ end
78
+
79
+ #.......................................................................................................
80
+ def each(&block)
81
+ each_element {|item| yield(item) if item.kind_of?(RosterItem)}
82
+ end
83
+
84
+ #.......................................................................................................
85
+ def [](jid)
86
+ each{|item| return(item) if item.jid == jid}
87
+ nil
88
+ end
89
+
90
+ #.......................................................................................................
91
+ def to_a
92
+ a = []
93
+ each{|item| a.push(item)}
94
+ a
95
+ end
96
+
97
+ #.......................................................................................................
98
+ def receive_iq(iq, filter=true)
99
+ return if filter && (((iq.type != :set) && (iq.type != :result)) || (iq.queryns != 'jabber:iq:roster'))
100
+ import(iq.query)
101
+ end
102
+
103
+ #.......................................................................................................
104
+ def inspect
105
+ jids = to_a.collect { |item| item.jid.inspect }
106
+ jids.join(', ')
107
+ end
108
+
109
+ #### IqQueryRoster
110
+ end
111
+
112
+ #####-------------------------------------------------------------------------------------------------------
113
+ class RosterItem < Element
114
+
115
+ #.......................................................................................................
116
+ name_xmlns 'item', 'jabber:iq:roster'
117
+ xmpp_attribute :subscription, :sym => true
118
+
119
+ #.......................................................................................................
120
+ def initialize(jid=nil, iname=nil, subscription=nil, ask=nil)
121
+ super()
122
+ self.jid = jid
123
+ self.iname = iname if iname
124
+ self.subscription = subscription if subscription
125
+ self.ask = ask if ask
126
+ end
127
+
128
+ #.......................................................................................................
129
+ def iname
130
+ attributes['name']
131
+ end
132
+
133
+ #.......................................................................................................
134
+ def iname=(val)
135
+ attributes['name'] = val
136
+ end
137
+
138
+ #.......................................................................................................
139
+ def jid
140
+ (a = attributes['jid']) ? Jid.new(a) : nil
141
+ end
142
+
143
+ #.......................................................................................................
144
+ def jid=(val)
145
+ attributes['jid'] = val.nil? ? nil : val.to_s
146
+ end
147
+
148
+ #.......................................................................................................
149
+ def ask
150
+ case attributes['ask']
151
+ when 'subscribe' then :subscribe
152
+ else nil
153
+ end
154
+ end
155
+
156
+ #.......................................................................................................
157
+ def ask=(val)
158
+ case val
159
+ when :subscribe then attributes['ask'] = 'subscribe'
160
+ else attributes['ask'] = nil
161
+ end
162
+ end
163
+
164
+ #.......................................................................................................
165
+ def groups
166
+ elements.inject('group', []) {|r, g| r << g.text}.uniq
167
+ end
168
+
169
+ #.......................................................................................................
170
+ def groups=(ary)
171
+ delete_elements('group')
172
+ ary.uniq.each{|group| add_element('group').text = group}
173
+ end
174
+
175
+ #### RosterItem
176
+ end
177
+
178
+ #### XMPP
179
+ end
180
+
181
+ #### AgentXmpp
182
+ end
183
+