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,148 @@
1
+ require 'xmpp4r/framework/base'
2
+
3
+ require 'xmpp4r'
4
+ require 'xmpp4r/roster'
5
+ require 'xmpp4r/caps/c'
6
+ require 'xmpp4r/discovery'
7
+ require 'xmpp4r/xhtml'
8
+
9
+ module Jabber
10
+ module Framework
11
+ ##
12
+ # Abstract handler methods that *may* be implemented by a deriving class:
13
+ # * on_message(text)
14
+ # * on_message_xhtml(html_body, text)
15
+ class Bot < Base
16
+ helper :roster, Roster::Helper
17
+ helper(:disco_default) { |cl|
18
+ Discovery::Responder.new(cl,
19
+ nil,
20
+ [Jabber::Discovery::Identity.new('client', 'XMPP4R Bot', 'bot')]
21
+ )
22
+ }
23
+ helper(:disco_caps) { |cl|
24
+ Discovery::Responder.new(cl,
25
+ "http://home.gna.org/xmpp4r/#{Jabber::XMPP4R_VERSION}",
26
+ [Jabber::Discovery::Identity.new('client', 'XMPP4R Bot', 'bot')]
27
+ )
28
+ }
29
+
30
+ def initialize(jid, password)
31
+ cl = Jabber::Client.new(jid)
32
+ cl.connect
33
+ cl.auth(password)
34
+
35
+ super(cl)
36
+
37
+ roster.add_subscription_request_callback do |item,presence|
38
+ if accept_subscription_from?(presence.from.strip)
39
+ roster.accept_subscription(presence.from.strip)
40
+ else
41
+ roster.decline_subscription(presence.from.strip)
42
+ end
43
+ end
44
+
45
+ @pep_notifications = []
46
+ cl.add_message_callback do |msg|
47
+ if msg.type != :error and msg.body
48
+ if (html = msg.first_element('html')) and respond_to? :on_message_xhtml
49
+ on_message_xhtml(html.body, msg.body)
50
+ elsif respond_to? :on_message
51
+ on_message(msg.body)
52
+ end
53
+ elsif msg.type != :error and (event = msg.first_element('event'))
54
+ event.each_element('items') do |items|
55
+ node = items.attributes['node']
56
+ items.each_element('item') do |item|
57
+ @pep_notifications.each { |notification_node,callback|
58
+ if node == notification_node
59
+ callback.call(msg.from, item)
60
+ end
61
+ }
62
+ end
63
+ end
64
+ else
65
+ false
66
+ end
67
+ end
68
+
69
+ add_cap('presence')
70
+ add_cap(Caps::NS_CAPS)
71
+ add_cap('message') if respond_to? :on_message
72
+ add_cap(XHTML::NS_XHTML_IM) if respond_to? :on_message_xhtml
73
+
74
+ @presence_show = nil
75
+ @presence_status = nil
76
+ end
77
+
78
+ ##
79
+ # Add feature namespace to Capabilities Discovery
80
+ def add_cap(capability)
81
+ disco_default.add_feature(capability)
82
+ disco_caps.add_feature(capability)
83
+ end
84
+
85
+ ##
86
+ # Front-end for Roster::Helper#add_subscription_request_callback
87
+ #
88
+ # Can be overwritten, must return true or false
89
+ def accept_subscription_from?(jid)
90
+ true
91
+ end
92
+
93
+ ##
94
+ # Send a simple text chat message
95
+ def send_message(to, text)
96
+ msg = Message.new
97
+ msg.type = :chat
98
+ msg.to = to
99
+ msg.body = text
100
+ @stream.send(msg)
101
+ end
102
+
103
+ ##
104
+ # Send an XHTML chat message
105
+ # text:: [String] alternate plain text body, generated from xhtml_contents if nil
106
+ def send_message_xhtml(to, xhtml_contents, text=nil)
107
+ msg = Message.new
108
+ msg.type = :chat
109
+ msg.to = to
110
+ html = msg.add(XHTML::HTML.new(xhtml_contents))
111
+ msg.body = text ? text : html.to_text
112
+ @stream.send(msg)
113
+ end
114
+
115
+ ##
116
+ # Set and send a Presence
117
+ def set_presence(show=nil, status=nil)
118
+ @presence_show = show
119
+ @presence_status = status
120
+ send_presence
121
+ end
122
+
123
+ private
124
+
125
+ def send_presence
126
+ roster.wait_for_roster
127
+
128
+ # TODO: vcard photo hash
129
+ if @presence_show == :unavailable
130
+ presence = Presence.new(nil, @presence_status)
131
+ presence.type = :unavailable
132
+ else
133
+ presence = Presence.new(@presence_show, @presence_status)
134
+ end
135
+ presence.add(disco_caps.generate_caps)
136
+ @stream.send(presence)
137
+ end
138
+
139
+ public
140
+
141
+ def add_pep_notification(node, &callback)
142
+ add_cap("#{node}+notify")
143
+ @pep_notifications << [node, callback]
144
+ end
145
+ end
146
+
147
+ end
148
+ end
@@ -0,0 +1,5 @@
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/httpbinding/client'
@@ -0,0 +1,384 @@
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
+ # TODO: eval <body type='terminate' condition=
6
+
7
+ require 'xmpp4r/client'
8
+ require 'xmpp4r/semaphore'
9
+ require 'net/http'
10
+ require 'uri'
11
+ require 'cgi' # for escaping
12
+ require 'pry'
13
+
14
+ module Jabber
15
+ module HTTPBinding
16
+ ##
17
+ # This class implements an alternative Client
18
+ # using HTTP Binding (JEP0124).
19
+ #
20
+ # This class is designed to be a drop-in replacement
21
+ # for Jabber::Client, except for the
22
+ # Jabber::HTTP::Client#connect method which takes an URI
23
+ # as argument.
24
+ #
25
+ # HTTP requests are buffered to not exceed the negotiated
26
+ # 'polling' and 'requests' parameters.
27
+ #
28
+ # Stanzas in HTTP resonses may be delayed to arrive in the
29
+ # order defined by 'rid' parameters.
30
+ #
31
+ # =Debugging
32
+ # Turning Jabber::debug to true will make debug output
33
+ # not only spit out stanzas but HTTP request/response
34
+ # bodies, too.
35
+ class Client < Jabber::Client
36
+
37
+ # Content-Type to be used for communication
38
+ # (you can set this to "text/html")
39
+ attr_accessor :http_content_type
40
+ # The server should wait this value seconds if
41
+ # there is no stanza to be received
42
+ attr_accessor :http_wait
43
+ # The server may hold this amount of stanzas
44
+ # to reduce number of HTTP requests
45
+ attr_accessor :http_hold
46
+ # Hook to initialize SSL parameters on Net::HTTP
47
+ attr_accessor :http_ssl_setup
48
+
49
+ ##
50
+ # Initialize
51
+ # jid:: [JID or String]
52
+ def initialize(jid)
53
+ super
54
+
55
+ @lock = Mutex.new
56
+ @pending_requests = 0
57
+ @last_send = Time.at(0)
58
+ @send_buffer = ''
59
+
60
+ @http_requests = 1
61
+ @http_wait = 20
62
+ @http_hold = 1
63
+ @http_content_type = 'text/xml; charset=utf-8'
64
+
65
+ @no_proxy = []
66
+ @proxy_args = []
67
+ end
68
+
69
+ ##
70
+ # Set up proxy from the given url
71
+
72
+ # url:: [URI::Generic or String] of the form:
73
+ # when without proxy authentication
74
+ # http://proxy_host:proxy_port/
75
+ # when with proxy authentication
76
+ # http://proxy_user:proxy_password@proxy_host:proxy_port/
77
+ def http_proxy_uri=(uri)
78
+ uri = URI.parse(uri) unless uri.respond_to?(:host)
79
+ @proxy_args = [
80
+ uri.host,
81
+ uri.port,
82
+ uri.user,
83
+ uri.password,
84
+ ]
85
+ end
86
+
87
+ ##
88
+ # Set up proxy from the environment variables
89
+ #
90
+ # Following environment variables are considered
91
+ # HTTP_PROXY, http_proxy
92
+ # NO_PROXY, no_proxy
93
+ # HTTP_PROXY_USER, HTTP_PROXY_PASSWORD
94
+ def http_proxy_env
95
+ @proxy_args = []
96
+ env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
97
+ return if env_proxy.nil? or env_proxy.empty?
98
+
99
+ uri = URI.parse env_proxy
100
+ unless uri.user or uri.password then
101
+ uri.user = CGI.escape (ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER']) rescue nil
102
+ uri.password = CGI.escape (ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS']) rescue nil
103
+ end
104
+
105
+ self.http_proxy_uri = uri
106
+
107
+ @no_proxy = (ENV['NO_PROXY'] || ENV['no_proxy'] || 'localhost, 127.0.0.1').split(/\s*,\s*/)
108
+ end
109
+
110
+ ##
111
+ # Set up the stream using uri as the HTTP Binding URI
112
+ #
113
+ # You may optionally pass host and port parameters
114
+ # to make use of the JEP0124 'route' feature.
115
+ #
116
+ # uri:: [URI::Generic or String]
117
+ # host:: [String] Optional host to route to
118
+ # port:: [Fixnum] Port for route feature
119
+ def connect(uri, host=nil, port=5222)
120
+ uri = URI::parse(uri) unless uri.kind_of? URI::Generic
121
+ @uri = uri
122
+
123
+ @allow_tls = false # Shall be done at HTTP level
124
+ @stream_mechanisms = []
125
+ @stream_features = {}
126
+ @http_rid = IdGenerator.generate_id.to_i
127
+ @pending_rid = @http_rid
128
+ @pending_rid_lock = Semaphore.new
129
+
130
+ req_body = REXML::Element.new('body')
131
+ req_body.attributes['rid'] = @http_rid
132
+ req_body.attributes['content'] = @http_content_type
133
+ req_body.attributes['hold'] = @http_hold.to_s
134
+ req_body.attributes['wait'] = @http_wait.to_s
135
+ req_body.attributes['to'] = @jid.domain
136
+ if host
137
+ req_body.attributes['route'] = "xmpp:#{host}:#{port}"
138
+ end
139
+ req_body.attributes['secure'] = 'true'
140
+ req_body.attributes['xmlns'] = 'http://jabber.org/protocol/httpbind'
141
+ res_body = post(req_body)
142
+ unless res_body.name == 'body'
143
+ raise 'Response body is no <body/> element'
144
+ end
145
+
146
+ @streamid = res_body.attributes['authid']
147
+ @status = CONNECTED
148
+ @http_sid = res_body.attributes['sid']
149
+ @http_wait = res_body.attributes['wait'].to_i if res_body.attributes['wait']
150
+ @http_hold = res_body.attributes['hold'].to_i if res_body.attributes['hold']
151
+ @http_inactivity = res_body.attributes['inactivity'].to_i
152
+ @http_polling = res_body.attributes['polling'].to_i
153
+ @http_polling = 5 if @http_polling == 0
154
+ @http_requests = res_body.attributes['requests'].to_i
155
+ @http_requests = 1 if @http_requests == 0
156
+
157
+ receive_elements_with_rid(@http_rid, res_body.children)
158
+
159
+ @features_sem.run
160
+ end
161
+
162
+ ##
163
+ # Ensure that there is one pending request
164
+ #
165
+ # Will be automatically called if you've sent
166
+ # a stanza.
167
+ def ensure_one_pending_request
168
+ return if is_disconnected?
169
+
170
+ if @lock.synchronize { @pending_requests } < 1
171
+ send_data('')
172
+ end
173
+ end
174
+
175
+ ##
176
+ # Close the session by sending
177
+ # <body type='terminate'/>
178
+ def close
179
+ @status = DISCONNECTED
180
+ req_body = nil
181
+ @lock.synchronize {
182
+ req_body = "<body"
183
+ req_body += " rid='#{@http_rid += 1}'"
184
+ req_body += " sid='#{@http_sid}'"
185
+ req_body += " type='terminate'"
186
+ req_body += " xmlns='http://jabber.org/protocol/httpbind'"
187
+ req_body += ">"
188
+ req_body += "<presence type='unavailable' xmlns='jabber:client'/>"
189
+ req_body += "</body>"
190
+ current_rid = @http_rid
191
+ @pending_requests += 1
192
+ @last_send = Time.now
193
+ }
194
+ res_body = post(req_body)
195
+ sleep(3)
196
+ Jabber::debuglog("Connection closed")
197
+ end
198
+
199
+ private
200
+
201
+ ##
202
+ # Receive stanzas ensuring that the 'rid' order is kept
203
+ # result:: [REXML::Element]
204
+ def receive_elements_with_rid(rid, elements)
205
+ while rid > @pending_rid
206
+ @pending_rid_lock.wait
207
+ end
208
+ @pending_rid = rid + 1
209
+
210
+ elements.each { |e|
211
+ receive(e)
212
+ }
213
+
214
+ @pending_rid_lock.run
215
+ end
216
+
217
+ ##
218
+ # Do a POST request
219
+ def post(body)
220
+ body = body.to_s
221
+ request = Net::HTTP::Post.new(@uri.path)
222
+ request.content_length = body.size
223
+ request.body = body
224
+ request['Content-Type'] = @http_content_type
225
+ Jabber::debuglog("HTTP REQUEST (#{@pending_requests}/#{@http_requests}):\n#{request.body}")
226
+
227
+ net_http_args = [@uri.host, @uri.port]
228
+ unless @proxy_args.empty?
229
+ unless no_proxy?(@uri)
230
+ net_http_args.concat @proxy_args
231
+ end
232
+ end
233
+
234
+ http = Net::HTTP.new(*net_http_args)
235
+ if @uri.kind_of? URI::HTTPS
236
+ http.use_ssl = true
237
+ @http_ssl_setup and @http_ssl_setup.call(http)
238
+ end
239
+ http.read_timeout = @http_wait * 1.1
240
+
241
+ response = http.start { |http|
242
+ http.request(request)
243
+ }
244
+ Jabber::debuglog("HTTP RESPONSE (#{@pending_requests}/#{@http_requests}): #{response.class}\n#{response.body}")
245
+
246
+ unless response.kind_of? Net::HTTPSuccess
247
+ # Unfortunately, HTTPResponses aren't exceptions
248
+ # TODO: rescue'ing code should be able to distinguish
249
+ raise Net::HTTPBadResponse, "#{response.class}"
250
+ end
251
+
252
+ body = REXML::Document.new(response.body).root
253
+ if body.name != 'body' and body.namespace != 'http://jabber.org/protocol/httpbind'
254
+ raise REXML::ParseException.new('Malformed body')
255
+ end
256
+ body
257
+ end
258
+
259
+ ##
260
+ # Check whether uri should be accessed without proxy
261
+ def no_proxy?(uri)
262
+ @no_proxy.each do |host_addr|
263
+ return true if uri.host.match(Regexp.quote(host_addr) + '$')
264
+ end
265
+ return false
266
+ end
267
+
268
+ ##
269
+ # Prepare data to POST and
270
+ # handle the result
271
+ def post_data(data, restart = false)
272
+ req_body = nil
273
+ current_rid = nil
274
+
275
+ begin
276
+ begin
277
+ @lock.synchronize {
278
+ # Do not send unneeded requests
279
+ if data.size < 1 and @pending_requests > 0 and !restart
280
+ @pending_requests += 1 # compensate for decrement in ensure clause
281
+ return
282
+ end
283
+
284
+ req_body = "<body"
285
+ req_body += " rid='#{@http_rid += 1}'"
286
+ req_body += " sid='#{@http_sid}'"
287
+ req_body += " xmlns='http://jabber.org/protocol/httpbind'"
288
+ req_body += " xml:lang='en' xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'" if restart
289
+ req_body += ">"
290
+ req_body += data unless restart
291
+ req_body += "</body>"
292
+ current_rid = @http_rid
293
+
294
+ @pending_requests += 1
295
+ @last_send = Time.now
296
+ }
297
+
298
+ res_body = post(req_body)
299
+
300
+ ensure
301
+ @lock.synchronize {
302
+ @pending_requests -= 1
303
+ }
304
+ end
305
+
306
+ receive_elements_with_rid(current_rid, res_body.children)
307
+ ensure_one_pending_request if @authenticated
308
+
309
+ rescue REXML::ParseException
310
+ if @exception_block
311
+ Thread.new do
312
+ Thread.current.abort_on_exception = true
313
+ close; @exception_block.call(e, self, :parser)
314
+ end
315
+ else
316
+ Jabber::debuglog "Exception caught when parsing HTTP response!"
317
+ close
318
+ raise
319
+ end
320
+
321
+ rescue StandardError => e
322
+ Jabber::debuglog("POST error (will retry): #{e.class}: #{e}")
323
+ receive_elements_with_rid(current_rid, [])
324
+ # It's not good to resend on *any* exception,
325
+ # but there are too many cases (Timeout, 404, 502)
326
+ # where resending is appropriate
327
+ # TODO: recognize these conditions and act appropriate
328
+ send_data(data)
329
+ end
330
+ end
331
+
332
+ ##
333
+ # Restart stream after SASL authentication
334
+ def restart
335
+ Jabber::debuglog("Restarting after SASL")
336
+ @stream_mechanisms = []
337
+ @stream_features = {}
338
+ @features_sem = Semaphore.new
339
+ send_data('', true) # restart
340
+ end
341
+
342
+ ##
343
+ # Send data,
344
+ # buffered and obeying 'polling' and 'requests' limits
345
+ def send_data(data, restart = false)
346
+ @lock.synchronize do
347
+ Jabber::debuglog("send_data")
348
+
349
+ @send_buffer += data
350
+ limited_by_polling = false
351
+ if @pending_requests + 1 == @http_requests
352
+ limited_by_polling = (@last_send + @http_polling >= Time.now)
353
+ end
354
+ limited_by_requests = (@pending_requests + 1 > @http_requests)
355
+
356
+ # Can we send?
357
+ if !limited_by_polling and !limited_by_requests or !@authenticated
358
+ Jabber::debuglog("send_data non_limited")
359
+ data = @send_buffer
360
+ @send_buffer = ''
361
+
362
+ Thread.new do
363
+ Thread.current.abort_on_exception = true
364
+ sleep(0.05)
365
+ post_data(data, restart)
366
+ end
367
+
368
+ elsif !limited_by_requests
369
+ Jabber::debuglog("send_data limited")
370
+ Thread.new do
371
+ Thread.current.abort_on_exception = true
372
+ # Defer until @http_polling has expired
373
+ wait = @last_send + @http_polling - Time.now
374
+ sleep(wait) if wait > 0
375
+ # Ignore locking, it's already threaded ;-)
376
+ send_data('', restart)
377
+ end
378
+ end
379
+
380
+ end
381
+ end
382
+ end
383
+ end
384
+ end