~singpolyma/sgx-jmp

ref: d0f1728dfd86ae689b9e322306763d29275a682e sgx-jmp/sgx_jmp.rb -rw-r--r-- 3.4 KiB
d0f1728dStephen Paul Weber Initial commit 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true

require "blather/client"
require "dhall"

require_relative "em_promise"

CONFIG = Dhall::Coder.load(ARGV[0])

def node(name, parent, ns: nil)
	Niceogiri::XML::Node.new(
		name,
		parent.document,
		ns || parent.class.registered_ns
	)
end

def escape_jid(localpart)
	# TODO: proper XEP-0106 Sec 4.3, ie. pre-escaped
	localpart
		.to_s
		.gsub("\\", "\\\\5c")
		.gsub(" ", "\\\\20")
		.gsub("\"", "\\\\22")
		.gsub("&", "\\\\26")
		.gsub("'", "\\\\27")
		.gsub("/", "\\\\2f")
		.gsub(":", "\\\\3a")
		.gsub("<", "\\\\3c")
		.gsub(">", "\\\\3e")
		.gsub("@", "\\\\40")
end

def unescape_jid(localpart)
	localpart
		.to_s
		.gsub("\\20", " ")
		.gsub("\\22", "\"")
		.gsub("\\26", "&")
		.gsub("\\27", "'")
		.gsub("\\2f", "/")
		.gsub("\\3a", ":")
		.gsub("\\3c", "<")
		.gsub("\\3e", ">")
		.gsub("\\40", "@")
		.gsub("\\5c", "\\")
end

def proxy_jid(jid)
	Blather::JID.new(
		escape_jid(jid.stripped),
		CONFIG["component"]["jid"],
		jid.resource
	)
end

def unproxy_jid(jid)
	parsed = Blather::JID.new(unescape_jid(jid.node))
	Blather::JID.new(parsed.node, parsed.domain, jid.resource)
end

class IBR < Blather::Stanza::Iq::Query
	register :ibr, nil, "jabber:iq:register"

	def registered=(reg)
		query.at_xpath("./ns:registered", ns: self.class.registered_ns)&.remove
		query << node("registered", self) if reg
	end

	def registered?
		!!query.at_xpath("./ns:registered", ns: self.class.registered_ns)
	end

	[
		"instructions",
		"username",
		"nick",
		"password",
		"name",
		"first",
		"last",
		"last",
		"email",
		"address",
		"city",
		"state",
		"zip",
		"phone",
		"url",
		"date"
	].each do |tag|
		define_method("#{tag}=") do |v|
			query.at_xpath("./ns:#{tag}", ns: self.class.registered_ns)&.remove
			query << (i = node(tag, self))
			i.content = v
		end

		define_method(tag) do
			query.at_xpath("./ns:#{tag}", ns: self.class.registered_ns)&.content
		end
	end
end

Blather::DSL.append_features(self.class)

when_ready do
	EM.add_periodic_timer(3600) do
		ping = Blather::Stanza::Iq::Ping.new(:get, CONFIG["server"]["host"])
		ping.from = CONFIG["component"]["jid"]
		self << ping
	end
end

# workqueue_count MUST be 0 or else Blather uses threads!
setup(
	CONFIG["component"]["jid"],
	CONFIG["component"]["secret"],
	CONFIG["server"]["host"],
	CONFIG["server"]["port"],
	nil,
	nil,
	workqueue_count: 0
)

message :error? do |m|
	puts "MESSAGE ERROR: #{m.inspect}"
end

ibr :get? do |iq|
	fwd = iq.dup
	fwd.from = proxy_jid(iq.from)
	fwd.to = Blather::JID.new(nil, CONFIG["sgx"], iq.to.resource)
	fwd.id = "JMPGET%#{iq.id}"
	self << fwd
end

ibr :result? do |iq|
	if iq.id.start_with?("JMPGET")
		reply = iq.reply
		reply.instructions =
			"Please enter the phone number you wish to register with JMP.chat"
		reply.registered = iq.registered?
		reply.phone = iq.phone
	else
		reply = iq.dup
	end

	reply.id = iq.id.sub(/JMP[GS]ET%/, "")
	reply.from = Blather::JID.new(
		nil,
		CONFIG["component"]["jid"],
		iq.from.resource
	)
	reply.to = unproxy_jid(iq.to)
	self << reply
end

ibr :error? do |iq|
	reply = iq.dup
	reply.id = iq.id.sub(/JMP[GS]ET%/, "")
	reply.from = Blather::JID.new(
		nil,
		CONFIG["component"]["jid"],
		iq.from.resource
	)
	reply.to = unproxy_jid(iq.to)
	self << reply
end

ibr :set? do |iq|
	fwd = iq.dup
	CONFIG["creds"].each do |k, v|
		fwd.public_send("#{k}=", v)
	end
	fwd.from = proxy_jid(iq.from)
	fwd.to = Blather::JID.new(nil, CONFIG["sgx"], iq.to.resource)
	fwd.id = "JMPSET%#{iq.id}"
	self << fwd
end