strophe_ruby

stropheruby.rubyforge.org

DESCRIPTION:

Ruby bindings for Strophe ‘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, which is an implementation I wrote on top of StropheRuby.

IMPORTANT : This gem is quite experimental currently… it is not ready for production!

Strophe ‘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 :

  1. I wanted to learn how to write a C extension for ruby

  2. 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.

CURRENT PROBLEMS:

  • Currently no Support for TLS encryption (Coming soon as it’s rather important)

  • Cannot terminate the loop with CTRL-C

  • Socket disconnects after being inactive for too long

  • Cannot output log data in 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’

INSTALL:

gem install strophe_ruby

LICENSE:

Copyright © 2008 François Lamontagne

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.