553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
# File 'lib/flapjack/gateways/jabber.rb', line 553
def start
Flapjack.logger.debug("I will respond to the following identifiers: #{@identifiers.join(', ')}")
@lock.synchronize do
interpreter = self.siblings ? self.siblings.detect {|sib| sib.respond_to?(:interpret)} : nil
Flapjack.logger.info("starting")
Flapjack.logger.debug("new jabber pikelet with the following options: #{@config.inspect}")
jabber_id = @config['jabberid'] || 'flapjack'
jabber_id += '/' + @hostname unless jabber_id.include?('/')
flapjack_jid = ::Jabber::JID.new(jabber_id)
client = ::Jabber::Client.new(flapjack_jid)
client.on_exception do |exc, stream, loc|
leave_and_rejoin = nil
@lock.synchronize do
if exc
Flapjack.logger.error exc.class.name
Flapjack.logger.error ":#{loc.to_s}"
Flapjack.logger.error exc.message
Flapjack.logger.error exc.backtrace.join("\n")
end
leave_and_rejoin = @joined && !@should_quit
if leave_and_rejoin
@state_buffer << 'leave'
@stop_cond.signal
end
end
if leave_and_rejoin
sleep 3
@lock.synchronize do
unless @should_quit
@state_buffer << 'rejoin'
@stop_cond.signal
end
end
end
end
check_xml = Proc.new do |data|
if data.nil?
nil
else
Flapjack.logger.debug "xml_data: #{data}"
text = ''
begin
enc_name = Encoding.default_external.name
REXML::Document.new("<?xml version=\"1.0\" encoding=\"#{enc_name}\"?>" + data).
each_element_with_text do |elem|
text += elem.texts.join(" ")
end
text = data if text.empty? && !data.empty?
rescue REXML::ParseException
text = data.gsub(/<[^>]+>/, '').strip
end
text
end
end
client.add_message_callback do |m|
unless (m.type != :chat) || m.body.nil? || m.body.strip.empty?
Flapjack.logger.debug "received message #{m.inspect}"
nick = m.from
time = nil
m.each_element('x') { |x|
if x.kind_of?(::Jabber::Delay::XDelay)
time = x.stamp
end
}
unless interpreter.nil?
interpreter.receive_message(nil, nick, time, check_xml.call(m.body))
end
end
end
muc_clients = @config['rooms'].inject({}) do |memo, room|
muc_client = ::Jabber::MUC::SimpleMUCClient.new(client)
muc_client.on_message do |time, nick, text|
Flapjack.logger.debug("message #{text} -- #{time} -- #{nick}")
next if nick == jabber_id
identifier = identifiers.detect {|id| /^(?:@#{id}|#{id}:)\s*(.*)/m === check_xml.call(text) }
unless identifier.nil?
the_command = Regexp.last_match(1)
Flapjack.logger.debug("matched identifier: #{identifier}, command: #{the_command.inspect}")
if interpreter
interpreter.receive_message(room, nick, time, the_command)
end
end
end
memo[room] = muc_client
memo
end
attempts_allowed = 3
attempts_remaining = attempts_allowed
@joined = false
loop do
if @joined
@stop_cond.wait_until { @should_quit || !@state_buffer.empty? }
elsif attempts_remaining > 0
unless @should_quit || (attempts_remaining == attempts_allowed)
@stop_cond.wait(3)
end
unless @should_quit begin
attempts_remaining -= 1
_join(client, muc_clients)
@joined = true
rescue Errno::ECONNREFUSED, ::Jabber::JabberError => je
report_error("Couldn't join Jabber server #{@hostname}", je)
end
end
else
Flapjack.logger.error "stopping jabber bot, couldn't connect in #{attempts_allowed} attempts"
@should_quit = true
end
break if @should_quit
handle_state_change(client, muc_clients) unless @state_buffer.empty?
end
_leave(client, muc_clients) if client.is_connected?
end
end
|