gmcmillan-xmpp4r 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +91 -0
  3. data/COPYING +340 -0
  4. data/LICENSE +59 -0
  5. data/README.rdoc +122 -0
  6. data/README_ruby19.txt +43 -0
  7. data/Rakefile +258 -0
  8. data/data/doc/xmpp4r/examples/advanced/adventure/README +56 -0
  9. data/data/doc/xmpp4r/examples/advanced/adventure/adventure.rb +23 -0
  10. data/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb +136 -0
  11. data/data/doc/xmpp4r/examples/advanced/adventure/cube.xml +15 -0
  12. data/data/doc/xmpp4r/examples/advanced/adventure/tower.xml +69 -0
  13. data/data/doc/xmpp4r/examples/advanced/adventure/world.rb +424 -0
  14. data/data/doc/xmpp4r/examples/advanced/fileserve.conf +11 -0
  15. data/data/doc/xmpp4r/examples/advanced/fileserve.rb +346 -0
  16. data/data/doc/xmpp4r/examples/advanced/getonline.rb +56 -0
  17. data/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb +315 -0
  18. data/data/doc/xmpp4r/examples/advanced/migrate.rb +88 -0
  19. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +266 -0
  20. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/index.xsl +235 -0
  21. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/pep-aggregator.rb +147 -0
  22. data/data/doc/xmpp4r/examples/advanced/recvfile.rb +85 -0
  23. data/data/doc/xmpp4r/examples/advanced/rosterdiscovery.rb +129 -0
  24. data/data/doc/xmpp4r/examples/advanced/sendfile.conf +10 -0
  25. data/data/doc/xmpp4r/examples/advanced/sendfile.rb +72 -0
  26. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb +51 -0
  27. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_jabber.rb +43 -0
  28. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_test.rb +10 -0
  29. data/data/doc/xmpp4r/examples/advanced/versionpoll.rb +109 -0
  30. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +146 -0
  31. data/data/doc/xmpp4r/examples/advanced/xmppingrc.sample +14 -0
  32. data/data/doc/xmpp4r/examples/basic/change_password.rb +41 -0
  33. data/data/doc/xmpp4r/examples/basic/client.rb +70 -0
  34. data/data/doc/xmpp4r/examples/basic/component.rb +11 -0
  35. data/data/doc/xmpp4r/examples/basic/echo.rb +37 -0
  36. data/data/doc/xmpp4r/examples/basic/jabbersend.rb +41 -0
  37. data/data/doc/xmpp4r/examples/basic/mass_sender.rb +68 -0
  38. data/data/doc/xmpp4r/examples/basic/muc_owner_config.rb +12 -0
  39. data/data/doc/xmpp4r/examples/basic/mucinfo.rb +41 -0
  40. data/data/doc/xmpp4r/examples/basic/mucsimplebot.rb +82 -0
  41. data/data/doc/xmpp4r/examples/basic/register.rb +42 -0
  42. data/data/doc/xmpp4r/examples/basic/remove_registration.rb +18 -0
  43. data/data/doc/xmpp4r/examples/basic/roster.rb +44 -0
  44. data/data/doc/xmpp4r/examples/basic/rosterprint.rb +50 -0
  45. data/data/doc/xmpp4r/examples/basic/rosterrename.rb +34 -0
  46. data/data/doc/xmpp4r/examples/basic/rosterwatch.rb +171 -0
  47. data/data/doc/xmpp4r/examples/basic/send_vcard.rb +67 -0
  48. data/data/doc/xmpp4r/examples/basic/tune_client.rb +56 -0
  49. data/data/doc/xmpp4r/examples/basic/tune_server.rb +58 -0
  50. data/data/doc/xmpp4r/examples/basic/versionbot.rb +75 -0
  51. data/lib/xmpp4r.rb +118 -0
  52. data/lib/xmpp4r/base64.rb +32 -0
  53. data/lib/xmpp4r/bytestreams.rb +15 -0
  54. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +321 -0
  55. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +257 -0
  56. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +31 -0
  57. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +54 -0
  58. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +153 -0
  59. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +86 -0
  60. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +198 -0
  61. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb +65 -0
  62. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +82 -0
  63. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +170 -0
  64. data/lib/xmpp4r/bytestreams/iq/si.rb +206 -0
  65. data/lib/xmpp4r/callbacks.rb +133 -0
  66. data/lib/xmpp4r/caps.rb +1 -0
  67. data/lib/xmpp4r/caps/c.rb +67 -0
  68. data/lib/xmpp4r/caps/helper/generator.rb +160 -0
  69. data/lib/xmpp4r/caps/helper/helper.rb +84 -0
  70. data/lib/xmpp4r/client.rb +345 -0
  71. data/lib/xmpp4r/command/helper/responder.rb +53 -0
  72. data/lib/xmpp4r/command/iq/command.rb +154 -0
  73. data/lib/xmpp4r/component.rb +103 -0
  74. data/lib/xmpp4r/connection.rb +231 -0
  75. data/lib/xmpp4r/dataforms.rb +5 -0
  76. data/lib/xmpp4r/dataforms/x/data.rb +297 -0
  77. data/lib/xmpp4r/debuglog.rb +63 -0
  78. data/lib/xmpp4r/delay.rb +5 -0
  79. data/lib/xmpp4r/delay/x/delay.rb +99 -0
  80. data/lib/xmpp4r/discovery.rb +8 -0
  81. data/lib/xmpp4r/discovery/helper/helper.rb +58 -0
  82. data/lib/xmpp4r/discovery/helper/responder.rb +165 -0
  83. data/lib/xmpp4r/discovery/iq/discoinfo.rb +211 -0
  84. data/lib/xmpp4r/discovery/iq/discoitems.rb +147 -0
  85. data/lib/xmpp4r/entity_time.rb +6 -0
  86. data/lib/xmpp4r/entity_time/iq.rb +45 -0
  87. data/lib/xmpp4r/entity_time/responder.rb +57 -0
  88. data/lib/xmpp4r/errors.rb +284 -0
  89. data/lib/xmpp4r/feature_negotiation.rb +5 -0
  90. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +28 -0
  91. data/lib/xmpp4r/framework/base.rb +55 -0
  92. data/lib/xmpp4r/framework/bot.rb +148 -0
  93. data/lib/xmpp4r/httpbinding.rb +5 -0
  94. data/lib/xmpp4r/httpbinding/client.rb +275 -0
  95. data/lib/xmpp4r/idgenerator.rb +37 -0
  96. data/lib/xmpp4r/iq.rb +221 -0
  97. data/lib/xmpp4r/jid.rb +167 -0
  98. data/lib/xmpp4r/last.rb +2 -0
  99. data/lib/xmpp4r/last/helper/helper.rb +37 -0
  100. data/lib/xmpp4r/last/iq/last.rb +67 -0
  101. data/lib/xmpp4r/location.rb +2 -0
  102. data/lib/xmpp4r/location/helper/helper.rb +56 -0
  103. data/lib/xmpp4r/location/location.rb +179 -0
  104. data/lib/xmpp4r/message.rb +226 -0
  105. data/lib/xmpp4r/muc.rb +14 -0
  106. data/lib/xmpp4r/muc/helper/mucbrowser.rb +92 -0
  107. data/lib/xmpp4r/muc/helper/mucclient.rb +469 -0
  108. data/lib/xmpp4r/muc/helper/simplemucclient.rb +332 -0
  109. data/lib/xmpp4r/muc/iq/mucadmin.rb +23 -0
  110. data/lib/xmpp4r/muc/iq/mucadminitem.rb +20 -0
  111. data/lib/xmpp4r/muc/iq/mucowner.rb +15 -0
  112. data/lib/xmpp4r/muc/item.rb +143 -0
  113. data/lib/xmpp4r/muc/x/muc.rb +70 -0
  114. data/lib/xmpp4r/muc/x/mucuserinvite.rb +60 -0
  115. data/lib/xmpp4r/muc/x/mucuseritem.rb +36 -0
  116. data/lib/xmpp4r/presence.rb +232 -0
  117. data/lib/xmpp4r/pubsub.rb +8 -0
  118. data/lib/xmpp4r/pubsub/children/configuration.rb +86 -0
  119. data/lib/xmpp4r/pubsub/children/event.rb +49 -0
  120. data/lib/xmpp4r/pubsub/children/item.rb +35 -0
  121. data/lib/xmpp4r/pubsub/children/items.rb +53 -0
  122. data/lib/xmpp4r/pubsub/children/node_config.rb +48 -0
  123. data/lib/xmpp4r/pubsub/children/publish.rb +38 -0
  124. data/lib/xmpp4r/pubsub/children/retract.rb +41 -0
  125. data/lib/xmpp4r/pubsub/children/subscription.rb +62 -0
  126. data/lib/xmpp4r/pubsub/children/subscription_config.rb +67 -0
  127. data/lib/xmpp4r/pubsub/children/unsubscribe.rb +63 -0
  128. data/lib/xmpp4r/pubsub/helper/nodebrowser.rb +129 -0
  129. data/lib/xmpp4r/pubsub/helper/nodehelper.rb +156 -0
  130. data/lib/xmpp4r/pubsub/helper/oauth_service_helper.rb +90 -0
  131. data/lib/xmpp4r/pubsub/helper/servicehelper.rb +490 -0
  132. data/lib/xmpp4r/pubsub/iq/pubsub.rb +19 -0
  133. data/lib/xmpp4r/query.rb +15 -0
  134. data/lib/xmpp4r/reliable.rb +168 -0
  135. data/lib/xmpp4r/rexmladdons.rb +197 -0
  136. data/lib/xmpp4r/roster.rb +7 -0
  137. data/lib/xmpp4r/roster/helper/roster.rb +532 -0
  138. data/lib/xmpp4r/roster/iq/roster.rb +215 -0
  139. data/lib/xmpp4r/roster/x/roster.rb +138 -0
  140. data/lib/xmpp4r/rpc.rb +2 -0
  141. data/lib/xmpp4r/rpc/helper/client.rb +123 -0
  142. data/lib/xmpp4r/rpc/helper/server.rb +74 -0
  143. data/lib/xmpp4r/rpc/helper/xmlrpcaddons.rb +67 -0
  144. data/lib/xmpp4r/rpc/iq/rpc.rb +23 -0
  145. data/lib/xmpp4r/sasl.rb +259 -0
  146. data/lib/xmpp4r/semaphore.rb +38 -0
  147. data/lib/xmpp4r/stream.rb +611 -0
  148. data/lib/xmpp4r/streamparser.rb +66 -0
  149. data/lib/xmpp4r/test/listener_mocker.rb +118 -0
  150. data/lib/xmpp4r/tune.rb +2 -0
  151. data/lib/xmpp4r/tune/helper/helper.rb +58 -0
  152. data/lib/xmpp4r/tune/tune.rb +113 -0
  153. data/lib/xmpp4r/vcard.rb +6 -0
  154. data/lib/xmpp4r/vcard/helper/vcard.rb +84 -0
  155. data/lib/xmpp4r/vcard/iq/vcard.rb +109 -0
  156. data/lib/xmpp4r/version.rb +7 -0
  157. data/lib/xmpp4r/version/helper/responder.rb +72 -0
  158. data/lib/xmpp4r/version/helper/simpleresponder.rb +44 -0
  159. data/lib/xmpp4r/version/iq/version.rb +105 -0
  160. data/lib/xmpp4r/x.rb +37 -0
  161. data/lib/xmpp4r/xhtml.rb +1 -0
  162. data/lib/xmpp4r/xhtml/html.rb +115 -0
  163. data/lib/xmpp4r/xmpp4r.rb +20 -0
  164. data/lib/xmpp4r/xmppelement.rb +168 -0
  165. data/lib/xmpp4r/xmppstanza.rb +162 -0
  166. data/setup.rb +1586 -0
  167. data/test/bytestreams/tc_ibb.rb +188 -0
  168. data/test/bytestreams/tc_socks5bytestreams.rb +114 -0
  169. data/test/caps/tc_helper.rb +158 -0
  170. data/test/dataforms/tc_data.rb +81 -0
  171. data/test/delay/tc_xdelay.rb +51 -0
  172. data/test/discovery/tc_responder.rb +91 -0
  173. data/test/entity_time/tc_responder.rb +65 -0
  174. data/test/last/tc_helper.rb +75 -0
  175. data/test/lib/assert_equal_xml.rb +14 -0
  176. data/test/lib/clienttester.rb +149 -0
  177. data/test/muc/tc_muc_mucclient.rb +834 -0
  178. data/test/muc/tc_muc_simplemucclient.rb +114 -0
  179. data/test/muc/tc_mucowner.rb +50 -0
  180. data/test/pubsub/tc_helper.rb +785 -0
  181. data/test/pubsub/tc_nodeconfig.rb +61 -0
  182. data/test/pubsub/tc_subscriptionconfig.rb +41 -0
  183. data/test/roster/tc_helper.rb +523 -0
  184. data/test/roster/tc_iqqueryroster.rb +173 -0
  185. data/test/roster/tc_xroster.rb +73 -0
  186. data/test/rpc/tc_helper.rb +96 -0
  187. data/test/tc_callbacks.rb +129 -0
  188. data/test/tc_class_names.rb +146 -0
  189. data/test/tc_client.rb +30 -0
  190. data/test/tc_errors.rb +146 -0
  191. data/test/tc_idgenerator.rb +30 -0
  192. data/test/tc_iq.rb +113 -0
  193. data/test/tc_iqquery.rb +31 -0
  194. data/test/tc_jid.rb +204 -0
  195. data/test/tc_presence.rb +150 -0
  196. data/test/tc_rexml.rb +139 -0
  197. data/test/tc_stream.rb +193 -0
  198. data/test/tc_streamComponent.rb +105 -0
  199. data/test/tc_streamError.rb +129 -0
  200. data/test/tc_streamSend.rb +59 -0
  201. data/test/tc_streamparser.rb +137 -0
  202. data/test/tc_xmppstanza.rb +135 -0
  203. data/test/ts_xmpp4r.rb +44 -0
  204. data/test/tune/tc_helper_recv.rb +82 -0
  205. data/test/tune/tc_helper_send.rb +74 -0
  206. data/test/tune/tc_tune.rb +79 -0
  207. data/test/vcard/tc_helper.rb +49 -0
  208. data/test/vcard/tc_iqvcard.rb +62 -0
  209. data/test/version/tc_helper.rb +60 -0
  210. data/test/version/tc_iqqueryversion.rb +97 -0
  211. data/test/xhtml/tc_html.rb +41 -0
  212. data/tools/gen_requires.bash +31 -0
  213. data/tools/xmpp4r-gemspec-test.rb +11 -0
  214. data/xmpp4r.gemspec +249 -0
  215. metadata +299 -0
@@ -0,0 +1,611 @@
1
+ # =XMPP4R - XMPP Library for Ruby
2
+ # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
3
+ # Website::http://home.gna.org/xmpp4r/
4
+
5
+ require 'xmpp4r/callbacks'
6
+ require 'socket'
7
+ require 'thread'
8
+ require 'xmpp4r/semaphore'
9
+ require 'xmpp4r/streamparser'
10
+ require 'xmpp4r/presence'
11
+ require 'xmpp4r/message'
12
+ require 'xmpp4r/iq'
13
+ require 'xmpp4r/debuglog'
14
+ require 'xmpp4r/idgenerator'
15
+
16
+ module Jabber
17
+ ##
18
+ # The stream class manages a connection stream (a file descriptor using which
19
+ # XML messages are read and sent)
20
+ #
21
+ # You may register callbacks for the three Jabber stanzas
22
+ # (message, presence and iq) and use the send and send_with_id
23
+ # methods.
24
+ #
25
+ # To ensure the order of received stanzas, callback blocks are
26
+ # launched in the parser thread. If further blocking operations
27
+ # are intended in those callbacks, run your own thread there.
28
+ class Stream
29
+ DISCONNECTED = 1
30
+ CONNECTED = 2
31
+
32
+ # file descriptor used
33
+ attr_reader :fd
34
+
35
+ # connection status
36
+ attr_reader :status
37
+
38
+ # number of stanzas currently being processed
39
+ attr_reader :processing
40
+
41
+ ##
42
+ # Initialize a new stream
43
+ def initialize
44
+ @fd = nil
45
+ @status = DISCONNECTED
46
+ @xmlcbs = CallbackList.new
47
+ @stanzacbs = CallbackList.new
48
+ @messagecbs = CallbackList.new
49
+ @iqcbs = CallbackList.new
50
+ @presencecbs = CallbackList.new
51
+ @send_lock = Mutex.new
52
+ @last_send = Time.now
53
+ @exception_block = nil
54
+ @tbcbmutex = Mutex.new
55
+ @threadblocks = []
56
+ @wakeup_thread = nil
57
+ @streamid = nil
58
+ @streamns = 'jabber:client'
59
+ @features_sem = Semaphore.new
60
+ @parser_thread = nil
61
+ @processing = 0
62
+ end
63
+
64
+ ##
65
+ # Start the XML parser on the fd
66
+ def start(fd)
67
+ @stream_mechanisms = []
68
+ @stream_features = {}
69
+
70
+ @fd = fd
71
+ @doc = StreamParser.new(self)
72
+ @parser = Nokogiri::XML::SAX::PushParser.new(@doc)
73
+
74
+ @parser_thread = Thread.new do
75
+ Thread.current.abort_on_exception = true
76
+
77
+ begin
78
+ until @fd.eof?
79
+ @parser << @fd.read_nonblock(500)
80
+ end
81
+ @parser.finish
82
+
83
+ Jabber::debuglog("DISCONNECTED\n")
84
+
85
+ if @exception_block
86
+ Thread.new { close!; @exception_block.call(nil, self, :disconnected) }
87
+ else
88
+ close!
89
+ end
90
+ rescue Exception => e
91
+ Jabber::warnlog("EXCEPTION:\n#{e.class}\n#{e.message}\n#{e.backtrace.join("\n")}")
92
+
93
+ if @exception_block
94
+ Thread.new do
95
+ Thread.current.abort_on_exception = true
96
+ close
97
+ @exception_block.call(e, self, :start)
98
+ end
99
+ else
100
+ Jabber::warnlog "Exception caught in Parser thread! (#{e.class})\n#{e.backtrace.join("\n")}"
101
+ close!
102
+ raise
103
+ end
104
+ end
105
+ end
106
+
107
+ @status = CONNECTED
108
+ end
109
+
110
+ def stop
111
+ @parser_thread.kill
112
+ @parser = nil
113
+ end
114
+
115
+ ##
116
+ # Mounts a block to handle exceptions if they occur during the
117
+ # poll send. This will likely be the first indication that
118
+ # the socket dropped in a Jabber Session.
119
+ #
120
+ # The block has to take three arguments:
121
+ # * the Exception
122
+ # * the Jabber::Stream object (self)
123
+ # * a symbol where it happened, namely :start, :parser, :sending and :end
124
+ def on_exception(&block)
125
+ @exception_block = block
126
+ end
127
+
128
+ ##
129
+ # This method is called by the parser when a failure occurs
130
+ def parse_failure(e)
131
+ Jabber::warnlog("EXCEPTION:\n#{e.class}\n#{e.message}\n#{e.backtrace.join("\n")}")
132
+
133
+ # A new thread has to be created because close will cause the thread
134
+ # to commit suicide(???)
135
+ if @exception_block
136
+ # New thread, because close will kill the current thread
137
+ Thread.new do
138
+ Thread.current.abort_on_exception = true
139
+ close
140
+ @exception_block.call(e, self, :parser)
141
+ end
142
+ else
143
+ Jabber::warnlog "Stream#parse_failure was called by XML parser. Dumping " +
144
+ "backtrace...\n" + e.exception + "\n#{e.backtrace.join("\n")}"
145
+ close
146
+ raise
147
+ end
148
+ end
149
+
150
+ ##
151
+ # This method is called by the parser upon receiving <tt></stream:stream></tt>
152
+ def parser_end
153
+ if @exception_block
154
+ Thread.new do
155
+ Thread.current.abort_on_exception = true
156
+ close
157
+ @exception_block.call(nil, self, :close)
158
+ end
159
+ else
160
+ close
161
+ end
162
+ end
163
+
164
+ ##
165
+ # Returns if this connection is connected to a Jabber service
166
+ # return:: [Boolean] Connection status
167
+ def is_connected?
168
+ return @status == CONNECTED
169
+ end
170
+
171
+ ##
172
+ # Returns if this connection is NOT connected to a Jabber service
173
+ #
174
+ # return:: [Boolean] Connection status
175
+ def is_disconnected?
176
+ return @status == DISCONNECTED
177
+ end
178
+
179
+ ##
180
+ # Processes a received REXML::Element and executes
181
+ # registered thread blocks and filters against it.
182
+ #
183
+ # element:: [REXML::Element] The received element
184
+ def receive(element)
185
+ @tbcbmutex.synchronize { @processing += 1 }
186
+ Jabber::debuglog("RECEIVED:\n#{element.to_s}")
187
+
188
+ if element.namespace('').to_s == '' # REXML namespaces are always strings
189
+ element.add_namespace(@streamns)
190
+ end
191
+
192
+ case element.prefix
193
+ when 'stream'
194
+ case element.name
195
+ when 'stream'
196
+ stanza = element
197
+ @streamid = element.attributes['id']
198
+ @streamns = element.namespace('') if element.namespace('')
199
+
200
+ # Hack: component streams are basically client streams.
201
+ # Someday we may want to create special stanza classes
202
+ # for components/s2s deriving from normal stanzas but
203
+ # posessing these namespaces
204
+ @streamns = 'jabber:client' if @streamns == 'jabber:component:accept'
205
+
206
+ unless element.attributes['version'] # isn't XMPP compliant, so
207
+ Jabber::debuglog("FEATURES: server not XMPP compliant, will not wait for features")
208
+ @features_sem.run # don't wait for <stream:features/>
209
+ end
210
+ when 'features'
211
+ stanza = element
212
+ element.each { |e|
213
+ if e.name == 'mechanisms' and e.namespace == 'urn:ietf:params:xml:ns:xmpp-sasl'
214
+ e.each_element('mechanism') { |mech|
215
+ @stream_mechanisms.push(mech.text)
216
+ }
217
+ else
218
+ @stream_features[e.name] = e.namespace
219
+ end
220
+ }
221
+ Jabber::debuglog("FEATURES: received")
222
+ @features_sem.run
223
+ else
224
+ stanza = element
225
+ end
226
+ else
227
+ # Any stanza, classes are registered by XMPPElement::name_xmlns
228
+ begin
229
+ stanza = XMPPStanza::import(element)
230
+ rescue NoNameXmlnsRegistered
231
+ stanza = element
232
+ end
233
+ end
234
+
235
+ if @xmlcbs.process(stanza)
236
+ @tbcbmutex.synchronize { @processing -= 1 }
237
+ return true
238
+ end
239
+
240
+ # Iterate through blocked threads (= waiting for an answer)
241
+ #
242
+ # We're dup'ping the @threadblocks here, so that we won't end up in an
243
+ # endless loop if Stream#send is being nested. That means, the nested
244
+ # threadblock won't receive the stanza currently processed, but the next
245
+ # one.
246
+ threadblocks = nil
247
+ @tbcbmutex.synchronize do
248
+ threadblocks = @threadblocks.dup
249
+ end
250
+ threadblocks.each { |threadblock|
251
+ exception = nil
252
+ r = false
253
+ begin
254
+ r = threadblock.call(stanza)
255
+ rescue Exception => e
256
+ exception = e
257
+ end
258
+
259
+ if r == true
260
+ @tbcbmutex.synchronize do
261
+ @threadblocks.delete(threadblock)
262
+ end
263
+ threadblock.wakeup
264
+ @tbcbmutex.synchronize { @processing -= 1 }
265
+ return true
266
+ elsif exception
267
+ @tbcbmutex.synchronize do
268
+ @threadblocks.delete(threadblock)
269
+ end
270
+ threadblock.raise(exception)
271
+ end
272
+ }
273
+
274
+ Jabber::debuglog("PROCESSING:\n#{stanza.to_s} (#{stanza.class})")
275
+ Jabber::debuglog("TRYING stanzacbs...")
276
+ if @stanzacbs.process(stanza)
277
+ @tbcbmutex.synchronize { @processing -= 1 }
278
+ return true
279
+ end
280
+ r = false
281
+ Jabber::debuglog("TRYING message/iq/presence/cbs...")
282
+ case stanza
283
+ when Message
284
+ r = @messagecbs.process(stanza)
285
+ when Iq
286
+ r = @iqcbs.process(stanza)
287
+ when Presence
288
+ r = @presencecbs.process(stanza)
289
+ end
290
+ @tbcbmutex.synchronize { @processing -= 1 }
291
+ return r
292
+ end
293
+
294
+ ##
295
+ # Get the list of iq callbacks.
296
+ def iq_callbacks
297
+ @iqcbs
298
+ end
299
+
300
+ ##
301
+ # Get the list of message callbacks.
302
+ def message_callbacks
303
+ @messagecbs
304
+ end
305
+
306
+ ##
307
+ # Get the list of presence callbacks.
308
+ def presence_callbacks
309
+ @presencecbs
310
+ end
311
+
312
+ ##
313
+ # Get the list of stanza callbacks.
314
+ def stanza_callbacks
315
+ @stanzacbs
316
+ end
317
+
318
+ ##
319
+ # Get the list of xml callbacks.
320
+ def xml_callbacks
321
+ @xmlcbs
322
+ end
323
+
324
+ ##
325
+ # This is used by Jabber::Stream internally to
326
+ # keep track of any blocks which were passed to
327
+ # Stream#send.
328
+ class ThreadBlock
329
+ def initialize(block)
330
+ @block = block
331
+ @waiter = Semaphore.new
332
+ @exception = nil
333
+ end
334
+ def call(*args)
335
+ @block.call(*args)
336
+ end
337
+ def wait
338
+ @waiter.wait
339
+ raise @exception if @exception
340
+ end
341
+ def wakeup
342
+ @waiter.run
343
+ end
344
+ def raise(exception)
345
+ @exception = exception
346
+ @waiter.run
347
+ end
348
+ end
349
+
350
+ def send_data(data)
351
+ @send_lock.synchronize do
352
+ @last_send = Time.now
353
+ @fd << data
354
+ @fd.flush
355
+ end
356
+ end
357
+
358
+ ##
359
+ # Sends XML data to the socket and (optionally) waits
360
+ # to process received data.
361
+ #
362
+ # Do not invoke this in a callback but in a seperate thread
363
+ # because we may not suspend the parser-thread (in whose
364
+ # context callbacks are executed).
365
+ #
366
+ # Pass in a callback_timeout to force a Timeout::Error
367
+ # if the callback is not completed within that time (in seconds).
368
+ # By default there is not timeout on the callback.
369
+ # TODO Would like to set a default here, but will require lots of testing
370
+ #
371
+ # xml:: [String] The xml data to send
372
+ # &block:: [Block] The optional block
373
+ def send(xml, callback_timeout=nil, &block)
374
+ Jabber::debuglog("SENDING:\n#{xml}")
375
+ if block
376
+ threadblock = ThreadBlock.new(block)
377
+ @tbcbmutex.synchronize do
378
+ @threadblocks.unshift(threadblock)
379
+ end
380
+ end
381
+ begin
382
+ # Temporarily remove stanza's namespace to
383
+ # reduce bandwidth consumption
384
+ if xml.kind_of? XMPPStanza and xml.namespace == 'jabber:client' and
385
+ xml.prefix != 'stream' and xml.name != 'stream'
386
+ xml.delete_namespace
387
+ send_data(xml.to_s)
388
+ xml.add_namespace(@streamns)
389
+ else
390
+ send_data(xml.to_s)
391
+ end
392
+ rescue Exception => e
393
+ Jabber::warnlog("EXCEPTION:\n#{e.class}\n#{e.message}\n#{e.backtrace.join("\n")}")
394
+
395
+ if @exception_block
396
+ Thread.new do
397
+ Thread.current.abort_on_exception = true
398
+ close!
399
+ @exception_block.call(e, self, :sending)
400
+ end
401
+ else
402
+ Jabber::warnlog "Exception caught while sending! (#{e.class})\n#{e.backtrace.join("\n")}"
403
+ close!
404
+ raise
405
+ end
406
+ end
407
+ # The parser thread might be running this (think of a callback running send())
408
+ # If this is the case, we mustn't stop (or we would cause a deadlock)
409
+ if block and Thread.current != @parser_thread
410
+ Timeout.timeout(callback_timeout) {threadblock.wait}
411
+ elsif block
412
+ Jabber::warnlog("WARNING:\nCannot stop current thread in Jabber::Stream#send because it is the parser thread!")
413
+ end
414
+ end
415
+
416
+ ##
417
+ # Send an XMMP stanza with an Jabber::XMPPStanza#id. The id will be
418
+ # generated by Jabber::IdGenerator if not already set.
419
+ #
420
+ # The block will be called once: when receiving a stanza with the
421
+ # same Jabber::XMPPStanza#id. There is no need to return true to
422
+ # complete this! Instead the return value of the block will be
423
+ # returned. This is a direct result of unique request/response
424
+ # stanza identification via the id attribute.
425
+ #
426
+ # The block may be omitted. Then, the result will be the response
427
+ # stanza.
428
+ #
429
+ # Be aware that if a stanza with <tt>type='error'</tt> is received
430
+ # the function does not yield but raises an ServerError with
431
+ # the corresponding error element.
432
+ #
433
+ # Please see Stream#send for some implementational details.
434
+ #
435
+ # Please read the note about nesting at Stream#send
436
+ # xml:: [XMPPStanza]
437
+ def send_with_id(xml, &block)
438
+ if xml.id.nil?
439
+ xml.id = Jabber::IdGenerator.instance.generate_id
440
+ end
441
+
442
+ res = nil
443
+ error = nil
444
+ send(xml) do |received|
445
+ if received.kind_of? XMPPStanza and received.id == xml.id
446
+ if received.type == :error
447
+ error = (received.error ? received.error : ErrorResponse.new)
448
+ true
449
+ elsif block_given?
450
+ res = yield(received)
451
+ true
452
+ else
453
+ res = received
454
+ true
455
+ end
456
+ else
457
+ false
458
+ end
459
+ end
460
+
461
+ unless error.nil?
462
+ raise ServerError.new(error)
463
+ end
464
+
465
+ res
466
+ end
467
+
468
+ ##
469
+ # Adds a callback block to process received XML messages, these
470
+ # will be handled before any blocks given to Stream#send or other
471
+ # callbacks.
472
+ #
473
+ # priority:: [Integer] The callback's priority, the higher, the sooner
474
+ # ref:: [String] The callback's reference
475
+ # &block:: [Block] The optional block
476
+ def add_xml_callback(priority = 0, ref = nil, &block)
477
+ @tbcbmutex.synchronize do
478
+ @xmlcbs.add(priority, ref, block)
479
+ end
480
+ end
481
+
482
+ ##
483
+ # Delete an XML-messages callback
484
+ #
485
+ # ref:: [String] The reference of the callback to delete
486
+ def delete_xml_callback(ref)
487
+ @tbcbmutex.synchronize do
488
+ @xmlcbs.delete(ref)
489
+ end
490
+ end
491
+
492
+ ##
493
+ # Adds a callback block to process received Messages
494
+ #
495
+ # priority:: [Integer] The callback's priority, the higher, the sooner
496
+ # ref:: [String] The callback's reference
497
+ # &block:: [Block] The optional block
498
+ def add_message_callback(priority = 0, ref = nil, &block)
499
+ @tbcbmutex.synchronize do
500
+ @messagecbs.add(priority, ref, block)
501
+ end
502
+ end
503
+
504
+ ##
505
+ # Delete an Message callback
506
+ #
507
+ # ref:: [String] The reference of the callback to delete
508
+ def delete_message_callback(ref)
509
+ @tbcbmutex.synchronize do
510
+ @messagecbs.delete(ref)
511
+ end
512
+ end
513
+
514
+ ##
515
+ # Adds a callback block to process received Stanzas
516
+ #
517
+ # priority:: [Integer] The callback's priority, the higher, the sooner
518
+ # ref:: [String] The callback's reference
519
+ # &block:: [Block] The optional block
520
+ def add_stanza_callback(priority = 0, ref = nil, &block)
521
+ @tbcbmutex.synchronize do
522
+ @stanzacbs.add(priority, ref, block)
523
+ end
524
+ end
525
+
526
+ ##
527
+ # Delete a Stanza callback
528
+ #
529
+ # ref:: [String] The reference of the callback to delete
530
+ def delete_stanza_callback(ref)
531
+ @tbcbmutex.synchronize do
532
+ @stanzacbs.delete(ref)
533
+ end
534
+ end
535
+
536
+ ##
537
+ # Adds a callback block to process received Presences
538
+ #
539
+ # priority:: [Integer] The callback's priority, the higher, the sooner
540
+ # ref:: [String] The callback's reference
541
+ # &block:: [Block] The optional block
542
+ def add_presence_callback(priority = 0, ref = nil, &block)
543
+ @tbcbmutex.synchronize do
544
+ @presencecbs.add(priority, ref, block)
545
+ end
546
+ end
547
+
548
+ ##
549
+ # Delete a Presence callback
550
+ #
551
+ # ref:: [String] The reference of the callback to delete
552
+ def delete_presence_callback(ref)
553
+ @tbcbmutex.synchronize do
554
+ @presencecbs.delete(ref)
555
+ end
556
+ end
557
+
558
+ ##
559
+ # Adds a callback block to process received Iqs
560
+ #
561
+ # priority:: [Integer] The callback's priority, the higher, the sooner
562
+ # ref:: [String] The callback's reference
563
+ # &block:: [Block] The optional block
564
+ def add_iq_callback(priority = 0, ref = nil, &block)
565
+ @tbcbmutex.synchronize do
566
+ @iqcbs.add(priority, ref, block)
567
+ end
568
+ end
569
+
570
+ ##
571
+ # Delete an Iq callback
572
+ #
573
+ # ref:: [String] The reference of the callback to delete
574
+ #
575
+ def delete_iq_callback(ref)
576
+ @tbcbmutex.synchronize do
577
+ @iqcbs.delete(ref)
578
+ end
579
+ end
580
+ ##
581
+ # Closes the connection to the Jabber service
582
+ def close
583
+ close!
584
+ end
585
+
586
+ def close!
587
+ pr = 1
588
+ n = 0
589
+ # In some cases, we might lost count of some stanzas
590
+ # (for example, if the handler raises an exception)
591
+ # so we can't block forever.
592
+ while pr > 0 and n <= 20
593
+ @tbcbmutex.synchronize { pr = @processing }
594
+ if pr > 0
595
+ n += 1
596
+ Jabber::debuglog("TRYING TO CLOSE, STILL PROCESSING #{pr} STANZAS")
597
+ #puts("TRYING TO CLOSE, STILL PROCESSING #{pr} STANZAS")
598
+ sleep 0.1
599
+ end
600
+ end
601
+
602
+ # Order Matters here! If this method is called from within
603
+ # @parser_thread then killing @parser_thread first would
604
+ # mean the other parts of the method fail to execute.
605
+ # That would be bad. So kill parser_thread last
606
+ @fd.close if @fd and !@fd.closed?
607
+ @status = DISCONNECTED
608
+ @parser_thread.kill if @parser_thread
609
+ end
610
+ end
611
+ end