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/.freeze
 8
 9	def unproxied
10		Blather::JID.new(
11			node.gsub(/\\(#{ESCAPED})/) { |s|
12				s[1..-1].to_i(16).chr
13			}
14		)
15	end
16
17	def self.proxy(jid, suffix=CONFIG[:upstream_domain])
18		ProxiedJID.new(
19			Blather::JID.new(
20				jid.stripped.to_s
21					.gsub(/([ "&'\/:<>@]|\\(?=#{ESCAPED}))/) { |s|
22						"\\#{s.ord.to_s(16)}"
23					},
24				suffix,
25				jid.resource
26			)
27		)
28	end
29end