jabber4r-revive 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. data/.gitignore +5 -4
  2. data/.rspec +3 -3
  3. data/.travis.yml +7 -7
  4. data/CHANGELOG +11 -1
  5. data/Gemfile +3 -3
  6. data/README.md +29 -29
  7. data/Rakefile +70 -70
  8. data/jabber4r-revive.gemspec +25 -25
  9. data/lib/jabber4r.rb +38 -33
  10. data/lib/jabber4r/bosh.rb +21 -0
  11. data/lib/jabber4r/bosh/authentication.rb +13 -0
  12. data/lib/jabber4r/bosh/authentication/non_sasl.rb +219 -0
  13. data/lib/jabber4r/bosh/authentication/sasl.rb +239 -0
  14. data/lib/jabber4r/bosh/session.rb +144 -0
  15. data/lib/jabber4r/connection.rb +259 -258
  16. data/lib/jabber4r/debugger.rb +60 -60
  17. data/lib/jabber4r/jid.rb +20 -19
  18. data/lib/jabber4r/protocol.rb +249 -257
  19. data/lib/jabber4r/protocol/authentication.rb +14 -0
  20. data/lib/jabber4r/protocol/authentication/non_sasl.rb +138 -0
  21. data/lib/jabber4r/protocol/authentication/sasl.rb +88 -0
  22. data/lib/jabber4r/protocol/iq.rb +259 -259
  23. data/lib/jabber4r/protocol/message.rb +245 -245
  24. data/lib/jabber4r/protocol/parsed_xml_element.rb +207 -207
  25. data/lib/jabber4r/protocol/presence.rb +160 -160
  26. data/lib/jabber4r/protocol/xml_element.rb +143 -143
  27. data/lib/jabber4r/rexml_1.8_patch.rb +15 -15
  28. data/lib/jabber4r/roster.rb +38 -38
  29. data/lib/jabber4r/session.rb +615 -615
  30. data/lib/jabber4r/version.rb +10 -3
  31. data/spec/lib/jabber4r/bosh/authentication/non_sasl_spec.rb +79 -0
  32. data/spec/lib/jabber4r/bosh/authentication/sasl_spec.rb +42 -0
  33. data/spec/lib/jabber4r/bosh/session_spec.rb +406 -0
  34. data/spec/lib/jabber4r/bosh_spec.rb +0 -0
  35. data/spec/lib/jabber4r/connection_spec.rb +174 -174
  36. data/spec/lib/jabber4r/debugger_spec.rb +35 -35
  37. data/spec/lib/jabber4r/jid_spec.rb +197 -197
  38. data/spec/lib/jabber4r/protocol/authentication/non_sasl_spec.rb +79 -0
  39. data/spec/lib/jabber4r/protocol/authentication/sasl_spec.rb +42 -0
  40. data/spec/spec_helper.rb +11 -11
  41. data/spec/support/mocks/tcp_socket_mock.rb +8 -8
  42. metadata +61 -45
  43. data/Gemfile.lock +0 -45
  44. data/lib/jabber4r/bosh_session.rb +0 -224
  45. data/spec/lib/jabber4r/bosh_session_spec.rb +0 -150
@@ -1,246 +1,246 @@
1
- # coding: utf-8
2
-
3
- # License: see LICENSE
4
- # Jabber4R - Jabber Instant Messaging Library for Ruby
5
- # Copyright (C) 2002 Rich Kilmer <rich@infoether.com>
6
-
7
- module Jabber::Protocol
8
- class Message
9
- attr_accessor :to, :from, :id, :type, :body, :xhtml, :subject, :thread, :x, :oobData, :errorcode, :error
10
- NORMAL = "normal"
11
- ERROR="error"
12
- CHAT="chat"
13
- GROUPCHAT="groupchat"
14
- HEADLINE="headline"
15
-
16
- ##
17
- # Factory to build a Message from an XMLElement
18
- #
19
- # session:: [Jabber::Session] The Jabber session instance
20
- # element:: [Jabber::Protocol::ParsedXMLElement] The received XML object
21
- # return:: [Jabber::Protocol::Message] The newly created Message object
22
- #
23
- def self.from_element(session, element)
24
- message = Message.new(element.attr_to)
25
- message.from = Jabber::JID.new(element.attr_from) if element.attr_from
26
- message.type = element.attr_type
27
- message.id = element.attr_id
28
- message.thread = element.thread.element_data
29
- message.body = element.body.element_data
30
- message.xhtml = element.xhtml.element_data
31
- message.subject = element.subject.element_data
32
- message.oobData = element.x.element_data
33
- message.session=session
34
- return message
35
- end
36
-
37
- ##
38
- # Creates a Message
39
- #
40
- # to:: [String | Jabber::JID] The jabber id to send this message to (or from)
41
- # type:: [Integer=NORMAL] The type of message...Message::(NORMAL, CHAT, GROUPCHAT, HEADLINE)
42
- #
43
- def initialize(to, type=NORMAL)
44
- return unless to
45
- to = Jabber::JID.new(to) if to.kind_of? String
46
- @to = to if to.kind_of? Jabber::JID
47
- @type = type
48
- end
49
-
50
- ##
51
- # Chaining method...sets the body of the message
52
- #
53
- # body:: [String] The message body
54
- # return:: [Jabber::Protocol::Message] The current Message object
55
- #
56
- def set_body(body)
57
- @body = body.gsub(/[&]/, '&amp;').gsub(/[<]/, '&lt;').gsub(/[']/, '&apos;')
58
- self
59
- end
60
-
61
- ##
62
- # Chaining method...sets the subject of the message
63
- #
64
- # subject:: [String] The message subject
65
- # return:: [Jabber::Protocol::Message] The current Message object
66
- #
67
- def set_subject(subject)
68
- @subject = subject.gsub(/[&]/, '&amp;').gsub(/[<]/, '&lt;').gsub(/[']/, '&apos;')
69
- self
70
- end
71
-
72
- ##
73
- # Chaining method...sets the XHTML body of the message
74
- #
75
- # body:: [String] The message body
76
- # return:: [Jabber::Protocol::Message] The current message object
77
- #
78
- def set_xhtml(xhtml)
79
- @xhtml=xhtml
80
- self
81
- end
82
-
83
- ##
84
- # Chaining method...sets the thread of the message
85
- #
86
- # thread:: [String] The message thread id
87
- # return:: [Jabber::Protocol::Message] The current Message object
88
- #
89
- def set_thread(thread)
90
- @thread = thread
91
- self
92
- end
93
-
94
- ##
95
- # Chaining method...sets the OOB data of the message
96
- #
97
- # data:: [String] The message OOB data
98
- # return:: [Jabber::Protocol::Message] The current Message object
99
- #
100
- def set_outofband(data)
101
- @oobData = data
102
- self
103
- end
104
-
105
- ##
106
- # Chaining method...sets the extended data of the message
107
- #
108
- # x:: [String] The message x data
109
- # return:: [Jabber::Protocol::Message] The current Message object
110
- #
111
- def set_x(x)
112
- @x = x
113
- self
114
- end
115
-
116
- ##
117
- # Sets an error code to be returned(chaining method)
118
- #
119
- # code:: [Integer] the jabber error code
120
- # reason:: [String] Why the error was reported
121
- # return:: [Jabber::Protocol::Message] The current Message object
122
- #
123
-
124
- def set_error(code,reason)
125
- @errorcode=code
126
- @error=reason
127
- @type="error"
128
- self
129
- end
130
-
131
- ##
132
- # Convenience method for send(true)
133
- #
134
- # ttl:: [Integer = nil] The time (in seconds) to wait for a reply before assuming nil
135
- # &block:: [Block] A block to process the message replies
136
- #
137
- def request(ttl=nil, &block)
138
- send(true, ttl, &block)
139
- end
140
-
141
- ##
142
- # Sends the message to the Jabber service for delivery
143
- #
144
- # wait:: [Boolean = false] Wait for reply before return?
145
- # ttl:: [Integer = nil] The time (in seconds) to wait for a reply before assuming nil
146
- # &block:: [Block] A block to process the message replies
147
- #
148
- def send(wait=false, ttl=nil, &block)
149
- if wait
150
- message = nil
151
- blockedThread = Thread.current
152
- timer_thread = nil
153
- timeout = false
154
- unless ttl.nil?
155
- timer_thread = Thread.new {
156
- sleep ttl
157
- timeout = true
158
- blockedThread.wakeup
159
- }
160
- end
161
- @session.connection.send(self.to_s, block) do |je|
162
- if je.element_tag == "message" and je.thread.element_data == @thread
163
- je.consume_element
164
- message = Message.from_element(@session, je)
165
- blockedThread.wakeup unless timeout
166
- unless timer_thread.nil?
167
- timer_thread.kill
168
- timer_thread = nil
169
- end
170
- end
171
- end
172
- Thread.stop
173
- return message
174
- else
175
- @session.connection.send(self.to_s, block) if @session
176
- end
177
- end
178
-
179
- ##
180
- # Sets the session instance
181
- #
182
- # session:: [Jabber::Session] The session instance
183
- # return:: [Jabber::Protocol::Message] The current Message object
184
- #
185
- def session=(session)
186
- @session = session
187
- self
188
- end
189
-
190
- ##
191
- # Builds a reply to an existing message by setting:
192
- # 1. to = from
193
- # 2. id = id
194
- # 3. thread = thread
195
- # 4. type = type
196
- # 5. session = session
197
- #
198
- # return:: [Jabber::Protocol::Message] The reply message
199
- #
200
- def reply
201
- message = Message.new(nil)
202
- message.to = @from
203
- message.id = @id
204
- message.thread = @thread
205
- message.type = @type
206
- message.session = @session
207
- @is_reply = true
208
- return message
209
- end
210
-
211
- ##
212
- # Generates XML that complies with the Jabber protocol for
213
- # sending the message through the Jabber service.
214
- #
215
- # return:: [String] The XML string.
216
- #
217
- def to_xml
218
- @thread = Jabber.gen_random_thread if @thread.nil? and (not @is_reply)
219
- elem = XMLElement.new("message", {"to"=>@to, "type"=>@type})
220
- elem.add_attribute("id", @id) if @id
221
- elem.add_child("thread").add_data(@thread) if @thread
222
- elem.add_child("subject").add_data(@subject) if @subject
223
- elem.add_child("body").add_data(@body) if @body
224
- if @xhtml then
225
- t=elem.add_child("xhtml").add_attribute("xmlns","http://www.w3.org/1999/xhtml")
226
- t.add_child("body").add_data(@xhtml)
227
- end
228
- if @type=="error" then
229
- e=elem.add_child("error");
230
- e.add_attribute("code",@errorcode) if @errorcode
231
- e.add_data(@error) if @error
232
- end
233
- elem.add_child("x").add_attribute("xmlns", "jabber:x:oob").add_data(@oobData) if @oobData
234
- elem.add_xml(@x.to_s) if @x
235
- return elem.to_s
236
- end
237
-
238
- ##
239
- # see to_xml
240
- #
241
- def to_s
242
- to_xml
243
- end
244
-
245
- end
1
+ # coding: utf-8
2
+
3
+ # License: see LICENSE
4
+ # Jabber4R - Jabber Instant Messaging Library for Ruby
5
+ # Copyright (C) 2002 Rich Kilmer <rich@infoether.com>
6
+
7
+ module Jabber::Protocol
8
+ class Message
9
+ attr_accessor :to, :from, :id, :type, :body, :xhtml, :subject, :thread, :x, :oobData, :errorcode, :error
10
+ NORMAL = "normal"
11
+ ERROR="error"
12
+ CHAT="chat"
13
+ GROUPCHAT="groupchat"
14
+ HEADLINE="headline"
15
+
16
+ ##
17
+ # Factory to build a Message from an XMLElement
18
+ #
19
+ # session:: [Jabber::Session] The Jabber session instance
20
+ # element:: [Jabber::Protocol::ParsedXMLElement] The received XML object
21
+ # return:: [Jabber::Protocol::Message] The newly created Message object
22
+ #
23
+ def self.from_element(session, element)
24
+ message = Message.new(element.attr_to)
25
+ message.from = Jabber::JID.new(element.attr_from) if element.attr_from
26
+ message.type = element.attr_type
27
+ message.id = element.attr_id
28
+ message.thread = element.thread.element_data
29
+ message.body = element.body.element_data
30
+ message.xhtml = element.xhtml.element_data
31
+ message.subject = element.subject.element_data
32
+ message.oobData = element.x.element_data
33
+ message.session=session
34
+ return message
35
+ end
36
+
37
+ ##
38
+ # Creates a Message
39
+ #
40
+ # to:: [String | Jabber::JID] The jabber id to send this message to (or from)
41
+ # type:: [Integer=NORMAL] The type of message...Message::(NORMAL, CHAT, GROUPCHAT, HEADLINE)
42
+ #
43
+ def initialize(to, type=NORMAL)
44
+ return unless to
45
+ to = Jabber::JID.new(to) if to.kind_of? String
46
+ @to = to if to.kind_of? Jabber::JID
47
+ @type = type
48
+ end
49
+
50
+ ##
51
+ # Chaining method...sets the body of the message
52
+ #
53
+ # body:: [String] The message body
54
+ # return:: [Jabber::Protocol::Message] The current Message object
55
+ #
56
+ def set_body(body)
57
+ @body = body.gsub(/[&]/, '&amp;').gsub(/[<]/, '&lt;').gsub(/[']/, '&apos;')
58
+ self
59
+ end
60
+
61
+ ##
62
+ # Chaining method...sets the subject of the message
63
+ #
64
+ # subject:: [String] The message subject
65
+ # return:: [Jabber::Protocol::Message] The current Message object
66
+ #
67
+ def set_subject(subject)
68
+ @subject = subject.gsub(/[&]/, '&amp;').gsub(/[<]/, '&lt;').gsub(/[']/, '&apos;')
69
+ self
70
+ end
71
+
72
+ ##
73
+ # Chaining method...sets the XHTML body of the message
74
+ #
75
+ # body:: [String] The message body
76
+ # return:: [Jabber::Protocol::Message] The current message object
77
+ #
78
+ def set_xhtml(xhtml)
79
+ @xhtml=xhtml
80
+ self
81
+ end
82
+
83
+ ##
84
+ # Chaining method...sets the thread of the message
85
+ #
86
+ # thread:: [String] The message thread id
87
+ # return:: [Jabber::Protocol::Message] The current Message object
88
+ #
89
+ def set_thread(thread)
90
+ @thread = thread
91
+ self
92
+ end
93
+
94
+ ##
95
+ # Chaining method...sets the OOB data of the message
96
+ #
97
+ # data:: [String] The message OOB data
98
+ # return:: [Jabber::Protocol::Message] The current Message object
99
+ #
100
+ def set_outofband(data)
101
+ @oobData = data
102
+ self
103
+ end
104
+
105
+ ##
106
+ # Chaining method...sets the extended data of the message
107
+ #
108
+ # x:: [String] The message x data
109
+ # return:: [Jabber::Protocol::Message] The current Message object
110
+ #
111
+ def set_x(x)
112
+ @x = x
113
+ self
114
+ end
115
+
116
+ ##
117
+ # Sets an error code to be returned(chaining method)
118
+ #
119
+ # code:: [Integer] the jabber error code
120
+ # reason:: [String] Why the error was reported
121
+ # return:: [Jabber::Protocol::Message] The current Message object
122
+ #
123
+
124
+ def set_error(code,reason)
125
+ @errorcode=code
126
+ @error=reason
127
+ @type="error"
128
+ self
129
+ end
130
+
131
+ ##
132
+ # Convenience method for send(true)
133
+ #
134
+ # ttl:: [Integer = nil] The time (in seconds) to wait for a reply before assuming nil
135
+ # &block:: [Block] A block to process the message replies
136
+ #
137
+ def request(ttl=nil, &block)
138
+ send(true, ttl, &block)
139
+ end
140
+
141
+ ##
142
+ # Sends the message to the Jabber service for delivery
143
+ #
144
+ # wait:: [Boolean = false] Wait for reply before return?
145
+ # ttl:: [Integer = nil] The time (in seconds) to wait for a reply before assuming nil
146
+ # &block:: [Block] A block to process the message replies
147
+ #
148
+ def send(wait=false, ttl=nil, &block)
149
+ if wait
150
+ message = nil
151
+ blockedThread = Thread.current
152
+ timer_thread = nil
153
+ timeout = false
154
+ unless ttl.nil?
155
+ timer_thread = Thread.new {
156
+ sleep ttl
157
+ timeout = true
158
+ blockedThread.wakeup
159
+ }
160
+ end
161
+ @session.connection.send(self.to_s, block) do |je|
162
+ if je.element_tag == "message" and je.thread.element_data == @thread
163
+ je.consume_element
164
+ message = Message.from_element(@session, je)
165
+ blockedThread.wakeup unless timeout
166
+ unless timer_thread.nil?
167
+ timer_thread.kill
168
+ timer_thread = nil
169
+ end
170
+ end
171
+ end
172
+ Thread.stop
173
+ return message
174
+ else
175
+ @session.connection.send(self.to_s, block) if @session
176
+ end
177
+ end
178
+
179
+ ##
180
+ # Sets the session instance
181
+ #
182
+ # session:: [Jabber::Session] The session instance
183
+ # return:: [Jabber::Protocol::Message] The current Message object
184
+ #
185
+ def session=(session)
186
+ @session = session
187
+ self
188
+ end
189
+
190
+ ##
191
+ # Builds a reply to an existing message by setting:
192
+ # 1. to = from
193
+ # 2. id = id
194
+ # 3. thread = thread
195
+ # 4. type = type
196
+ # 5. session = session
197
+ #
198
+ # return:: [Jabber::Protocol::Message] The reply message
199
+ #
200
+ def reply
201
+ message = Message.new(nil)
202
+ message.to = @from
203
+ message.id = @id
204
+ message.thread = @thread
205
+ message.type = @type
206
+ message.session = @session
207
+ @is_reply = true
208
+ return message
209
+ end
210
+
211
+ ##
212
+ # Generates XML that complies with the Jabber protocol for
213
+ # sending the message through the Jabber service.
214
+ #
215
+ # return:: [String] The XML string.
216
+ #
217
+ def to_xml
218
+ @thread = Jabber.gen_random_thread if @thread.nil? and (not @is_reply)
219
+ elem = XMLElement.new("message", {"to"=>@to, "type"=>@type})
220
+ elem.add_attribute("id", @id) if @id
221
+ elem.add_child("thread").add_data(@thread) if @thread
222
+ elem.add_child("subject").add_data(@subject) if @subject
223
+ elem.add_child("body").add_data(@body) if @body
224
+ if @xhtml then
225
+ t=elem.add_child("xhtml").add_attribute("xmlns","http://www.w3.org/1999/xhtml")
226
+ t.add_child("body").add_data(@xhtml)
227
+ end
228
+ if @type=="error" then
229
+ e=elem.add_child("error");
230
+ e.add_attribute("code",@errorcode) if @errorcode
231
+ e.add_data(@error) if @error
232
+ end
233
+ elem.add_child("x").add_attribute("xmlns", "jabber:x:oob").add_data(@oobData) if @oobData
234
+ elem.add_xml(@x.to_s) if @x
235
+ return elem.to_s
236
+ end
237
+
238
+ ##
239
+ # see to_xml
240
+ #
241
+ def to_s
242
+ to_xml
243
+ end
244
+
245
+ end
246
246
  end