Method: Flapjack::Gateways::Jabber::Bot#_join

Defined in:
lib/flapjack/gateways/jabber.rb

#_join(client, muc_clients, opts = {}) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
# File 'lib/flapjack/gateways/jabber.rb', line 751

def _join(client, muc_clients, opts = {})
  client.connect
  client.auth(@config['password'])
  client.send(::Jabber::Presence.new)
  muc_clients.each_pair do |room, muc_client|
    attempts_allowed = 3
    attempts_remaining = attempts_allowed
    joined = nil
    while !joined && (attempts_remaining > 0)
      @lock.synchronize do
        unless @should_quit || (attempts_remaining == attempts_allowed)
          # The only thing that should be interrupting this wait is
          # a pikelet.stop, which would set @should_quit to true;
          # thus we shouldn't see multiple connection attempts happening
          # too quickly.
          @stop_cond.wait(3)
        end
      end

      # may have changed during previous wait
      sq = nil
      @lock.synchronize do
        sq = @should_quit
      end

      unless sq
        attempts_remaining -= 1
        begin
          muc_client.join(room + '/' + @alias, nil, :history => false)
          t = Time.now
          msg = opts[:rejoin] ? "flapjack jabber gateway rejoining at #{t}, hello again!" :
                                "flapjack jabber gateway started at #{t}, hello! Try typing 'help'."
          muc_client.say(msg) if @config['chatbot_announce']
          joined = true
        rescue Errno::ECONNREFUSED, ::Jabber::JabberError => muc_je
          report_error("Couldn't join MUC room #{room}, #{attempts_remaining} attempts remaining", muc_je)
          raise if attempts_remaining <= 0
          joined = false
        end
      end
    end
  end
end