backend_sgx.rb

 1# frozen_string_literal: true
 2
 3class BackendSgx
 4	def initialize(customer_id, jid=CONFIG[:sgx], creds=CONFIG[:creds])
 5		@customer_id = customer_id
 6		@jid = jid
 7		@creds = creds
 8	end
 9
10	def register!(tel)
11		ibr = mkibr(:set)
12		ibr.nick = @creds[:account]
13		ibr.username = @creds[:username]
14		ibr.password = @creds[:password]
15		ibr.phone = tel
16		IQ_MANAGER.write(ibr)
17	end
18
19	def registered?
20		IQ_MANAGER.write(mkibr(:get)).catch { nil }.then do |result|
21			if result&.respond_to?(:registered?) && result&.registered?
22				result
23			else
24				false
25			end
26		end
27	end
28
29	def stanza(s)
30		s.dup.tap do |stanza|
31			stanza.to = stanza.to.with(domain: @jid)
32			stanza.from = from_jid.with(resource: stanza.from.resource)
33		end
34	end
35
36protected
37
38	def from_jid
39		Blather::JID.new(
40			"customer_#{@customer_id}",
41			CONFIG[:component][:jid]
42		)
43	end
44
45	def mkibr(type)
46		ibr = IBR.new(type, @jid)
47		ibr.from = from_jid
48		ibr
49	end
50end