xmpp4r 0.3.2 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/{ChangeLog → CHANGELOG} +33 -0
  2. data/LICENSE +9 -9
  3. data/README.rdoc +110 -0
  4. data/README_ruby19.txt +43 -0
  5. data/Rakefile +235 -74
  6. data/data/doc/xmpp4r/examples/advanced/adventure/README +0 -1
  7. data/data/doc/xmpp4r/examples/advanced/adventure/adventure.rb +1 -1
  8. data/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb +12 -12
  9. data/data/doc/xmpp4r/examples/advanced/adventure/world.rb +10 -11
  10. data/data/doc/xmpp4r/examples/advanced/fileserve.rb +11 -11
  11. data/data/doc/xmpp4r/examples/advanced/getonline.rb +7 -7
  12. data/data/doc/xmpp4r/examples/advanced/gtkmucclient.rb +3 -3
  13. data/data/doc/xmpp4r/examples/advanced/migrate.rb +21 -22
  14. data/data/doc/xmpp4r/examples/advanced/minimuc.rb +22 -22
  15. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/index.xsl +235 -0
  16. data/data/doc/xmpp4r/examples/advanced/pep-aggregator/pep-aggregator.rb +147 -0
  17. data/data/doc/xmpp4r/examples/advanced/recvfile.rb +0 -1
  18. data/data/doc/xmpp4r/examples/advanced/rosterdiscovery.rb +2 -3
  19. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr.rb +3 -3
  20. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_jabber.rb +10 -10
  21. data/data/doc/xmpp4r/examples/advanced/shellmgr/shellmgr_test.rb +2 -2
  22. data/data/doc/xmpp4r/examples/advanced/versionpoll.rb +12 -12
  23. data/data/doc/xmpp4r/examples/advanced/xmpping.rb +2 -3
  24. data/data/doc/xmpp4r/examples/basic/change_password.rb +3 -3
  25. data/data/doc/xmpp4r/examples/basic/client.rb +5 -3
  26. data/data/doc/xmpp4r/examples/basic/component.rb +1 -1
  27. data/data/doc/xmpp4r/examples/basic/{echo_threaded.rb → echo.rb} +8 -7
  28. data/data/doc/xmpp4r/examples/basic/jabbersend.rb +5 -5
  29. data/data/doc/xmpp4r/examples/basic/mass_sender.rb +11 -11
  30. data/data/doc/xmpp4r/examples/basic/muc_owner_config.rb +0 -1
  31. data/data/doc/xmpp4r/examples/basic/mucinfo.rb +3 -1
  32. data/data/doc/xmpp4r/examples/basic/mucsimplebot.rb +0 -1
  33. data/data/doc/xmpp4r/examples/basic/register.rb +21 -4
  34. data/data/doc/xmpp4r/examples/basic/roster.rb +7 -5
  35. data/data/doc/xmpp4r/examples/basic/rosterprint.rb +0 -0
  36. data/data/doc/xmpp4r/examples/basic/rosterrename.rb +2 -2
  37. data/data/doc/xmpp4r/examples/basic/rosterwatch.rb +6 -7
  38. data/data/doc/xmpp4r/examples/basic/send_vcard.rb +3 -4
  39. data/data/doc/xmpp4r/examples/basic/tune_client.rb +56 -0
  40. data/data/doc/xmpp4r/examples/basic/tune_server.rb +58 -0
  41. data/data/doc/xmpp4r/examples/basic/versionbot.rb +1 -1
  42. data/lib/xmpp4r.rb +4 -4
  43. data/lib/xmpp4r/base64.rb +32 -0
  44. data/lib/xmpp4r/bytestreams/helper/filetransfer.rb +10 -11
  45. data/lib/xmpp4r/bytestreams/helper/ibb/base.rb +6 -8
  46. data/lib/xmpp4r/bytestreams/helper/ibb/initiator.rb +3 -6
  47. data/lib/xmpp4r/bytestreams/helper/ibb/target.rb +0 -1
  48. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/base.rb +8 -11
  49. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb +4 -7
  50. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/server.rb +7 -3
  51. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb +10 -5
  52. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb +14 -4
  53. data/lib/xmpp4r/bytestreams/helper/socks5bytestreams/target.rb.orig +62 -0
  54. data/lib/xmpp4r/bytestreams/iq/bytestreams.rb +0 -1
  55. data/lib/xmpp4r/callbacks.rb +5 -5
  56. data/lib/xmpp4r/caps.rb +1 -0
  57. data/lib/xmpp4r/caps/c.rb +53 -0
  58. data/lib/xmpp4r/caps/helper/generator.rb +160 -0
  59. data/lib/xmpp4r/caps/helper/helper.rb +87 -0
  60. data/lib/xmpp4r/client.rb +111 -40
  61. data/lib/xmpp4r/command/iq/command.rb +1 -1
  62. data/lib/xmpp4r/component.rb +8 -8
  63. data/lib/xmpp4r/connection.rb +25 -9
  64. data/lib/xmpp4r/dataforms/x/data.rb +17 -5
  65. data/lib/xmpp4r/debuglog.rb +13 -5
  66. data/lib/xmpp4r/delay/x/delay.rb +1 -1
  67. data/lib/xmpp4r/discovery.rb +3 -2
  68. data/lib/xmpp4r/discovery/helper/responder.rb +165 -0
  69. data/lib/xmpp4r/discovery/iq/discoinfo.rb +11 -12
  70. data/lib/xmpp4r/discovery/iq/discoitems.rb +12 -4
  71. data/lib/xmpp4r/{error.rb → errors.rb} +66 -10
  72. data/lib/xmpp4r/framework/base.rb +55 -0
  73. data/lib/xmpp4r/framework/bot.rb +148 -0
  74. data/lib/xmpp4r/httpbinding/client.rb +1 -1
  75. data/lib/xmpp4r/idgenerator.rb +1 -1
  76. data/lib/xmpp4r/iq.rb +34 -23
  77. data/lib/xmpp4r/jid.rb +2 -2
  78. data/lib/xmpp4r/message.rb +1 -1
  79. data/lib/xmpp4r/muc.rb +2 -0
  80. data/lib/xmpp4r/muc/helper/mucbrowser.rb +4 -19
  81. data/lib/xmpp4r/muc/helper/mucclient.rb +54 -23
  82. data/lib/xmpp4r/muc/helper/simplemucclient.rb +107 -1
  83. data/lib/xmpp4r/muc/iq/mucadmin.rb +23 -0
  84. data/lib/xmpp4r/muc/iq/mucadminitem.rb +20 -0
  85. data/lib/xmpp4r/muc/iq/mucowner.rb +5 -1
  86. data/lib/xmpp4r/muc/item.rb +143 -0
  87. data/lib/xmpp4r/muc/x/mucuserinvite.rb +2 -2
  88. data/lib/xmpp4r/muc/x/mucuseritem.rb +3 -117
  89. data/lib/xmpp4r/presence.rb +2 -1
  90. data/lib/xmpp4r/pubsub.rb +7 -0
  91. data/lib/xmpp4r/pubsub/children/configuration.rb +86 -0
  92. data/lib/xmpp4r/pubsub/{stanzas → children}/event.rb +13 -13
  93. data/lib/xmpp4r/pubsub/{stanzas → children}/item.rb +9 -1
  94. data/lib/xmpp4r/pubsub/{stanzas → children}/items.rb +10 -1
  95. data/lib/xmpp4r/pubsub/children/node_config.rb +48 -0
  96. data/lib/xmpp4r/pubsub/children/publish.rb +24 -0
  97. data/lib/xmpp4r/pubsub/{stanzas → children}/subscription.rb +16 -12
  98. data/lib/xmpp4r/pubsub/children/subscription_config.rb +67 -0
  99. data/lib/xmpp4r/pubsub/children/unsubscribe.rb +48 -0
  100. data/lib/xmpp4r/pubsub/helper/nodebrowser.rb +49 -93
  101. data/lib/xmpp4r/pubsub/helper/nodehelper.rb +39 -36
  102. data/lib/xmpp4r/pubsub/helper/servicehelper.rb +253 -162
  103. data/lib/xmpp4r/pubsub/iq/pubsub.rb +1 -1
  104. data/lib/xmpp4r/rexmladdons.rb +56 -28
  105. data/lib/xmpp4r/roster.rb +0 -1
  106. data/lib/xmpp4r/roster/helper/roster.rb +26 -15
  107. data/lib/xmpp4r/roster/iq/roster.rb +19 -19
  108. data/lib/xmpp4r/roster/x/roster.rb +10 -10
  109. data/lib/xmpp4r/rpc/helper/client.rb +17 -8
  110. data/lib/xmpp4r/rpc/helper/server.rb +1 -2
  111. data/lib/xmpp4r/rpc/helper/xmlrpcaddons.rb +14 -4
  112. data/lib/xmpp4r/rpc/iq/rpc.rb +0 -1
  113. data/lib/xmpp4r/sasl.rb +27 -6
  114. data/lib/xmpp4r/stream.rb +39 -44
  115. data/lib/xmpp4r/streamparser.rb +4 -4
  116. data/lib/xmpp4r/tune.rb +2 -0
  117. data/lib/xmpp4r/tune/helper/helper.rb +58 -0
  118. data/lib/xmpp4r/tune/tune.rb +113 -0
  119. data/lib/xmpp4r/vcard.rb +0 -1
  120. data/lib/xmpp4r/vcard/helper/vcard.rb +1 -3
  121. data/lib/xmpp4r/vcard/iq/vcard.rb +14 -0
  122. data/lib/xmpp4r/version/iq/version.rb +0 -1
  123. data/lib/xmpp4r/x.rb +1 -1
  124. data/lib/xmpp4r/xhtml.rb +1 -0
  125. data/lib/xmpp4r/xhtml/html.rb +115 -0
  126. data/lib/xmpp4r/xmpp4r.rb +7 -5
  127. data/lib/xmpp4r/xmppelement.rb +21 -5
  128. data/lib/xmpp4r/xmppstanza.rb +6 -6
  129. data/setup.rb +4 -4
  130. data/test/bytestreams/tc_ibb.rb +2 -2
  131. data/test/bytestreams/tc_socks5bytestreams.rb +12 -2
  132. data/test/caps/tc_helper.rb +156 -0
  133. data/test/dataforms/tc_data.rb +0 -0
  134. data/test/delay/tc_xdelay.rb +2 -2
  135. data/test/discovery/tc_responder.rb +91 -0
  136. data/test/lib/assert_equal_xml.rb +14 -0
  137. data/test/lib/clienttester.rb +31 -24
  138. data/test/muc/tc_muc_mucclient.rb +249 -13
  139. data/test/muc/tc_muc_simplemucclient.rb +41 -2
  140. data/test/muc/tc_mucowner.rb +0 -0
  141. data/test/pubsub/tc_helper.rb +486 -51
  142. data/test/pubsub/tc_nodeconfig.rb +54 -0
  143. data/test/pubsub/tc_subscriptionconfig.rb +41 -0
  144. data/test/roster/tc_helper.rb +6 -7
  145. data/test/roster/tc_iqqueryroster.rb +16 -16
  146. data/test/roster/tc_xroster.rb +1 -1
  147. data/test/rpc/tc_helper.rb +15 -3
  148. data/test/tc_callbacks.rb +7 -7
  149. data/test/tc_class_names.rb +15 -6
  150. data/test/tc_client.rb +3 -3
  151. data/test/{tc_error.rb → tc_errors.rb} +54 -12
  152. data/test/tc_idgenerator.rb +0 -0
  153. data/test/tc_iq.rb +32 -29
  154. data/test/tc_iqquery.rb +2 -2
  155. data/test/tc_jid.rb +44 -42
  156. data/test/tc_message.rb +13 -12
  157. data/test/tc_presence.rb +25 -24
  158. data/test/tc_rexml.rb +81 -2
  159. data/test/tc_stream.rb +15 -13
  160. data/test/tc_streamComponent.rb +6 -5
  161. data/test/tc_streamError.rb +21 -15
  162. data/test/tc_streamSend.rb +9 -9
  163. data/test/tc_streamparser.rb +112 -0
  164. data/test/tc_xmppstanza.rb +19 -9
  165. data/test/ts_xmpp4r.rb +10 -1
  166. data/test/tune/tc_helper_recv.rb +84 -0
  167. data/test/tune/tc_helper_send.rb +74 -0
  168. data/test/tune/tc_tune.rb +79 -0
  169. data/test/vcard/tc_helper.rb +2 -2
  170. data/test/vcard/tc_iqvcard.rb +13 -3
  171. data/test/version/tc_helper.rb +2 -2
  172. data/test/version/tc_iqqueryversion.rb +10 -10
  173. data/test/xhtml/tc_html.rb +41 -0
  174. data/tools/gen_requires.bash +29 -8
  175. data/tools/xmpp4r-gemspec-test.rb +11 -0
  176. data/xmpp4r.gemspec +291 -0
  177. metadata +277 -218
  178. data/README +0 -28
  179. data/UPDATING +0 -40
  180. data/data/doc/xmpp4r/examples/buggy/jabber2jabber/jabber2jabber.rb +0 -18
  181. data/data/doc/xmpp4r/examples/buggy/miniedgarr_cgi.rb +0 -192
  182. data/data/doc/xmpp4r/examples/buggy/miniedgarr_watch.rb +0 -82
  183. data/lib/xmpp4r/authenticationfailure.rb +0 -13
  184. data/lib/xmpp4r/errorexception.rb +0 -32
  185. data/tools/doctoweb.bash +0 -30
@@ -75,7 +75,7 @@ module Jabber
75
75
  # result:: [JID]
76
76
  def from
77
77
  if attributes['from']
78
- JID::new(attributes['from'])
78
+ JID.new(attributes['from'])
79
79
  else
80
80
  nil
81
81
  end
@@ -2,5 +2,6 @@
2
2
  # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
3
3
  # Website::http://home.gna.org/xmpp4r/
4
4
 
5
- require 'xmpp4r/discovery/iq/discoinfo.rb'
6
- require 'xmpp4r/discovery/iq/discoitems.rb'
5
+ require 'xmpp4r/discovery/iq/discoinfo'
6
+ require 'xmpp4r/discovery/iq/discoitems'
7
+ require 'xmpp4r/discovery/helper/responder'
@@ -0,0 +1,165 @@
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/iq'
6
+ require 'xmpp4r/discovery/iq/discoinfo'
7
+ require 'xmpp4r/discovery/iq/discoitems'
8
+ require 'xmpp4r/caps/helper/generator'
9
+
10
+ module Jabber
11
+ module Discovery
12
+ ##
13
+ # Responds to Service Discovery queries on a given node
14
+ #
15
+ # Modify returned elements by these attributes:
16
+ # * Responder#identities
17
+ # * Responder#features (Responder#add_features is a short-cut accepting an Array of Strings, too)
18
+ # * Responder#forms
19
+ # * Responder#items
20
+ class Responder
21
+ ##
22
+ # Service Discovery node this Responder is responsible for
23
+ # (will not answer queries for other nodes)
24
+ attr_reader :node
25
+
26
+ ##
27
+ # Identities returned on Discovery Info query
28
+ #
29
+ # Array of [Discovery::Identity]
30
+ attr_accessor :identities
31
+ ##
32
+ # Features returned on Discovery Info query,
33
+ #
34
+ # Array of [Discovery::Feature]
35
+ attr_accessor :features
36
+ ##
37
+ # Forms returned on Discovery Info query
38
+ # (such as Software Information)
39
+ #
40
+ # Array of [Dataforms::XData]
41
+ attr_accessor :forms
42
+
43
+ ##
44
+ # Children returned on Discovery Item query
45
+ #
46
+ # May contain other Discovery::Responder instances
47
+ # which will generate an item dynamically from their
48
+ # first identity
49
+ #
50
+ # Array of [Discovery::Item] or [Discovery::Responder] (mixed)
51
+ attr_accessor :items
52
+
53
+ ##
54
+ # Set the JID this helper feels responsible for
55
+ # (default: nil, responsible for any JID)
56
+ attr_accessor :my_jid
57
+
58
+ CALLBACK_PRIORITY = 180
59
+
60
+ ##
61
+ # Initialize responder for a specific node
62
+ # stream:: [Jabber::Stream]
63
+ # node:: [nil] or [String]
64
+ def initialize(stream, node=nil, identities=[], features=[], items=[])
65
+ @stream = stream
66
+ @my_jid = nil
67
+ @node = node
68
+ @identities = identities
69
+ @features = []
70
+ add_features(features)
71
+ @forms = []
72
+ @items = items
73
+
74
+ @stream.add_iq_callback(CALLBACK_PRIORITY, self) do |iq|
75
+ my_nodes = [@node, "#{@node}##{generate_ver}"]
76
+
77
+ if iq.type == :get and
78
+ iq.query.kind_of? IqQueryDiscoInfo and
79
+ my_nodes.include?(iq.query.node)
80
+
81
+ answer = iq.answer(false)
82
+ answer.type = :result
83
+ query = answer.add(IqQueryDiscoInfo.new)
84
+ query.node = iq.query.node
85
+ (@identities + @features + @forms).each do |element|
86
+ query.add(element)
87
+ end
88
+ @stream.send(answer)
89
+
90
+ true # handled
91
+
92
+ elsif iq.type == :get and
93
+ iq.query.kind_of? IqQueryDiscoItems and
94
+ my_nodes.include?(iq.query.node)
95
+
96
+ answer = iq.answer(false)
97
+ answer.type = :result
98
+ query = answer.add(IqQueryDiscoItems.new)
99
+ query.node = iq.query.node
100
+ @items.each do |item|
101
+ if item.kind_of? Responder
102
+ query.add(item.generate_item)
103
+ else
104
+ query.add(item)
105
+ end
106
+ end
107
+ @stream.send(answer)
108
+
109
+ true # handled
110
+
111
+ else
112
+ false # not handled
113
+ end
114
+ end
115
+ end
116
+
117
+ ##
118
+ # Add a feature
119
+ # feature:: [Jabber::Discovery::Feature] or [String]
120
+ def add_feature(feature)
121
+ if feature.kind_of? Feature
122
+ @features << feature
123
+ else
124
+ @features << Feature.new(feature.to_s)
125
+ end
126
+ end
127
+
128
+ ##
129
+ # Add a series of features
130
+ # features:: Array of [Jabber::Discovery::Feature] or [String]
131
+ def add_features(features)
132
+ features.each { |feature|
133
+ add_feature(feature)
134
+ }
135
+ end
136
+
137
+ ##
138
+ # Generate a XEP-0115: Entity Capabilities <c/> element
139
+ # for inclusion in Presence stanzas. This enables efficient
140
+ # caching of Service Discovery information.
141
+ def generate_caps
142
+ Caps::C.new(@node, generate_ver)
143
+ end
144
+
145
+ ##
146
+ # Generate an item for inclusion in items discovery in other
147
+ # responders
148
+ # return:: [Discovery::Item] or nil
149
+ def generate_item
150
+ i = @identities.first
151
+ if i
152
+ Item.new(@my_jid || @stream.jid, i.iname, @node)
153
+ else
154
+ nil
155
+ end
156
+ end
157
+
158
+ private
159
+
160
+ def generate_ver
161
+ Caps::generate_ver(@identities, @features, @forms)
162
+ end
163
+ end
164
+ end
165
+ end
@@ -6,6 +6,8 @@ require 'xmpp4r/query'
6
6
 
7
7
  module Jabber
8
8
  module Discovery
9
+ NS_DISCO_INFO = 'http://jabber.org/protocol/disco#info'
10
+
9
11
  ##
10
12
  # Class for handling Service Discovery queries,
11
13
  # info
@@ -15,7 +17,7 @@ module Jabber
15
17
  # elements, describing the type and the supported namespaces of
16
18
  # the service.
17
19
  class IqQueryDiscoInfo < IqQuery
18
- name_xmlns 'query', 'http://jabber.org/protocol/disco#info'
20
+ name_xmlns 'query', NS_DISCO_INFO
19
21
 
20
22
  ##
21
23
  # Get the queried Service Discovery node or nil
@@ -53,11 +55,7 @@ module Jabber
53
55
  # Get list of identities
54
56
  # result:: [Array] of [Identity]
55
57
  def identities
56
- res = []
57
- each_element('identity') { |id|
58
- res.push(id)
59
- }
60
- res
58
+ get_elements('identity')
61
59
  end
62
60
 
63
61
  ##
@@ -76,9 +74,10 @@ module Jabber
76
74
  ##
77
75
  # Service Discovery identity to add() to IqQueryDiscoInfo
78
76
  #
79
- # Please note that JEP 0030 requires both category and type to occur
77
+ # Please note that XEP 0030 requires both category and type to occur,
78
+ # for a reference see: http://www.xmpp.org/registrar/disco-categories.html
80
79
  class Identity < XMPPElement
81
- name_xmlns 'identity', 'http://jabber.org/protocol/disco#info'
80
+ name_xmlns 'identity', NS_DISCO_INFO
82
81
 
83
82
  ##
84
83
  # Initialize a new Identity
@@ -171,9 +170,10 @@ module Jabber
171
170
  ##
172
171
  # Service Discovery feature to add() to IqQueryDiscoInfo
173
172
  #
174
- # Please note that JEP 0030 requires var to be set
173
+ # Please note that JEP 0030 requires var to be set,
174
+ # for a reference see: http://www.xmpp.org/registrar/disco-features.html
175
175
  class Feature < XMPPElement
176
- name_xmlns 'feature', 'http://jabber.org/protocol/disco#info'
176
+ name_xmlns 'feature', NS_DISCO_INFO
177
177
 
178
178
  ##
179
179
  # Create a new <feature/> element
@@ -192,7 +192,7 @@ module Jabber
192
192
 
193
193
  ##
194
194
  # Set the feature's var
195
- #
195
+ #
196
196
  # This is a namespace the identity supports.
197
197
  # val:: [String]
198
198
  def var=(val)
@@ -209,4 +209,3 @@ module Jabber
209
209
  end
210
210
  end
211
211
  end
212
-
@@ -6,6 +6,8 @@ require 'xmpp4r/query'
6
6
 
7
7
  module Jabber
8
8
  module Discovery
9
+ NS_DISCO_ITEMS = 'http://jabber.org/protocol/disco#items'
10
+
9
11
  ##
10
12
  # Class for handling Service Discovery queries,
11
13
  # items
@@ -17,7 +19,7 @@ module Jabber
17
19
  # querying IqQueryDiscoInfo and further sub-items by querying
18
20
  # IqQueryDiscoItems.
19
21
  class IqQueryDiscoItems < IqQuery
20
- name_xmlns 'query', 'http://jabber.org/protocol/disco#items'
22
+ name_xmlns 'query', NS_DISCO_ITEMS
21
23
 
22
24
  ##
23
25
  # Get the queried Service Discovery node or nil
@@ -42,6 +44,13 @@ module Jabber
42
44
  self.node = val
43
45
  self
44
46
  end
47
+
48
+ ##
49
+ # Get all item children
50
+ # result:: Array of [Discovery::Item]
51
+ def items
52
+ get_elements('item')
53
+ end
45
54
  end
46
55
 
47
56
 
@@ -50,7 +59,7 @@ module Jabber
50
59
  #
51
60
  # Please note that JEP 0030 requires the jid to occur
52
61
  class Item < XMPPElement
53
- name_xmlns 'item', 'http://jabber.org/protocol/disco#items'
62
+ name_xmlns 'item', NS_DISCO_ITEMS
54
63
 
55
64
  ##
56
65
  # Initialize a new Service Discovery <item/>
@@ -69,7 +78,7 @@ module Jabber
69
78
  # Get the item's jid or nil
70
79
  # result:: [String]
71
80
  def jid
72
- JID::new(attributes['jid'])
81
+ JID.new(attributes['jid'])
73
82
  end
74
83
 
75
84
  ##
@@ -136,4 +145,3 @@ module Jabber
136
145
  end
137
146
  end
138
147
  end
139
-
@@ -3,10 +3,66 @@
3
3
  # Website::http://home.gna.org/xmpp4r/
4
4
 
5
5
  module Jabber
6
+
7
+ # CUSTOM ERROR CLASSES
8
+
9
+ # All of our custom errors are superclassed by JabberError < StandardError
10
+ class JabberError < StandardError; end
11
+
12
+ # A client-side only argument error
13
+ class ArgumentError < JabberError; end
14
+
15
+ ##
16
+ # An error returned from the server
17
+ #
18
+ # e.g. This exception can be raised by helpers when they
19
+ # receive a server reply with <tt>type='error'</tt>
20
+ #
21
+ # The ServerError carries a Jabber::ErrorResponse element
22
+ #
23
+ class ServerError < JabberError #:nodoc:
24
+
25
+ ##
26
+ # The error element which caused this exception
27
+ attr_reader :error
28
+
29
+ ##
30
+ # Initialize a ServerError by passing an ErrorResponse instance
31
+ # error:: [Error]
32
+ def initialize(error)
33
+ @error = error
34
+ end
35
+
36
+ ##
37
+ # Sample output:
38
+ # 'subscription-required: Please subscribe first'
39
+ def to_s
40
+ "#{@error.error}: #{@error.text}"
41
+ end
42
+ end
43
+
44
+ class ClientAuthenticationFailure < JabberError; end
45
+
46
+ class ComponentAuthenticationFailure < JabberError; end
47
+
48
+ # TODO : Give this a better name
49
+ class NoNameXmlnsRegistered < JabberError
50
+ def initialize(klass)
51
+ super "Class #{klass} has not set name and xmlns"
52
+ end
53
+ end
54
+
55
+ # TODO : Give this a better name
56
+ class SOCKS5Error < JabberError; end
57
+
6
58
  ##
7
59
  # A class used to build/parse <error/> elements.
8
- # Look at JEP 0086 for explanation.
9
- class Error < XMPPElement
60
+ # Look at XEP-0086 for explanation:
61
+ # http://www.xmpp.org/extensions/xep-0086.html
62
+ #
63
+ # FIXME : XEP-0086 is officially deprecated. What effect does that have on this class? Any?
64
+ #
65
+ class ErrorResponse < XMPPElement
10
66
  name_xmlns 'error'
11
67
 
12
68
  ##
@@ -35,9 +91,9 @@ module Jabber
35
91
  # * "unexpected-request"
36
92
  # Will raise an [Exception] if not [nil] and none of the above
37
93
  #
38
- # Does also set type and code to appropriate values according to errorcondition
94
+ # Also sets type and code to appropriate values according to errorcondition
39
95
  #
40
- # text: [nil] or [String] Error text
96
+ # text: [nil] or [String] ErrorResponse text
41
97
  def initialize(errorcondition=nil, text=nil)
42
98
  if errorcondition.nil?
43
99
  super()
@@ -53,9 +109,9 @@ module Jabber
53
109
  }
54
110
 
55
111
  if errortype.nil? || errorcode.nil?
56
- raise("Unknown error condition when initializing Error")
112
+ raise ArgumentError, "Unknown error condition when initializing ErrorReponse"
57
113
  end
58
-
114
+
59
115
  super()
60
116
  set_error(errorcondition)
61
117
  set_type(errortype)
@@ -155,7 +211,7 @@ module Jabber
155
211
  self.text = s
156
212
  self
157
213
  end
158
-
214
+
159
215
  ##
160
216
  # Get the type of error
161
217
  # (meaning how to proceed)
@@ -177,7 +233,7 @@ module Jabber
177
233
  end
178
234
 
179
235
  ##
180
- # Set the type of error (see Error#type)
236
+ # Set the type of error (see ErrorResponse#type)
181
237
  def type=(t)
182
238
  case t
183
239
  when :auth then attributes['type'] = 'auth'
@@ -197,8 +253,7 @@ module Jabber
197
253
  end
198
254
 
199
255
  ##
200
- # Possible XMPP error conditions, types and codes
201
- # (JEP 0086)
256
+ # Possible XMPP error conditions, types and codes (XEP-0086)
202
257
  @@Errors = [['bad-request', :modify, 400],
203
258
  ['conflict', :cancel, 409],
204
259
  ['feature-not-implemented', :cancel, 501],
@@ -222,4 +277,5 @@ module Jabber
222
277
  ['undefined-condition', nil, 500],
223
278
  ['unexpected-request', :wait, 400]]
224
279
  end
280
+
225
281
  end
@@ -0,0 +1,55 @@
1
+ require 'thread'
2
+
3
+ module Jabber
4
+ module Framework
5
+ class Base
6
+
7
+ def self.helper(name, klass=nil, &factory)
8
+ if klass.nil? and factory.nil?
9
+ raise "helper #{name} needs a class or factory"
10
+ end
11
+
12
+ define_method(name) do
13
+ @helpers_lock.synchronize do
14
+ if @helpers[name]
15
+ @helpers[name]
16
+ else
17
+ if factory
18
+ @helpers[name] = instance_eval { factory.call(@stream) }
19
+ elsif klass
20
+ @helpers[name] = klass.new(@stream)
21
+ else
22
+ raise
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ attr_accessor :stream
30
+
31
+ def initialize(stream)
32
+ @stream = stream
33
+ @helpers = {}
34
+ @helpers_lock = Mutex.new
35
+
36
+ # Catch all unhandled iq stanzas (lowest priority)
37
+ @stream.add_iq_callback(-1000, self, &method(:on_unhandled_iq))
38
+ end
39
+
40
+ def on_unhandled_iq(iq)
41
+ if iq.type == :get or iq.type == :set
42
+ answer = iq.answer(true)
43
+ answer.type = :error
44
+ answer.add(ErrorResponse.new('feature-not-implemented'))
45
+ @stream.send(answer)
46
+
47
+ true
48
+ else
49
+ false
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end