yong-stropheruby 0.0.6 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +1 -0
- data/README.rdoc +15 -136
- data/Rakefile +1 -1
- data/examples/xmpp_client.rb +65 -0
- metadata +2 -1
data/Manifest.txt
CHANGED
data/README.rdoc
CHANGED
@@ -1,143 +1,22 @@
|
|
1
|
-
|
1
|
+
== OVERVIEW
|
2
2
|
|
3
|
-
|
3
|
+
Stropheruby is a ruby bindings for Strophe, a C library for writing XMPP clients.
|
4
4
|
|
5
|
-
|
5
|
+
This is a fork of flamontagne's stropheruby (http://github.com/flamontagne/stropheruby/tree/master) with the following improvements:
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
* A patched version of libstrophe (trunk) is included. So there is no extra library to install.
|
8
|
+
* (libstrophe) Fixed a timeout issue on Mac OSX: http://groups.google.com/group/strophe-dev/browse_thread/thread/ef4cb19785020fb6
|
9
|
+
* (libstrophe) Fixed basic auth: http://groups.google.com/group/strophe-dev/browse_thread/thread/b770f72c83d1a0b9
|
10
|
+
* (libstrophe) Changed xmpp_run_once's return code so that the application can tell if an error or timeout occurs
|
11
|
+
* (stropheruby) Added send_raw_string method
|
12
|
+
* (stropheruby) Detect login failure
|
13
|
+
* (stropheruby) Fix resource leak
|
11
14
|
|
12
|
-
|
13
|
-
for production!
|
15
|
+
== INSTALLATION
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
sudo gem sources -a http://gems.github.com
|
18
|
+
sudo gem install yong-stropheruby
|
17
19
|
|
18
|
-
|
19
|
-
syntax. I didn't use SWIG to generate the bindings for 2 reasons :
|
20
|
+
== EXAMPLE
|
20
21
|
|
21
|
-
|
22
|
-
2. I didn't like how SWIG generate gazilions of lines of code
|
23
|
-
|
24
|
-
My other project, XMPPBot, is an implementation of Strophe Ruby that
|
25
|
-
allows the ruby developer to write a XMPP bot in a few lines of code.
|
26
|
-
|
27
|
-
== CURRENT PROBLEMS:
|
28
|
-
|
29
|
-
- Currently no Support for TLS encryption (Coming soon as it's rather important)
|
30
|
-
- Cannot terminate the loop with CTRL-C
|
31
|
-
- Socket disconnects after being inactive for too long
|
32
|
-
- Cannot output log data in a file
|
33
|
-
|
34
|
-
|
35
|
-
== EXAMPLE OF USE
|
36
|
-
|
37
|
-
require 'strophe_ruby'
|
38
|
-
|
39
|
-
def announce_presence
|
40
|
-
stanza = StropheRuby::Stanza.new
|
41
|
-
stanza.name = "presence"
|
42
|
-
@conn.send(stanza)
|
43
|
-
end
|
44
|
-
|
45
|
-
def register_callbacks
|
46
|
-
#Accept all subscriptions
|
47
|
-
@conn.add_handler("presence") do |pres|
|
48
|
-
if pres.type == "subscribe"
|
49
|
-
#we accept
|
50
|
-
p = StropheRuby::Stanza.new
|
51
|
-
p.name = "presence"
|
52
|
-
p.type = "subscribed"
|
53
|
-
p.set_attribute("to",pres.attribute("from"))
|
54
|
-
@conn.send(p)
|
55
|
-
|
56
|
-
#then it's our turn to send our subscription request
|
57
|
-
p = StropheRuby::Stanza.new
|
58
|
-
p.name = "presence"
|
59
|
-
p.type = "subscribe"
|
60
|
-
p.set_attribute("to",pres.attribute("from"))
|
61
|
-
@conn.send(p)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
#simple echo for messages
|
66
|
-
@conn.add_handler("message") do |msg|
|
67
|
-
|
68
|
-
if msg.child_by_name("body")
|
69
|
-
top_stanza = StropheRuby::Stanza.new
|
70
|
-
top_stanza.name="message"
|
71
|
-
top_stanza.type="chat"
|
72
|
-
top_stanza.set_attribute("to", msg.attribute("from"))
|
73
|
-
|
74
|
-
body_stanza = StropheRuby::Stanza.new
|
75
|
-
body_stanza.name="body"
|
76
|
-
|
77
|
-
text_stanza = StropheRuby::Stanza.new
|
78
|
-
text_stanza.text = msg.child_by_name("body").text
|
79
|
-
|
80
|
-
body_stanza.add_child(text_stanza)
|
81
|
-
top_stanza.add_child(body_stanza)
|
82
|
-
@conn.send(top_stanza)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
|
88
|
-
#Prepare the strophe library
|
89
|
-
StropheRuby::EventLoop.prepare
|
90
|
-
|
91
|
-
#create the runtime context and specify the logging level (WARN,INFO,ERROR or DEBUG)
|
92
|
-
@ctx=StropheRuby::Context.new(StropheRuby::Logging::DEBUG)
|
93
|
-
|
94
|
-
#create the connection passing it the context
|
95
|
-
@conn=StropheRuby::Connection.new(@ctx)
|
96
|
-
|
97
|
-
#set the jid and password
|
98
|
-
@conn.jid = 'bot@example.com'
|
99
|
-
@conn.password = 'secret'
|
100
|
-
|
101
|
-
#Try to connect
|
102
|
-
@conn.connect do |status|
|
103
|
-
if status == StropheRuby::ConnectionEvents::CONNECT
|
104
|
-
#We are connected.
|
105
|
-
announce_presence
|
106
|
-
register_callbacks
|
107
|
-
else
|
108
|
-
@conn.disconnect
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
|
113
|
-
#Start the event loop
|
114
|
-
StropheRuby::EventLoop.run(@ctx)
|
115
|
-
|
116
|
-
puts 'Disconnected'
|
117
|
-
|
118
|
-
== INSTALL:
|
119
|
-
|
120
|
-
gem install strophe_ruby
|
121
|
-
|
122
|
-
== LICENSE:
|
123
|
-
|
124
|
-
Copyright (c) 2008 François Lamontagne
|
125
|
-
|
126
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
127
|
-
a copy of this software and associated documentation files (the
|
128
|
-
'Software'), to deal in the Software without restriction, including
|
129
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
130
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
131
|
-
permit persons to whom the Software is furnished to do so, subject to
|
132
|
-
the following conditions:
|
133
|
-
|
134
|
-
The above copyright notice and this permission notice shall be
|
135
|
-
included in all copies or substantial portions of the Software.
|
136
|
-
|
137
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
138
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
139
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
140
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
141
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
142
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
143
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
See example.rb
|
data/Rakefile
CHANGED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'strophe_ruby'
|
2
|
+
|
3
|
+
class XmppClient
|
4
|
+
def query_roster(jid, password)
|
5
|
+
new_connection(jid, password) {
|
6
|
+
current_stage = 1
|
7
|
+
@connection.add_handler("iq") do |iq|
|
8
|
+
case current_stage
|
9
|
+
when 1
|
10
|
+
@connection.send_raw_string("<iq type='get' id='1238647002'><query xmlns='jabber:iq:roster'/></iq>")
|
11
|
+
current_stage += 1
|
12
|
+
else
|
13
|
+
StropheRuby::EventLoop.stop(@ctx)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def new_connection(jid, password)
|
22
|
+
lib = StropheRuby::EventLoop.prepare
|
23
|
+
@ctx = StropheRuby::Context.new(StropheRuby::Logging::DEBUG)
|
24
|
+
@connection = StropheRuby::Connection.new(@ctx)
|
25
|
+
@connection.jid = jid
|
26
|
+
@connection.password = password
|
27
|
+
connected = false
|
28
|
+
@connection.connect do |status|
|
29
|
+
if !connected
|
30
|
+
connected = true
|
31
|
+
if status != StropheRuby::ConnectionEvents::CONNECT
|
32
|
+
#do somthing after login fails
|
33
|
+
raise 'failed to login'
|
34
|
+
else
|
35
|
+
#do something after login
|
36
|
+
yield if block_given?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
run_event_loop_until_timeout_or_stopped_by_callback!
|
42
|
+
end
|
43
|
+
|
44
|
+
#run event loop until timeout or 'StropheRuby::EventLoop.stop' is called inside callback
|
45
|
+
def run_event_loop_until_timeout_or_stopped_by_callback!(timeout = 3000)
|
46
|
+
raise 'unexpected state' if (@ctx.loop_status != StropheRuby::EventLoop::LOOP_NOTSTARTED)
|
47
|
+
@ctx.loop_status = StropheRuby::EventLoop::LOOP_RUNNING
|
48
|
+
|
49
|
+
while @ctx.loop_status == StropheRuby::EventLoop::LOOP_RUNNING
|
50
|
+
#return if the socket is idle for more than timeout (in millisecond)
|
51
|
+
if StropheRuby::EventLoop.run_once(@ctx, timeout) != 0
|
52
|
+
@connection.disconnect
|
53
|
+
return false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
@connection.disconnect
|
58
|
+
return true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
if $0 == __FILE__
|
63
|
+
client = XmppClient.new
|
64
|
+
client.query_roster("a1@localhost", 'a1')
|
65
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yong-stropheruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Fran\xC3\xA7ois Lamontagne"
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- test/test_strophe_ruby.rb
|
75
75
|
- test/test_strophe_ruby_extn.rb
|
76
76
|
- ext/extconf.rb
|
77
|
+
- examples/xmpp_client.rb
|
77
78
|
has_rdoc: true
|
78
79
|
homepage:
|
79
80
|
post_install_message:
|