bryanl-xmpp4r 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/COPYING +340 -0
  2. data/ChangeLog +81 -0
  3. data/LICENSE +59 -0
  4. data/README.rdoc +83 -0
  5. data/README_ruby19.txt +43 -0
  6. data/Rakefile +93 -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 +84 -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 +90 -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 +68 -0
  33. data/data/doc/xmpp4r/examples/basic/component.rb +11 -0
  34. data/data/doc/xmpp4r/examples/basic/echo.rb +35 -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 +39 -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 +42 -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 +54 -0
  48. data/data/doc/xmpp4r/examples/basic/tune_server.rb +56 -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 +320 -0
  54. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +259 -0
  55. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +33 -0
  56. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +47 -0
  57. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +155 -0
  58. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +89 -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 +62 -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 +124 -0
  65. data/lib/xmpp4r/caps.rb +2 -0
  66. data/lib/xmpp4r/caps/c.rb +53 -0
  67. data/lib/xmpp4r/caps/helper/generator.rb +160 -0
  68. data/lib/xmpp4r/caps/helper/helper.rb +87 -0
  69. data/lib/xmpp4r/client.rb +332 -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 +219 -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 +38 -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 +7 -0
  80. data/lib/xmpp4r/discovery/helper/responder.rb +165 -0
  81. data/lib/xmpp4r/discovery/iq/discoinfo.rb +211 -0
  82. data/lib/xmpp4r/discovery/iq/discoitems.rb +147 -0
  83. data/lib/xmpp4r/errors.rb +281 -0
  84. data/lib/xmpp4r/feature_negotiation.rb +5 -0
  85. data/lib/xmpp4r/feature_negotiation/iq/feature.rb +28 -0
  86. data/lib/xmpp4r/framework/base.rb +55 -0
  87. data/lib/xmpp4r/framework/bot.rb +148 -0
  88. data/lib/xmpp4r/httpbinding.rb +5 -0
  89. data/lib/xmpp4r/httpbinding/client.rb +285 -0
  90. data/lib/xmpp4r/idgenerator.rb +37 -0
  91. data/lib/xmpp4r/iq.rb +221 -0
  92. data/lib/xmpp4r/jid.rb +167 -0
  93. data/lib/xmpp4r/message.rb +148 -0
  94. data/lib/xmpp4r/muc.rb +14 -0
  95. data/lib/xmpp4r/muc/helper/mucbrowser.rb +106 -0
  96. data/lib/xmpp4r/muc/helper/mucclient.rb +464 -0
  97. data/lib/xmpp4r/muc/helper/simplemucclient.rb +250 -0
  98. data/lib/xmpp4r/muc/iq/mucadmin.rb +23 -0
  99. data/lib/xmpp4r/muc/iq/mucadminitem.rb +20 -0
  100. data/lib/xmpp4r/muc/iq/mucowner.rb +15 -0
  101. data/lib/xmpp4r/muc/item.rb +143 -0
  102. data/lib/xmpp4r/muc/x/muc.rb +70 -0
  103. data/lib/xmpp4r/muc/x/mucuserinvite.rb +60 -0
  104. data/lib/xmpp4r/muc/x/mucuseritem.rb +36 -0
  105. data/lib/xmpp4r/presence.rb +232 -0
  106. data/lib/xmpp4r/pubsub.rb +8 -0
  107. data/lib/xmpp4r/pubsub/children/configuration.rb +81 -0
  108. data/lib/xmpp4r/pubsub/children/event.rb +49 -0
  109. data/lib/xmpp4r/pubsub/children/item.rb +35 -0
  110. data/lib/xmpp4r/pubsub/children/items.rb +44 -0
  111. data/lib/xmpp4r/pubsub/children/node_config.rb +48 -0
  112. data/lib/xmpp4r/pubsub/children/publish.rb +24 -0
  113. data/lib/xmpp4r/pubsub/children/subscription.rb +62 -0
  114. data/lib/xmpp4r/pubsub/children/subscription_config.rb +67 -0
  115. data/lib/xmpp4r/pubsub/children/unsubscribe.rb +48 -0
  116. data/lib/xmpp4r/pubsub/helper/nodebrowser.rb +158 -0
  117. data/lib/xmpp4r/pubsub/helper/nodehelper.rb +156 -0
  118. data/lib/xmpp4r/pubsub/helper/servicehelper.rb +428 -0
  119. data/lib/xmpp4r/pubsub/iq/pubsub.rb +19 -0
  120. data/lib/xmpp4r/query.rb +15 -0
  121. data/lib/xmpp4r/rexmladdons.rb +136 -0
  122. data/lib/xmpp4r/roster.rb +7 -0
  123. data/lib/xmpp4r/roster/helper/roster.rb +519 -0
  124. data/lib/xmpp4r/roster/iq/roster.rb +215 -0
  125. data/lib/xmpp4r/roster/x/roster.rb +138 -0
  126. data/lib/xmpp4r/rpc.rb +2 -0
  127. data/lib/xmpp4r/rpc/helper/client.rb +126 -0
  128. data/lib/xmpp4r/rpc/helper/server.rb +74 -0
  129. data/lib/xmpp4r/rpc/helper/xmlrpcaddons.rb +67 -0
  130. data/lib/xmpp4r/rpc/iq/rpc.rb +23 -0
  131. data/lib/xmpp4r/sasl.rb +237 -0
  132. data/lib/xmpp4r/semaphore.rb +38 -0
  133. data/lib/xmpp4r/stream.rb +497 -0
  134. data/lib/xmpp4r/streamparser.rb +77 -0
  135. data/lib/xmpp4r/tune.rb +2 -0
  136. data/lib/xmpp4r/tune/helper/helper.rb +58 -0
  137. data/lib/xmpp4r/tune/tune.rb +113 -0
  138. data/lib/xmpp4r/vcard.rb +6 -0
  139. data/lib/xmpp4r/vcard/helper/vcard.rb +84 -0
  140. data/lib/xmpp4r/vcard/iq/vcard.rb +109 -0
  141. data/lib/xmpp4r/version.rb +7 -0
  142. data/lib/xmpp4r/version/helper/responder.rb +72 -0
  143. data/lib/xmpp4r/version/helper/simpleresponder.rb +44 -0
  144. data/lib/xmpp4r/version/iq/version.rb +105 -0
  145. data/lib/xmpp4r/x.rb +37 -0
  146. data/lib/xmpp4r/xhtml.rb +1 -0
  147. data/lib/xmpp4r/xhtml/html.rb +115 -0
  148. data/lib/xmpp4r/xmpp4r.rb +18 -0
  149. data/lib/xmpp4r/xmppelement.rb +168 -0
  150. data/lib/xmpp4r/xmppstanza.rb +162 -0
  151. data/test/bytestreams/tc_ibb.rb +186 -0
  152. data/test/bytestreams/tc_socks5bytestreams.rb +103 -0
  153. data/test/caps/tc_helper.rb +156 -0
  154. data/test/dataforms/tc_data.rb +81 -0
  155. data/test/delay/tc_xdelay.rb +51 -0
  156. data/test/discovery/tc_responder.rb +91 -0
  157. data/test/lib/clienttester.rb +120 -0
  158. data/test/muc/tc_muc_mucclient.rb +830 -0
  159. data/test/muc/tc_muc_simplemucclient.rb +114 -0
  160. data/test/muc/tc_mucowner.rb +50 -0
  161. data/test/pubsub/tc_helper.rb +638 -0
  162. data/test/pubsub/tc_nodeconfig.rb +54 -0
  163. data/test/pubsub/tc_subscriptionconfig.rb +41 -0
  164. data/test/roster/tc_helper.rb +514 -0
  165. data/test/roster/tc_iqqueryroster.rb +173 -0
  166. data/test/roster/tc_xroster.rb +73 -0
  167. data/test/rpc/tc_helper.rb +96 -0
  168. data/test/tc_callbacks.rb +129 -0
  169. data/test/tc_class_names.rb +146 -0
  170. data/test/tc_client.rb +30 -0
  171. data/test/tc_errors.rb +146 -0
  172. data/test/tc_idgenerator.rb +30 -0
  173. data/test/tc_iq.rb +111 -0
  174. data/test/tc_iqquery.rb +31 -0
  175. data/test/tc_jid.rb +204 -0
  176. data/test/tc_message.rb +116 -0
  177. data/test/tc_presence.rb +150 -0
  178. data/test/tc_rexml.rb +139 -0
  179. data/test/tc_stream.rb +229 -0
  180. data/test/tc_streamComponent.rb +95 -0
  181. data/test/tc_streamError.rb +131 -0
  182. data/test/tc_streamSend.rb +59 -0
  183. data/test/tc_streamparser.rb +96 -0
  184. data/test/tc_xmppstanza.rb +135 -0
  185. data/test/ts_xmpp4r.rb +53 -0
  186. data/test/tune/tc_helper_recv.rb +84 -0
  187. data/test/tune/tc_helper_send.rb +74 -0
  188. data/test/tune/tc_tune.rb +79 -0
  189. data/test/vcard/tc_helper.rb +49 -0
  190. data/test/vcard/tc_iqvcard.rb +62 -0
  191. data/test/version/tc_helper.rb +60 -0
  192. data/test/version/tc_iqqueryversion.rb +97 -0
  193. data/test/xhtml/tc_html.rb +41 -0
  194. metadata +327 -0
@@ -0,0 +1,156 @@
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
+ # For documentation of the return values please see [Jabber::PubSub::ServiceHelper]
6
+ # This class is only a wrapper around [Jabber::PubSub::ServiceHelper]
7
+ #
8
+
9
+
10
+ require 'xmpp4r/pubsub/helper/servicehelper'
11
+ require 'xmpp4r/pubsub/helper/nodebrowser'
12
+
13
+ module Jabber
14
+ module PubSub
15
+ class NodeHelper < ServiceHelper
16
+
17
+ attr_reader :nodename
18
+ attr_reader :name
19
+ attr_reader :jiod
20
+ attr_reader :my_subscriptions
21
+ ##
22
+ # creates a new node
23
+ # new(client,service,nodename)
24
+ # stream:: [Jabber::Stream]
25
+ # jid:: [String] (jid of the pubsub service)
26
+ # nodename:: [String]
27
+ def initialize(stream,jid,nodename = nil,create_if_not_exist = true)
28
+ super(stream,jid)
29
+ @nodename = nodename
30
+ @jid = jid
31
+ @stream = stream
32
+
33
+ #if create_if_not_exist and not node_exist?
34
+ # if no nodename is given a instant node will created
35
+ # (if the service supports instant nodes)
36
+ # @nodename = create_node
37
+ #else
38
+ get_subscriptions
39
+ #end
40
+ end
41
+
42
+ ##
43
+ # creates the node
44
+ # create(configuration=nil)
45
+ # configuration:: [Jabber::XData]
46
+ def create_node(configuration = nil)
47
+ if ! node_exist?
48
+ super.create_node(@nodename,configuration)
49
+ else
50
+ false
51
+ end
52
+ end
53
+
54
+ ##
55
+ # get the configuration of the node
56
+ # get_configuration(configuration=nil)
57
+ # configuration:: [Jabber::XData]
58
+ def get_configuration(subid = nil)
59
+ get_options(@nodename,subid)
60
+ end
61
+
62
+ ##
63
+ # set the configuration of the node
64
+ # set_configuration(configuration=nil)
65
+ # configuration:: [Jabber::XData]
66
+ # subid:: [String] default is nil
67
+ def set_configuration(configuration,subid = nil)
68
+ set_options(@nodename,configuration,subid)
69
+ end
70
+
71
+ ##
72
+ # deletes the node
73
+ # delete
74
+ def delete_node
75
+ delete(@nodename)
76
+ end
77
+
78
+ ##
79
+ # publishing content on this node
80
+ # publish_content(items)
81
+ # items:: [REXML::Element]
82
+ def publish_content(items)
83
+ publish(@nodename,items)
84
+ end
85
+
86
+ ##
87
+ # gets all items from the node
88
+ # get_all_items
89
+ def get_all_items
90
+ items(@nodename)
91
+ end
92
+
93
+ ##
94
+ # get a count of items
95
+ # get_items(count)
96
+ # count:: [Fixnum]
97
+ def get_items(count)
98
+ items(@nodename,count)
99
+ end
100
+
101
+ ##
102
+ # get all node affiliations
103
+ # get_affiliations
104
+ def get_affiliations
105
+ affiliations
106
+ end
107
+
108
+ ##
109
+ # get all subscriptions on this node
110
+ # get_subscriptions
111
+ def get_subscriptions
112
+ get_subscriptions_from(@nodename)
113
+ end
114
+
115
+ ##
116
+ # get all subscribers subscribed on this node
117
+ # get_subscribers
118
+ def get_subscribers
119
+ @subscriptions = subscribers(@nodename)
120
+ end
121
+
122
+ ##
123
+ # subscribe to this node
124
+ # do_subscribe
125
+ def do_subscribe
126
+ subscribe(@nodename)
127
+ get_subscriptions
128
+ end
129
+
130
+ ##
131
+ # unsubscribe from this node
132
+ # do_unsubscribe(subid = nil)
133
+ # subid:: [String]
134
+ def do_unsubscribe(subid)
135
+ unsubscribe(@nodename,subid)
136
+ end
137
+
138
+ ##
139
+ # purge all items from this node
140
+ # purge_items
141
+ def purge_items
142
+ purge(@nodename)
143
+ end
144
+
145
+ private
146
+
147
+ def node_exist?
148
+ nodebrowser = PubSub::NodeBrowser.new(@stream)
149
+ nodebrowser.nodes(@jid).include?(@nodename)
150
+ end
151
+ def disco_info
152
+ end
153
+
154
+ end #class
155
+ end #module
156
+ end #module
@@ -0,0 +1,428 @@
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
+ # It's recommented to read the XEP-0066 before you use this Helper. (Maybe its
6
+ # better not use the helper for now ) ;)
7
+ # The whole code is getting better, but may still contain bugs - be careful!
8
+ #
9
+ # Maybe the following structure is good
10
+ # ( taken form the xep-0060 )
11
+ #
12
+ # entity usecases
13
+ # retrieve all subscriptions
14
+ # retrieve all affiliations
15
+ # NOTE: the disco stuff will done by the nodebrowserhelper
16
+ # subscriber usecases
17
+ # subscribe
18
+ # unsubscribe
19
+ # configure subscription options
20
+ # retrieve items from a node
21
+ # publisher usecases
22
+ # publish a item to a node
23
+ # delete a item from a node
24
+ # owner usecases
25
+ # create a node
26
+ # configure a node
27
+ # request default configuration options
28
+ # delete a node
29
+ # purge all node items
30
+ # manage subscription requests
31
+ # process pending subscriptions
32
+ # manage subscriptions
33
+ # manage affiliations
34
+ #
35
+ # collection nodes
36
+ #
37
+ # If someone want to implement something i think its better to do this in
38
+ # this order because everyone who reads the xep-0060 do know where to search in the file
39
+ #
40
+ require 'xmpp4r/pubsub/iq/pubsub'
41
+ require 'xmpp4r/pubsub/children/event'
42
+ require 'xmpp4r/pubsub/children/item'
43
+ require 'xmpp4r/pubsub/children/items'
44
+ require 'xmpp4r/pubsub/children/subscription'
45
+ require 'xmpp4r/pubsub/children/unsubscribe'
46
+ require 'xmpp4r/pubsub/children/node_config'
47
+ require 'xmpp4r/pubsub/children/subscription_config'
48
+ require 'xmpp4r/dataforms'
49
+
50
+ module Jabber
51
+ module PubSub
52
+ ##
53
+ # A Helper representing a PubSub Service
54
+ class ServiceHelper
55
+
56
+ ##
57
+ # Creates a new representation of a pubsub service
58
+ # stream:: [Jabber::Stream]
59
+ # pubsubjid:: [String] or [Jabber::JID]
60
+ def initialize(stream, pubsubjid)
61
+ @stream = stream
62
+ @pubsubjid = pubsubjid
63
+ @event_cbs = CallbackList.new
64
+ @stream.add_message_callback(200,self) { |message|
65
+ handle_message(message)
66
+ }
67
+ end
68
+
69
+ ##
70
+ # get all subscriptions on a pubsub component
71
+ # return:: [Hash] of [PubSub::Subscription]
72
+ def get_subscriptions_from_all_nodes
73
+ iq = basic_pubsub_query(:get)
74
+ entities = iq.pubsub.add(REXML::Element.new('subscriptions'))
75
+ res = nil
76
+ @stream.send_with_id(iq) { |reply|
77
+ if reply.pubsub.first_element('subscriptions')
78
+ res = []
79
+ reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription|
80
+ res << Jabber::PubSub::Subscription.import(subscription)
81
+ }
82
+ end
83
+ true
84
+ }
85
+
86
+ res
87
+ end
88
+ ##
89
+ # subscribe to a node
90
+ # node:: [String]
91
+ # return:: [Hash] of { attributename => value }
92
+ def subscribe_to(node)
93
+ iq = basic_pubsub_query(:set)
94
+ sub = REXML::Element.new('subscribe')
95
+ sub.attributes['node'] = node
96
+ sub.attributes['jid'] = @stream.jid.strip.to_s
97
+ iq.pubsub.add(sub)
98
+ res = nil
99
+ @stream.send_with_id(iq) do |reply|
100
+ pubsubanswer = reply.pubsub
101
+ if pubsubanswer.first_element('subscription')
102
+ res = PubSub::Subscription.import(pubsubanswer.first_element('subscription'))
103
+ end
104
+ true
105
+ end # @stream.send_with_id(iq)
106
+ res
107
+ end
108
+
109
+ ##
110
+ # Unsubscribe from a node with an optional subscription id
111
+ #
112
+ # May raise ServerError
113
+ # node:: [String]
114
+ # subid:: [String] or nil (not supported)
115
+ # return:: true
116
+ def unsubscribe_from(node,subid=nil)
117
+ iq = basic_pubsub_query(:set)
118
+ unsub = PubSub::Unsubscribe.new
119
+ unsub.node = node
120
+ unsub.jid = @stream.jid.strip
121
+ iq.pubsub.add(unsub)
122
+ ret = false
123
+ @stream.send_with_id(iq) { |reply|
124
+ ret = reply.kind_of?(Jabber::Iq) and reply.type == :result
125
+ true
126
+ } # @stream.send_with_id(iq)
127
+ ret
128
+ end
129
+
130
+ ##
131
+ # gets all items from a pubsub node
132
+ # node:: [String]
133
+ # count:: [Fixnum]
134
+ # return:: [Hash] { id => [Jabber::PubSub::Item] }
135
+ def get_items_from(node,count=nil)
136
+ iq = basic_pubsub_query(:get)
137
+ items = Jabber::PubSub::Items.new
138
+ items.node = node
139
+ iq.pubsub.add(items)
140
+ res = nil
141
+ @stream.send_with_id(iq) { |reply|
142
+ if reply.kind_of?(Iq) and reply.pubsub and reply.pubsub.first_element('items')
143
+ res = {}
144
+ reply.pubsub.first_element('items').each_element('item') do |item|
145
+ res[item.attributes['id']] = item.children.first if item.children.first
146
+ end
147
+ end
148
+ true
149
+ }
150
+ res
151
+ end
152
+
153
+ ##
154
+ # NOTE: this method sends only one item per publish request because some services
155
+ # may not allow batch processing. Maybe this will changed in the future?
156
+ # node:: [String]
157
+ # item:: [Jabber::PubSub::Item]
158
+ # return:: true
159
+ # it automatically generates an id for the item
160
+ def publish_item_to(node,item)
161
+ iq = basic_pubsub_query(:set)
162
+ publish = iq.pubsub.add(REXML::Element.new('publish'))
163
+ publish.attributes['node'] = node
164
+
165
+ if item.kind_of?(Jabber::PubSub::Item)
166
+ item.id = Jabber::IdGenerator.generate_id
167
+ publish.add(item)
168
+ @stream.send_with_id(iq) { |reply| true }
169
+ end
170
+ end
171
+
172
+ ##
173
+ # node:: [String]
174
+ # item:: [REXML::Element]
175
+ # id:: [String]
176
+ # return:: true
177
+ def publish_item_with_id_to(node,item,id)
178
+ iq = basic_pubsub_query(:set)
179
+ publish = iq.pubsub.add(REXML::Element.new('publish'))
180
+ publish.attributes['node'] = node
181
+
182
+ if item.kind_of?(REXML::Element)
183
+ xmlitem = Jabber::PubSub::Item.new
184
+ xmlitem.id = id
185
+ xmlitem.import(item)
186
+ publish.add(xmlitem)
187
+ else
188
+ raise "given item is not a proper xml document or Jabber::PubSub::Item"
189
+ end
190
+ @stream.send_with_id(iq) { |reply| true }
191
+ end
192
+
193
+ ##
194
+ # purges all items on a persistent node
195
+ # node:: [String]
196
+ # return:: true
197
+ def purge_items_from(node)
198
+ iq = basic_pubsub_query(:set)
199
+ purge = REXML::Element.new('purge')
200
+ purge.attributes['node'] = node
201
+ iq.pubsub.add(purge)
202
+ @stream.send_with_id(iq) { |reply| true }
203
+ end
204
+
205
+ ##
206
+ # Create a new node on the pubsub service
207
+ # node:: [String] you node name - otherwise you get a automaticly generated one (in most cases)
208
+ # configure:: [Jabber::PubSub::NodeConfig] if you want to configure your node (default nil)
209
+ # return:: [String]
210
+ def create_node(node = nil, configure = Jabber::PubSub::NodeConfig.new)
211
+ rnode = nil
212
+ iq = basic_pubsub_query(:set)
213
+ iq.pubsub.add(REXML::Element.new('create')).attributes['node'] = node
214
+ if configure
215
+ if configure.kind_of?(Jabber::PubSub::NodeConfig)
216
+ iq.pubsub.add(configure)
217
+ end
218
+ end
219
+
220
+ @stream.send_with_id(iq) do |reply|
221
+ if reply.kind_of?(Jabber::Iq) and reply.type == :result
222
+ rnode = node
223
+ end
224
+ true
225
+ end
226
+
227
+ rnode
228
+ end
229
+
230
+ ##
231
+ # get configuration from a node
232
+ # node:: [String]
233
+ # return:: [Jabber::PubSub::Configure]
234
+ def get_config_from(node)
235
+ iq = basic_pubsub_query(:get, true)
236
+ iq.pubsub.add(Jabber::PubSub::OwnerNodeConfig.new(node))
237
+ ret = nil
238
+ @stream.send_with_id(iq) do |reply|
239
+ ret = reply.pubsub.first_element('configure')
240
+ end
241
+ ret
242
+ end
243
+
244
+ ##
245
+ # set configuration for a node
246
+ # node:: [String]
247
+ # options:: [Jabber::PubSub::NodeConfig]
248
+ # return:: true on success
249
+ def set_config_for(node, config)
250
+ iq = basic_pubsub_query( :set )
251
+ iq.pubsub.add(Jabber::PubSub::NodeConfig(options).form)
252
+ @stream.send_with_id(iq) { |reply| true }
253
+ end
254
+
255
+ ##
256
+ # Delete a pubsub node
257
+ # node:: [String]
258
+ # return:: true
259
+ def delete_node(node)
260
+ iq = basic_pubsub_query(:set,true)
261
+ iq.pubsub.add(REXML::Element.new('delete')).attributes['node'] = node
262
+ @stream.send_with_id(iq) { |reply|
263
+ true
264
+ }
265
+ end
266
+
267
+ ##
268
+ # sets the affiliation for a jid
269
+ # node:: [String]
270
+ # jid:: [String] or [Jabber::ID]
271
+ # type:: [String] (should be 'publisher', 'member', 'none', 'outcast')
272
+ def set_affiliation(node, jid, type)
273
+ iq = basic_pubsub_query(:set, true)
274
+ jid = jid.kind_of?(String) ? Jabber::JID.new(jid).strip : jid.strip
275
+ affiliations = iq.pubsub.add(REXML::Element.new('affiliations'))
276
+ affiliations.attributes['node'] = node
277
+ affiliation = affiliations.add(REXML::Element.new('affiliation'))
278
+ affiliation.attributes['jid'] = jid.to_s
279
+ affiliation.attributes['affiliation'] = type
280
+ @stream.send_with_id(iq) { |reploy|
281
+ true
282
+ }
283
+ end
284
+
285
+ ##
286
+ # shows the affiliations on a pubsub service
287
+ # node:: [String]
288
+ # return:: [Hash] of { node => symbol }
289
+ def get_affiliations(node = nil)
290
+ iq = basic_pubsub_query(:get)
291
+ affiliations = iq.pubsub.add(REXML::Element.new('affiliations'))
292
+ affiliations.attributes['node'] = node
293
+ res = nil
294
+ @stream.send_with_id(iq) { |reply|
295
+ if reply.pubsub.first_element('affiliations')
296
+ res = {}
297
+ reply.pubsub.first_element('affiliations').each_element('affiliation') do |affiliation|
298
+ # TODO: This should be handled by an affiliation element class
299
+ aff = case affiliation.attributes['affiliation']
300
+ when 'owner' then :owner
301
+ when 'publisher' then :publisher
302
+ when 'none' then :none
303
+ when 'outcast' then :outcast
304
+ else nil
305
+ end
306
+ res[affiliation.attributes['node']] = aff
307
+ end
308
+ end
309
+ true
310
+ }
311
+ res
312
+ end
313
+
314
+ ##
315
+ # shows all subscriptions on the given node
316
+ # node:: [String]
317
+ # return:: [Array] of [Jabber::Pubsub::Subscription]
318
+ def get_subscriptions_from(node)
319
+ iq = basic_pubsub_query(:get)
320
+ entities = iq.pubsub.add(REXML::Element.new('subscriptions'))
321
+ entities.attributes['node'] = node
322
+ res = nil
323
+ @stream.send_with_id(iq) { |reply|
324
+ if reply.pubsub.first_element('subscriptions')
325
+ res = []
326
+ if reply.pubsub.first_element('subscriptions').attributes['node'] == node
327
+ reply.pubsub.first_element('subscriptions').each_element('subscription') { |subscription|
328
+ res << PubSub::Subscription.import(subscription)
329
+ }
330
+ end
331
+ end
332
+ true
333
+ }
334
+ res
335
+ end
336
+
337
+ ##
338
+ # shows all jids of subscribers of a node
339
+ # node:: [String]
340
+ # return:: [Array] of [String]
341
+ def get_subscribers_from(node)
342
+ res = []
343
+ get_subscriptions_from(node).each { |sub|
344
+ res << sub.jid
345
+ }
346
+ res
347
+ end
348
+
349
+
350
+ ##
351
+ # get options from a subscription
352
+ # node:: [String]
353
+ # jid:: [Jabber::JID] or [String]
354
+ # subid:: [String] or nil
355
+ # return:: [Jabber::PubSub::OwnerConfigure]
356
+ def get_options_from(node, jid, subid = nil)
357
+ iq = basic_pubsub_query(:get)
358
+ iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(node, jid.kind_of?(String) ? Jabber::JID.new(jid).strip: jid.strip, subid))
359
+ ret = nil
360
+ @stream.send_with_id(iq) do |reply|
361
+ ret = reply.pubsub.first_element('options')
362
+ end
363
+ ret
364
+ end
365
+
366
+ ##
367
+ # set options for a subscription
368
+ # node:: [String]
369
+ # jid:: [Jabber::JID] or [String]
370
+ # options:: [Jabber::PubSub::SubscriptionConfig} specifying configuration options
371
+ # subid:: [String] or nil
372
+ # return:: true
373
+ def set_options_for(node, jid, options, subid = nil)
374
+ iq = basic_pubsub_query(:set)
375
+ iq.pubsub.add(Jabber::PubSub::SubscriptionConfig.new(node, jid.kind_of?(String) ? Jabber::JID.new(jid).strip: jid.strip, options, subid))
376
+ ret = false
377
+ @stream.send_with_id(iq) do |reply|
378
+ ret = ( reply.type == :result )
379
+ true
380
+ end
381
+
382
+ ret
383
+ end
384
+
385
+ ##
386
+ # String representation
387
+ # result:: [String] The PubSub service's JID
388
+ def to_s
389
+ @pubsubjid.to_s
390
+ end
391
+
392
+ ##
393
+ # Register callbacks for incoming events
394
+ # (i.e. Message stanzas containing) PubSub notifications
395
+ def add_event_callback(prio = 200, ref = nil, &block)
396
+ @event_cbs.add(prio, ref, block)
397
+ end
398
+
399
+ private
400
+
401
+ ##
402
+ # creates a basic pubsub iq
403
+ # basic_pubsub_query(type)
404
+ # type:: [Symbol]
405
+ def basic_pubsub_query(type,ownerusecase = false)
406
+ iq = Jabber::Iq.new(type,@pubsubjid)
407
+ if ownerusecase
408
+ iq.add(IqPubSubOwner.new)
409
+ else
410
+ iq.add(IqPubSub.new)
411
+ end
412
+ iq.from = @stream.jid #.strip
413
+ iq
414
+ end
415
+
416
+ ##
417
+ # handling incoming events
418
+ # handle_message(message)
419
+ # message:: [Jabber::Message]
420
+ def handle_message(message)
421
+ if message.from == @pubsubjid and message.first_element('event').kind_of?(Jabber::PubSub::Event)
422
+ event = message.first_element('event')
423
+ @event_cbs.process(event)
424
+ end
425
+ end
426
+ end
427
+ end
428
+ end