Permalink
Browse files

ahn-xmpp implementation

  • Loading branch information...
1 parent 0765586 commit 256fb8f8dfc74dd803e1715d87d8f3456e155ee3 @juandebravo juandebravo committed Nov 27, 2011
View
2 .rspec
@@ -0,0 +1,2 @@
+--colour
+--format doc
View
456 LICENSE

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,59 @@
+ahn-xmpp
+========
+
+ahn-xmpp is an Adhearsion Plugin providing XMPP connectivity.
+
+Features
+--------
+
+* FIXME (list of features and unsolved problems)
+
+Requirements
+------------
+
+* Adhearsion 2.0+
+
+Install
+-------
+
+Add `ahn-xmpp` to your Adhearsion app's Gemfile.
+
+Configuration
+-------------
+
+In your Adhearsion app configuration file, add the following values:
+
+```ruby
+Adhearsion.config do |config|
+ config.ahn_xmpp.jid = "valid-jid"
+ config.ahn_xmpp.password = "valid-password"
+ config.ahn_xmpp.host = "valid-host"
+ config.ahn_xmpp.port = "valid-port"
+end
+```
+
+Author
+------
+
+Original author: [Juan de Bravo](https://github.com/juandebravo)
+
+Links
+-----
+* [Source](https://github.com/adhearsion/ahn-xmpp)
+* [Documentation](http://rdoc.info/github/adhearsion/ahn-xmpp/master/frames)
+* [Bug Tracker](https://github.com/adhearsion/ahn-xmpp/issues)
+
+Note on Patches/Pull Requests
+-----------------------------
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ * If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
+* Send me a pull request. Bonus points for topic branches.
+
+Copyright
+---------
+
+Check [License file](https://github.com/adhearsion/ahn-xmpp/blob/master/LICENSE)
View
@@ -1 +1,16 @@
require "bundler/gem_tasks"
+
+require "bundler/setup"
+
+
+task :default => :spec
+
+require 'rspec/core'
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rspec_opts = '--colour --format doc'
+end
+
+task :default => :spec
View
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
-require "ahn-xmpp/version"
+require "ahn_xmpp/version"
Gem::Specification.new do |s|
s.name = "ahn-xmpp"
- s.version = Ahn::Xmpp::VERSION
+ s.version = AhnXMPP::VERSION
s.authors = ["juandebravo", "Ben Langfeld", "Jason Goecke"]
s.email = ["juandebravo@gmail.com", "ben@langfeld.me", "jsgoecke@voxeo.com"]
s.homepage = ""
@@ -20,8 +20,8 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "activesupport", [">= 3.0.10"]
s.add_runtime_dependency "i18n", ">= 0.5.0"
+ s.add_runtime_dependency "blather", ">= 0.5.7"
s.add_development_dependency "rspec", ">= 2.7.0"
- s.add_development_dependency "flexmock"
s.add_development_dependency "rake", ">= 0.9.2"
end
View
@@ -1,7 +0,0 @@
-require "ahn-xmpp/version"
-
-module Ahn
- module Xmpp
- # Your code goes here...
- end
-end
View
@@ -1,5 +0,0 @@
-module Ahn
- module Xmpp
- VERSION = "0.0.1"
- end
-end
View
@@ -0,0 +1,18 @@
+require "adhearsion"
+require "active_support/dependencies/autoload"
+
+begin
+ require 'blather/client/client'
+ require 'blather/client/dsl'
+rescue LoadError
+ logger.fatal "XMPP support requires the \"blather\" gem."
+ # Silence the abort so we don't get an ugly backtrace
+ abort ""
+end
+
+require "ahn_xmpp/version"
+require "ahn_xmpp/plugin"
+
+module AhnXMPP
+
+end
View
@@ -0,0 +1,29 @@
+
+module AhnXMPP
+
+ ##
+ # Adhearsion Plugin that defines the XMPP configuration options
+ # and includes a hook to start the XMPP service in Adhearsion initialization process
+ class Plugin < Adhearsion::Plugin
+ extend ActiveSupport::Autoload
+
+ autoload :Service
+ autoload :Connection
+
+ # Default configuration for XMPP connection.
+ config :ahn_xmpp do
+ jid nil , :desc => "XMPP connection: client/component JID to connect to. Must be a string"
+ password nil , :desc => "XMPP connection: password identifier. Must be a string"
+ server "localhost", :desc => "XMPP connection: destination host. Must be a string"
+ port 5222 , :desc => "XMPP connection: destination port. Must be an integer"
+
+ end
+
+ # Include the XMPP service in plugins initialization process
+ init :ahn_xmpp do
+ Service.start
+ end
+
+ end
+
+end
@@ -0,0 +1,63 @@
+module AhnXMPP
+ class Plugin
+ module Connection
+
+ class << self
+
+ attr_accessor :client
+
+ ##
+ # Open the XMPP connection
+ #
+ # @param [String] jid the client/component JID to connect to
+ # @param [String] password
+ # @param [String] server
+ # @param [Integer] port
+ def start(jid, password, server, port)
+ Blather.logger = logger
+ setup_client_object(jid, password, server, port)
+ register_event_namespaces
+ register_default_client_handlers
+ Adhearsion::Events.register_callback(:after_initialized) do
+ connect
+ end
+ end
+
+ # Close the XMPP connection
+ def stop
+ shutdown
+ end
+
+ private
+
+ def setup_client_object(jid, password, server, port)
+ self.client = Blather::Client.setup(jid, password, server, port)
+ end
+
+ def connect
+ EventMachine.run {client.connect}
+ end
+
+ def register_event_namespaces
+ Adhearsion::Events.register_namespace_name "/xmpp"
+ end
+
+ def register_default_client_handlers
+ client.register_handler(:ready) do
+ logger.info "Connected to XMPP server! Send messages to #{client.jid.stripped}."
+ end
+
+ client.register_handler(:disconnected) do
+ if Adhearsion.status == :running
+ logger.warn "XMPP Disconnected. Reconnecting."
+ connect
+ end
+ # TODO: fix this to reconnect XMPP cleanly
+ end
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,36 @@
+module AhnXMPP
+ class Plugin
+ class Service
+ class << self
+
+ ##
+ # Start the XMPP connection
+ def start
+ raise "Must supply a jid argument to the XMPP configuration" if (config.jid.nil? || config.jid.empty?)
+ raise "Must supply a password argument to the XMPP configuration" if (config.password.nil? || config.password.empty?)
+ raise "Must supply a server argument to the XMPP configuration" if (config.server.nil? || config.server.empty?)
+ raise "Must supply a valid port to the XMPP configuration" unless config.port.is_a? Integer
+
+ Connection.extend Blather::DSL
+
+ Connection.start(config.jid, config.password, config.server, config.port)
+ end
+
+ ##
+ # Stop the XMPP connection
+ def stop
+ Connection.stop
+ end
+
+ private
+
+ ##
+ # Access to XMPP plugin configuration
+ def config
+ @config ||= Adhearsion.config[:ahn_xmpp]
+ end
+
+ end # class << self
+ end # Service
+ end # Plugin
+end # AhnXmpp
View
@@ -0,0 +1,4 @@
+
+module AhnXMPP
+ VERSION = "0.1.0"
+end
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe AhnXMPP::Plugin::Connection do
+
+ describe "while starting the connection" do
+
+ subject {AhnXMPP::Plugin::Connection}
+
+ let :blather_client do
+ client = double(Blather::Client)
+ client.should_receive(:register_handler).twice.and_return true
+ client
+ end
+
+ let :events do
+ events = double(Adhearsion::Events)
+ events.should_receive(:register_namespace_name).with "/xmpp"
+ events.should_receive(:register_callback).with(:after_initialized)
+ events
+ end
+
+ it "should setup the proper values" do
+ Blather::Client.should_receive(:setup).with("usera", "p", "localhost", 5222).and_return blather_client
+ Adhearsion::Events = events
+
+ subject.start "usera", "p", "localhost", 5222
+ end
+
+ end
+end
@@ -0,0 +1,51 @@
+require 'spec_helper'
+
+describe AhnXMPP::Plugin::Service do
+
+ describe "while initializing" do
+ after do
+ reset_ahn_xmpp_config
+ end
+
+ it "should raise an exception when no jid has been configured" do
+ lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a jid argument to the XMPP configuration"
+ end
+
+ it "should raise an exception when no password has been configured" do
+ Adhearsion.config[:ahn_xmpp].jid = "usera"
+ lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a password argument to the XMPP configuration"
+ end
+
+ it "should raise an exception when no server has been configured" do
+ Adhearsion.config[:ahn_xmpp].jid = "usera"
+ Adhearsion.config[:ahn_xmpp].password = "p"
+ Adhearsion.config[:ahn_xmpp].server = ""
+ lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a server argument to the XMPP configuration"
+ end
+
+ it "should raise an exception when an invalid port has been configured" do
+ Adhearsion.config[:ahn_xmpp].jid = "usera"
+ Adhearsion.config[:ahn_xmpp].password = "p"
+ Adhearsion.config[:ahn_xmpp].port = "52222"
+ lambda {Adhearsion::Plugin.load}.should raise_error "Must supply a valid port to the XMPP configuration"
+ end
+ end
+
+ describe "when starting the XMPP connection" do
+ before do
+ Adhearsion.config[:ahn_xmpp].jid = "usera"
+ Adhearsion.config[:ahn_xmpp].password = "p"
+ end
+
+ after do
+ reset_ahn_xmpp_config
+ end
+
+ it "should call Connection.start method with the valid parameters" do
+ AhnXMPP::Plugin::Connection.should_receive(:start).with("usera", "p", "localhost", 5222).and_return true
+ Adhearsion::Plugin.load
+ end
+
+ end
+
+end
Oops, something went wrong.

0 comments on commit 256fb8f

Please sign in to comment.