stropheruby
stropheruby copied to clipboard
Ruby bindings for Strophe, a C library for writing XMPP clients
= strophe_ruby
http://stropheruby.rubyforge.org
== DESCRIPTION:
Ruby bindings for Strophe 'http://code.stanziq.com/strophe/', a C library for writing XMPP clients. If all you need is a simple XMPP bot that react to message and presence notifications, you might be better off with XMPPBot 'http://github.com/flamontagne/xmppbot/', which is an implementation I wrote on top of StropheRuby.
Note that this gem is quite experimental and is not ready for production.
Strophe 'http://code.stanziq.com/strophe/' is a robust and well written C library that allows the developer to implement XMPP clients.
I wanted to be able to use the power of this library with the ruby syntax. I didn't use SWIG to generate the bindings for 2 reasons :
- I wanted to learn how to write a C extension for ruby
- I didn't like how SWIG generate gazilions of lines of code
My other project, XMPPBot, is an implementation of Strophe Ruby that allows the ruby developer to write a XMPP bot in a few lines of code.
== INSTALL:
gem install strophe_ruby
== CURRENT PROBLEMS:
- Currently no Support for TLS encryption
- Cannot terminate the loop with CTRL-C
- Socket disconnects after being inactive for too long
- Cannot output log data to a file
== EXAMPLE OF USE
require 'strophe_ruby'
def announce_presence stanza = StropheRuby::Stanza.new stanza.name = "presence" @conn.send(stanza) end
def register_callbacks #Accept all subscriptions @conn.add_handler("presence") do |pres| if pres.type == "subscribe" #we accept p = StropheRuby::Stanza.new p.name = "presence" p.type = "subscribed" p.set_attribute("to",pres.attribute("from")) @conn.send(p)
#then it's our turn to send our subscription request
p = StropheRuby::Stanza.new
p.name = "presence"
p.type = "subscribe"
p.set_attribute("to",pres.attribute("from"))
@conn.send(p)
end
end
#simple echo for messages
@conn.add_handler("message") do |msg|
if msg.child_by_name("body")
top_stanza = StropheRuby::Stanza.new
top_stanza.name="message"
top_stanza.type="chat"
top_stanza.set_attribute("to", msg.attribute("from"))
body_stanza = StropheRuby::Stanza.new
body_stanza.name="body"
text_stanza = StropheRuby::Stanza.new
text_stanza.text = msg.child_by_name("body").text
body_stanza.add_child(text_stanza)
top_stanza.add_child(body_stanza)
@conn.send(top_stanza)
end
end
end
#Prepare the strophe library StropheRuby::EventLoop.prepare
#create the runtime context and specify the logging level (WARN,INFO,ERROR or DEBUG) @ctx=StropheRuby::Context.new(StropheRuby::Logging::DEBUG)
#create the connection passing it the context @conn=StropheRuby::Connection.new(@ctx)
#set the jid and password @conn.jid = '[email protected]' @conn.password = 'secret'
#Try to connect @conn.connect do |status| if status == StropheRuby::ConnectionEvents::CONNECT #We are connected. announce_presence register_callbacks else @conn.disconnect end end
#Start the event loop StropheRuby::EventLoop.run(@ctx)
puts 'Disconnected'