mad-p-xmpp4r 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. data/CHANGELOG +91 -0
  2. data/COPYING +340 -0
  3. data/LICENSE +59 -0
  4. data/README.rdoc +113 -0
  5. data/README_ruby19.txt +43 -0
  6. data/Rakefile +252 -0
  7. data/data/doc/xmpp4r/examples/advanced/adventure/README +56 -0
  8. data/data/doc/xmpp4r/examples/advanced/adventure/adventure.rb +23 -0
  9. data/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb +136 -0
  10. data/data/doc/xmpp4r/examples/advanced/adventure/cube.xml +15 -0
  11. data/data/doc/xmpp4r/examples/advanced/adventure/tower.xml +69 -0
  12. data/data/doc/xmpp4r/examples/advanced/adventure/world.rb +424 -0
  13. data/data/doc/xmpp4r/examples/advanced/fileserve.conf +11 -0
  14. data/data/doc/xmpp4r/examples/advanced/fileserve.rb +346 -0
  15. data/data/doc/xmpp4r/examples/advanced/getonline.rb +56 -0
  16. data/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb +315 -0
  17. data/data/doc/xmpp4r/examples/advanced/migrate.rb +88 -0
  18. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +266 -0
  19. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/index.xsl +235 -0
  20. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/pep-aggregator.rb +147 -0
  21. data/data/doc/xmpp4r/examples/advanced/recvfile.rb +85 -0
  22. data/data/doc/xmpp4r/examples/advanced/rosterdiscovery.rb +129 -0
  23. data/data/doc/xmpp4r/examples/advanced/sendfile.conf +10 -0
  24. data/data/doc/xmpp4r/examples/advanced/sendfile.rb +72 -0
  25. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb +51 -0
  26. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_jabber.rb +43 -0
  27. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_test.rb +10 -0
  28. data/data/doc/xmpp4r/examples/advanced/versionpoll.rb +109 -0
  29. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +146 -0
  30. data/data/doc/xmpp4r/examples/advanced/xmppingrc.sample +14 -0
  31. data/data/doc/xmpp4r/examples/basic/change_password.rb +41 -0
  32. data/data/doc/xmpp4r/examples/basic/client.rb +70 -0
  33. data/data/doc/xmpp4r/examples/basic/component.rb +11 -0
  34. data/data/doc/xmpp4r/examples/basic/echo.rb +37 -0
  35. data/data/doc/xmpp4r/examples/basic/jabbersend.rb +41 -0
  36. data/data/doc/xmpp4r/examples/basic/mass_sender.rb +68 -0
  37. data/data/doc/xmpp4r/examples/basic/muc_owner_config.rb +12 -0
  38. data/data/doc/xmpp4r/examples/basic/mucinfo.rb +41 -0
  39. data/data/doc/xmpp4r/examples/basic/mucsimplebot.rb +82 -0
  40. data/data/doc/xmpp4r/examples/basic/register.rb +42 -0
  41. data/data/doc/xmpp4r/examples/basic/remove_registration.rb +18 -0
  42. data/data/doc/xmpp4r/examples/basic/roster.rb +44 -0
  43. data/data/doc/xmpp4r/examples/basic/rosterprint.rb +50 -0
  44. data/data/doc/xmpp4r/examples/basic/rosterrename.rb +34 -0
  45. data/data/doc/xmpp4r/examples/basic/rosterwatch.rb +171 -0
  46. data/data/doc/xmpp4r/examples/basic/send_vcard.rb +67 -0
  47. data/data/doc/xmpp4r/examples/basic/tune_client.rb +56 -0
  48. data/data/doc/xmpp4r/examples/basic/tune_server.rb +58 -0
  49. data/data/doc/xmpp4r/examples/basic/versionbot.rb +75 -0
  50. data/lib/xmpp4r.rb +116 -0
  51. data/lib/xmpp4r/base64.rb +32 -0
  52. data/lib/xmpp4r/bytestreams.rb +15 -0
  53. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +321 -0
  54. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +257 -0
  55. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +31 -0
  56. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +54 -0
  57. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +153 -0
  58. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +86 -0
  59. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +198 -0
  60. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb +65 -0
  61. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +82 -0
  62. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +170 -0
  63. data/lib/xmpp4r/bytestreams/iq/si.rb +206 -0
  64. data/lib/xmpp4r/callbacks.rb +133 -0
  65. data/lib/xmpp4r/caps.rb +1 -0
  66. data/lib/xmpp4r/caps/c.rb +67 -0
  67. data/lib/xmpp4r/caps/helper/generator.rb +160 -0
  68. data/lib/xmpp4r/caps/helper/helper.rb +84 -0
  69. data/lib/xmpp4r/client.rb +358 -0
  70. data/lib/xmpp4r/command/helper/responder.rb +53 -0
  71. data/lib/xmpp4r/command/iq/command.rb +154 -0
  72. data/lib/xmpp4r/component.rb +103 -0
  73. data/lib/xmpp4r/connection.rb +226 -0
  74. data/lib/xmpp4r/dataforms.rb +5 -0
  75. data/lib/xmpp4r/dataforms/x/data.rb +297 -0
  76. data/lib/xmpp4r/debuglog.rb +63 -0
  77. data/lib/xmpp4r/delay.rb +5 -0
  78. data/lib/xmpp4r/delay/x/delay.rb +99 -0
  79. data/lib/xmpp4r/discovery.rb +8 -0
  80. data/lib/xmpp4r/discovery/helper/helper.rb +58 -0
  81. data/lib/xmpp4r/discovery/helper/responder.rb +165 -0
  82. data/lib/xmpp4r/discovery/iq/discoinfo.rb +211 -0
  83. data/lib/xmpp4r/discovery/iq/discoitems.rb +147 -0
  84. data/lib/xmpp4r/entity_time.rb +6 -0
  85. data/lib/xmpp4r/entity_time/iq.rb +45 -0
  86. data/lib/xmpp4r/entity_time/responder.rb +57 -0
  87. data/lib/xmpp4r/errors.rb +284 -0
  88. data/lib/xmpp4r/feature_negotiation.rb +5 -0
  89. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +28 -0
  90. data/lib/xmpp4r/framework/base.rb +55 -0
  91. data/lib/xmpp4r/framework/bot.rb +148 -0
  92. data/lib/xmpp4r/httpbinding.rb +5 -0
  93. data/lib/xmpp4r/httpbinding/client.rb +384 -0
  94. data/lib/xmpp4r/idgenerator.rb +37 -0
  95. data/lib/xmpp4r/iq.rb +221 -0
  96. data/lib/xmpp4r/jid.rb +167 -0
  97. data/lib/xmpp4r/last.rb +2 -0
  98. data/lib/xmpp4r/last/helper/helper.rb +37 -0
  99. data/lib/xmpp4r/last/iq/last.rb +67 -0
  100. data/lib/xmpp4r/location.rb +2 -0
  101. data/lib/xmpp4r/location/helper/helper.rb +56 -0
  102. data/lib/xmpp4r/location/location.rb +179 -0
  103. data/lib/xmpp4r/message.rb +226 -0
  104. data/lib/xmpp4r/muc.rb +14 -0
  105. data/lib/xmpp4r/muc/helper/mucbrowser.rb +92 -0
  106. data/lib/xmpp4r/muc/helper/mucclient.rb +469 -0
  107. data/lib/xmpp4r/muc/helper/simplemucclient.rb +332 -0
  108. data/lib/xmpp4r/muc/iq/mucadmin.rb +23 -0
  109. data/lib/xmpp4r/muc/iq/mucadminitem.rb +20 -0
  110. data/lib/xmpp4r/muc/iq/mucowner.rb +15 -0
  111. data/lib/xmpp4r/muc/item.rb +143 -0
  112. data/lib/xmpp4r/muc/x/muc.rb +70 -0
  113. data/lib/xmpp4r/muc/x/mucuserinvite.rb +60 -0
  114. data/lib/xmpp4r/muc/x/mucuseritem.rb +36 -0
  115. data/lib/xmpp4r/observable.rb +9 -0
  116. data/lib/xmpp4r/observable/contact.rb +61 -0
  117. data/lib/xmpp4r/observable/helper.rb +389 -0
  118. data/lib/xmpp4r/observable/observable_thing.rb +191 -0
  119. data/lib/xmpp4r/observable/pubsub.rb +211 -0
  120. data/lib/xmpp4r/observable/subscription.rb +57 -0
  121. data/lib/xmpp4r/observable/thread_store.rb +65 -0
  122. data/lib/xmpp4r/presence.rb +232 -0
  123. data/lib/xmpp4r/pubsub.rb +8 -0
  124. data/lib/xmpp4r/pubsub/children/configuration.rb +86 -0
  125. data/lib/xmpp4r/pubsub/children/event.rb +49 -0
  126. data/lib/xmpp4r/pubsub/children/item.rb +35 -0
  127. data/lib/xmpp4r/pubsub/children/items.rb +53 -0
  128. data/lib/xmpp4r/pubsub/children/node_config.rb +48 -0
  129. data/lib/xmpp4r/pubsub/children/publish.rb +38 -0
  130. data/lib/xmpp4r/pubsub/children/retract.rb +41 -0
  131. data/lib/xmpp4r/pubsub/children/subscription.rb +62 -0
  132. data/lib/xmpp4r/pubsub/children/subscription_config.rb +67 -0
  133. data/lib/xmpp4r/pubsub/children/unsubscribe.rb +63 -0
  134. data/lib/xmpp4r/pubsub/helper/nodebrowser.rb +129 -0
  135. data/lib/xmpp4r/pubsub/helper/nodehelper.rb +156 -0
  136. data/lib/xmpp4r/pubsub/helper/oauth_service_helper.rb +90 -0
  137. data/lib/xmpp4r/pubsub/helper/servicehelper.rb +490 -0
  138. data/lib/xmpp4r/pubsub/iq/pubsub.rb +19 -0
  139. data/lib/xmpp4r/query.rb +15 -0
  140. data/lib/xmpp4r/reliable.rb +168 -0
  141. data/lib/xmpp4r/rexmladdons.rb +197 -0
  142. data/lib/xmpp4r/roster.rb +7 -0
  143. data/lib/xmpp4r/roster/helper/roster.rb +532 -0
  144. data/lib/xmpp4r/roster/iq/roster.rb +215 -0
  145. data/lib/xmpp4r/roster/x/roster.rb +138 -0
  146. data/lib/xmpp4r/rpc.rb +2 -0
  147. data/lib/xmpp4r/rpc/helper/client.rb +123 -0
  148. data/lib/xmpp4r/rpc/helper/server.rb +74 -0
  149. data/lib/xmpp4r/rpc/helper/xmlrpcaddons.rb +67 -0
  150. data/lib/xmpp4r/rpc/iq/rpc.rb +23 -0
  151. data/lib/xmpp4r/sasl.rb +248 -0
  152. data/lib/xmpp4r/semaphore.rb +38 -0
  153. data/lib/xmpp4r/stream.rb +599 -0
  154. data/lib/xmpp4r/streamparser.rb +85 -0
  155. data/lib/xmpp4r/test/listener_mocker.rb +118 -0
  156. data/lib/xmpp4r/tune.rb +2 -0
  157. data/lib/xmpp4r/tune/helper/helper.rb +58 -0
  158. data/lib/xmpp4r/tune/tune.rb +113 -0
  159. data/lib/xmpp4r/vcard.rb +6 -0
  160. data/lib/xmpp4r/vcard/helper/vcard.rb +84 -0
  161. data/lib/xmpp4r/vcard/iq/vcard.rb +109 -0
  162. data/lib/xmpp4r/version.rb +7 -0
  163. data/lib/xmpp4r/version/helper/responder.rb +72 -0
  164. data/lib/xmpp4r/version/helper/simpleresponder.rb +44 -0
  165. data/lib/xmpp4r/version/iq/version.rb +105 -0
  166. data/lib/xmpp4r/x.rb +37 -0
  167. data/lib/xmpp4r/xhtml.rb +1 -0
  168. data/lib/xmpp4r/xhtml/html.rb +115 -0
  169. data/lib/xmpp4r/xmpp4r.rb +20 -0
  170. data/lib/xmpp4r/xmppelement.rb +168 -0
  171. data/lib/xmpp4r/xmppstanza.rb +162 -0
  172. data/mad-p-xmpp4r.gemspec +249 -0
  173. data/setup.rb +1586 -0
  174. data/test/bytestreams/tc_ibb.rb +188 -0
  175. data/test/bytestreams/tc_socks5bytestreams.rb +114 -0
  176. data/test/caps/tc_helper.rb +158 -0
  177. data/test/dataforms/tc_data.rb +81 -0
  178. data/test/delay/tc_xdelay.rb +51 -0
  179. data/test/discovery/tc_responder.rb +91 -0
  180. data/test/entity_time/tc_responder.rb +65 -0
  181. data/test/last/tc_helper.rb +75 -0
  182. data/test/lib/assert_equal_xml.rb +14 -0
  183. data/test/lib/clienttester.rb +149 -0
  184. data/test/muc/tc_muc_mucclient.rb +834 -0
  185. data/test/muc/tc_muc_simplemucclient.rb +114 -0
  186. data/test/muc/tc_mucowner.rb +50 -0
  187. data/test/pubsub/tc_helper.rb +785 -0
  188. data/test/pubsub/tc_nodeconfig.rb +61 -0
  189. data/test/pubsub/tc_subscriptionconfig.rb +41 -0
  190. data/test/reliable/tc_disconnect_cleanup.rb +334 -0
  191. data/test/reliable/tc_disconnect_exception.rb +37 -0
  192. data/test/reliable/tc_listener_mocked_test.rb +68 -0
  193. data/test/reliable/tc_reliable_connection.rb +31 -0
  194. data/test/roster/tc_helper.rb +523 -0
  195. data/test/roster/tc_iqqueryroster.rb +173 -0
  196. data/test/roster/tc_xroster.rb +73 -0
  197. data/test/rpc/tc_helper.rb +96 -0
  198. data/test/tc_callbacks.rb +129 -0
  199. data/test/tc_class_names.rb +146 -0
  200. data/test/tc_client.rb +30 -0
  201. data/test/tc_errors.rb +146 -0
  202. data/test/tc_idgenerator.rb +30 -0
  203. data/test/tc_iq.rb +113 -0
  204. data/test/tc_iqquery.rb +31 -0
  205. data/test/tc_jid.rb +204 -0
  206. data/test/tc_message.rb +182 -0
  207. data/test/tc_presence.rb +150 -0
  208. data/test/tc_rexml.rb +139 -0
  209. data/test/tc_stream.rb +167 -0
  210. data/test/tc_streamComponent.rb +105 -0
  211. data/test/tc_streamError.rb +129 -0
  212. data/test/tc_streamSend.rb +59 -0
  213. data/test/tc_streamparser.rb +125 -0
  214. data/test/tc_xmppstanza.rb +135 -0
  215. data/test/ts_xmpp4r.rb +44 -0
  216. data/test/tune/tc_helper_recv.rb +82 -0
  217. data/test/tune/tc_helper_send.rb +74 -0
  218. data/test/tune/tc_tune.rb +79 -0
  219. data/test/vcard/tc_helper.rb +49 -0
  220. data/test/vcard/tc_iqvcard.rb +62 -0
  221. data/test/version/tc_helper.rb +60 -0
  222. data/test/version/tc_iqqueryversion.rb +97 -0
  223. data/test/xhtml/tc_html.rb +41 -0
  224. data/tools/gen_requires.bash +31 -0
  225. data/tools/xmpp4r-gemspec-test.rb +11 -0
  226. metadata +286 -0
@@ -0,0 +1,70 @@
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/muc/x/mucuseritem'
6
+ require 'xmpp4r/muc/x/mucuserinvite'
7
+
8
+ module Jabber
9
+ module MUC
10
+ ##
11
+ # Class for <x/> elements
12
+ # with namespace http://jabber.org/protocol/muc
13
+ #
14
+ # See JEP-0045 for details
15
+ class XMUC < X
16
+ name_xmlns 'x', 'http://jabber.org/protocol/muc'
17
+
18
+ ##
19
+ # Text content of the <tt><password/></tt> element
20
+ def password
21
+ first_element_text('password')
22
+ end
23
+
24
+ ##
25
+ # Set the password for joining a room
26
+ # (text content of the <tt><password/></tt> element)
27
+ def password=(s)
28
+ if s
29
+ replace_element_text('password', s)
30
+ else
31
+ delete_elements('password')
32
+ end
33
+ end
34
+ end
35
+
36
+ ##
37
+ # Class for <x/> elements
38
+ # with namespace http://jabber.org/protocol/muc#user
39
+ #
40
+ # See JEP-0058 for details
41
+ class XMUCUser < X
42
+ name_xmlns 'x', 'http://jabber.org/protocol/muc#user'
43
+
44
+ ##
45
+ # Retrieve the three-digit code in
46
+ # <tt><x xmlns='http://jabber.org/protocol/muc#user'><status code='...'/></x></tt>
47
+ # result:: [Fixnum] or nil
48
+ def status_code
49
+ e = nil
50
+ each_element('status') { |xe| e = xe }
51
+ if e and e.attributes['code'].size == 3 and e.attributes['code'].to_i != 0
52
+ e.attributes['code'].to_i
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ ##
59
+ # Get all <item/> elements
60
+ # result:: [Array] of [XMUCUserItem]
61
+ def items
62
+ res = []
63
+ each_element('item') { |item|
64
+ res << item
65
+ }
66
+ res
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,60 @@
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
+ module Jabber
6
+ module MUC
7
+ class XMUCUserInvite < XMPPElement
8
+ name_xmlns 'invite', 'http://jabber.org/protocol/muc#user'
9
+
10
+ def initialize(to=nil, reason=nil)
11
+ super()
12
+ set_to(to)
13
+ set_reason(reason)
14
+ end
15
+
16
+ def to
17
+ attributes['to'].nil? ? nil : JID.new(attributes['to'])
18
+ end
19
+
20
+ def to=(j)
21
+ attributes['to'] = j.nil? ? nil : j.to_s
22
+ end
23
+
24
+ def set_to(j)
25
+ self.to = j
26
+ self
27
+ end
28
+
29
+ def from
30
+ attributes['from'].nil? ? nil : JID.new(attributes['from'])
31
+ end
32
+
33
+ def from=(j)
34
+ attributes['from'] = (j.nil? ? nil : j.to_s)
35
+ end
36
+
37
+ def set_from(j)
38
+ self.from = j
39
+ self
40
+ end
41
+
42
+ def reason
43
+ first_element_text('reason')
44
+ end
45
+
46
+ def reason=(s)
47
+ if s
48
+ replace_element_text('reason', s)
49
+ else
50
+ delete_elements('reason')
51
+ end
52
+ end
53
+
54
+ def set_reason(s)
55
+ self.reason = s
56
+ self
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,36 @@
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/muc/item'
6
+
7
+ module Jabber
8
+ module MUC
9
+ class XMUCUserItem < MUC::UserItem
10
+ name_xmlns 'item', 'http://jabber.org/protocol/muc#user'
11
+
12
+ def initialize(affiliation=nil, role=nil, jid=nil)
13
+ super()
14
+ set_affiliation(affiliation)
15
+ set_role(role)
16
+ set_jid(jid)
17
+ end
18
+
19
+ def continue
20
+ c = nil
21
+ each_element('continue') { |xe| c = xe }
22
+ c.nil?
23
+ end
24
+
25
+ def continue=(c)
26
+ delete_elements('continue')
27
+ add_element('continue') if c
28
+ end
29
+
30
+ def set_continue(c)
31
+ self.continue = c
32
+ self
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ # =XMPP4R - XMPP Library for Ruby
2
+ #
3
+ # This file's copyright (c) 2009 by Pablo Lorenzzoni <pablo@propus.com.br>
4
+ # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
5
+ # Website::http://home.gna.org/xmpp4r/
6
+
7
+ require 'xmpp4r/observable/thread_store.rb'
8
+ require 'xmpp4r/observable/observable_thing.rb'
9
+ require 'xmpp4r/observable/helper.rb'
@@ -0,0 +1,61 @@
1
+ # =XMPP4R - XMPP Library for Ruby
2
+ #
3
+ # This file's copyright (c) 2009 by Pablo Lorenzzoni <pablo@propus.com.br>
4
+ # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
5
+ # Website::http://home.gna.org/xmpp4r/
6
+ #
7
+ module Jabber
8
+ class Observable
9
+ # Jabber::Observable::Contact - Convenience subclass to deal with Contacts
10
+ class Contact
11
+
12
+ # Creates a new Jabber::Observable::Contact
13
+ #
14
+ # jid:: jid to be used
15
+ # observable:: observable to be used
16
+ def initialize(jid, observable)
17
+ @jid = jid.respond_to?(:resource) ? jid : JID.new(jid)
18
+ @observable = observable
19
+ end
20
+
21
+ # Returns the stripped version of the JID
22
+ def jid; @jid.strip; end
23
+
24
+ def inspect #:nodoc:
25
+ sprintf("#<%s:0x%x @jid=%s>", self.class.name, __id__, @jid.to_s)
26
+ end
27
+
28
+ # Are e subscribed?
29
+ def subscribed?
30
+ [:to, :both].include?(subscription)
31
+ end
32
+
33
+ # Get the subscription from the roster_item
34
+ def subscription
35
+ items = @observable.roster.items
36
+ return false unless items.include?(jid)
37
+ items[jid].subscription
38
+ end
39
+
40
+ # Send a request asking for authorization
41
+ def ask_for_authorization!
42
+ request!(:subscribe)
43
+ end
44
+
45
+ # Sends a request asking for unsubscription
46
+ def unsubscribe!
47
+ request!(:unsubscribe)
48
+ end
49
+
50
+ private
51
+
52
+ # Really send the request.
53
+ def request!(type)
54
+ request = Jabber::Presence.new.set_type(type)
55
+ request.to = jid
56
+ @observable.send!(request)
57
+ end
58
+
59
+ end # of class Contact
60
+ end # of class Observable
61
+ end # of module Jabber
@@ -0,0 +1,389 @@
1
+ # =XMPP4R - XMPP Library for Ruby
2
+ #
3
+ # This file's copyright (c) 2009 by Pablo Lorenzzoni <pablo@propus.com.br>
4
+ # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
5
+ # Website::http://home.gna.org/xmpp4r/
6
+ #
7
+ # This is the helper for xmpp4r/observable, and was based on XMPP4R-Observable
8
+ # by Pablo Lorenzoni <pablo@propus.com.br>.
9
+ require 'time'
10
+ require 'xmpp4r'
11
+ require 'xmpp4r/roster'
12
+ require 'xmpp4r/observable/contact'
13
+ require 'xmpp4r/observable/pubsub'
14
+ require 'xmpp4r/observable/subscription'
15
+ module Jabber
16
+
17
+ class NotConnected < StandardError; end #:nodoc:
18
+
19
+ # Jabber::Observable - Creates observable Jabber Clients
20
+ class Observable
21
+
22
+ # This is what actually makes our object Observable.
23
+ include ObservableThing
24
+
25
+ attr_reader :subs, :pubsub, :jid, :auto
26
+
27
+ # Create a new Jabber::Observable client. You will be automatically connected
28
+ # to the Jabber server and your status message will be set to the string
29
+ # passed in as the status_message argument.
30
+ #
31
+ # jabber = Jabber::Observable.new("me@example.com", "password", nil, "Talk to me - Please!")
32
+ #
33
+ # jid:: your jid (either a string or a JID object)
34
+ # password:: your password
35
+ # status:: your status. Check Jabber::Observable#status for documentation
36
+ # status_message:: some funny string
37
+ # host:: the server host (if different from the one in the jid)
38
+ # port:: the server port (default: 5222)
39
+ def initialize(jid, password, status=nil, status_message="Available", host=nil, port=5222)
40
+
41
+ # Basic stuff
42
+ @jid = jid.respond_to?(:resource) ? jid : Jabber::JID.new(jid)
43
+ @password = password
44
+ @host = host
45
+ @port = port
46
+
47
+ # Message dealing
48
+ @delivered_messages = 0
49
+ @deferred_messages = Queue.new
50
+ start_deferred_delivery_thread
51
+
52
+ # Connection stuff
53
+ @connect_mutex = Mutex.new
54
+ @client = nil
55
+ @roster = nil
56
+ @disconnected = false
57
+
58
+ # Tell everybody I am here
59
+ status(status, status_message)
60
+
61
+ # Subscription Accessor
62
+ @subs = Jabber::Observable::Subscriptions.new(self)
63
+
64
+ # PubSub Accessor
65
+ @pubsub = Jabber::Observable::PubSub.new(self)
66
+
67
+ # Auto Observer placeholder
68
+ @auto = nil
69
+
70
+ # Our contacts Hash
71
+ @contacts = Hash.new
72
+ end
73
+
74
+ def inspect # :nodoc:
75
+ sprintf("#<%s:0x%x @jid=%s, @delivered_messages=%d, @deferred_messages=%d, @observer_count=%s, @notification_count=%s, @pubsub=%s>", self.class.name, __id__, @jid, @delivered_messages, @deferred_messages.length, observer_count.inspect, notification_count.inspect, @pubsub.inspect)
76
+ end
77
+
78
+ # Count the registered observers in each thing
79
+ def observer_count
80
+ h = {}
81
+ [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
82
+ h[thing] = count_observers(thing)
83
+ end
84
+ h
85
+ end
86
+
87
+ # Count the notifications really sent for each thing
88
+ def notification_count
89
+ h = {}
90
+ [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
91
+ h[thing] = count_notifications(thing)
92
+ end
93
+ h
94
+ end
95
+
96
+ # Attach an auto-observer based on QObserver
97
+ def attach_auto_observer
98
+ raise StandardError, "Already attached." unless @auto.nil?
99
+
100
+ @auto = QObserver.new
101
+ [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
102
+ self.add_observer(thing, @auto)
103
+ end
104
+ end
105
+
106
+ # Dettach the auto-observer
107
+ def dettach_auto_observer
108
+ raise StandardError, "Not attached." if @auto.nil?
109
+
110
+ [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
111
+ self.delete_observer(thing, @auto)
112
+ end
113
+ @auto = nil
114
+ end
115
+
116
+ # Send a message to jabber user jid.
117
+ #
118
+ # jid:: jid of the recipient
119
+ # message:: what is to be delivered (either a string or a Jabber::Message)
120
+ # type:: can be either one of:
121
+ # * :normal: a normal message.
122
+ # * :chat (default): a one-to-one chat message.
123
+ # * :groupchat: a group-chat message.
124
+ # * :headline: a "headline" message.
125
+ # * :error: an error message.
126
+ #
127
+ # If the recipient is not in your contacts list, the message will be queued
128
+ # for later delivery, and the Contact will be automatically asked for
129
+ # authorization (see Jabber::Observable#add).
130
+ def deliver(jid, message, type=nil)
131
+ contacts(jid).each do |contact|
132
+ # Check if we're subscribed to contact
133
+ if @subs.subscribed_to?(contact)
134
+ # Yes! we are!
135
+ if message.instance_of?(Jabber::Message)
136
+ msg = message
137
+ msg.to = contact.jid
138
+ msg.type = type unless type.nil? # Let's keep the Jabber::Message type unless passed
139
+ else
140
+ msg = Jabber::Message.new(contact.jid)
141
+ msg.body = message
142
+ msg.type = type.nil? ? :chat : type
143
+ end
144
+ @delivered_messages += 1
145
+ send!(msg)
146
+ else
147
+ # No... Let's add it and defer the delivery.
148
+ @subs.add(contact.jid)
149
+ deliver_deferred(contact.jid, message, type)
150
+ end
151
+ end
152
+ end
153
+
154
+ # Set your presence, with a message.
155
+ #
156
+ # presence:: any of these:
157
+ # * nil: online.
158
+ # * :chat: free for chat.
159
+ # * :away: away from the computer.
160
+ # * :dnd: do not disturb.
161
+ # * :xa: extended away.
162
+ # message:: a string that you wish your contacts see when you change your presence.
163
+ def status(presence, message)
164
+ @status_message = message
165
+ @presence = presence
166
+ send!(Jabber::Presence.new(@presence, @status_message))
167
+ end
168
+
169
+ # Transform a passed list of contacts in one or more Jabber::Observable::Contact objects.
170
+ #
171
+ # contact:: one of more jids of contacts
172
+ def contacts(*contact)
173
+ ret = []
174
+ contact.each do |c|
175
+ jid = c.to_s
176
+ # Do we already have it?
177
+ if ! @contacts.include?(jid)
178
+ # Nope.
179
+ @contacts[jid] = c.instance_of?(Jabber::Observable::Contact) ? c : Jabber::Observable::Contact.new(c, self)
180
+ end
181
+ ret << @contacts[jid]
182
+ end
183
+ ret
184
+ end
185
+
186
+ # Returns true if the Jabber client is connected to the Jabber server,
187
+ # false otherwise.
188
+ def connected?
189
+ @client.respond_to?(:is_connected?) && @client.is_connected?
190
+ end
191
+
192
+ # Pass the underlying Roster helper.
193
+ def roster
194
+ return @roster unless @roster.nil?
195
+ @roster = Jabber::Roster::Helper.new(client)
196
+ end
197
+
198
+ # Pass the underlying Jabber client.
199
+ def client
200
+ connect! unless connected?
201
+ @client
202
+ end
203
+
204
+ # Send a Jabber stanza over-the-wire.
205
+ #
206
+ # msg:: the stanza to be sent.
207
+ def send!(msg)
208
+ retries = 0
209
+ max = 4
210
+ begin
211
+ retries += 1
212
+ client.send(msg)
213
+ rescue Errno::ECONNRESET => e
214
+ # Connection reset. Sleep progressively and retry until success or exhaustion.
215
+ sleep ((retries^2) * 60) + 30
216
+ disconnect
217
+ reconnect
218
+ retry unless retries > max
219
+ raise e
220
+ rescue Errno::EPIPE, IOError => e
221
+ # Some minor error. Sleep 2 seconds and retry until success or exhaustion.
222
+ sleep 2
223
+ disconnect
224
+ reconnect
225
+ retry unless retries > max
226
+ raise e
227
+ end
228
+ end
229
+
230
+ # Use this to force the client to reconnect after a disconnect.
231
+ def reconnect
232
+ @disconnected = false
233
+ connect!
234
+ end
235
+
236
+ # Use this to force the client to disconnect and not automatically
237
+ # reconnect.
238
+ def disconnect
239
+ disconnect!(false)
240
+ end
241
+
242
+ # Queue messages for delivery once a user has accepted our authorization
243
+ # request. Works in conjunction with the deferred delivery thread.
244
+ #
245
+ # You can use this method if you want to manually add friends and still
246
+ # have the message queued for later delivery.
247
+ def deliver_deferred(jid, message, type)
248
+ msg = {:to => jid, :message => message, :type => type, :time => Time.now}
249
+ @deferred_messages.enq msg
250
+ end
251
+
252
+ # Sets the maximum time to wait for a message to be delivered (in
253
+ # seconds). It will be removed of the queue afterwards.
254
+ def deferred_max_wait=(seconds)
255
+ @deferred_max_wait = seconds
256
+ end
257
+
258
+ # Get the maximum time to wait for a message to be delivered. Default: 600
259
+ # seconds (10 minutes).
260
+ def deferred_max_wait
261
+ @deferred_max_wait || 600
262
+ end
263
+
264
+ private
265
+
266
+ # Create the infrastructure for connection and do it.
267
+ #
268
+ # Note that we use a Mutex to prevent double connection attempts and will
269
+ # raise a SecurityError if that happens.
270
+ def connect!
271
+ raise RuntimeError, "Connections disabled - use Jabber::Observable::reconnect() to reconnect." if @disconnected
272
+ raise SecurityError, "Connection attempt while already trying to connect!" if @connect_mutex.locked?
273
+
274
+ @connect_mutex.synchronize do
275
+ # Assure we're not connected.
276
+ disconnect!(false) if connected?
277
+
278
+ # Connection
279
+ jid = Jabber::JID.new(@jid)
280
+ my_client = Jabber::Client.new(jid)
281
+ my_client.connect(@host, @port)
282
+ my_client.auth(@password)
283
+ @roster = nil
284
+ @client = my_client
285
+
286
+ # Post-connect
287
+ register_default_callbacks
288
+ status(@presence, @status_message)
289
+ if @pubsub.nil?
290
+ @pubsub = Jabber::Observable::PubSub.new(self)
291
+ else
292
+ @pubsub.attach!
293
+ end
294
+ end
295
+ end
296
+
297
+ # Really disconnect the client
298
+ #
299
+ # auto_reconnect:: Sets the flag for auto-reconnection
300
+ def disconnect!(auto_reconnect = true)
301
+ if connected?
302
+ client.close
303
+ end
304
+ @roster = nil
305
+ @client = nil
306
+ @pubsub.set_service(nil)
307
+ @disconnected = auto_reconnect
308
+ end
309
+
310
+ # This will register callbacks for every "thing" we made observable.
311
+ #
312
+ # The observable things we register here are :message, :presence, :iq,
313
+ # :new_subscription, and :subscription_request
314
+ #
315
+ # Note we can also observe :event, but that is dealt with in
316
+ # Jabber::Observable::PubSub
317
+ def register_default_callbacks
318
+
319
+ # The three basic "things": :message, :presence and :iq
320
+ # (note that :presence is based on roster)
321
+ client.add_message_callback do |message|
322
+ unless message.body.nil?
323
+ changed(:message)
324
+ notify_observers(:message, message)
325
+ end
326
+ end
327
+
328
+ roster.add_presence_callback do |roster_item, old_presence, new_presence|
329
+ simple_jid = roster_item.jid.strip.to_s
330
+ presence = case new_presence.type
331
+ when nil then new_presence.show || :online
332
+ when :unavailable then :unavailable
333
+ else
334
+ nil
335
+ end
336
+
337
+ changed(:presence)
338
+ notify_observers(:presence, simple_jid, presence, new_presence)
339
+ end
340
+
341
+ client.add_iq_callback do |iq|
342
+ changed(:iq)
343
+ notify_observers(:iq, iq)
344
+ end
345
+
346
+ # We'll also expose roster's :new_subscription and :subscription_request
347
+ roster.add_subscription_callback do |roster_item, presence|
348
+ if presence.type == :subscribed
349
+ changed(:new_subscription)
350
+ notify_observers(:new_subscription, [roster_item, presence])
351
+ end
352
+ end
353
+
354
+ roster.add_subscription_request_callback do |roster_item, presence|
355
+ roster.accept_subscription(presence.from) if @subs.accept?
356
+ changed(:subscription_request)
357
+ notify_observers(:subscription_request, [roster_item, presence])
358
+ end
359
+ end
360
+
361
+ # Starts the deferred delivery thread
362
+ #
363
+ # This will monitor the @deferred_messages queue and try to deliver messages.
364
+ def start_deferred_delivery_thread
365
+ return if ! @deferred_delivery_thread.nil? and @deferred_delivery_thread.alive?
366
+
367
+ @deferred_delivery_thread = Thread.new do
368
+ loop do
369
+ # Check the queue every 10 seconds. Effectivelly block if nothing is queued.
370
+ sleep 10 while @deferred_messages.empty?
371
+
372
+ # Hm... something has been queued
373
+ message = @deferred_messages.deq
374
+ if @subs.subscribed_to?(message[:to])
375
+ # Great! We're subscribed!
376
+ deliver(message[:to], message[:message], message[:type])
377
+ else
378
+ # Still not subscribed. Enqueue it again (unless deferred_max_wait was reached)
379
+ @deferred_messages.enq message unless Time.now > (deferred_max_wait + message[:time])
380
+ end
381
+
382
+ # Wait 5 seconds between every message still in the queue
383
+ sleep 5
384
+ end
385
+ end
386
+ end
387
+ end # of class Observable
388
+ end # of module Jabber
389
+