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.
- data/README.rdoc +397 -2
- data/Rakefile +33 -21
- data/VERSION +1 -1
- data/agent_xmpp.gemspec +78 -33
- data/lib/agent_xmpp.rb +11 -7
- data/lib/agent_xmpp/app.rb +0 -5
- data/lib/agent_xmpp/app/boot.rb +47 -33
- data/lib/agent_xmpp/app/controller.rb +135 -19
- data/lib/agent_xmpp/client.rb +6 -1
- data/lib/agent_xmpp/client/client.rb +22 -155
- data/lib/agent_xmpp/client/connection.rb +41 -250
- data/lib/agent_xmpp/client/message_delegate.rb +706 -0
- data/lib/agent_xmpp/client/message_pipe.rb +176 -0
- data/lib/agent_xmpp/client/publish_model.rb +65 -0
- data/lib/agent_xmpp/{app/format.rb → client/response.rb} +19 -24
- data/lib/agent_xmpp/client/roster_model.rb +139 -0
- data/lib/agent_xmpp/client/services_model.rb +97 -0
- data/lib/agent_xmpp/config.rb +45 -0
- data/lib/agent_xmpp/main.rb +126 -0
- data/lib/agent_xmpp/patches.rb +6 -2
- data/lib/agent_xmpp/patches/{standard_library_patches/array.rb → array.rb} +14 -7
- data/lib/agent_xmpp/patches/{standard_library_patches/float.rb → float.rb} +0 -0
- data/lib/agent_xmpp/patches/{standard_library_patches/hash.rb → hash.rb} +3 -3
- data/lib/agent_xmpp/patches/{standard_library_patches/object.rb → object.rb} +3 -3
- data/lib/agent_xmpp/patches/rexml.rb +69 -0
- data/lib/agent_xmpp/patches/{standard_library_patches/string.rb → string.rb} +5 -0
- data/lib/agent_xmpp/xmpp.rb +17 -0
- data/lib/agent_xmpp/xmpp/element.rb +159 -0
- data/lib/agent_xmpp/xmpp/error_response.rb +179 -0
- data/lib/agent_xmpp/xmpp/iq.rb +90 -0
- data/lib/agent_xmpp/xmpp/iq_command.rb +47 -0
- data/lib/agent_xmpp/xmpp/iq_disco.rb +205 -0
- data/lib/agent_xmpp/xmpp/iq_pubsub.rb +268 -0
- data/lib/agent_xmpp/xmpp/iq_roster.rb +183 -0
- data/lib/agent_xmpp/xmpp/iq_version.rb +89 -0
- data/lib/agent_xmpp/xmpp/jid.rb +150 -0
- data/lib/agent_xmpp/xmpp/message.rb +82 -0
- data/lib/agent_xmpp/xmpp/presence.rb +127 -0
- data/lib/agent_xmpp/xmpp/sasl.rb +241 -0
- data/lib/agent_xmpp/xmpp/stanza.rb +107 -0
- data/lib/agent_xmpp/xmpp/x_data.rb +269 -0
- data/test/app/app.rb +134 -0
- data/test/cases/test_application_message_processing.rb +65 -0
- data/test/cases/test_errors.rb +24 -0
- data/test/cases/test_presence_management.rb +139 -0
- data/test/cases/test_roster_management.rb +214 -0
- data/test/cases/test_service_discovery.rb +168 -0
- data/test/cases/test_session_management.rb +120 -0
- data/test/cases/test_version_discovery.rb +67 -0
- data/test/helpers/matchers.rb +23 -0
- data/test/helpers/mocks.rb +82 -0
- data/test/helpers/test_case_extensions.rb +45 -0
- data/test/helpers/test_client.rb +44 -0
- data/test/helpers/test_delegate.rb +61 -0
- data/test/helpers/test_helper.rb +91 -0
- data/test/messages/application_messages.rb +206 -0
- data/test/messages/error_messages.rb +35 -0
- data/test/messages/presence_messages.rb +66 -0
- data/test/messages/roster_messages.rb +126 -0
- data/test/messages/service_discovery_messages.rb +201 -0
- data/test/messages/session_messages.rb +158 -0
- data/test/messages/version_discovery_messages.rb +69 -0
- data/test/peer/peer.rb +36 -0
- data/test/xml_messages.txt +197 -0
- metadata +82 -42
- data/bin/agent_xmpp +0 -56
- data/lib/agent_xmpp/app/chat_message_body_controller.rb +0 -16
- data/lib/agent_xmpp/app/map.rb +0 -45
- data/lib/agent_xmpp/app/routes.rb +0 -77
- data/lib/agent_xmpp/app/view.rb +0 -49
- data/lib/agent_xmpp/client/parser.rb +0 -71
- data/lib/agent_xmpp/patches/standard_library_patches.rb +0 -5
- data/lib/agent_xmpp/patches/xmpp4r_patches.rb +0 -3
- data/lib/agent_xmpp/patches/xmpp4r_patches/command.rb +0 -26
- data/lib/agent_xmpp/patches/xmpp4r_patches/iq.rb +0 -26
- data/lib/agent_xmpp/patches/xmpp4r_patches/x_data.rb +0 -116
- data/lib/agent_xmpp/utils.rb +0 -2
- data/lib/agent_xmpp/utils/logger.rb +0 -24
- data/lib/agent_xmpp/utils/roster.rb +0 -23
- data/lib/agent_xmpp/version.rb +0 -6
- data/test/agent_xmpp_test.rb +0 -7
- data/test/test_helper.rb +0 -10
data/lib/agent_xmpp/client.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
-
require 'agent_xmpp/client/
|
1
|
+
require 'agent_xmpp/client/response'
|
2
|
+
require 'agent_xmpp/client/message_pipe'
|
3
|
+
require 'agent_xmpp/client/message_delegate'
|
2
4
|
require 'agent_xmpp/client/connection'
|
3
5
|
require 'agent_xmpp/client/client'
|
6
|
+
require 'agent_xmpp/client/roster_model'
|
7
|
+
require 'agent_xmpp/client/services_model'
|
8
|
+
require 'agent_xmpp/client/publish_model'
|
@@ -4,192 +4,59 @@ module AgentXmpp
|
|
4
4
|
#####-------------------------------------------------------------------------------------------------------
|
5
5
|
class Client
|
6
6
|
|
7
|
+
#####-------------------------------------------------------------------------------------------------------
|
8
|
+
class << self
|
9
|
+
|
10
|
+
#### self
|
11
|
+
end
|
12
|
+
|
7
13
|
#---------------------------------------------------------------------------------------------------------
|
8
|
-
attr_reader :
|
14
|
+
attr_reader :connection
|
9
15
|
#---------------------------------------------------------------------------------------------------------
|
10
16
|
|
11
17
|
#.........................................................................................................
|
12
|
-
def initialize
|
13
|
-
@password = config['password']
|
14
|
-
@port = config['port'] || 5222
|
15
|
-
resource = config['resource'] || Socket.gethostname
|
16
|
-
@jid = Jabber::JID.new("#{config['jid']}/#{resource}")
|
17
|
-
@roster = Roster.new(@jid, config['contacts'])
|
18
|
+
def initialize
|
18
19
|
end
|
19
20
|
|
20
21
|
#.........................................................................................................
|
21
22
|
def connect
|
22
23
|
while (true)
|
23
24
|
EventMachine.run do
|
24
|
-
@connection = EventMachine.connect(jid.domain, port, Connection, self
|
25
|
+
@connection = EventMachine.connect(AgentXmpp.jid.domain, AgentXmpp.port, Connection, self)
|
25
26
|
end
|
27
|
+
Boot.call_if_implemented(:call_restarting_client, pipe)
|
26
28
|
sleep(10.0)
|
27
|
-
AgentXmpp.logger.warn "RESTARTING
|
29
|
+
AgentXmpp.logger.warn "RESTARTING CLIENT"
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
33
|
+
#.........................................................................................................
|
34
|
+
def close_connection
|
35
|
+
AgentXmpp.logger.info "CLOSE CONNECTION"
|
36
|
+
connection.close_connection_after_writing unless connection.nil?
|
37
|
+
end
|
38
|
+
|
31
39
|
#.........................................................................................................
|
32
40
|
def reconnect
|
33
41
|
AgentXmpp.logger.info "RECONNECTING"
|
34
|
-
connection.reconnect(jid.domain, port)
|
42
|
+
connection.reconnect(jid.domain, port) unless connection.nil?
|
35
43
|
end
|
36
44
|
|
37
45
|
#.........................................................................................................
|
38
|
-
def
|
39
|
-
connection
|
46
|
+
def pipe
|
47
|
+
connection.pipe
|
40
48
|
end
|
41
49
|
|
42
50
|
#.........................................................................................................
|
43
51
|
def add_delegate(delegate)
|
44
|
-
connection.add_delegate(delegate)
|
52
|
+
connection.pipe.add_delegate(delegate)
|
45
53
|
end
|
46
54
|
|
47
55
|
#.........................................................................................................
|
48
56
|
def remove_delegate(delegate)
|
49
|
-
connection.remove_delegate(delegate)
|
57
|
+
connection.pipe.remove_delegate(delegate)
|
50
58
|
end
|
51
59
|
|
52
|
-
#---------------------------------------------------------------------------------------------------------
|
53
|
-
# AgentXmpp::Connection delegate
|
54
|
-
#.........................................................................................................
|
55
|
-
# connection
|
56
|
-
#.........................................................................................................
|
57
|
-
def did_connect(client_connection)
|
58
|
-
AgentXmpp.logger.info "CONNECTED"
|
59
|
-
end
|
60
|
-
|
61
|
-
#.........................................................................................................
|
62
|
-
def did_disconnect(client_connection)
|
63
|
-
AgentXmpp.logger.warn "DISCONNECTED"
|
64
|
-
EventMachine::stop_event_loop
|
65
|
-
end
|
66
|
-
|
67
|
-
#.........................................................................................................
|
68
|
-
def did_not_connect(client_connection)
|
69
|
-
AgentXmpp.logger.warn "CONNECTION FAILED"
|
70
|
-
end
|
71
|
-
|
72
|
-
#.........................................................................................................
|
73
|
-
# authentication
|
74
|
-
#.........................................................................................................
|
75
|
-
def did_authenticate(client_connection, stanza)
|
76
|
-
AgentXmpp.logger.info "AUTHENTICATED"
|
77
|
-
end
|
78
|
-
|
79
|
-
#.........................................................................................................
|
80
|
-
def did_not_authenticate(client_connection, stanza)
|
81
|
-
AgentXmpp.logger.info "AUTHENTICATION FAILED"
|
82
|
-
end
|
83
|
-
|
84
|
-
#.........................................................................................................
|
85
|
-
def did_bind(client_connection, stanza)
|
86
|
-
AgentXmpp.logger.info "BIND ACKNOWLEDGED"
|
87
|
-
end
|
88
|
-
|
89
|
-
#.........................................................................................................
|
90
|
-
# presence
|
91
|
-
#.........................................................................................................
|
92
|
-
def did_receive_presence(client_connection, presence)
|
93
|
-
from_jid = presence.from.to_s
|
94
|
-
from_bare_jid = presence.from.bare.to_s
|
95
|
-
if roster.has_key?(from_bare_jid)
|
96
|
-
roster[from_bare_jid.to_s][:resources][from_jid] = {} if roster[from_bare_jid.to_s][:resources][from_jid].nil?
|
97
|
-
roster[from_bare_jid.to_s][:resources][from_jid][:presence] = presence
|
98
|
-
client_connection.get_client_version(from_jid) if not from_jid.eql?(client_connection.jid.to_s) and presence.type.nil?
|
99
|
-
AgentXmpp.logger.info "RECEIVED PRESENCE FROM: #{from_jid}"
|
100
|
-
else
|
101
|
-
AgentXmpp.logger.info "RECEIVED PRESENCE FROM JID NOT IN CONTACT LIST: #{from_jid}"
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
#.........................................................................................................
|
106
|
-
def did_receive_subscribe_request(client_connection, presence)
|
107
|
-
from_jid = presence.from.to_s
|
108
|
-
if roster.has_key?(presence.from.bare.to_s )
|
109
|
-
client_connection.accept_contact_request(from_jid)
|
110
|
-
AgentXmpp.logger.info "RECEIVED SUBSCRIBE REQUEST: #{from_jid}"
|
111
|
-
else
|
112
|
-
client_connection.reject_contact_request(from_jid)
|
113
|
-
AgentXmpp.logger.info "RECEIVED SUBSCRIBE REQUEST FROM JID NOT IN CONTACT LIST: #{from_jid}"
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
#.........................................................................................................
|
118
|
-
def did_receive_unsubscribed_request(client_connection, presence)
|
119
|
-
from_jid = presence.from.to_s
|
120
|
-
if roster.delete(presence.from.bare.to_s )
|
121
|
-
client_connection.remove_contact(presence.from)
|
122
|
-
AgentXmpp.logger.info "RECEIVED UNSUBSCRIBED REQUEST: #{from_jid}"
|
123
|
-
else
|
124
|
-
AgentXmpp.logger.warn "RECEIVED UNSUBSCRIBED REQUEST FROM JID NOT IN CONTACT LIST: #{from_jid}"
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
#.........................................................................................................
|
129
|
-
# roster management
|
130
|
-
#.........................................................................................................
|
131
|
-
def did_receive_roster_item(client_connection, roster_item)
|
132
|
-
AgentXmpp.logger.info "RECEIVED ROSTER ITEM"
|
133
|
-
roster_item_jid = roster_item.jid.to_s
|
134
|
-
if roster.has_key?(roster_item_jid)
|
135
|
-
roster[roster_item_jid][:activated] = true
|
136
|
-
roster[roster_item_jid][:roster_item] = roster_item
|
137
|
-
AgentXmpp.logger.info "ACTIVATING CONTACT: #{roster_item_jid}"
|
138
|
-
else
|
139
|
-
client_connection.remove_contact(roster_item.jid)
|
140
|
-
AgentXmpp.logger.info "REMOVING CONTACT: #{roster_item_jid}"
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
#.........................................................................................................
|
145
|
-
def did_remove_roster_item(client_connection, roster_item)
|
146
|
-
AgentXmpp.logger.info "REMOVE ROSTER ITEM"
|
147
|
-
roster_item_jid = roster_item.jid.to_s
|
148
|
-
if roster.has_key?(roster_item_jid)
|
149
|
-
roster.delete(roster_item_jid)
|
150
|
-
AgentXmpp.logger.info "REMOVED CONTACT: #{roster_item_jid}"
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
#.........................................................................................................
|
155
|
-
def did_receive_all_roster_items(client_connection)
|
156
|
-
AgentXmpp.logger.info "RECEIVED ALL ROSTER ITEMS"
|
157
|
-
roster.select{|j,r| not r[:activated]}.each do |j,r|
|
158
|
-
AgentXmpp.logger.info "ADDING CONTACT: #{j}"
|
159
|
-
client_connection.add_contact(Jabber::JID.new(j))
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
#.........................................................................................................
|
164
|
-
def did_acknowledge_add_contact(client_connection, response, contact_jid)
|
165
|
-
AgentXmpp.logger.info "CONTACT ADD ACKNOWLEDGED: #{contact_jid.to_s}"
|
166
|
-
end
|
167
|
-
|
168
|
-
#.........................................................................................................
|
169
|
-
def did_remove_contact(client_connection, response, contact_jid)
|
170
|
-
AgentXmpp.logger.info "CONTACT REMOVED: #{contact_jid.to_s}"
|
171
|
-
end
|
172
|
-
|
173
|
-
#.........................................................................................................
|
174
|
-
def did_add_contact(client_connection, roster_item)
|
175
|
-
AgentXmpp.logger.info "CONTACT ADDED: #{roster_item.jid.to_s}"
|
176
|
-
end
|
177
|
-
|
178
|
-
#.........................................................................................................
|
179
|
-
# service discovery management
|
180
|
-
#.........................................................................................................
|
181
|
-
def did_receive_client_version_result(client_connection, from, version)
|
182
|
-
roster[from.bare.to_s][:resources][from.to_s][:version] = version \
|
183
|
-
unless roster[from.bare.to_s][:resources][from.to_s].nil?
|
184
|
-
AgentXmpp.logger.info "RECEIVED CLIENT VERSION RESULT: #{from.to_s}, #{version.iname}, #{version.version}"
|
185
|
-
end
|
186
|
-
|
187
|
-
#.........................................................................................................
|
188
|
-
def did_receive_client_version_request(client_connection, request)
|
189
|
-
client_connection.send_client_version(request)
|
190
|
-
AgentXmpp.logger.info "RECEIVED CLIENT VERSION REQUEST: #{request.from.to_s}"
|
191
|
-
end
|
192
|
-
|
193
60
|
#### Client
|
194
61
|
end
|
195
62
|
|
@@ -1,67 +1,35 @@
|
|
1
1
|
##############################################################################################################
|
2
2
|
module AgentXmpp
|
3
3
|
|
4
|
-
#####-------------------------------------------------------------------------------------------------------
|
5
|
-
class NotConnected < Exception; end
|
6
|
-
|
7
4
|
#####-------------------------------------------------------------------------------------------------------
|
8
5
|
class Connection < EventMachine::Connection
|
9
6
|
|
10
7
|
#---------------------------------------------------------------------------------------------------------
|
11
|
-
include
|
8
|
+
include EventMachine::XmlPushParser
|
12
9
|
#---------------------------------------------------------------------------------------------------------
|
13
10
|
|
14
11
|
#---------------------------------------------------------------------------------------------------------
|
15
|
-
attr_reader :client, :
|
12
|
+
attr_reader :client, :delegates, :keepalive, :pipe
|
16
13
|
#---------------------------------------------------------------------------------------------------------
|
17
14
|
|
18
15
|
#.........................................................................................................
|
19
|
-
def initialize(client
|
20
|
-
@client
|
21
|
-
@
|
22
|
-
@id_callbacks = {}
|
23
|
-
@delegates = []
|
16
|
+
def initialize(client)
|
17
|
+
@client = client
|
18
|
+
@pipe = MessagePipe.new(self)
|
24
19
|
end
|
25
20
|
|
26
|
-
#.........................................................................................................
|
27
|
-
def add_delegate(delegate)
|
28
|
-
@delegates << delegate
|
29
|
-
end
|
30
|
-
|
31
|
-
#.........................................................................................................
|
32
|
-
def remove_delegate(delegate)
|
33
|
-
@delegates.delete(delegate)
|
34
|
-
end
|
35
|
-
|
36
|
-
#.........................................................................................................
|
37
|
-
def send(data, &blk)
|
38
|
-
raise NotConnected if error?
|
39
|
-
if block_given? and data.is_a? Jabber::XMPPStanza
|
40
|
-
if data.id.nil?
|
41
|
-
data.id = Jabber::IdGenerator.instance.generate_id
|
42
|
-
end
|
43
|
-
@id_callbacks[data.id] = blk
|
44
|
-
end
|
45
|
-
send_data(data.to_s)
|
46
|
-
AgentXmpp.logger.info "SEND: #{data.to_s}"
|
47
|
-
end
|
48
|
-
|
49
21
|
#---------------------------------------------------------------------------------------------------------
|
50
22
|
# EventMachine::Connection callbacks
|
51
23
|
#.........................................................................................................
|
52
24
|
def connection_completed
|
53
|
-
init_connection
|
54
25
|
@keepalive = EventMachine::PeriodicTimer.new(60) do
|
55
26
|
send_data("\n")
|
56
27
|
end
|
57
|
-
|
58
|
-
AgentXmpp::Boot.call_after_connection_completed(self) if AgentXmpp::Boot.respond_to?(:call_after_connection_completed)
|
59
|
-
broadcast_to_delegates(:did_connect, self)
|
28
|
+
pipe.connection_completed
|
60
29
|
end
|
61
30
|
|
62
31
|
#.........................................................................................................
|
63
32
|
def receive_data(data)
|
64
|
-
AgentXmpp.logger.info "RECV: #{data.to_s}"
|
65
33
|
super(data)
|
66
34
|
end
|
67
35
|
|
@@ -71,242 +39,65 @@ module AgentXmpp
|
|
71
39
|
@keepalive.cancel
|
72
40
|
@keepalive = nil
|
73
41
|
end
|
74
|
-
|
75
|
-
broadcast_to_delegates(:did_disconnect, self)
|
76
|
-
end
|
77
|
-
|
78
|
-
#---------------------------------------------------------------------------------------------------------
|
79
|
-
# service discovery
|
80
|
-
#.........................................................................................................
|
81
|
-
def get_client_version(contact_jid)
|
82
|
-
iq = Jabber::Iq.new(:get, contact_jid)
|
83
|
-
iq.query = Jabber::Version::IqQueryVersion.new
|
84
|
-
send(iq) do |r|
|
85
|
-
if (r.type == :result) && r.query.kind_of?(Jabber::Version::IqQueryVersion)
|
86
|
-
broadcast_to_delegates(:did_receive_client_version_result, self, r.from, r.query)
|
87
|
-
end
|
88
|
-
end
|
42
|
+
pipe.unbind
|
89
43
|
end
|
90
44
|
|
91
|
-
#.........................................................................................................
|
92
|
-
def send_client_version(request)
|
93
|
-
iq = Jabber::Iq.new(:result, request.from.to_s)
|
94
|
-
iq.id = request.id unless request.id.nil?
|
95
|
-
iq.query = Jabber::Version::IqQueryVersion.new
|
96
|
-
iq.query.set_iname(AgentXmpp::AGENT_XMPP_NAME).set_version(AgentXmpp::VERSION).set_os(AgentXmpp::OS_VERSION)
|
97
|
-
send(iq)
|
98
|
-
end
|
99
|
-
|
100
45
|
#---------------------------------------------------------------------------------------------------------
|
101
|
-
#
|
102
|
-
#.........................................................................................................
|
103
|
-
def get_roster
|
104
|
-
send(Jabber::Iq.new_rosterget) do |r|
|
105
|
-
if r.type == :result and r.query.kind_of?(Jabber::Roster::IqQueryRoster)
|
106
|
-
r.query.each_element {|i| broadcast_to_delegates(:did_receive_roster_item, self, i)}
|
107
|
-
broadcast_to_delegates(:did_receive_all_roster_items, self)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
#.........................................................................................................
|
113
|
-
def add_contact(contact_jid)
|
114
|
-
request = Jabber::Iq.new_rosterset
|
115
|
-
request.query.add(Jabber::Roster::RosterItem.new(contact_jid))
|
116
|
-
send(request) do |r|
|
117
|
-
send(Jabber::Presence.new.set_type(:subscribe).set_to(contact_jid))
|
118
|
-
broadcast_to_delegates(:did_acknowledge_add_contact, self, r, contact_jid)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
#.........................................................................................................
|
123
|
-
def remove_contact(contact_jid)
|
124
|
-
request = Jabber::Iq.new_rosterset
|
125
|
-
request.query.add(Jabber::Roster::RosterItem.new(contact_jid, nil, :remove))
|
126
|
-
send(request) do |r|
|
127
|
-
broadcast_to_delegates(:did_remove_contact, self, r, contact_jid)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
#.........................................................................................................
|
132
|
-
def accept_contact_request(contact_jid)
|
133
|
-
presence = Jabber::Presence.new.set_type(:subscribed)
|
134
|
-
presence.to = contact_jid
|
135
|
-
send(presence)
|
136
|
-
end
|
137
|
-
|
46
|
+
# EventMachine::XmlPushParser callbacks
|
138
47
|
#.........................................................................................................
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
send(presence)
|
143
|
-
end
|
144
|
-
|
145
|
-
#---------------------------------------------------------------------------------------------------------
|
146
|
-
# process commands
|
48
|
+
def start_document
|
49
|
+
end
|
50
|
+
|
147
51
|
#.........................................................................................................
|
148
|
-
def
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
52
|
+
def start_element(name, attrs)
|
53
|
+
e = REXML::Element.new(name)
|
54
|
+
e.add_attributes(attrs)
|
55
|
+
@current = @current.nil? ? e : @current.add_element(e)
|
56
|
+
if @current.xpath == 'stream:stream'
|
57
|
+
process
|
58
|
+
@current = nil
|
155
59
|
end
|
156
60
|
end
|
157
|
-
|
158
|
-
#---------------------------------------------------------------------------------------------------------
|
159
|
-
# process messages
|
160
|
-
#.........................................................................................................
|
161
|
-
def process_chat_message_body(stanza)
|
162
|
-
params = {:xmlns => 'message:chat', :to => stanza.from.to_s, :from => stanza.from.to_s, :id => stanza.id,
|
163
|
-
:body => stanza.body}
|
164
|
-
Routing::Routes.invoke_chat_message_body_response(self, params)
|
165
|
-
AgentXmpp.logger.info "RECEIVED MESSAGE BODY: #{stanza.body}"
|
166
|
-
end
|
167
|
-
|
168
|
-
#---------------------------------------------------------------------------------------------------------
|
169
|
-
# AgentXmpp::Parser callbacks
|
61
|
+
|
170
62
|
#.........................................................................................................
|
171
|
-
def
|
172
|
-
if
|
173
|
-
@
|
174
|
-
blk.call(stanza)
|
175
|
-
return
|
176
|
-
end
|
177
|
-
|
178
|
-
case stanza.xpath
|
179
|
-
when 'stream:features'
|
180
|
-
@stream_features, @stream_mechanisms = {}, []
|
181
|
-
@current.each do |e|
|
182
|
-
if e.name == 'mechanisms' and e.namespace == 'urn:ietf:params:xml:ns:xmpp-sasl'
|
183
|
-
e.each_element('mechanism') {|mech| @stream_mechanisms.push(mech.text)}
|
184
|
-
else
|
185
|
-
@stream_features[e.name] = e.namespace
|
186
|
-
end
|
187
|
-
end
|
188
|
-
if @connection_status.eql?(:offline)
|
189
|
-
authenticate
|
190
|
-
elsif @connection_status.eql?(:authenticated)
|
191
|
-
bind(stanza)
|
192
|
-
end
|
193
|
-
when 'stream:stream'
|
194
|
-
when 'success'
|
195
|
-
case connection_status
|
196
|
-
when :offline
|
197
|
-
reset_parser
|
198
|
-
init_connection(false)
|
199
|
-
@connection_status = :authenticated
|
200
|
-
end
|
201
|
-
return
|
202
|
-
when 'failure'
|
203
|
-
case connection_status
|
204
|
-
when :offline
|
205
|
-
reset_parser
|
206
|
-
broadcast_to_delegates(:did_not_authenticate, self, stanza)
|
207
|
-
end
|
63
|
+
def end_element(name)
|
64
|
+
if @current.parent
|
65
|
+
@current = @current.parent
|
208
66
|
else
|
209
|
-
|
67
|
+
process
|
68
|
+
@current = nil
|
210
69
|
end
|
211
|
-
|
212
70
|
end
|
213
71
|
|
214
|
-
#---------------------------------------------------------------------------------------------------------
|
215
|
-
protected
|
216
|
-
#---------------------------------------------------------------------------------------------------------
|
217
|
-
|
218
|
-
#---------------------------------------------------------------------------------------------------------
|
219
|
-
# Process XMPP messages
|
220
72
|
#.........................................................................................................
|
221
|
-
def
|
222
|
-
|
223
|
-
Jabber::SASL.new(self, 'PLAIN').auth(password)
|
224
|
-
rescue
|
225
|
-
raise ClientAuthenticationFailure.new, $!.to_s
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
#.........................................................................................................
|
230
|
-
def bind(stanza)
|
231
|
-
if stream_features.has_key?('bind')
|
232
|
-
iq = Jabber::Iq.new(:set)
|
233
|
-
bind = iq.add(REXML::Element.new('bind'))
|
234
|
-
bind.add_namespace(stream_features['bind'])
|
235
|
-
resource = bind.add REXML::Element.new('resource')
|
236
|
-
resource.text = jid.resource
|
237
|
-
send(iq) do |r|
|
238
|
-
if r.type == :result and full_jid = r.first_element('//jid') and full_jid.text
|
239
|
-
@connection_status = :bind
|
240
|
-
jid = Jabber::JID.new(full_jid.text) unless jid.to_s.eql?(full_jid.text)
|
241
|
-
broadcast_to_delegates(:did_bind, self, stanza)
|
242
|
-
session(stanza)
|
243
|
-
end
|
244
|
-
end
|
245
|
-
end
|
73
|
+
def characters(text)
|
74
|
+
@current.text = @current.text.to_s + text if @current
|
246
75
|
end
|
247
76
|
|
248
77
|
#.........................................................................................................
|
249
|
-
def
|
250
|
-
|
251
|
-
iq = Jabber::Iq.new(:set)
|
252
|
-
session = iq.add REXML::Element.new('session')
|
253
|
-
session.add_namespace stream_features['session']
|
254
|
-
send(iq) do |r|
|
255
|
-
if r.type == :result
|
256
|
-
@connection_status = :active
|
257
|
-
broadcast_to_delegates(:did_authenticate, self, stanza)
|
258
|
-
send(Jabber::Presence.new(nil, nil, 1))
|
259
|
-
get_roster
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
78
|
+
def error(*args)
|
79
|
+
AgentXmpp.logger.error *args
|
263
80
|
end
|
264
81
|
|
265
82
|
#.........................................................................................................
|
266
|
-
def
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
def demux_channel(stanza)
|
273
|
-
stanza_class = stanza.class.to_s
|
274
|
-
#### roster update
|
275
|
-
if stanza.type == :set and stanza.query.kind_of?(Jabber::Roster::IqQueryRoster)
|
276
|
-
stanza.query.each_element do |i|
|
277
|
-
method = case i.subscription
|
278
|
-
when :remove then :did_remove_roster_item
|
279
|
-
when :none then :did_receive_roster_item
|
280
|
-
when :to then :did_add_contact
|
281
|
-
end
|
282
|
-
broadcast_to_delegates(method, self, i) unless method.nil?
|
283
|
-
end
|
284
|
-
#### presence subscription request
|
285
|
-
elsif stanza.type.eql?(:subscribe) and stanza_class.eql?('Jabber::Presence')
|
286
|
-
broadcast_to_delegates(:did_receive_subscribe_request, self, stanza)
|
287
|
-
#### presence unsubscribe
|
288
|
-
elsif stanza.type.eql?(:unsubscribed) and stanza_class.eql?('Jabber::Presence')
|
289
|
-
broadcast_to_delegates(:did_receive_unsubscribed_request, self, stanza)
|
290
|
-
#### client version request
|
291
|
-
elsif stanza.type.eql?(:get) and stanza.query.kind_of?(Jabber::Version::IqQueryVersion)
|
292
|
-
broadcast_to_delegates(:did_receive_client_version_request, self, stanza)
|
293
|
-
#### received command
|
294
|
-
elsif stanza.type.eql?(:set) and stanza.command.kind_of?(Jabber::Command::IqCommand)
|
295
|
-
process_command(stanza)
|
296
|
-
#### chat message received
|
297
|
-
elsif stanza_class.eql?('Jabber::Message') and stanza.type.eql?(:chat) and stanza.respond_to?(:body)
|
298
|
-
process_chat_message_body(stanza)
|
299
|
-
else
|
300
|
-
method = ('did_receive_' + /.*::(.*)/.match(stanza_class).to_a.last.downcase).to_sym
|
301
|
-
broadcast_to_delegates(method, self, stanza)
|
83
|
+
def process
|
84
|
+
@current.add_namespace(@streamns) if @current.namespace('').to_s.eql?('')
|
85
|
+
begin
|
86
|
+
stanza = Xmpp::Stanza::import(@current)
|
87
|
+
rescue Xmpp::NoNameXmlnsRegistered
|
88
|
+
stanza = @current
|
302
89
|
end
|
90
|
+
if @current.xpath.eql?('stream:stream')
|
91
|
+
@streamns = @current.namespace('') if @current.namespace('')
|
92
|
+
end
|
93
|
+
receive(stanza) if respond_to?(:receive)
|
303
94
|
end
|
304
95
|
|
305
96
|
#.........................................................................................................
|
306
|
-
def
|
307
|
-
|
97
|
+
def receive(stanza)
|
98
|
+
pipe.receive(stanza)
|
308
99
|
end
|
309
|
-
|
100
|
+
|
310
101
|
#### Connection
|
311
102
|
end
|
312
103
|
|