blather_notify.rb

 1# frozen_string_literal: true
 2
 3require "blather/client/dsl"
 4require "timeout"
 5
 6module BlatherNotify
 7	extend Blather::DSL
 8
 9	@ready = Queue.new
10
11	when_ready { @ready << :ready }
12
13	def self.start(jid, password)
14		# workqueue_count MUST be 0 or else Blather uses threads!
15		setup(jid, password, nil, nil, nil, nil, workqueue_count: 0)
16
17		EM.error_handler(&method(:panic))
18
19		@thread = Thread.new {
20			EM.run do
21				client.run
22			end
23		}
24
25		Timeout.timeout(30) { @ready.pop }
26		at_exit { wait_then_exit }
27	end
28
29	def self.panic(e)
30		warn e.message
31		exit 2
32	end
33
34	def self.wait_then_exit
35		disconnected { EM.stop }
36		EM.add_timer(30) { EM.stop }
37		shutdown
38		@thread.join
39	end
40end