xmpp4r 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/COPYING +340 -0
  2. data/ChangeLog +28 -0
  3. data/LICENSE +59 -0
  4. data/README +20 -0
  5. data/Rakefile +103 -0
  6. data/UPDATING +40 -0
  7. data/data/doc/xmpp4r/examples/advanced/adventure/README +57 -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 +425 -0
  13. data/data/doc/xmpp4r/examples/advanced/fileserve.conf +11 -0
  14. data/data/doc/xmpp4r/examples/advanced/fileserve.rb +344 -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 +89 -0
  18. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +266 -0
  19. data/data/doc/xmpp4r/examples/advanced/recvfile.rb +83 -0
  20. data/data/doc/xmpp4r/examples/advanced/rosterdiscovery.rb +130 -0
  21. data/data/doc/xmpp4r/examples/advanced/sendfile.conf +10 -0
  22. data/data/doc/xmpp4r/examples/advanced/sendfile.rb +72 -0
  23. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb +51 -0
  24. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_jabber.rb +43 -0
  25. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_test.rb +10 -0
  26. data/data/doc/xmpp4r/examples/advanced/versionpoll.rb +90 -0
  27. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +134 -0
  28. data/data/doc/xmpp4r/examples/advanced/xmppingrc.sample +9 -0
  29. data/data/doc/xmpp4r/examples/basic/change_password.rb +41 -0
  30. data/data/doc/xmpp4r/examples/basic/client.rb +68 -0
  31. data/data/doc/xmpp4r/examples/basic/component.rb +11 -0
  32. data/data/doc/xmpp4r/examples/basic/echo_nonthreaded.rb +32 -0
  33. data/data/doc/xmpp4r/examples/basic/echo_threaded.rb +32 -0
  34. data/data/doc/xmpp4r/examples/basic/jabbersend.rb +41 -0
  35. data/data/doc/xmpp4r/examples/basic/mass_sender.rb +67 -0
  36. data/data/doc/xmpp4r/examples/basic/mucinfo.rb +39 -0
  37. data/data/doc/xmpp4r/examples/basic/mucsimplebot.rb +83 -0
  38. data/data/doc/xmpp4r/examples/basic/register.rb +25 -0
  39. data/data/doc/xmpp4r/examples/basic/remove_registration.rb +18 -0
  40. data/data/doc/xmpp4r/examples/basic/roster.rb +42 -0
  41. data/data/doc/xmpp4r/examples/basic/rosterprint.rb +50 -0
  42. data/data/doc/xmpp4r/examples/basic/rosterrename.rb +34 -0
  43. data/data/doc/xmpp4r/examples/basic/rosterwatch.rb +172 -0
  44. data/data/doc/xmpp4r/examples/basic/send_vcard.rb +68 -0
  45. data/data/doc/xmpp4r/examples/basic/versionbot.rb +75 -0
  46. data/data/doc/xmpp4r/examples/buggy/jabber2jabber/jabber2jabber.rb +18 -0
  47. data/data/doc/xmpp4r/examples/buggy/miniedgarr_cgi.rb +192 -0
  48. data/data/doc/xmpp4r/examples/buggy/miniedgarr_watch.rb +82 -0
  49. data/lib/callbacks.rb +122 -0
  50. data/lib/xmpp4r/authenticationfailure.rb +13 -0
  51. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +315 -0
  52. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +256 -0
  53. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +30 -0
  54. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +46 -0
  55. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +151 -0
  56. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +85 -0
  57. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +178 -0
  58. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb +56 -0
  59. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +61 -0
  60. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +177 -0
  61. data/lib/xmpp4r/bytestreams/iq/si.rb +221 -0
  62. data/lib/xmpp4r/bytestreams.rb +11 -0
  63. data/lib/xmpp4r/client.rb +249 -0
  64. data/lib/xmpp4r/component.rb +103 -0
  65. data/lib/xmpp4r/connection.rb +166 -0
  66. data/lib/xmpp4r/dataforms/x/data.rb +248 -0
  67. data/lib/xmpp4r/dataforms.rb +1 -0
  68. data/lib/xmpp4r/debuglog.rb +34 -0
  69. data/lib/xmpp4r/delay/x/delay.rb +100 -0
  70. data/lib/xmpp4r/delay.rb +1 -0
  71. data/lib/xmpp4r/discovery/iq/discoinfo.rb +225 -0
  72. data/lib/xmpp4r/discovery/iq/discoitems.rb +162 -0
  73. data/lib/xmpp4r/discovery.rb +2 -0
  74. data/lib/xmpp4r/error.rb +230 -0
  75. data/lib/xmpp4r/errorexception.rb +32 -0
  76. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +42 -0
  77. data/lib/xmpp4r/feature_negotiation.rb +1 -0
  78. data/lib/xmpp4r/idgenerator.rb +37 -0
  79. data/lib/xmpp4r/iq.rb +229 -0
  80. data/lib/xmpp4r/jid.rb +167 -0
  81. data/lib/xmpp4r/message.rb +171 -0
  82. data/lib/xmpp4r/muc/helper/mucbrowser.rb +107 -0
  83. data/lib/xmpp4r/muc/helper/mucclient.rb +382 -0
  84. data/lib/xmpp4r/muc/helper/simplemucclient.rb +222 -0
  85. data/lib/xmpp4r/muc/x/muc.rb +98 -0
  86. data/lib/xmpp4r/muc/x/mucuserinvite.rb +58 -0
  87. data/lib/xmpp4r/muc/x/mucuseritem.rb +148 -0
  88. data/lib/xmpp4r/muc.rb +6 -0
  89. data/lib/xmpp4r/presence.rb +255 -0
  90. data/lib/xmpp4r/query.rb +43 -0
  91. data/lib/xmpp4r/rexmladdons.rb +826 -0
  92. data/lib/xmpp4r/roster/helper/roster.rb +514 -0
  93. data/lib/xmpp4r/roster/iq/roster.rb +244 -0
  94. data/lib/xmpp4r/roster/x/roster.rb +155 -0
  95. data/lib/xmpp4r/roster.rb +4 -0
  96. data/lib/xmpp4r/sasl.rb +167 -0
  97. data/lib/xmpp4r/stream.rb +543 -0
  98. data/lib/xmpp4r/streamparser.rb +77 -0
  99. data/lib/xmpp4r/vcard/helper/vcard.rb +86 -0
  100. data/lib/xmpp4r/vcard/iq/vcard.rb +102 -0
  101. data/lib/xmpp4r/vcard.rb +3 -0
  102. data/lib/xmpp4r/version/helper/responder.rb +71 -0
  103. data/lib/xmpp4r/version/helper/simpleresponder.rb +44 -0
  104. data/lib/xmpp4r/version/iq/version.rb +118 -0
  105. data/lib/xmpp4r/version.rb +3 -0
  106. data/lib/xmpp4r/x.rb +43 -0
  107. data/lib/xmpp4r/xmlstanza.rb +174 -0
  108. data/lib/xmpp4r/xmpp4r.rb +16 -0
  109. data/lib/xmpp4r.rb +122 -0
  110. data/setup.rb +1360 -0
  111. data/test/bytestreams/tc_ibb.rb +186 -0
  112. data/test/bytestreams/tc_socks5bytestreams.rb +57 -0
  113. data/test/delay/tc_xdelay.rb +51 -0
  114. data/test/lib/clienttester.rb +110 -0
  115. data/test/muc/tc_muc_mucclient.rb +569 -0
  116. data/test/muc/tc_muc_simplemucclient.rb +72 -0
  117. data/test/roster/.tc_helper.rb.swp +0 -0
  118. data/test/roster/tc_helper.rb +389 -0
  119. data/test/roster/tc_iqqueryroster.rb +140 -0
  120. data/test/roster/tc_xroster.rb +70 -0
  121. data/test/tc_callbacks.rb +128 -0
  122. data/test/tc_class_names.rb +129 -0
  123. data/test/tc_client.rb +30 -0
  124. data/test/tc_error.rb +103 -0
  125. data/test/tc_idgenerator.rb +30 -0
  126. data/test/tc_iq.rb +109 -0
  127. data/test/tc_iqquery.rb +31 -0
  128. data/test/tc_jid.rb +202 -0
  129. data/test/tc_message.rb +114 -0
  130. data/test/tc_presence.rb +148 -0
  131. data/test/tc_stream.rb +182 -0
  132. data/test/tc_streamError.rb +87 -0
  133. data/test/tc_streamSend.rb +59 -0
  134. data/test/tc_streamThreaded.rb +168 -0
  135. data/test/tc_xmlstanza.rb +76 -0
  136. data/test/ts_xmpp4r.rb +34 -0
  137. data/test/vcard/tc_iqvcard.rb +52 -0
  138. data/test/version/tc_helper.rb +46 -0
  139. data/test/version/tc_iqqueryversion.rb +96 -0
  140. data/tools/doctoweb.bash +30 -0
  141. data/tools/gen_requires.bash +10 -0
  142. metadata +232 -0
@@ -0,0 +1,569 @@
1
+ #!/usr/bin/ruby
2
+
3
+
4
+ $:.unshift File::dirname(__FILE__) + '/../../lib'
5
+
6
+ require 'test/unit'
7
+ require File::dirname(__FILE__) + '/../lib/clienttester'
8
+ require 'xmpp4r/muc'
9
+ include Jabber
10
+
11
+ class MUCClientTest < Test::Unit::TestCase
12
+ include ClientTester
13
+
14
+ def test_new1
15
+ m = MUC::MUCClient.new(@client)
16
+ assert_equal(nil, m.jid)
17
+ assert_equal(nil, m.my_jid)
18
+ assert_equal({}, m.roster)
19
+ assert(!m.active?)
20
+ end
21
+
22
+ # JEP-0045: 6.3 Entering a Room
23
+ def test_enter_room
24
+ state { |pres|
25
+ assert_kind_of(Presence, pres)
26
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
27
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
28
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
29
+ "<error code='400' type='modify'>" +
30
+ "<jid-malformed xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
31
+ "</error></presence>")
32
+ }
33
+ state { |pres|
34
+ assert_kind_of(Presence, pres)
35
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
36
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
37
+ send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
38
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='owner' role='moderator'/></x>" +
39
+ "</presence>" +
40
+ "<presence from='darkcave@macbeth.shakespeare.lit/secondwitch' to='hag66@shakespeare.lit/pda'>" +
41
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='admin' role='moderator'/></x>" +
42
+ "</presence>" +
43
+ "<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
44
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
45
+ "</presence>")
46
+ }
47
+
48
+
49
+ m = MUC::MUCClient.new(@client)
50
+ m.my_jid = 'hag66@shakespeare.lit/pda'
51
+ assert(!m.active?)
52
+ assert_nil(m.room)
53
+
54
+ assert_raises(ErrorException) {
55
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
56
+ }
57
+ assert(!m.active?)
58
+ assert_nil(m.room)
59
+
60
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
61
+ assert(m.active?)
62
+ assert_equal('darkcave', m.room)
63
+ assert_equal(3, m.roster.size)
64
+ m.roster.each { |resource,pres|
65
+ assert_equal(resource, pres.from.resource)
66
+ assert_equal('darkcave', pres.from.node)
67
+ assert_equal('macbeth.shakespeare.lit', pres.from.domain)
68
+ assert_kind_of(String, resource)
69
+ assert_kind_of(Presence, pres)
70
+ assert(%w(firstwitch secondwitch thirdwitch).include?(resource))
71
+ assert_kind_of(MUC::XMUCUser, pres.x)
72
+ assert_kind_of(Array, pres.x.items)
73
+ assert_equal(1, pres.x.items.size)
74
+ }
75
+ assert_equal(:owner, m.roster['firstwitch'].x.items[0].affiliation)
76
+ assert_equal(:moderator, m.roster['firstwitch'].x.items[0].role)
77
+ assert_equal(:admin, m.roster['secondwitch'].x.items[0].affiliation)
78
+ assert_equal(:moderator, m.roster['secondwitch'].x.items[0].role)
79
+ assert_equal(:member, m.roster['thirdwitch'].x.items[0].affiliation)
80
+ assert_equal(:participant, m.roster['thirdwitch'].x.items[0].role)
81
+ assert_nil(m.roster['thirdwitch'].x.items[0].jid)
82
+
83
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='crone1@shakespeare.lit/desktop'>" +
84
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='none' jid='hag66@shakespeare.lit/pda' role='participant'/></x>" +
85
+ "</presence>")
86
+ sleep 0.1
87
+ assert_equal(3, m.roster.size)
88
+ assert_equal(:none, m.roster['thirdwitch'].x.items[0].affiliation)
89
+ assert_equal(:participant, m.roster['thirdwitch'].x.items[0].role)
90
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), m.roster['thirdwitch'].x.items[0].jid)
91
+ end
92
+
93
+ def test_enter_room_password
94
+ state { |pres|
95
+ assert_kind_of(Presence, pres)
96
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
97
+ "<error code='401' type='auth'><not-authorized xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
98
+ "</presence>")
99
+ }
100
+ state { |pres|
101
+ assert_kind_of(Presence, pres)
102
+ assert_equal('cauldron', pres.x.password)
103
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
104
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
105
+ "</presence>")
106
+ }
107
+
108
+ m = MUC::MUCClient.new(@client)
109
+ m.my_jid = 'hag66@shakespeare.lit/pda'
110
+ assert_raises(ErrorException) {
111
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
112
+ }
113
+ assert(!m.active?)
114
+
115
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch', 'cauldron'))
116
+ assert(m.active?)
117
+ end
118
+
119
+ def test_members_only_room
120
+ state { |pres|
121
+ assert_kind_of(Presence, pres)
122
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
123
+ "<error code='407' type='auth'><registration-required xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
124
+ "</presence>")
125
+ }
126
+
127
+ m = MUC::MUCClient.new(@client)
128
+ m.my_jid = 'hag66@shakespeare.lit/pda'
129
+ assert_raises(ErrorException) {
130
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
131
+ }
132
+ assert(!m.active?)
133
+ end
134
+
135
+ def test_banned_users
136
+ state { |pres|
137
+ assert_kind_of(Presence, pres)
138
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
139
+ "<error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
140
+ "</presence>")
141
+ }
142
+
143
+ m = MUC::MUCClient.new(@client)
144
+ m.my_jid = 'hag66@shakespeare.lit/pda'
145
+ assert_raises(ErrorException) {
146
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
147
+ }
148
+ assert(!m.active?)
149
+ end
150
+
151
+ def test_nickname_conflict
152
+ state { |pres|
153
+ assert_kind_of(Presence, pres)
154
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
155
+ "<error code='409' type='cancel'><conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
156
+ "</presence>")
157
+ }
158
+
159
+ m = MUC::MUCClient.new(@client)
160
+ m.my_jid = 'hag66@shakespeare.lit/pda'
161
+ assert_raises(ErrorException) {
162
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
163
+ }
164
+ assert(!m.active?)
165
+ end
166
+
167
+ def test_max_users
168
+ state { |pres|
169
+ assert_kind_of(Presence, pres)
170
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
171
+ "<error code='503' type='wait'><service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
172
+ "</presence>")
173
+ }
174
+
175
+ m = MUC::MUCClient.new(@client)
176
+ m.my_jid = 'hag66@shakespeare.lit/pda'
177
+ assert_raises(ErrorException) {
178
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
179
+ }
180
+ assert(!m.active?)
181
+ end
182
+
183
+ def test_locked_room
184
+ state { |pres|
185
+ assert_kind_of(Presence, pres)
186
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
187
+ "<error code='404' type='cancel'><item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
188
+ "</presence>")
189
+ }
190
+
191
+ m = MUC::MUCClient.new(@client)
192
+ m.my_jid = 'hag66@shakespeare.lit/pda'
193
+ assert_raises(ErrorException) {
194
+ m.join('darkcave@macbeth.shakespeare.lit/thirdwitch')
195
+ }
196
+ assert(!m.active?)
197
+ end
198
+
199
+ def test_exit_room
200
+ state { |pres|
201
+ assert_kind_of(Presence, pres)
202
+ assert_nil(pres.type)
203
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
204
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
205
+ "</presence>")
206
+ }
207
+ state { |pres|
208
+ assert_kind_of(Presence, pres)
209
+ assert_equal(:unavailable, pres.type)
210
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
211
+ assert_nil(pres.status)
212
+ send("<presence from='darkcave@macbeth.shakespeare.lit/secondwitch' to='hag66@shakespeare.lit/pda'>" +
213
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
214
+ "</presence>")
215
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
216
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
217
+ "</presence>")
218
+ }
219
+
220
+ ignored_stanzas = 0
221
+ @client.add_stanza_callback { |stanza|
222
+ ignored_stanzas += 1
223
+ }
224
+
225
+ m = MUC::MUCClient.new(@client)
226
+ m.my_jid = 'hag66@shakespeare.lit/pda'
227
+ assert_equal(0, ignored_stanzas)
228
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
229
+ assert(m.active?)
230
+
231
+ assert_equal(0, ignored_stanzas)
232
+ assert_equal(m, m.exit)
233
+ assert(!m.active?)
234
+ assert_equal(1, ignored_stanzas)
235
+ end
236
+
237
+ def test_custom_exit_message
238
+ state { |pres|
239
+ assert_kind_of(Presence, pres)
240
+ assert_nil(pres.type)
241
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
242
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
243
+ "</presence>")
244
+ }
245
+ state { |pres|
246
+ assert_kind_of(Presence, pres)
247
+ assert_equal(:unavailable, pres.type)
248
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
249
+ assert_equal('gone where the goblins go', pres.status)
250
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
251
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
252
+ "</presence>")
253
+ }
254
+
255
+ m = MUC::MUCClient.new(@client)
256
+ m.my_jid = 'hag66@shakespeare.lit/pda'
257
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
258
+ assert(m.active?)
259
+
260
+ assert_equal(m, m.exit('gone where the goblins go'))
261
+ assert(!m.active?)
262
+ end
263
+
264
+ def test_joins
265
+ state { |pres|
266
+ assert_kind_of(Presence, pres)
267
+ assert_nil(pres.type)
268
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
269
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
270
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
271
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
272
+ "</presence>")
273
+ }
274
+ state { |pres|
275
+ assert_kind_of(Presence, pres)
276
+ assert_equal(:unavailable, pres.type)
277
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
278
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
279
+ assert_nil(pres.status)
280
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
281
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
282
+ "</presence>")
283
+ }
284
+ state { |pres|
285
+ assert_kind_of(Presence, pres)
286
+ assert_nil(pres.type)
287
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
288
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/fourthwitch'), pres.to)
289
+ send("<presence from='darkcave@macbeth.shakespeare.lit/fourthwitch' to='hag66@shakespeare.lit/pda'>" +
290
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
291
+ "</presence>")
292
+ }
293
+ state { |pres|
294
+ assert_kind_of(Presence, pres)
295
+ assert_equal(:unavailable, pres.type)
296
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
297
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/fourthwitch'), pres.to)
298
+ assert_equal(pres.status, 'Exiting one last time')
299
+ send("<presence from='darkcave@macbeth.shakespeare.lit/fourthwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
300
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='none'/></x>" +
301
+ "</presence>")
302
+ }
303
+
304
+ m = MUC::MUCClient.new(@client)
305
+ m.my_jid = 'hag66@shakespeare.lit/pda'
306
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
307
+ assert(m.active?)
308
+
309
+ assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/thirdwitch') }
310
+ assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/fourthwitch') }
311
+ assert(m.active?)
312
+
313
+ assert_equal(m, m.exit)
314
+ assert(!m.active?)
315
+ assert_raises(RuntimeError) { m.exit }
316
+ assert(!m.active?)
317
+
318
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/fourthwitch'))
319
+ assert(m.active?)
320
+
321
+ assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/thirdwitch') }
322
+ assert_raises(RuntimeError) { m.join('darkcave@macbeth.shakespeare.lit/fourthwitch') }
323
+ assert(m.active?)
324
+
325
+ assert_equal(m, m.exit('Exiting one last time'))
326
+ assert(!m.active?)
327
+ assert_raises(RuntimeError) { m.exit }
328
+ assert(!m.active?)
329
+ end
330
+
331
+ def test_message_callback
332
+ state { |pres|
333
+ assert_kind_of(Presence, pres)
334
+ assert_equal('cauldron', pres.x.password)
335
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
336
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
337
+ "</presence>")
338
+ }
339
+
340
+ message_lock = Mutex.new
341
+ message_lock.lock
342
+
343
+ messages_client = 0
344
+ @client.add_message_callback { |msg|
345
+ messages_client += 1
346
+ message_lock.unlock
347
+ }
348
+
349
+ m = MUC::MUCClient.new(@client)
350
+ m.my_jid = 'hag66@shakespeare.lit/pda'
351
+ messages_muc = 0
352
+ m.add_message_callback { |msg|
353
+ messages_muc += 1
354
+ message_lock.unlock
355
+ }
356
+ messages_muc_private = 0
357
+ m.add_private_message_callback { |msg|
358
+ messages_muc_private += 1
359
+ message_lock.unlock
360
+ }
361
+
362
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch', 'cauldron'))
363
+ assert(m.active?)
364
+
365
+ assert_equal(0, messages_client)
366
+ assert_equal(0, messages_muc)
367
+ assert_equal(0, messages_muc_private)
368
+
369
+ send("<message from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'><body>Hello</body></message>")
370
+ message_lock.lock
371
+
372
+ assert_equal(0, messages_client)
373
+ assert_equal(1, messages_muc)
374
+ assert_equal(0, messages_muc_private)
375
+
376
+ send("<message from='user@domain/resource' to='hag66@shakespeare.lit/pda'><body>Hello</body></message>")
377
+ message_lock.lock
378
+
379
+ assert_equal(1, messages_client)
380
+ assert_equal(1, messages_muc)
381
+ assert_equal(0, messages_muc_private)
382
+
383
+ send("<message type='chat' from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'><body>Hello</body></message>")
384
+ message_lock.lock
385
+
386
+ assert_equal(1, messages_client)
387
+ assert_equal(1, messages_muc)
388
+ assert_equal(1, messages_muc_private)
389
+ end
390
+
391
+ def test_presence_callbacks
392
+ state { |pres|
393
+ assert_kind_of(Presence, pres)
394
+ assert_nil(pres.x.password)
395
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
396
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
397
+ "</presence>")
398
+ }
399
+
400
+ presence_lock = Mutex.new
401
+ presence_lock.lock
402
+
403
+ presences_client = 0
404
+ @client.add_presence_callback { |pres|
405
+ presences_client += 1
406
+ presence_lock.unlock
407
+ }
408
+ m = MUC::MUCClient.new(@client)
409
+ m.my_jid = 'hag66@shakespeare.lit/pda'
410
+ presences_join = 0
411
+ m.add_join_callback { |pres|
412
+ presences_join += 1
413
+ presence_lock.unlock
414
+ }
415
+ presences_leave = 0
416
+ m.add_leave_callback { |pres|
417
+ presences_leave += 1
418
+ presence_lock.unlock
419
+ }
420
+ presences_muc = 0
421
+ m.add_presence_callback { |pres|
422
+ presences_muc += 1
423
+ presence_lock.unlock
424
+ }
425
+
426
+ assert_equal(0, presences_client)
427
+ assert_equal(0, presences_join)
428
+ assert_equal(0, presences_leave)
429
+ assert_equal(0, presences_muc)
430
+
431
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
432
+ assert(m.active?)
433
+
434
+ assert_equal(0, presences_client)
435
+ assert_equal(0, presences_join) # Joins before own join won't be called back
436
+ assert_equal(0, presences_leave)
437
+ assert_equal(0, presences_muc)
438
+
439
+ send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
440
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
441
+ "</presence>")
442
+ presence_lock.lock
443
+ assert_equal(0, presences_client)
444
+ assert_equal(1, presences_join)
445
+ assert_equal(0, presences_leave)
446
+ assert_equal(0, presences_muc)
447
+
448
+ send("<presence from='user@domain/resource' to='hag66@shakespeare.lit/pda'>" +
449
+ "<show>chat</show>" +
450
+ "</presence>")
451
+ presence_lock.lock
452
+ assert_equal(1, presences_client)
453
+ assert_equal(1, presences_join)
454
+ assert_equal(0, presences_leave)
455
+ assert_equal(0, presences_muc)
456
+
457
+ send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
458
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
459
+ "<show>away</show></presence>")
460
+ presence_lock.lock
461
+ assert_equal(1, presences_client)
462
+ assert_equal(1, presences_join)
463
+ assert_equal(0, presences_leave)
464
+ assert_equal(1, presences_muc)
465
+
466
+ send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda' type='unavailable'/>")
467
+ presence_lock.lock
468
+ assert_equal(1, presences_client)
469
+ assert_equal(1, presences_join)
470
+ assert_equal(1, presences_leave)
471
+ assert_equal(1, presences_muc)
472
+ end
473
+
474
+ def test_send
475
+ state { |pres|
476
+ assert_kind_of(Presence, pres)
477
+ assert_nil(pres.x.password)
478
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
479
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
480
+ "</presence>")
481
+ }
482
+ state { |stanza|
483
+ assert_kind_of(Message, stanza)
484
+ assert(:groupchat, stanza.type)
485
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), stanza.from)
486
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit'), stanza.to)
487
+ assert_equal('First message', stanza.body)
488
+ }
489
+ state { |stanza|
490
+ assert_kind_of(Message, stanza)
491
+ assert(:chat, stanza.type)
492
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), stanza.from)
493
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/secondwitch'), stanza.to)
494
+ assert_equal('Second message', stanza.body)
495
+ }
496
+ state { |stanza|
497
+ assert_kind_of(Message, stanza)
498
+ assert(:chat, stanza.type)
499
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), stanza.from)
500
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/firstwitch'), stanza.to)
501
+ assert_equal('Third message', stanza.body)
502
+ }
503
+
504
+ m = MUC::MUCClient.new(@client)
505
+ m.my_jid = 'hag66@shakespeare.lit/pda'
506
+
507
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
508
+ wait_state
509
+ assert(m.active?)
510
+
511
+ m.send(Jabber::Message.new(nil, 'First message'))
512
+ wait_state
513
+ m.send(Jabber::Message.new(nil, 'Second message'), 'secondwitch')
514
+ wait_state
515
+ m.send(Jabber::Message.new('secondwitch', 'Third message'), 'firstwitch')
516
+ wait_state
517
+ end
518
+
519
+ def test_nick
520
+ state { |pres|
521
+ assert_kind_of(Presence, pres)
522
+ assert_nil(pres.x.password)
523
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
524
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
525
+ "</presence>")
526
+ }
527
+ state { |pres|
528
+ assert_kind_of(Presence, pres)
529
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
530
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/secondwitch'), pres.to)
531
+ assert_nil(pres.type)
532
+ send("<presence from='darkcave@macbeth.shakespeare.lit' to='hag66@shakespeare.lit/pda' type='error'>" +
533
+ "<error code='409' type='cancel'><conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>" +
534
+ "</presence>")
535
+ }
536
+ state { |pres|
537
+ assert_kind_of(Presence, pres)
538
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
539
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/oldhag'), pres.to)
540
+ assert_nil(pres.type)
541
+ send("<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda' type='unavailable'>" +
542
+ "<x xmlns='http://jabber.org/protocol/muc#user'>" +
543
+ "<item affiliation='member' jid='hag66@shakespeare.lit/pda' nick='oldhag' role='participant'/>" +
544
+ "<status code='303'/>" +
545
+ "</x></presence>" +
546
+ "<presence from='darkcave@macbeth.shakespeare.lit/oldhag' to='hag66@shakespeare.lit/pda'>" +
547
+ "<x xmlns='http://jabber.org/protocol/muc#user'>" +
548
+ "<item affiliation='member' jid='hag66@shakespeare.lit/pda' role='participant'/>" +
549
+ "</x></presence>")
550
+ }
551
+
552
+ m = MUC::MUCClient.new(@client)
553
+ m.my_jid = 'hag66@shakespeare.lit/pda'
554
+
555
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
556
+ assert(m.active?)
557
+ assert_equal('thirdwitch', m.nick)
558
+
559
+ assert_raises(ErrorException) {
560
+ m.nick = 'secondwitch'
561
+ }
562
+ assert(m.active?)
563
+ assert_equal('thirdwitch', m.nick)
564
+
565
+ m.nick = 'oldhag'
566
+ assert(m.active?)
567
+ assert_equal('oldhag', m.nick)
568
+ end
569
+ end
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/ruby
2
+
3
+
4
+ $:.unshift File::dirname(__FILE__) + '/../../lib'
5
+
6
+ require 'test/unit'
7
+ require File::dirname(__FILE__) + '/../lib/clienttester'
8
+ require 'xmpp4r/muc'
9
+ include Jabber
10
+
11
+ class SimpleMUCClientTest < Test::Unit::TestCase
12
+ include ClientTester
13
+
14
+ def test_new1
15
+ m = MUC::SimpleMUCClient.new(@client)
16
+ assert_equal(nil, m.jid)
17
+ assert_equal(nil, m.my_jid)
18
+ assert_equal({}, m.roster)
19
+ assert(!m.active?)
20
+ end
21
+
22
+ def test_complex
23
+ m = MUC::SimpleMUCClient.new(@client)
24
+
25
+ block_args = []
26
+ wait = Mutex.new
27
+ block = lambda { |*a| block_args = a; wait.unlock }
28
+ m.on_room_message(&block)
29
+ m.on_message(&block)
30
+ m.on_private_message(&block)
31
+ m.on_subject(&block)
32
+ m.on_join(&block)
33
+ m.on_leave(&block)
34
+ m.on_self_leave(&block)
35
+
36
+ state { |pres|
37
+ assert_kind_of(Presence, pres)
38
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), pres.from)
39
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit/thirdwitch'), pres.to)
40
+ send("<presence from='darkcave@macbeth.shakespeare.lit/firstwitch' to='hag66@shakespeare.lit/pda'>" +
41
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='owner' role='moderator'/></x>" +
42
+ "</presence>" +
43
+ "<presence from='darkcave@macbeth.shakespeare.lit/secondwitch' to='hag66@shakespeare.lit/pda'>" +
44
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='admin' role='moderator'/></x>" +
45
+ "</presence>" +
46
+ "<presence from='darkcave@macbeth.shakespeare.lit/thirdwitch' to='hag66@shakespeare.lit/pda'>" +
47
+ "<x xmlns='http://jabber.org/protocol/muc#user'><item affiliation='member' role='participant'/></x>" +
48
+ "</presence>")
49
+ }
50
+ m.my_jid = 'hag66@shakespeare.lit/pda'
51
+ assert_equal(m, m.join('darkcave@macbeth.shakespeare.lit/thirdwitch'))
52
+ assert(m.active?)
53
+ assert_equal(3, m.roster.size)
54
+
55
+ state { |msg|
56
+ assert_kind_of(Message, msg)
57
+ assert_equal(:groupchat, msg.type)
58
+ assert_equal(JID.new('hag66@shakespeare.lit/pda'), msg.from)
59
+ assert_equal(JID.new('darkcave@macbeth.shakespeare.lit'), msg.to)
60
+ assert_equal('TestCasing room', msg.subject)
61
+ assert_nil(msg.body)
62
+ send(msg.set_from('darkcave@macbeth.shakespeare.lit/thirdwitch').set_to('hag66@shakespeare.lit/pda'))
63
+ }
64
+ assert_nil(m.subject)
65
+ wait.lock
66
+ m.subject = 'TestCasing room'
67
+ wait.lock
68
+ assert_equal([nil, 'thirdwitch', 'TestCasing room'], block_args)
69
+ assert_equal('TestCasing room', m.subject)
70
+ end
71
+
72
+ end
Binary file