class DaemonKit::XMPP

Thin wrapper around the blather DSL

Public Class Methods

new() click to toggle source
# File lib/daemon_kit/xmpp.rb, line 29
def initialize
  @config = DaemonKit::Config.load('xmpp')

  if @config.enable_logging
    Blather.logger = DaemonKit.logger
  end

  jid = if @config.resource
    "#{@config.jabber_id}/#{@config.resource}"
  else
    @config.jabber_id
  end

  # host & port allow nil, defaults to the jabber id host and default port
  # so if those keys are not present in the config, its ok.
  setup jid, @config.password, @config.host, @config.port

  when_ready do
    configure_roster!
    become_available
  end

  return if @config['require_master'] == false

  message do |m|
    trusted?( m ) ? pass : halt
  end
end
run( &block ) click to toggle source
# File lib/daemon_kit/xmpp.rb, line 12
def run( &block )
  if Blather::VERSION < '0.8.0'
    DaemonKit.logger.warn "Blather version to old, please upgrade to 0.8.x"
  end

  DaemonKit.trap('INT') { ::EM.stop }
  DaemonKit.trap('TERM') { ::EM.stop }

  DaemonKit::EM.run {

    xmpp = new
    xmpp.instance_eval( &block )
    xmpp.run
  }
end

Public Instance Methods

become_available() click to toggle source
# File lib/daemon_kit/xmpp.rb, line 82
def become_available
  set_status( :chat, "#{DaemonKit.configuration.daemon_name} is available" )
end
busy( message = nil, &block ) click to toggle source
# File lib/daemon_kit/xmpp.rb, line 102
def busy( message = nil, &block )
  set_status( :dnd, message )

  block.call

  become_available
end
configure_roster!() click to toggle source
# File lib/daemon_kit/xmpp.rb, line 58
def configure_roster!
  DaemonKit.logger.debug 'Configuring roster'

  my_roster.each do |item|
    unless valid_contact?( item.jid )
      DaemonKit.logger.debug "Removing #{item.jid} from roster"

      my_roster.delete( item.jid )
      next
    end
  end

  contacts.each do |jid|
    DaemonKit.logger.debug "Adding #{jid} to roster"

    my_roster.add( Blather::JID.new( jid ) )
  end

  my_roster.each do |item|
    item.subscription = :both
    item.ask = :subscribe
  end
end
contacts() click to toggle source
# File lib/daemon_kit/xmpp.rb, line 90
def contacts
  @config.masters | ( @config.supporters || [] )
end
run() click to toggle source
# File lib/daemon_kit/xmpp.rb, line 98
def run
  client.run
end
trusted?( message ) click to toggle source
# File lib/daemon_kit/xmpp.rb, line 86
def trusted?( message )
  @config.masters.include?( message.from.stripped.to_s )
end
valid_contact?( jid ) click to toggle source
# File lib/daemon_kit/xmpp.rb, line 94
def valid_contact?( jid )
  contacts.include?( jid.stripped.to_s )
end