Class: Flapjack::Gateways::Oobetet::Bot

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/flapjack/gateways/oobetet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#hashify, #load_template, #local_timezone, #relative_time_ago, #remove_utc_offset, #stringify, #symbolize, #time_period_in_words, #truncate

Constructor Details

#initialize(opts = {}) ⇒ Bot

Returns a new instance of Bot.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/flapjack/gateways/oobetet.rb', line 207

def initialize(opts = {})
  @lock = opts[:lock]
  @stop_cond = opts[:stop_condition]
  @config = opts[:config]

  @hostname = Socket.gethostname

  unless @config['watched_check']
    raise RuntimeError, 'Flapjack::Oobetet: watched_check must be defined in the config'
  end
  @check_matcher = '"' + @config['watched_check'] + '"'

  Flapjack.logger.debug("new oobetet pikelet with the following options: #{@config.inspect}")
end

Instance Attribute Details

#siblingsObject

Returns the value of attribute siblings.



205
206
207
# File 'lib/flapjack/gateways/oobetet.rb', line 205

def siblings
  @siblings
end

Instance Method Details

#announce(msg) ⇒ Object

TODO buffer if not connected?



280
281
282
283
284
285
286
287
288
# File 'lib/flapjack/gateways/oobetet.rb', line 280

def announce(msg)
  @lock.synchronize do
    unless @muc_clients.empty?
      @muc_clients.each_pair do |room, muc_client|
        muc_client.say(msg)
      end
    end
  end
end

#startObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/flapjack/gateways/oobetet.rb', line 222

def start
  @lock.synchronize do
    @time_checker ||= @siblings && @siblings.detect {|sib| sib.respond_to?(:receive_status) }

    Flapjack.logger.info("starting")

    # ::Jabber::debug = true

    jabber_id = @config['jabberid'] || 'flapjack'

    @flapjack_jid = ::Jabber::JID.new(jabber_id + '/' + @hostname)
    @client = ::Jabber::Client.new(@flapjack_jid)

    @muc_clients = @config['rooms'].inject({}) do |memo, room|
      muc_client = ::Jabber::MUC::SimpleMUCClient.new(@client)
      memo[room] = muc_client
      memo
    end

    @client.connect
    @client.auth(@config['password'])
    @client.send(::Jabber::Presence.new.set_type(:available))

    @muc_clients.each_pair do |room, muc_client|
      muc_client.on_message do |time, nick, text|
        next if nick == jabber_id

        if @time_checker
          Flapjack.logger.debug("group message received: #{room}, #{text}")
          if (text =~ /^((?i:problem|recovery|acknowledgement)).*#{Regexp.escape(@check_matcher)}/)
            # got something interesting
            status = Regexp.last_match(1).downcase
            Flapjack.logger.debug("found the following state for #{@check_matcher}: #{status}")
            @time_checker.receive_status(status, time.to_i)
          end
        end
      end

      muc_client.join(room + '/' + @config['alias'])
      muc_client.say("flapjack oobetet gateway started at #{Time.now}, hello!")
    end

    # block this thread until signalled to quit
    @stop_cond.wait_until { @should_quit }

    @muc_clients.each_pair do |room, muc_client|
      muc_client.exit if muc_client.active?
    end

    @client.close
  end
end

#stop_typeObject



275
276
277
# File 'lib/flapjack/gateways/oobetet.rb', line 275

def stop_type
  :signal
end