proxied_jid.rb

 1# frozen_string_literal: true
 2
 3require "delegate"
 4require "blather"
 5
 6class ProxiedJID < SimpleDelegator
 7	ESCAPED = /20|22|26|27|2f|3a|3c|3e|40|5c/
 8	def unproxied
 9		Blather::JID.new(
10			node.gsub(/\\(#{ESCAPED})/) { |s|
11				s[1..-1].to_i(16).chr
12			}
13		)
14	end
15
16	def self.proxy(jid, suffix=CONFIG[:upstream_domain])
17		ProxiedJID.new(
18			Blather::JID.new(
19				jid.stripped.to_s
20					.gsub(/([ "&'\/:<>@]|\\(?=#{ESCAPED}))/) { |s|
21						"\\#{s.ord.to_s(16)}"
22					},
23				suffix,
24				jid.resource
25			)
26		)
27	end
28end