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,191 @@
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 was based on Observable module from Ruby.
8
+ module ObservableThing
9
+ require 'xmpp4r/observable/thread_store'
10
+
11
+ # Adds an observer for some "thing".
12
+ #
13
+ # thing:: what will be observed.
14
+ # observer:: the observer.
15
+ # func:: the observer method that will be called (default: :update).
16
+ def add_observer(thing, observer, func = :update)
17
+ @things = {} unless defined? @things
18
+ @things[thing] = {} unless ! @things[thing].nil?
19
+ unless observer.respond_to? func
20
+ raise NoMethodError, "observer does not respond to `#{func.to_s}'"
21
+ end
22
+ @things[thing][observer] = func unless @things[thing].include?(observer)
23
+ end
24
+
25
+ # Deletes an observer for some "thing".
26
+ #
27
+ # thing:: what has been observed.
28
+ # observer:: the observer.
29
+ def delete_observer(thing, observer)
30
+ @things[thing].delete observer if defined? @things and ! @things[thing].nil?
31
+ end
32
+
33
+ # Delete observers for some "thing".
34
+ #
35
+ # thing:: what has been observed (if nil, deletes all observers).
36
+ def delete_observers(thing = nil)
37
+ if thing.nil?
38
+ @things.clear if defined? @things
39
+ else
40
+ @things[thing].clear if defined? @things and ! @things[thing].nil?
41
+ end
42
+ end
43
+
44
+ # Count the number of observers for some "thing".
45
+ #
46
+ # thing:: what has been observed (if nil, count all observers).
47
+ def count_observers(thing = nil)
48
+ return 0 if ! defined? @things
49
+ size = 0
50
+ if thing.nil?
51
+ @things.each { |thing, hash|
52
+ size += hash.size
53
+ }
54
+ else
55
+ size = @things[thing].size unless @things[thing].nil?
56
+ end
57
+ size
58
+ end
59
+
60
+ # Count the number of notifications for some "thing".
61
+ #
62
+ # thing:: what has been observed.
63
+ def count_notifications(thing)
64
+ return 0 if (! defined?(@things_counter)) or (! @things_counter.include?(thing))
65
+ @things_counter[thing]
66
+ end
67
+
68
+ # Change the state of some "thing".
69
+ #
70
+ # thing:: what will have the state changed.
71
+ # state:: the state (default = true).
72
+ def changed(thing, state = true)
73
+ @things_state = {} unless defined? @things_state
74
+ @things_state[thing] = state
75
+ end
76
+
77
+ # Check the state of some "thing".
78
+ #
79
+ # thing: what to have its state checked.
80
+ def changed?(thing)
81
+ if defined? @things_state and @things_state[thing]
82
+ true
83
+ else
84
+ false
85
+ end
86
+ end
87
+
88
+ # Notify all observers of "thing" about something. This will only be
89
+ # enforced if the state of that "thing" is true. Also, if the observer
90
+ # returns the Symbol :delete_me, it will be deleted after being notified.
91
+ #
92
+ # thing:: what has been observed.
93
+ # args:: notification to be sent to the observers of "thing".
94
+ def notify_observers(thing, *arg)
95
+ if changed?(thing)
96
+ if defined? @things and ! @things[thing].nil?
97
+ @things[thing].each { |observer, func|
98
+ increase_counter(thing)
99
+ @thread_store = ThreadStore.new if ! defined? @thread_store
100
+ @thread_store.add Thread.new {
101
+ if observer.send(func, thing, *arg) == :delete_me
102
+ delete_observer(thing, observer)
103
+ end
104
+ }
105
+ }
106
+ end
107
+ changed(thing, false)
108
+ end
109
+ end
110
+
111
+ # Is there pending notifications?
112
+ def pending_notifications?
113
+ return false if ! defined? @thread_store
114
+ @thread_store.size > 0
115
+ end
116
+
117
+ # Wait all notifications
118
+ def wait_notifications
119
+ sleep 1 while pending_notifications?
120
+ end
121
+
122
+ private
123
+ def increase_counter(thing)
124
+ @things_counter = {} unless defined? @things_counter
125
+ @things_counter[thing] = 0 unless @things_counter.include?(thing)
126
+ @things_counter[thing] += 1
127
+ end
128
+ end
129
+
130
+ # QObserver - simple observer-to-queue class
131
+ class QObserver
132
+ require 'thread'
133
+
134
+ def initialize
135
+ @queues = Hash.new
136
+ end
137
+
138
+ def inspect
139
+ h = {}
140
+ @queues.each do |q, queue|
141
+ h[q] = queue.size
142
+ end
143
+ if h.length > 0
144
+ sprintf("#<%s:0x%x size=%s>", self.class.name, __id__, h.inspect)
145
+ else
146
+ sprintf("#<%s:0x%x>", self.class.name, __id__)
147
+ end
148
+ end
149
+
150
+ # Return the queues we have registered
151
+ def queues
152
+ @queues.keys
153
+ end
154
+
155
+ # Received something in this queue?
156
+ #
157
+ # q:: queue
158
+ def received?(q)
159
+ @queues.include?(q) and ! @queues[q].empty?
160
+ end
161
+
162
+ # Get the contents of the queue in an array (or pass each item to the
163
+ # given block.
164
+ #
165
+ # q:: queue
166
+ def received(q)
167
+ return nil if ! @queues.include?(q)
168
+ if block_given?
169
+ yield @queues[q].deq while ! @queues[q].empty?
170
+ else
171
+ a = []
172
+ a << @queues[q].deq while ! @queues[q].empty?
173
+ return a
174
+ end
175
+ end
176
+
177
+ # Get the size of a given queue
178
+ #
179
+ # q:: queue
180
+ def size(q)
181
+ @queues[q].size rescue 0
182
+ end
183
+
184
+ # update method for our Observer
185
+ #
186
+ # thing:: what to be updated
187
+ def update(thing, *args)
188
+ @queues[thing] = Queue.new if ! @queues.include?(thing)
189
+ @queues[thing].enq args
190
+ end
191
+ end
@@ -0,0 +1,211 @@
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/pubsub'
8
+ require 'xmpp4r/pubsub/helper/servicehelper'
9
+ require 'xmpp4r/pubsub/helper/nodebrowser'
10
+ require 'xmpp4r/pubsub/helper/nodehelper'
11
+
12
+ module Jabber
13
+ class Observable
14
+ # Jabber::Observable::PubSub - Convenience subclass to deal with PubSub
15
+ class PubSub
16
+ class NoService < StandardError; end #:nodoc:
17
+
18
+ class AlreadySet < StandardError; end #:nodoc:
19
+
20
+ # Creates a new PubSub object
21
+ #
22
+ # observable:: points a Jabber::Observable object
23
+ def initialize(observable)
24
+ @observable = observable
25
+
26
+ @helper = @service_jid = nil
27
+ @disco = Jabber::Discovery::Helper.new(@observable.client)
28
+ attach!
29
+ end
30
+
31
+ def attach!
32
+ begin
33
+ domain = Jabber::JID.new(@observable.jid).domain
34
+ @service_jid = "pubsub." + domain
35
+ set_service(@service_jid)
36
+ rescue
37
+ @helper = @service_jid = nil
38
+ end
39
+ return has_service?
40
+ end
41
+
42
+ def inspect #:nodoc:
43
+ if has_service?
44
+ sprintf("#<%s:0x%x @service_jid=%s>", self.class.name, __id__, @service_jid)
45
+ else
46
+ sprintf("#<%s:0x%x @has_service?=false>", self.class.name, __id__)
47
+ end
48
+ end
49
+
50
+ # Checks if the PubSub service is set
51
+ def has_service?
52
+ ! @helper.nil?
53
+ end
54
+
55
+ # Sets the PubSub service. Just one service is allowed. If nil, reset.
56
+ def set_service(service)
57
+ if service.nil?
58
+ @helper = @service_jid = nil
59
+ else
60
+ raise NotConnected, "You are not connected" if ! @observable.connected?
61
+ raise AlreadySet, "You already have a PubSub service (#{@service_jid})." if has_service?
62
+ @helper = Jabber::PubSub::ServiceHelper.new(@observable.client, service)
63
+ @service_jid = service
64
+
65
+ @helper.add_event_callback do |event|
66
+ @observable.changed(:event)
67
+ @observable.notify_observers(:event, event)
68
+ end
69
+ end
70
+ end
71
+
72
+ # Subscribe to a node.
73
+ def subscribe_to(node)
74
+ raise_noservice if ! has_service?
75
+ @helper.subscribe_to(node) unless is_subscribed_to?(node)
76
+ end
77
+
78
+ # Unsubscribe from a node.
79
+ def unsubscribe_from(node)
80
+ raise_noservice if ! has_service?
81
+ @helper.unsubscribe_from(node)
82
+ end
83
+
84
+ # Return the subscriptions we have in the configured PubSub service.
85
+ def subscriptions
86
+ raise_noservice if ! has_service?
87
+ @helper.get_subscriptions_from_all_nodes()
88
+ end
89
+
90
+ # Create a PubSub node (Lots of options still have to be encoded!)
91
+ def create_node(node)
92
+ raise_noservice if ! has_service?
93
+ begin
94
+ @helper.create_node(node)
95
+ rescue => e
96
+ raise e
97
+ return nil
98
+ end
99
+ @my_nodes << node if defined? @my_nodes
100
+ node
101
+ end
102
+
103
+ # Return an array of nodes I own
104
+ def my_nodes
105
+ if ! defined? @my_nodes
106
+ ret = []
107
+ subscriptions.each do |sub|
108
+ ret << sub.node if sub.attributes['affiliation'] == 'owner'
109
+ end
110
+ @my_nodes = ret
111
+ end
112
+ return @my_nodes
113
+ end
114
+
115
+ # Return true if a given node exists
116
+ def node_exists?(node)
117
+ ret = []
118
+ if ! defined? @existing_nodes or ! @existing_nodes.include?(node)
119
+ # We'll renew @existing_nodes if we haven't got it the first time
120
+ reply = @disco.get_items_for(@service_jid)
121
+ reply.items.each do |item|
122
+ ret << item.node
123
+ end
124
+ @existing_nodes = ret
125
+ end
126
+ return @existing_nodes.include?(node)
127
+ end
128
+
129
+ # Returns an array of nodes I am subscribed to
130
+ def subscribed_nodes
131
+ ret = []
132
+ subscriptions.each do |sub|
133
+ next if sub.node.nil?
134
+ ret << sub.node if sub.attributes['subscription'] == 'subscribed' and ! my_nodes.include?(sub.node)
135
+ end
136
+ return ret
137
+ end
138
+
139
+ # Return true if we're subscribed to that node
140
+ def is_subscribed_to?(node)
141
+ ret = false
142
+ subscriptions.each do |sub|
143
+ ret = true if sub.node == node and sub.attributes['subscription'] == 'subscribed'
144
+ end
145
+ return ret
146
+ end
147
+
148
+ # Delete a PubSub node (Lots of options still have to be encoded!)
149
+ def delete_node(node)
150
+ raise_noservice if ! has_service?
151
+ begin
152
+ @helper.delete_node(node)
153
+ rescue => e
154
+ raise e
155
+ return nil
156
+ end
157
+ @my_nodes.delete(node) if defined? @my_nodes
158
+ node
159
+ end
160
+
161
+ # Publish an Item. This infers an item of Jabber::PubSub::Item kind is passed
162
+ def publish_item(node, item)
163
+ raise_noservice if ! has_service?
164
+ @helper.publish_item_to(node, item)
165
+ end
166
+
167
+ # Publish Simple Item. This is an item with one element and some text to it.
168
+ def publish_simple_item(node, text)
169
+ raise_noservice if ! has_service?
170
+
171
+ item = Jabber::PubSub::Item.new
172
+ xml = REXML::Element.new('value')
173
+ xml.text = text
174
+ item.add(xml)
175
+ publish_item(node, item)
176
+ end
177
+
178
+ # Publish atom Item. This is an item with one atom entry with title, body and time.
179
+ def publish_atom_item(node, title, body, time = Time.now)
180
+ raise_noservice if ! has_service?
181
+
182
+ item = Jabber::PubSub::Item.new
183
+ entry = REXML::Element.new('entry')
184
+ entry.add_namespace("http://www.w3.org/2005/Atom")
185
+ mytitle = REXML::Element.new('title')
186
+ mytitle.text = title
187
+ entry.add(mytitle)
188
+ mybody = REXML::Element.new('body')
189
+ mybody.text = body
190
+ entry.add(mybody)
191
+ published = REXML::Element.new("published")
192
+ published.text = time.utc.iso8601
193
+ entry.add(published)
194
+ item.add(entry)
195
+ publish_item(node, item)
196
+ end
197
+
198
+ # Get items from a node
199
+ def get_items_from(node, count = nil)
200
+ raise_noservice if ! has_service?
201
+ @helper.get_items_from(node, count)
202
+ end
203
+
204
+ private
205
+
206
+ def raise_noservice #:nodoc:
207
+ raise NoService, "Have you forgot to call #set_service ?"
208
+ end
209
+ end # of class PubSub
210
+ end # of class Observable
211
+ end # of module Jabber
@@ -0,0 +1,57 @@
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
+ module Jabber
7
+ class Observable
8
+ # Jabber::Observable::Subscriptions - convenience class to deal with
9
+ # Presence subscriptions
10
+ #
11
+ # observable:: points to a Jabber::Observable object
12
+ class Subscriptions
13
+ def initialize(observable)
14
+ @observable = observable
15
+ @accept = true
16
+ end
17
+
18
+ # Ask the users specified by jids for authorization (i.e., ask them to add
19
+ # you to their contact list), unless already in the contact list.
20
+ #
21
+ # Because the authorization process depends on the other user accepting your
22
+ # request, results are notified to observers of :new_subscription.
23
+ def add(*jids)
24
+ @observable.contacts(*jids).each do |contact|
25
+ next if subscribed_to?(contact)
26
+ contact.ask_for_authorization!
27
+ end
28
+ end
29
+
30
+ # Remove the jabber users specified by jids from the contact list.
31
+ def remove(*jids)
32
+ @observable.contacts(*jids).each do |contact|
33
+ contact.unsubscribe!
34
+ end
35
+ end
36
+
37
+ # Returns true if this Jabber account is subscribed to status updates for
38
+ # the jabber user jid, false otherwise.
39
+ def subscribed_to?(jid)
40
+ @observable.contacts(jid).each do |contact|
41
+ return contact.subscribed?
42
+ end
43
+ end
44
+
45
+ # Returns true if auto-accept subscriptions is enabled (default), false otherwise.
46
+ def accept?
47
+ @accept
48
+ end
49
+
50
+ # Change whether or not subscriptions are automatically accepted.
51
+ def accept=(accept_status)
52
+ @accept = accept_status
53
+ end
54
+ end # of class Subscription
55
+ end # of class Observable
56
+ end # of module Jabber
57
+