public
Description: XMPP DSL for Ruby written on EventMachine and Nokogiri.
Homepage: http://sprsquish.github.com/blather
Clone URL: git://github.com/sprsquish/blather.git
Click here to lend your support to: blather and make a donation at www.pledgie.com !
name age message
file .autotest Tue May 12 13:41:29 -0700 2009 remove autotest discovery (it caused all sorts ... [sprsquish]
file .gitignore Thu Jun 04 11:35:21 -0700 2009 remove pubsub notes [sprsquish]
file CHANGELOG Fri Oct 30 11:11:02 -0700 2009 Set a priority for authentication mechanisms (d... [sprsquish]
file LICENSE Thu May 28 23:23:29 -0700 2009 update README to include contributors [sprsquish]
file README.rdoc Wed Oct 28 08:55:41 -0700 2009 Change DSL#write to DSL#write_to_stream so it d... [sprsquish]
file Rakefile Mon Oct 12 08:29:36 -0700 2009 add gemcutter to the Rakefile [sprsquish]
file VERSION.yml Fri Oct 30 11:11:17 -0700 2009 Version bump to 0.4.6 [sprsquish]
file blather.gemspec Fri Oct 30 19:11:09 -0700 2009 update gemspec [sprsquish]
directory examples/ Wed Oct 28 08:55:41 -0700 2009 Change DSL#write to DSL#write_to_stream so it d... [sprsquish]
directory lib/ Fri Nov 06 21:19:10 -0800 2009 update push parser to work with nokogiri [sprsquish]
directory spec/ Wed Oct 28 08:55:41 -0700 2009 Change DSL#write to DSL#write_to_stream so it d... [sprsquish]
README.rdoc

Blather

XMPP DSL (and more) for Ruby written on EventMachine and Nokogiri.

Features

  • evented architecture
  • uses Nokogiri
  • simplified starting point

Project Pages

Blather:sprsquish.github.com/blather
GitHub:github.com/sprsquish/blather
GitHub Docs:docs.github.com/sprsquish/blather
RubyForge:rubyforge.org/projects/squishtech
RDocs:squishtech.rubyforge.org/blather
Google Group:groups.google.com/group/xmpp-blather

Usage

Installation

  sudo gem install blather

Example

See the examples directory for more advanced examples.

This will auto-accept any subscription requests and echo back any chat messages.

  require 'rubygems'
  require 'blather/client'

  setup 'echo@jabber.local', 'echo'

  # Auto approve subscription requests
  subscription :request? do |s|
    write_to_stream s.approve!
  end

  # Echo back what was said
  message :chat?, :body do |m|
    write_to_stream m.reply
  end

Handlers

Setup handlers by calling their names as methods.

  # Will only be called for messages where #chat? responds positively
  # and #body == 'exit'
  message :chat?, :body => 'exit'

Non-Stanza Handlers

So far there are two non-stanza related handlers.

  when_ready (or handle(:ready) {})
    Called after the connection has been connected. It's good for initializing your system.

  disconnected (or handle(:disconnected) {})
    Called after the connection has been terminated. Good for teardown or automatic reconnection.
    The following will reconnect every time the connection is lost:
    disconnected { client.connect }

Handler Guards

Guards act like AND statements. Each condition must be met if the handler is to be used.

  # Equivalent to saying (stanza.chat? && stanza.body)
  message :chat?, :body

The different types of guards are:

  # Symbol
  #   Checks for a non-false reply to calling the symbol on the stanza
  #   Equivalent to stanza.chat?
  message :chat?

  # Hash with any value (:body => 'exit')
  #   Calls the key on the stanza and checks for equality
  #   Equivalent to stanza.body == 'exit'
  message :body => 'exit'

  # Hash with regular expression (:body => /exit/)
  #   Calls the key on the stanza and checks for a match
  #   Equivalent to stanza.body.match /exit/
  message :body => /exit/

  # Hash with array (:name => [:gone, :forbidden])
  #   Calls the key on the stanza and check for inclusion in the array
  #   Equivalent to [:gone, :forbidden].include?(stanza.name)
  stanza_error :name => [:gone, :fobidden]

  # Proc
  #   Calls the proc passing in the stanza
  #   Checks that the ID is modulo 3
  message proc { |m| m.id % 3 == 0 }

  # Array
  #   Use arrays with the previous types effectively turns the guard into
  #   an OR statement.
  #   Equivalent to stanza.body == 'foo' || stanza.body == 'baz'
  message [{:body => 'foo'}, {:body => 'baz'}]

  # XPath
  #   Runs the xpath query on the stanza and checks for results
  #   This guard type cannot be combined with other guards
  #   Equivalent to !stanza.find('/iq/ns:pubsub', :ns => 'pubsub:namespace').empty?
  iq '/iq/ns:pubsub', :ns => 'pubsub:namespace'

Filters

Blather provides before and after filters that work much the way regular handlers work. Filters come in a before and after flavor. They’re called in order of definition and can be guarded like handlers.

  before { |s| "I'm run before any handler" }
  before { |s| "I'm run next" }

  before(:message) { |s| "I'm only run in front of message stanzas" }
  before(nil, :id => 1) { |s| "I'll only be run when the stanza's ID == 1" }

  # ... handlers

  after { |s| "I'm run after everything" }

On the Command Line:

Default usage is:

  [blather_script] [options] node@domain.com/resource password [host] [port]

Command line options:

  -D, --debug                      Run in debug mode (you will see all XMPP communication)
  -d, --daemonize                  Daemonize the process
      --pid=[PID]                  Write the PID to this file
      --log=[LOG]                  Write to the [LOG] file instead of stdout/stderr
  -h, --help                       Show this message
  -v, --version                    Show version

TODO

  • Add Disco the the DSL
  • More examples (Re-write XMPP4R examples into Blather)

Author

Jeff Smick <sprsquish@gmail.com>

Contributors

Nolan Darilek <nolan@thewordnerd.info>

License

Blather

Copyright © 2009 Jeff Smick

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.