基于 EventMachine 和 Nokogiri 编写的 XMPP/Jabber 库和 DSL 为 Ruby 使用。
XMPP DSL (and more) for Ruby written on EventMachine and Nokogiri.
develop branch contains breaking changes scheduled for release in v3.0.0
gem install blatherBlather comes with a DSL that makes writing XMPP bots quick and easy. See the examples directory for more advanced examples.
require 'blather/client'
setup '[email protected]', '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
endThe above example is for a standalone script, and can be run as a command line program. If you wish to integrate Blather into an existing application, you will need to avoid blather/client and instead do something like this:
require 'blather/client/dsl'
module App
extend Blather::DSL
def self.run
EM.run { client.run }
end
setup '[email protected]', '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
end
trap(:INT) { EM.stop }
trap(:TERM) { EM.stop }
App.runIf you need to ensure that Blather does not block the rest of your application, run the reactor in a new thread:
Thread.new { App.run }You can additionally send messages like so:
App.say '[email protected]', 'Hello there!'Handlers let Blather know how you'd like each type of stanza to be well.. handled. Each type of stanza has an associated handler which is part of a handler hierarchy. In the example above we're handling message and subscription stanzas.
XMPP is built on top of three main stanza types (presence, message, and iq). All other stanzas are built on these three base types. This creates a natural hierarchy of handlers. For example a subscription stanza is a type of presence stanza and can be processed by a subscription handler or a presence handler. Likewise, a PubSub::Items stanza has its own identifier :pubsub_items but it's also a :pubsub_node, :iq and :staza. Any or each of these could be used to handle the PubSub::Items stanza. If you've done any DOM programming you'll be familiar with this.
Incoming stanzas will be handled by the first handler found. Unlike the DOM this will stop the handling bubble unless the handler returns false.
The entire handler hierarchy can be seen below.
Here we have a presence handler and a subscription handler. When this script receives a subscription stanza the subscription handler will be notified first. If that handler doesn't know what to do it can return false and let the stanza bubble up to the presence handler.
# Handle all presence stanzas
presence do |stanza|
# do stuff
end
# Handle all subscription stanzas
subscription do |stanza|
# do stuff
endAdditionally, handlers may be 'guarded'. That is, they may have conditions set declaratively, against which the stanza must match in order to trigger the handler.
# Will only be called for messages where #chat? responds positively
# and #body == 'exit'
message :chat?, :body => 'exit'So far there are two non-stanza related handlers.
# Called after the connection has been connected. It's good for initializing
# your system.
# DSL:
when_ready {}
# Client:
client.register_handler(:ready) {}
# Called after the connection has been terminated. Good for teardown or
# automatic reconnection.
# DSL:
disconnected {}
# Client
client.register_handler(:disconnected) {}
# The following will reconnect every time the connection is lost:
disconnected { client.connect }Guards are a concept borrowed from Erlang. They help to better compartmentalize handlers.
There are a number of guard types and one bit of special syntax. 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?, :bodyThe different types of guards are:
…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" }…Default usage is:
[blather_script] [options] [email protected]/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 versionSome parts of Blather will allow you to do stupid things that don't conform to XMPP spec. You should exercise caution and read the relevant specifications (indicated in the preamble to most relevant classes).
Blather provides support in one way or another for many XMPP specifications. Below is a list of specifications and the status of support for them in Blather. This list may not be correct. If the list indicates a lack of support for a specification you wish to use, you are encouraged to check that this is correct. Likewise, if you find an overstatement of Blather's spec compliance, please point this out. Also note that even without built-in support for a specification, you can still manually construct and parse stanzas alongside use of Blather's built-in helpers.
| Specification | Support | Name | Notes |
|---|---|---|---|
| RFC 6120 | Full | XMPP: Core | |
| RFC 6121 | Full | XMPP: Instant Messaging and Presence | |
| RFC 6122 | Full | XMPP: Address Format | |
| XEP-0001 | N/A | XMPP Extension Protocols | |
| XEP-0002 | N/A | Special Interest Groups (SIGs) | |
| XEP-0004 | Partial | Data Forms | |
| XEP-0009 | None | Jabber-RPC | |
| XEP-0012 | None | Last Activity | |
| XEP-0013 | None | Flexible Offline Message Retrieval | |
| XEP-0016 | None | Privacy Lists | |
| XEP-0019 | N/A | Streamlining the SIGs | |
| XEP-0020 | Partial | Feature Negotiation | |
| XEP-0027 | None | Current Jabber OpenPGP Usage | |
| XEP-0030 | Partial | Service Discovery | |
| XEP-0033 | None | Extended Stanza Addressing | |
| XEP-0045 | Partial | Multi-User Chat | |
| XEP-0047 | None | In-Band Bytestreams | |
| XEP-0048 | None | Bookmarks | |
| XEP-0049 | None | Private XML Storage | |
| XEP-0050 | Partial | Ad-Hoc Commands | |
| XEP-0053 | None | XMPP Registrar Function | |
| XEP-0054 | None | vcard-temp | |
| XEP-0055 | None | Jabber Search | |
| XEP-0059 | None | Result Set Management | |
| XEP-0060 | Partial | Publish-Subscribe | |
| XEP-0065 | None | SOCKS5 Bytestreams | |
| XEP-0066 | None | Out of Band Data | |
| XEP-0068 | None | Field Standardization for Data Forms | |
| XEP-0070 | None | Verifying HTTP Requests via XMPP | |
| XEP-0071 | Partial | XHTML-IM | |
| XEP-0072 | None | SOAP Over XMPP | |
| XEP-0076 | None | Malicious Stanzas | |
| XEP-0077 | Full | In-Band Registration | |
| XEP-0079 | None | Advanced Message Processing | |
| XEP-0080 | None | User Location | |
| XEP-0082 | None | XMPP Date and Time Profiles | |
| XEP-0083 | None | Nested Roster Groups | |
| XEP-0084 | None | User Avatar | |
| XEP-0085 | Partial | Chat State Notifications | |
| XEP-0092 | None | Software Version | |
| XEP-0095 | Partial | Stream Initiation | |
| XEP-0096 | Partial | SI File Transfer | |
| XEP-0100 | None | Gateway Interaction | |
| XEP-0106 | None | JID Escaping | |
| XEP-0107 | None | User Mood | |
| XEP-0108 | None | User Activity | |
| XEP-0114 | Full | Jabber Component Protocol | |
| XEP-0115 | Partial | Entity Capabilities | |
| XEP-0118 | None | User Tune | |
| XEP-0122 | None | Data Forms Validation | |
| XEP-0124 | None | Bidirectional-streams Over Synchronous HTTP (BOSH) | |
| XEP-0126 | None | Invisibility | |
| XEP-0127 | None | Common Alerting Protocol (CAP) Over XMPP | |
| XEP-0128 | None | Service Discovery Extensions | |
| XEP-0130 | None | Waiting Lists | |
| XEP-0131 | None | Stanza Headers and Internet Metadata | |
| XEP-0132 | None | Presence Obtained via Kinesthetic Excitation (POKE) | |
| XEP-0133 | None | Service Administration | |
| XEP-0134 | None | XMPP Design Guidelines | |
| XEP-0136 | None | Message Archiving | |
| XEP-0137 | None | Publishing Stream Initiation Requests | |
| [XEP-0 |
暂无开放 Issues,或尚未同步最近议题。