~singpolyma/sgx-jmp

ref: aa3117a59797562156d6a502c0fb4e310cef8367 sgx-jmp/lib/tel_selections.rb -rw-r--r-- 4.5 KiB
aa3117a5Stephen Paul Weber Merge branch 'whitelist' 7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# frozen_string_literal: true

require "ruby-bandwidth-iris"
Faraday.default_adapter = :em_synchrony

require_relative "form_template"

class TelSelections
	THIRTY_DAYS = 60 * 60 * 24 * 30

	def initialize(redis: REDIS)
		@redis = redis
	end

	def set(jid, tel)
		@redis.setex("pending_tel_for-#{jid}", THIRTY_DAYS, tel)
	end

	def delete(jid)
		@redis.del("pending_tel_for-#{jid}")
	end

	def [](jid)
		@redis.get("pending_tel_for-#{jid}").then do |tel|
			tel ? HaveTel.new(tel) : ChooseTel.new
		end
	end

	class HaveTel
		def initialize(tel)
			@tel = tel
		end

		def choose_tel
			EMPromise.resolve(@tel)
		end
	end

	class ChooseTel
		def choose_tel(error: nil)
			Command.reply { |reply|
				reply.allowed_actions = [:next]
				reply.command << FormTemplate.render("tn_search", error: error)
			}.then do |iq|
				choose_from_list(AvailableNumber.for(iq.form).tns)
			rescue StandardError
				choose_tel(error: $!.to_s)
			end
		end

		def choose_from_list(tns)
			if tns.empty?
				choose_tel(error: "No numbers found, try another search.")
			else
				Command.reply { |reply|
					reply.allowed_actions = [:next]
					reply.command << FormTemplate.render("tn_list", tns: tns)
				}.then { |iq| iq.form.field("tel").value.to_s.strip }
			end
		end

		class AvailableNumber
			def self.for(form)
				new(
					Q
					.for(form.field("q").value.to_s.strip).iris_query
					.merge(enableTNDetail: true)
					.merge(Quantity.for(form).iris_query)
				)
			end

			def initialize(iris_query)
				@iris_query = iris_query
			end

			def tns
				Command.log.debug("BandwidthIris::AvailableNumber.list", @iris_query)
				BandwidthIris::AvailableNumber.list(@iris_query).map(&Tn.method(:new))
			end

			class Quantity
				def self.for(form)
					rsm_max = form.find(
						"ns:set/ns:max",
						ns: "http://jabber.org/protocol/rsm"
					).first
					if rsm_max
						new(rsm_max.content.to_i)
					else
						Default.new
					end
				end

				def initialize(quantity)
					@quantity = quantity
				end

				def iris_query
					{ quantity: @quantity }
				end

				# NOTE: Gajim sends back the whole list on submit, so big
				# lists can cause issues
				class Default
					def iris_query
						{ quantity: 10 }
					end
				end
			end
		end

		class Tn
			attr_reader :tel

			def initialize(full_number:, city:, state:, **)
				@tel = "+1#{full_number}"
				@locality = city
				@region = state
			end

			def formatted_tel
				@tel =~ /\A\+1(\d{3})(\d{3})(\d+)\Z/
				"(#{$1}) #{$2}-#{$3}"
			end

			def option
				op = Blather::Stanza::X::Field::Option.new(value: tel, label: to_s)
				op << reference
				op
			end

			def reference
				Nokogiri::XML::Builder.new { |xml|
					xml.reference(
						xmlns: "urn:xmpp:reference:0",
						begin: 0,
						end: formatted_tel.length - 1,
						type: "data",
						uri: "tel:#{tel}"
					)
				}.doc.root
			end

			def to_s
				"#{formatted_tel} (#{@locality}, #{@region})"
			end
		end

		class Q
			def self.register(regex, &block)
				@queries ||= []
				@queries << [regex, block]
			end

			def self.for(q)
				@queries.each do |(regex, block)|
					match_data = (q =~ regex)
					return block.call($1 || $&, *$~.to_a[2..-1]) if match_data
				end

				raise "Format not recognized: #{q}"
			end

			def initialize(q)
				@q = q
			end

			{
				areaCode: [:AreaCode, /\A[2-9][0-9]{2}\Z/],
				npaNxx: [:NpaNxx, /\A(?:[2-9][0-9]{2}){2}\Z/],
				npaNxxx: [:NpaNxxx, /\A(?:[2-9][0-9]{2}){2}[0-9]\Z/],
				zip: [:PostalCode, /\A\d{5}(?:-\d{4})?\Z/],
				localVanity: [:LocalVanity, /\A~(.+)\Z/]
			}.each do |k, args|
				klass = const_set(
					args[0],
					Class.new(Q) {
						define_method(:iris_query) do
							{ k => @q }
						end
					}
				)

				args[1..-1].each do |regex|
					register(regex) { |q| klass.new(q) }
				end
			end

			class CityState
				Q.register(/\A([^,]+)\s*,\s*([A-Z]{2})\Z/, &method(:new))

				CITY_MAP = {
					"ajax" => "Ajax-Pickering",
					"kitchener" => "Kitchener-Waterloo",
					"new york" => "New York City",
					"pickering" => "Ajax-Pickering",
					"sault ste marie" => "sault sainte marie",
					"sault ste. marie" => "sault sainte marie",
					"south durham" => "Durham",
					"township of langley" => "Langley",
					"waterloo" => "Kitchener-Waterloo",
					"west durham" => "Durham"
				}.freeze

				STATE_MAP = {
					"QC" => "PQ"
				}.freeze

				def initialize(city, state)
					@city = CITY_MAP.fetch(city.downcase, city)
					@state = STATE_MAP.fetch(state, state)
				end

				def iris_query
					{ city: @city, state: @state }
				end
			end
		end
	end
end