Class: DaemonKit::Jabber

Inherits:
Object show all
Defined in:
lib/daemon_kit/jabber.rb

Overview

Thin wrapper around xmpp4r-simple, specifically designed to ease configuration of a jabber daemon and provide some added simplicity.

Constant Summary collapse

@@instance =
nil
@@message_handler =
nil
@@presence_handler =
nil
@@subscription_handler =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJabber

Returns a new instance of Jabber.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/daemon_kit/jabber.rb', line 85

def initialize
  options = DaemonKit::Config.load( 'jabber' )

  @jabber_id  = options.delete("jabber_id")
  @password   = options.delete("password")
  @resource   = options.delete("resource") || 'daemon_kit'
  @masters    = options.delete("masters") || []
  @supporters = options.delete("supporters") || []

  raise ArgumentError if [ @jabber_id, @password ].any? { |a| a.nil? }
end

Instance Attribute Details

#connectionObject (readonly)

Jabber connection



9
10
11
# File 'lib/daemon_kit/jabber.rb', line 9

def connection
  @connection
end

Class Method Details

.deliver(jid, message) ⇒ Object

Deliver a message to the specified jid.



19
20
21
# File 'lib/daemon_kit/jabber.rb', line 19

def deliver( jid, message )
  instance.connection.deliver( jid, message )
end

.instanceObject

Use this instead of initializing, keeps it singleton



24
25
26
27
# File 'lib/daemon_kit/jabber.rb', line 24

def instance
  @instance ||= new
  @instance.startup!
end

.presence_updates(&block) ⇒ Object



75
76
77
# File 'lib/daemon_kit/jabber.rb', line 75

def presence_updates(&block)
  @presence_handler = block
end

.process_messagesObject



48
49
50
51
52
# File 'lib/daemon_kit/jabber.rb', line 48

def process_messages
  @message_handler ||= Proc.new { |m| DaemonKit.logger.info "Received message from #{m.from}: #{m.body}" }

  instance.valid_messages { |m| @message_handler.call(m) }
end

.process_subscriptionsObject



65
66
67
68
69
# File 'lib/daemon_kit/jabber.rb', line 65

def process_subscriptions
  @subscription_handler ||= Proc.new { |friend,presence| DaemonKit.logger.debug "Received presence update from #{friend}: #{presence}" }

  instance.connection.subscription_requests { |friend,presence| @subscription_handler.call(friend,presence) }
end

.process_updatesObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/daemon_kit/jabber.rb', line 54

def process_updates
  @presence_handler ||= Proc.new { |friend, old_presence, new_presence|
    DaemonKit.logger.debug "Received presence update: #{friend} went from #{old_presence} to #{new_presence}"
  }

  instance.connection.presence_updates { |friend, old_presence, new_presence|
    @presence_handler.call(friend, old_presence, new_presence)
  }

end

.received_messages(&block) ⇒ Object



71
72
73
# File 'lib/daemon_kit/jabber.rb', line 71

def received_messages(&block)
  @message_handler = block
end

.runObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/daemon_kit/jabber.rb', line 30

def run
  DaemonKit.logger.warn "Please use the new XMPP daemons, this class is deprecated"
  DaemonKit.logger.info "Starting jabber loop"

  loop do
    process_messages
    process_updates
    process_subscriptions

    begin
      sleep 1
    rescue Interrupt
      DaemonKit.logger.warn "Jabber loop interrupted"
      break
    end
  end
end

.subscription_requests(&block) ⇒ Object



79
80
81
# File 'lib/daemon_kit/jabber.rb', line 79

def subscription_requests(&block)
  @subscription_handler = block
end

Instance Method Details

#busy(&block) ⇒ Object



134
135
136
137
138
# File 'lib/daemon_kit/jabber.rb', line 134

def busy(&block)
  self.connection.status(:dnd, "Working...")
  yield
  self.connection.status(:chat, self.status_line )
end

#contactsObject



116
117
118
# File 'lib/daemon_kit/jabber.rb', line 116

def contacts
  @masters + @supporters
end

#shutdown!Object



111
112
113
114
# File 'lib/daemon_kit/jabber.rb', line 111

def shutdown!
  DaemonKit.logger.warn "Disconnecting jabber connection"
  self.connection.disconnect
end

#startup!Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/daemon_kit/jabber.rb', line 97

def startup!
  return self if @booted

  connect!
  setup_roster!

  DaemonKit.trap( 'INT', Proc.new { self.shutdown! } )
  DaemonKit.trap( 'TERM', Proc.new { self.shutdown! } )

  @booted = true

  self
end

#status_lineObject



140
141
142
# File 'lib/daemon_kit/jabber.rb', line 140

def status_line
  "#{DaemonKit.configuration.daemon_name} ready for instructions"
end

#valid_master?(jid) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/daemon_kit/jabber.rb', line 130

def valid_master?( jid )
  @masters.include?( jid.strip.to_s )
end

#valid_messages(&block) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/daemon_kit/jabber.rb', line 120

def valid_messages(&block)
  self.connection.received_messages.each do |message|
    next unless valid_master?( message.from )

    busy do
      block.call message
    end
  end
end