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,14 @@
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/dataforms'
6
+ require 'xmpp4r/muc/x/muc'
7
+ require 'xmpp4r/muc/x/mucuserinvite'
8
+ require 'xmpp4r/muc/x/mucuseritem'
9
+ require 'xmpp4r/muc/iq/mucowner'
10
+ require 'xmpp4r/muc/iq/mucadmin'
11
+ require 'xmpp4r/muc/iq/mucadminitem'
12
+ require 'xmpp4r/muc/helper/mucbrowser'
13
+ require 'xmpp4r/muc/helper/mucclient'
14
+ require 'xmpp4r/muc/helper/simplemucclient'
@@ -0,0 +1,92 @@
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/discovery'
6
+
7
+ module Jabber
8
+ module MUC
9
+ ##
10
+ # The MUCBrowser helper can be used to discover
11
+ # Multi-User-Chat components via Service Discovery
12
+ #
13
+ # See JEP 0045 sections 6.1. and 6.2.
14
+ #
15
+ # Usage of its functions should be threaded as
16
+ # responses can take a while
17
+ class MUCBrowser
18
+ ##
19
+ # Initialize a new MUCBrowser helper
20
+ def initialize(stream)
21
+ @stream = stream
22
+ end
23
+
24
+ ##
25
+ # Retrieve the name of a MUC component,
26
+ # depending upon whether the target entity supports
27
+ # the MUC protocol.
28
+ #
29
+ # A return-value of nil does *not* mean that the entity
30
+ # does not exist or does not support Service Discovery!
31
+ # nil just means that this is not a MUC-compliant service.
32
+ #
33
+ # Throws an ServerError when receiving
34
+ # <tt><iq type='error'/></tt>
35
+ # jid:: [JID] Target entity (set only domain!)
36
+ # return:: [String] or [nil]
37
+ def muc_name(jid)
38
+ iq = Iq.new(:get, jid)
39
+ iq.from = @stream.jid # Enable components to use this
40
+ iq.add(Discovery::IqQueryDiscoInfo.new)
41
+
42
+ res = nil
43
+
44
+ @stream.send_with_id(iq) do |answer|
45
+ if answer.type == :result
46
+ answer.query.each_element('feature') { |feature|
47
+ # Look if the component has a MUC or Groupchat feature
48
+ if feature.var == 'http://jabber.org/protocol/muc' or feature.var == 'gc-1.0'
49
+ # If so, get the identity
50
+ if answer.query.first_element('identity')
51
+ res = answer.query.first_element('identity').iname
52
+ end
53
+ end
54
+ }
55
+ true
56
+ else
57
+ false
58
+ end
59
+ end
60
+
61
+ res
62
+ end
63
+
64
+ ##
65
+ # Retrieve the existing rooms of a MUC component
66
+ #
67
+ # The resulting Hash contains pairs of room JID and room name
68
+ #
69
+ # Usage:
70
+ # my_mucbrowse_helper.muc_rooms('conference.jabber.org').each { |jid,name| ... }
71
+ #
72
+ # Throws an exception when receiving <tt><iq type='error'/></tt>
73
+ # jid:: [JID] Target entity (set only domain!)
74
+ # return:: [Hash]
75
+ def muc_rooms(jid)
76
+ iq = Iq.new(:get, jid)
77
+ iq.from = @stream.jid # Enable components to use this
78
+ iq.add(Discovery::IqQueryDiscoItems.new)
79
+
80
+ rooms = {}
81
+
82
+ @stream.send_with_id(iq) do |answer|
83
+ answer.query.each_element('item') { |item|
84
+ rooms[item.jid] = item.iname
85
+ }
86
+ end
87
+
88
+ rooms
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,469 @@
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/muc'
6
+ require 'xmpp4r/muc/iq/mucowner'
7
+ require 'xmpp4r/muc/iq/mucadmin'
8
+ require 'xmpp4r/dataforms'
9
+
10
+ module Jabber
11
+ module MUC
12
+ ##
13
+ # The MUCClient Helper handles low-level stuff of the
14
+ # Multi-User Chat (JEP 0045).
15
+ #
16
+ # Use one instance per room.
17
+ #
18
+ # Note that one client cannot join a single room multiple
19
+ # times. At least the clients' resources must be different.
20
+ # This is a protocol design issue. But don't consider it as
21
+ # a bug, it is just a clone-preventing feature.
22
+ class MUCClient
23
+ ##
24
+ # Sender JID, set this to use MUCClient from Components
25
+ # my_jid:: [JID] Defaults to nil
26
+ attr_accessor :my_jid
27
+
28
+ ##
29
+ # MUC room roster
30
+ # roster:: [Hash] of [String] Nick => [Presence]
31
+ attr_reader :roster
32
+
33
+ ##
34
+ # MUC JID
35
+ # jid:: [JID] room@component/nick
36
+ attr_reader :jid
37
+
38
+ ##
39
+ # Initialize a MUCClient
40
+ #
41
+ # Call MUCClient#join *after* you have registered your
42
+ # callbacks to avoid reception of stanzas after joining
43
+ # and before registration of callbacks.
44
+ # stream:: [Stream] to operate on
45
+ def initialize(stream)
46
+ # Attributes initialization
47
+ @stream = stream
48
+ @my_jid = nil
49
+ @jid = nil
50
+ @roster = {}
51
+ @roster_lock = Mutex.new
52
+
53
+ @active = false
54
+
55
+ @join_cbs = CallbackList.new
56
+ @leave_cbs = CallbackList.new
57
+ @presence_cbs = CallbackList.new
58
+ @message_cbs = CallbackList.new
59
+ @private_message_cbs = CallbackList.new
60
+ end
61
+
62
+ ##
63
+ # Join a room
64
+ #
65
+ # This registers its own callbacks on the stream
66
+ # provided to initialize and sends initial presence
67
+ # to the room. May throw ServerError if joining
68
+ # fails.
69
+ # jid:: [JID] room@component/nick
70
+ # password:: [String] Optional password
71
+ # opts:: [Hash] If the parameter :history => false is passed then you will receive no history messages after initial presence
72
+ # return:: [MUCClient] self (chain-able)
73
+ def join(jid, password=nil, opts={})
74
+ if active?
75
+ raise "MUCClient already active"
76
+ end
77
+
78
+ @jid = (jid.kind_of?(JID) ? jid : JID.new(jid))
79
+ activate
80
+
81
+ # Joining
82
+ pres = Presence.new
83
+ pres.to = @jid
84
+ pres.from = @my_jid
85
+ xmuc = XMUC.new
86
+ xmuc.password = password
87
+
88
+ # Add history/maxstanzas as requested. false=0
89
+ xmuc.add_element REXML::Element.new('history').tap { |hist|
90
+ hist.add_attribute 'maxstanzas', (opts[:history]||0).to_i.to_s
91
+ } unless opts[:history].nil?
92
+
93
+ pres.add(xmuc)
94
+
95
+ # We don't use Stream#send_with_id here as it's unknown
96
+ # if the MUC component *always* uses our stanza id.
97
+ error = nil
98
+ @stream.send(pres) { |r|
99
+ if from_room?(r.from) and r.kind_of?(Presence) and r.type == :error
100
+ # Error from room
101
+ error = r.error
102
+ true
103
+ # type='unavailable' may occur when the MUC kills our previous instance,
104
+ # but all join-failures should be type='error'
105
+ elsif r.from == @jid and r.kind_of?(Presence) and r.type != :unavailable
106
+ # Our own presence reflected back - success
107
+ if r.x(XMUCUser) and (i = r.x(XMUCUser).items.first)
108
+ @affiliation = i.affiliation # we're interested in if it's :owner
109
+ @role = i.role # :moderator ?
110
+ end
111
+
112
+ handle_presence(r, false)
113
+ true
114
+ else
115
+ # Everything else
116
+ false
117
+ end
118
+ }
119
+
120
+ if error
121
+ deactivate
122
+ raise ServerError.new(error)
123
+ end
124
+
125
+ self
126
+ end
127
+
128
+ ##
129
+ # Exit the room
130
+ #
131
+ # * Sends presence with type='unavailable' with an optional
132
+ # reason in <tt><status/></tt>,
133
+ # * then waits for a reply from the MUC component (will be
134
+ # processed by leave-callbacks),
135
+ # * then deletes callbacks from the stream.
136
+ # reason:: [String] Optional custom exit message
137
+ def exit(reason=nil)
138
+ unless active?
139
+ raise "MUCClient hasn't yet joined"
140
+ end
141
+
142
+ pres = Presence.new
143
+ pres.type = :unavailable
144
+ pres.to = jid
145
+ pres.from = @my_jid
146
+ pres.status = reason if reason
147
+ @stream.send(pres) { |r|
148
+ Jabber::debuglog "exit: #{r.to_s.inspect}"
149
+ if r.kind_of?(Presence) and r.type == :unavailable and r.from == jid
150
+ @leave_cbs.process(r)
151
+ true
152
+ else
153
+ false
154
+ end
155
+ }
156
+
157
+ deactivate
158
+
159
+ self
160
+ end
161
+
162
+ ##
163
+ # Is the MUC client active?
164
+ #
165
+ # This is false after initialization,
166
+ # true after joining and
167
+ # false after exit/kick
168
+ def active?
169
+ @active
170
+ end
171
+
172
+ ##
173
+ # The MUCClient's own nick
174
+ # (= resource)
175
+ # result:: [String] Nickname
176
+ def nick
177
+ @jid ? @jid.resource : nil
178
+ end
179
+
180
+ ##
181
+ # Change nick
182
+ #
183
+ # Threading is, again, suggested. This method waits for two
184
+ # <presence/> stanzas, one indicating unavailabilty of the old
185
+ # transient JID, one indicating availability of the new
186
+ # transient JID.
187
+ #
188
+ # If the service denies nick-change, ServerError will be raised.
189
+ def nick=(new_nick)
190
+ unless active?
191
+ raise "MUCClient not active"
192
+ end
193
+
194
+ new_jid = JID.new(@jid.node, @jid.domain, new_nick)
195
+
196
+ # Joining
197
+ pres = Presence.new
198
+ pres.to = new_jid
199
+ pres.from = @my_jid
200
+
201
+ error = nil
202
+ # Keeping track of the two stanzas enables us to process stanzas
203
+ # which don't arrive in the order specified by JEP-0045
204
+ presence_unavailable = false
205
+ presence_available = false
206
+ # We don't use Stream#send_with_id here as it's unknown
207
+ # if the MUC component *always* uses our stanza id.
208
+ @stream.send(pres) { |r|
209
+ if from_room?(r.from) and r.kind_of?(Presence) and r.type == :error
210
+ # Error from room
211
+ error = r.error
212
+ elsif r.from == @jid and r.kind_of?(Presence) and r.type == :unavailable and
213
+ r.x and r.x.kind_of?(XMUCUser) and r.x.status_code == 303
214
+ # Old JID is offline, but wait for the new JID and let stanza be handled
215
+ # by the standard callback
216
+ presence_unavailable = true
217
+ handle_presence(r)
218
+ elsif r.from == new_jid and r.kind_of?(Presence) and r.type != :unavailable
219
+ # Our own presence reflected back - success
220
+ presence_available = true
221
+ handle_presence(r)
222
+ end
223
+
224
+ if error or (presence_available and presence_unavailable)
225
+ true
226
+ else
227
+ false
228
+ end
229
+ }
230
+
231
+ if error
232
+ raise ServerError.new(error)
233
+ end
234
+
235
+ # Apply new JID
236
+ @jid = new_jid
237
+ end
238
+
239
+ ##
240
+ # The room name
241
+ # (= node)
242
+ # result:: [String] Room name
243
+ def room
244
+ @jid ? @jid.node : nil
245
+ end
246
+
247
+ ##
248
+ # Send a stanza to the room
249
+ #
250
+ # If stanza is a Jabber::Message, <tt>stanza.type</tt> will be
251
+ # automatically set to :groupchat if directed to room or :chat
252
+ # if directed to participant.
253
+ # stanza:: [XMPPStanza] to send
254
+ # to:: [String] Stanza destination recipient, or room if +nil+
255
+ def send(stanza, to=nil)
256
+ if stanza.kind_of? Message
257
+ stanza.type = to ? :chat : :groupchat
258
+ end
259
+ stanza.from = @my_jid
260
+ stanza.to = JID.new(jid.node, jid.domain, to)
261
+ @stream.send(stanza)
262
+ end
263
+
264
+ ##
265
+ # Add a callback for <presence/> stanzas indicating availability
266
+ # of a MUC participant
267
+ #
268
+ # This callback will *not* be called for initial presences when
269
+ # a client joins a room, but only for the presences afterwards.
270
+ #
271
+ # The callback will be called from MUCClient#handle_presence with
272
+ # one argument: the <presence/> stanza.
273
+ # Note that this stanza will have been already inserted into
274
+ # MUCClient#roster.
275
+ def add_join_callback(prio = 0, ref = nil, &block)
276
+ @join_cbs.add(prio, ref, block)
277
+ end
278
+
279
+ ##
280
+ # Add a callback for <presence/> stanzas indicating unavailability
281
+ # of a MUC participant
282
+ #
283
+ # The callback will be called with one argument: the <presence/> stanza.
284
+ #
285
+ # Note that this is called just *before* the stanza is removed from
286
+ # MUCClient#roster, so it is still possible to see the last presence
287
+ # in the given block.
288
+ #
289
+ # If the presence's origin is your MUC JID, the MUCClient will be
290
+ # deactivated *afterwards*.
291
+ def add_leave_callback(prio = 0, ref = nil, &block)
292
+ @leave_cbs.add(prio, ref, block)
293
+ end
294
+
295
+ ##
296
+ # Add a callback for a <presence/> stanza which is neither a join
297
+ # nor a leave. This will be called when a room participant simply
298
+ # changes his status.
299
+ def add_presence_callback(prio = 0, ref = nil, &block)
300
+ @presence_cbs.add(prio, ref, block)
301
+ end
302
+
303
+ ##
304
+ # Add a callback for <message/> stanza directed to the whole room.
305
+ #
306
+ # See MUCClient#add_private_message_callback for private messages
307
+ # between MUC participants.
308
+ def add_message_callback(prio = 0, ref = nil, &block)
309
+ @message_cbs.add(prio, ref, block)
310
+ end
311
+
312
+ ##
313
+ # Add a callback for <message/> stanza with type='chat'.
314
+ #
315
+ # These stanza are normally not broadcasted to all room occupants
316
+ # but are some sort of private messaging.
317
+ def add_private_message_callback(prio = 0, ref = nil, &block)
318
+ @private_message_cbs.add(prio, ref, block)
319
+ end
320
+
321
+ ##
322
+ # Does this JID belong to that room?
323
+ # jid:: [JID]
324
+ # result:: [true] or [false]
325
+ def from_room?(jid)
326
+ @jid.strip == jid.strip
327
+ end
328
+
329
+ private
330
+
331
+ ##
332
+ # call_join_cbs:: [Bool] Do not call them if we receive initial presences from room
333
+ def handle_presence(pres, call_join_cbs=true) # :nodoc:
334
+ if pres.type == :unavailable or pres.type == :error
335
+ @leave_cbs.process(pres)
336
+ @roster_lock.synchronize {
337
+ @roster.delete(pres.from.resource)
338
+ }
339
+
340
+ if pres.from == jid and !(pres.x and pres.x.kind_of?(XMUCUser) and pres.x.status_code == 303)
341
+ deactivate
342
+ end
343
+ else
344
+ is_join = ! @roster.has_key?(pres.from.resource)
345
+ @roster_lock.synchronize {
346
+ @roster[pres.from.resource] = pres
347
+ }
348
+ if is_join
349
+ @join_cbs.process(pres) if call_join_cbs
350
+ else
351
+ @presence_cbs.process(pres)
352
+ end
353
+ end
354
+ end
355
+
356
+ def handle_message(msg) # :nodoc:
357
+ if msg.type == :chat
358
+ @private_message_cbs.process(msg)
359
+ else # type == :groupchat or anything else
360
+ @message_cbs.process(msg)
361
+ end
362
+ end
363
+
364
+ def activate # :nodoc:
365
+ @active = true
366
+
367
+ # Callbacks
368
+ @stream.add_presence_callback(150, self) { |presence|
369
+ if from_room?(presence.from)
370
+ handle_presence(presence)
371
+ true
372
+ else
373
+ false
374
+ end
375
+ }
376
+
377
+ @stream.add_message_callback(150, self) { |message|
378
+ if from_room?(message.from)
379
+ handle_message(message)
380
+ true
381
+ else
382
+ false
383
+ end
384
+ }
385
+ end
386
+
387
+ def deactivate # :nodoc:
388
+ @active = false
389
+ @jid = nil
390
+
391
+ # Callbacks
392
+ @stream.delete_presence_callback(self)
393
+ @stream.delete_message_callback(self)
394
+ end
395
+
396
+ public
397
+ def owner?
398
+ @affiliation == :owner
399
+ end
400
+
401
+ ##
402
+ # Use this method to configure a MUC room of which you are the owner.
403
+ #
404
+ # options:: [Hash] where keys are the features of the room you wish
405
+ # to configure. See http://www.xmpp.org/extensions/xep-0045.html#registrar-formtype-owner
406
+ def configure(options={})
407
+ get_room_configuration
408
+ submit_room_configuration(options)
409
+ end
410
+
411
+ def get_room_configuration
412
+ raise 'You are not the owner' unless owner?
413
+
414
+ iq = Iq.new(:get, jid.strip)
415
+ iq.from = my_jid
416
+ iq.add(IqQueryMUCOwner.new)
417
+
418
+ fields = []
419
+
420
+ @stream.send_with_id(iq) do |answer|
421
+ raise "Configuration not possible for this room" unless answer.query && answer.query.x(Dataforms::XData)
422
+
423
+ answer.query.x(Dataforms::XData).fields.each do |field|
424
+ if (var = field.attributes['var'])
425
+ fields << var
426
+ end
427
+ end
428
+ end
429
+
430
+ fields
431
+ end
432
+
433
+ def submit_room_configuration(options)
434
+ # fill out the reply form
435
+ iq = Iq.new(:set, jid.strip)
436
+ iq.from = my_jid
437
+ query = IqQueryMUCOwner.new
438
+ form = Dataforms::XData.new
439
+ form.type = :submit
440
+ options.each do |var, values|
441
+ field = Dataforms::XDataField.new
442
+ values = [values] unless values.is_a?(Array)
443
+ field.var, field.values = var, values
444
+ form.add(field)
445
+ end
446
+ query.add(form)
447
+ iq.add(query)
448
+
449
+ @stream.send_with_id(iq)
450
+ end
451
+
452
+ ##
453
+ # Push a list of new affiliations to the room
454
+ # items:: [Array] of, or single [IqQueryMUCAdminItem]
455
+ def send_affiliations(items)
456
+ iq = Iq.new(:set, jid.strip)
457
+ iq.from = my_jid
458
+ iq.add(IqQueryMUCAdmin.new)
459
+
460
+ items = [items] unless items.kind_of? Array
461
+ items.each { |item|
462
+ iq.query.add(item)
463
+ }
464
+
465
+ @stream.send_with_id(iq)
466
+ end
467
+ end
468
+ end
469
+ end