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
data/lib/xmpp4r.rb ADDED
@@ -0,0 +1,116 @@
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
+ # ==Introduction
6
+ #
7
+ # XMPP4R is a XMPP/Jabber library for Ruby. It can be used to build scripts
8
+ # using Jabber, full-featured Jabber clients, and components. It is written
9
+ # with extensibility in mind.
10
+ #
11
+ # ==XML management
12
+ #
13
+ # All the XML parsing is REXML's, and XML stanzas like <message/> (class
14
+ # <tt>Jabber::Message</tt>) or <iq/> (class <tt>Jabber::Iq</tt>) are indirect
15
+ # derivatives from REXML's Element class. This provide a maximum flexibity:
16
+ # the user can access attributes and childs using either the XMPP4R's helpers
17
+ # or directly using REXML's methods.
18
+ #
19
+ # ===Automatic element casting
20
+ #
21
+ # Because there are special classes derived from REXML::Element to ease
22
+ # development on the protocol level, Elements must be cast to them. This is
23
+ # done via REXML::Element.import. This method is also used in import class
24
+ # methods of some Element classes.
25
+ #
26
+ # The first occurance of this feature is in Jabber::Stream::receive:
27
+ # * <tt><message/></tt> stanzas are cast to Jabber::Message class
28
+ # * <tt><presence/></tt> stanzas are cast to Jabber::Presence class
29
+ # * <tt><iq/></tt> stanzas are cast to Jabber::Iq class
30
+ #
31
+ # This is not only useful for stanzas but all other XML processing, too:
32
+ # * <tt><x/></tt> children elements of <tt><message/></tt> and <tt><presence/></tt> are converted to Jabber::X
33
+ # * <tt><error/></tt> children elements of all three stanzas are converted to Jabber::ErrorResponse
34
+ # * <tt><query/></tt> children elements of <tt><iq/></tt> are converted to Jabber::IqQuery
35
+ # * <tt><vCard/></tt> children elements of <tt><iq/></tt> are converted to Jabber::IqVcard
36
+ #
37
+ # The following conversion facilities are only executed if the respective
38
+ # library parts are loaded. See below for more details on Non-basic features.
39
+ # * Jabber::IqQuery elements are converted to Jabber::Roster::IqQueryRoster if their
40
+ # namespace is 'jabber:iq:roster'
41
+ # * Jabber::IqQuery elements are converted to Jabber::Version::IqQueryVersion if their
42
+ # namespace is 'jabber:iq:version'
43
+ # * Jabber::IqQuery elements are converted to Jabber::Discovery::IqQueryDiscoInfo if their
44
+ # namespace is 'http://jabber.org/protocol/disco#info'
45
+ # * Jabber::IqQuery elements are converted to Jabber::Discovery::IqQueryDiscoItems if their
46
+ # namespace is 'http://jabber.org/protocol/disco#items'
47
+ # * <tt><item/></tt> children elements of Jabber::Roster::IqQueryRoster are converted
48
+ # to Jabber::Roster::RosterItem
49
+ # * <tt><identity/></tt> children elements of Jabber::IqQueryDiscoInfo are converted
50
+ # to Jabber::Discovery::DiscoIdentity
51
+ # * <tt><feature/></tt> children elements of Jabber::IqQueryDiscoInfo are converted
52
+ # to Jabber::Discovery::DiscoFeature
53
+ # * <tt><item/></tt> children elements of Jabber::IqQueryDiscoItems are converted
54
+ # to Jabber::Discovery::DiscoItem
55
+ #
56
+ # To use this, don't check for:
57
+ # <tt>iq.queryns == 'http://jabber.org/protocol/disco#info'</tt>
58
+ #
59
+ # But instead check for the query's class:
60
+ # <tt>iq.query.kind_of?(Jabber::IqQueryDiscoInfo)</tt>
61
+ #
62
+ # ==Where to begin?
63
+ #
64
+ # Because it is built in an extensible way, it might be hard for newcomers to
65
+ # understand where to look at documentation for a specific method. For example,
66
+ # Client inherits from Connection, which itself inherits from Stream.
67
+ #
68
+ # A newcomer should have a look at the <tt>Jabber::Client</tt> and
69
+ # <tt>Jabber::Component</tt> classes, and their parent classes
70
+ # <tt>Jabber::Connection</tt> and <tt>Jabber::Stream</tt>. The best way to
71
+ # understand how to use them is probably to look at the examples in the
72
+ # <tt>examples/</tt> dir.
73
+ #
74
+ # ==Non-basic features
75
+ #
76
+ # <tt>require 'xmpp4r'</tt> only includes basic functionality for
77
+ # Connections, Authentication, Stream processing, Callbacks, Stanza handling
78
+ # and Debugging to keep the library's footprint small.
79
+ #
80
+ # There is code for features that aren't required by a *basic* client. These
81
+ # must be additionally included to use them.
82
+ #
83
+ # ===Protocol-level features
84
+ #
85
+ # You're highly advised to read the according RFCs and JEPs if you intend to
86
+ # use them. The benefit will be that you'll understand the protocols and be
87
+ # going to be more efficient when programming with them.
88
+ #
89
+ # * Jabber::Bytestreams, Jabber::FileTransfer: <tt>require 'xmpp4r/bytestreams'</tt>
90
+ # * Jabber::Dataforms: <tt>require 'xmpp4r/dataforms'</tt>
91
+ # * Jabber::Delay: <tt>require 'xmpp4r/delay'</tt>
92
+ # * Jabber::Discovery: <tt>require 'xmpp4r/discovery'</tt>
93
+ # * Jabber::FeatureNegotiation: <tt>require 'xmpp4r/feature_negotiation'</tt>
94
+ # * Jabber::MUC: <tt>require 'xmpp4r/muc'</tt>
95
+ # * Jabber::Roster: <tt>require 'xmpp4r/roster'</tt>
96
+ # * Jabber::Vcard: <tt>require 'xmpp4r/vcard'</tt>
97
+ # * Jabber::Version: <tt>require 'xmpp4r/version'</tt>
98
+ #
99
+ # ===Helpers
100
+ #
101
+ # Helpers are intended to give more simplistic interfaces to various tasks
102
+ # of Jabber clients at the cost of flexibility. But you won't need that
103
+ # level of flexibility in most cases.
104
+ #
105
+ # * Jabber::Roster::Helper: <tt>require 'xmpp4r/roster'</tt>
106
+ # * Jabber::MUC::MUCBrowser, Jabber::MUC::MUCClient, Jabber::MUC::SimpleMUCClient: <tt>require 'xmpp4r/muc'</tt>
107
+ # * Jabber::Version::SimpleResponder, Jabber::Version::Responder: <tt>require 'xmpp4r/version'</tt>
108
+ # * Jabber::Vcard::Helper: <tt>require 'xmpp4r/vcard'</tt>
109
+ # * Jabber::FileTransfer::Helper, Jabber::Bytestreams::SOCKS5BytestreamsServer: <tt>require 'xmpp4r/bytestreams'</tt>
110
+ #
111
+ # ==Debugging
112
+ #
113
+ # Dumping your Jabber stream can be enabled this way:
114
+ # Jabber::debug = true
115
+
116
+ require 'xmpp4r/xmpp4r'
@@ -0,0 +1,32 @@
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
+ begin
6
+ require 'base64'
7
+ rescue LoadError
8
+ ##
9
+ # Ruby 1.9 has dropped the Base64 module,
10
+ # this is a replacement
11
+ #
12
+ # We could replace all call by Array#pack('m')
13
+ # and String#unpack('m'), but this module
14
+ # improves readability.
15
+ module Base64
16
+ ##
17
+ # Encode a String
18
+ # data:: [String] Binary
19
+ # result:: [String] Binary in Base64
20
+ def self.encode64(data)
21
+ [data].pack('m')
22
+ end
23
+
24
+ ##
25
+ # Decode a Base64-encoded String
26
+ # data64:: [String] Binary in Base64
27
+ # result:: [String] Binary
28
+ def self.decode64(data64)
29
+ data64.unpack('m').first
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
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/bytestreams/iq/si.rb'
6
+ require 'xmpp4r/bytestreams/iq/bytestreams.rb'
7
+ require 'xmpp4r/bytestreams/helper/ibb/base.rb'
8
+ require 'xmpp4r/bytestreams/helper/ibb/initiator.rb'
9
+ require 'xmpp4r/bytestreams/helper/ibb/target.rb'
10
+ require 'xmpp4r/bytestreams/helper/filetransfer.rb'
11
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/base.rb'
12
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/initiator.rb'
13
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/server.rb'
14
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/target.rb'
15
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/socks5.rb'
@@ -0,0 +1,321 @@
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/callbacks'
6
+ require 'xmpp4r/bytestreams/iq/si'
7
+ require 'xmpp4r/bytestreams/iq/bytestreams'
8
+ require 'xmpp4r/dataforms/x/data'
9
+ require 'xmpp4r/bytestreams/helper/ibb/base'
10
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/base'
11
+ require 'xmpp4r/bytestreams/helper/socks5bytestreams/target'
12
+
13
+ module Jabber
14
+ module FileTransfer
15
+ ##
16
+ # The TransferSource is an interface (Mix-in)
17
+ # which sources for FileTransfer#offer should include
18
+ module TransferSource
19
+ ##
20
+ # Filename of the offered file
21
+ def filename
22
+ end
23
+ ##
24
+ # Mime-type of the offered file, can be nil
25
+ def mime
26
+ end
27
+ ##
28
+ # Size of the offered file
29
+ def size
30
+ end
31
+ ##
32
+ # MD5-Sum of the offered file, can be nil
33
+ def md5
34
+ end
35
+ ##
36
+ # Date of the offered file, can be nil
37
+ def date
38
+ end
39
+ ##
40
+ # Read a chunk from the source
41
+ #
42
+ # If this is a ranged transfer, it should
43
+ # implement length checking
44
+ # length:: [Fixnum]
45
+ def read(length=nil)
46
+ end
47
+ ##
48
+ # Seek in the source for ranged transfers
49
+ def seek(position)
50
+ end
51
+ ##
52
+ # Set the amount of data to send for ranged transfers
53
+ def length=(l)
54
+ end
55
+ ##
56
+ # Does implement the methods seek and length= ?
57
+ #
58
+ # FileTransfer will only then offer a ranged transfer.
59
+ # result:: [false] or [true]
60
+ def can_range?
61
+ false
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Simple implementation of TransferSource
67
+ # for sending simple files
68
+ # (supports ranged transfers)
69
+ class FileSource
70
+ include TransferSource
71
+
72
+ def initialize(filename)
73
+ @file = File.new(filename)
74
+ @filename = filename
75
+ @bytes_read = 0
76
+ @length = nil
77
+ end
78
+
79
+ def filename
80
+ File::basename @filename
81
+ end
82
+
83
+ ##
84
+ # Everything is 'application/octet-stream'
85
+ def mime
86
+ 'application/octet-stream'
87
+ end
88
+
89
+ def size
90
+ File.size @filename
91
+ end
92
+
93
+ def date
94
+ @file.mtime
95
+ end
96
+
97
+ ##
98
+ # Because it can_range?, this method implements length checking
99
+ def read(length=512)
100
+ if @length
101
+ return nil if @bytes_read >= @length # Already read everything requested
102
+ if @bytes_read + length > @length # Will we read more than requested?
103
+ length = @length - @bytes_read # Truncate it!
104
+ end
105
+ end
106
+
107
+ buf = @file.read(length)
108
+ @bytes_read += buf.size if buf
109
+ buf
110
+ end
111
+
112
+ def seek(position)
113
+ @file.seek(position)
114
+ end
115
+
116
+ def length=(l)
117
+ @length = l
118
+ end
119
+
120
+ def can_range?
121
+ true
122
+ end
123
+ end
124
+
125
+ ##
126
+ # The FileTransfer helper provides the ability to respond
127
+ # to incoming and to offer outgoing file-transfers.
128
+ class Helper
129
+ ##
130
+ # Set this if you want to use this helper in a Component
131
+ attr_accessor :my_jid
132
+ ##
133
+ # Set this to false if you don't want to use SOCKS5Bytestreams
134
+ attr_accessor :allow_bytestreams
135
+ ##
136
+ # Set this to false if you don't want to use IBB
137
+ attr_accessor :allow_ibb
138
+
139
+ ##
140
+ # Create a new FileTransfer instance
141
+ def initialize(stream)
142
+ @stream = stream
143
+ @my_jid = nil
144
+ @allow_bytestreams = true
145
+ @allow_ibb = true
146
+
147
+ @incoming_cbs = CallbackList.new
148
+
149
+ @stream.add_iq_callback(150, self) { |iq|
150
+ if iq.type == :set
151
+ file = iq.first_element('si/file')
152
+ field = nil
153
+ iq.each_element('si/feature/x') { |e| field = e.field('stream-method') }
154
+
155
+ if file and field
156
+ @incoming_cbs.process(iq, file)
157
+ true
158
+ else
159
+ false
160
+ end
161
+ else
162
+ false
163
+ end
164
+ }
165
+ end
166
+
167
+ ##
168
+ # Add a callback which will be invoked upon an incoming file-transfer
169
+ #
170
+ # block takes two arguments:
171
+ # * Iq
172
+ # * Bytestreams::IqSiFile in the Iq
173
+ # You may then invoke accept or decline
174
+ def add_incoming_callback(priority = 0, ref = nil, &block)
175
+ @incoming_cbs.add(priority, ref, block)
176
+ end
177
+
178
+ ##
179
+ # Accept an incoming file-transfer,
180
+ # to be used in a block given to add_incoming_callback
181
+ #
182
+ # offset and length will be ignored if there is no
183
+ # 'si/file/range' in iq.
184
+ # iq:: [Iq] of file-transfer we want to accept
185
+ # offset:: [Fixnum] or [nil]
186
+ # length:: [Fixnum] or [nil]
187
+ # result:: [Bytestreams::SOCKS5BytestreamsTarget] or [Bytestreams::IBBTarget] or [nil] if no valid stream-method
188
+ def accept(iq, offset=nil, length=nil)
189
+ oldsi = iq.first_element('si')
190
+
191
+ answer = iq.answer(false)
192
+ answer.type = :result
193
+
194
+ si = answer.add(Bytestreams::IqSi.new)
195
+ if (offset or length) and oldsi.file.range
196
+ si.add(Bytestreams::IqSiFile.new)
197
+ si.file.add(Bytestreams::IqSiFileRange.new(offset, length))
198
+ end
199
+ si.add(FeatureNegotiation::IqFeature.new.import(oldsi.feature))
200
+ si.feature.x.type = :submit
201
+ stream_method = si.feature.x.field('stream-method')
202
+
203
+ if stream_method.options.keys.include?(Bytestreams::NS_BYTESTREAMS) and @allow_bytestreams
204
+ stream_method.values = [Bytestreams::NS_BYTESTREAMS]
205
+ stream_method.options = []
206
+ @stream.send(answer)
207
+
208
+ Bytestreams::SOCKS5BytestreamsTarget.new(@stream, oldsi.id, iq.from, iq.to)
209
+ elsif stream_method.options.keys.include?(Bytestreams::IBB::NS_IBB) and @allow_ibb
210
+ stream_method.values = [Bytestreams::IBB::NS_IBB]
211
+ stream_method.options = []
212
+ @stream.send(answer)
213
+
214
+ Bytestreams::IBBTarget.new(@stream, oldsi.id, iq.from, iq.to)
215
+ else
216
+ eanswer = iq.answer(false)
217
+ eanswer.type = :error
218
+ eanswer.add(ErrorResponse.new('bad-request')).type = :cancel
219
+ eanswer.error.add(REXML::Element.new('no-valid-streams')).add_namespace('http://jabber.org/protocol/si')
220
+ @stream.send(eanswer)
221
+
222
+ nil
223
+ end
224
+ end
225
+
226
+ ##
227
+ # Decline an incoming file-transfer,
228
+ # to be used in a block given to add_incoming_callback
229
+ # iq:: [Iq] of file-transfer we want to decline
230
+ def decline(iq)
231
+ answer = iq.answer(false)
232
+ answer.type = :error
233
+ error = answer.add(ErrorResponse.new('forbidden', 'Offer declined'))
234
+ error.type = :cancel
235
+ @stream.send(answer)
236
+ end
237
+
238
+ ##
239
+ # Offer a file to somebody
240
+ #
241
+ # Will wait for a response from the peer
242
+ #
243
+ # The result is a stream which you can configure, or nil
244
+ # if the peer responded with an invalid stream-method.
245
+ #
246
+ # May raise an ServerError
247
+ # jid:: [JID] to send the file to
248
+ # source:: File-transfer source, implementing the FileSource interface
249
+ # desc:: [String] or [nil] Optional file description
250
+ # from:: [String] or [nil] Optional jid for components
251
+ # result:: [Bytestreams::SOCKS5BytestreamsInitiator] or [Bytestreams::IBBInitiator] or [nil]
252
+ def offer(jid, source, desc=nil, from=nil)
253
+ from = from || @my_jid || @stream.jid
254
+ session_id = Jabber::IdGenerator.instance.generate_id
255
+
256
+ offered_methods = {}
257
+ if @allow_bytestreams
258
+ offered_methods[Bytestreams::NS_BYTESTREAMS] = nil
259
+ end
260
+ if @allow_ibb
261
+ offered_methods[Bytestreams::IBB::NS_IBB] = nil
262
+ end
263
+
264
+ iq = Iq.new(:set, jid)
265
+ iq.from = from
266
+ si = iq.add(Bytestreams::IqSi.new(session_id, Bytestreams::PROFILE_FILETRANSFER, source.mime))
267
+
268
+ file = si.add(Bytestreams::IqSiFile.new(source.filename, source.size))
269
+ file.hash = source.md5
270
+ file.date = source.date
271
+ file.description = desc if desc
272
+ file.add(Bytestreams::IqSiFileRange.new) if source.can_range?
273
+
274
+ feature = si.add(REXML::Element.new('feature'))
275
+ feature.add_namespace 'http://jabber.org/protocol/feature-neg'
276
+ x = feature.add(Dataforms::XData.new(:form))
277
+ stream_method_field = x.add(Dataforms::XDataField.new('stream-method', :list_single))
278
+ stream_method_field.options = offered_methods
279
+
280
+ begin
281
+ stream_method = nil
282
+ response = nil
283
+ @stream.send_with_id(iq) do |r|
284
+ response = r
285
+ si = response.first_element('si')
286
+ if si and si.feature and si.feature.x
287
+ stream_method = si.feature.x.field('stream-method').values.first
288
+
289
+ if si.file and si.file.range
290
+ if source.can_range?
291
+ source.seek(si.file.range.offset) if si.file.range.offset
292
+ source.length = si.file.range.length if si.file.range.length
293
+ else
294
+ source.read(si.file.range.offset)
295
+ end
296
+ end
297
+ end
298
+ end
299
+ rescue ServerError => e
300
+ if e.error.code == 403 # Declined
301
+ return false
302
+ else
303
+ raise e
304
+ end
305
+ end
306
+
307
+ if stream_method == Bytestreams::NS_BYTESTREAMS and @allow_bytestreams
308
+ Bytestreams::SOCKS5BytestreamsInitiator.new(@stream, session_id, from, jid)
309
+ elsif stream_method == Bytestreams::IBB::NS_IBB and @allow_ibb
310
+ Bytestreams::IBBInitiator.new(@stream, session_id, from, jid)
311
+ else # Target responded with a stream_method we didn't offer
312
+ eanswer = response.answer
313
+ eanswer.type = :error
314
+ eanswer.add ErrorResponse.new('bad-request')
315
+ @stream.send(eanswer)
316
+ nil
317
+ end
318
+ end
319
+ end
320
+ end
321
+ end