Sha256: 95fc5a4dad16aab22e11ca528cbf9a4061fd717286b0abb63c873753cb6c5bcc

Contents?: true

Size: 1.82 KB

Versions: 4

Compression:

Stored size: 1.82 KB

Contents

require 'damagecontrol/publisher/base'
require 'jabber4r/jabber4r'


module DamageControl
  module Publisher
    class Jabber < Base
      register self

      ann :description => "DamageControl Jabber Id/Resource"
      attr_reader :id_resource

      ann :description => "DamageControl Jabber Password"
      attr_reader :password

      ann :description => "DamageControl's Friends"
      attr_reader :friends

      def initialize
        @id_resource = "damagecontrol@jabber.codehaus.org/damagecontrol"
        @friends = "aslak@jabber.codehaus.org"
      end
    
      def name
        "Jabber"
      end

      def publish(build)
        session = nil
        begin
          session = login
          message = nil
          if(build.successful?)
            message = "#{build.project.name}: #{build.status_message} build (by #{build.changeset.developer})"
          else
            message = "#{build.project.name}: #{build.changeset.developer} broke the build"
          end
          @friends.split(%r{,\s*}).each do |friend|
            begin
              session.new_message(friend).set_subject(message).set_body(message).send
            rescue Exception => e
              Log.error "Failed to send Jabber message to #{friend}"
            end
          end
        ensure
          session.release if session
        end
      end
      
    private

      # Logs in and tries to register (and log in again) if login fails
      def login(register_if_login_fails=true)
        begin
          session = ::Jabber::Session.bind(@id_resource, @password)
        rescue
          if(register_if_login_fails)
            if(::Jabber::Session.register(@id_resource, @password))
              login(false)
            else
              raise "Failed to register #{@id_resource}"
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
damagecontrol-0.5.0.1391 lib/damagecontrol/publisher/jabber.rb
damagecontrol-0.5.0.1392 lib/damagecontrol/publisher/jabber.rb
damagecontrol-0.5.0.1393 lib/damagecontrol/publisher/jabber.rb
damagecontrol-0.5.0.1404 lib/damagecontrol/publisher/jabber.rb