From a9537512cdccfa3eca594a2c0ac376242be734a1 Mon Sep 17 00:00:00 2001 From: Amolith Date: Thu, 16 Oct 2025 17:10:37 -0600 Subject: [PATCH] refactor: only show port-out cmd if tn eligible References: https://todo.sr.ht/~singpolyma/soprani.ca/380 Co-authored-by: Crush --- lib/bandwidth_tn_options.rb | 47 +++++++++++++++++++++++++++---------- sgx-bwmsgsv2.rb | 47 ++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/lib/bandwidth_tn_options.rb b/lib/bandwidth_tn_options.rb index 9602f40db53e646b1e554f216dcd527f4cd7eb5f..d893225133d7911c352c8cec2e486cae64d21dec 100644 --- a/lib/bandwidth_tn_options.rb +++ b/lib/bandwidth_tn_options.rb @@ -5,27 +5,50 @@ require "ruby-bandwidth-iris" Faraday.default_adapter = :em_synchrony class BandwidthTNOptions - def self.set_port_out_pin(user_id, token, secret, pin, tel) + def self.tn_eligible_for_port_out_pin?(creds) + user_id, token, secret, tel = creds + tel_local = tel.sub(/^\+1/, '') client = BandwidthIris::Client.new( account_id: user_id, username: token, password: secret ) + EMPromise.resolve(nil).then do + tn = BandwidthIris::Tn.get(client, tel_local) + details = tn.get_details() + details[:tier] == 0.0 && details[:on_net_vendor] == true + end + end + + def self.set_port_out_pin(creds, pin) + tn_eligible_for_port_out_pin?(creds).then do |eligible| + unless eligible + raise "TN not eligible for port-out PIN" + end - data = { - tn_option_groups: { - tn_option_group: [ - { - port_out_passcode: pin, - telephone_numbers: { - telephone_number: [tel] + user_id, token, secret, tel = creds + tel_local = tel.sub(/^\+1/, '') + + client = BandwidthIris::Client.new( + account_id: user_id, + username: token, + password: secret + ) + + + data = { + tn_option_groups: { + tn_option_group: [ + { + port_out_passcode: pin, + telephone_numbers: { + telephone_number: [tel] + } } - } - ] + ] + } } - } - EMPromise.resolve(nil).then do order = BandwidthIris::TnOptions.create_tn_option_order(client, data) order_id = order[:order_id] diff --git a/sgx-bwmsgsv2.rb b/sgx-bwmsgsv2.rb index fe35e809312b2e75dcc22acbbffa20cfcbe936f9..f5b243007e49472945637083e9027f456ae51e31 100755 --- a/sgx-bwmsgsv2.rb +++ b/sgx-bwmsgsv2.rb @@ -541,22 +541,26 @@ module SGXbwmsgsv2 to: Blather::JID.new(ARGV[0]), node: "http://jabber.org/protocol/commands" ) do |i| - # Ensure user is registered, but discard their credentials because we don't - # need them yet - fetch_catapult_cred_for(i.from).then { |_creds| - reply = i.reply - reply.node = 'http://jabber.org/protocol/commands' - - reply.items = [ - Blather::Stanza::DiscoItems::Item.new( - i.to, - 'set-port-out-pin', - 'Set Port-Out PIN' - ) - ] + fetch_catapult_cred_for(i.from).then { |creds| + BandwidthTNOptions.tn_eligible_for_port_out_pin?(creds).then { |eligible| + reply = i.reply + reply.node = 'http://jabber.org/protocol/commands' + + if eligible + reply.items = [ + Blather::Stanza::DiscoItems::Item.new( + i.to, + 'set-port-out-pin', + 'Set Port-Out PIN' + ) + ] + else + reply.items = [] + end - puts 'RESPONSE_CMD_DISCO: ' + reply.inspect - write_to_stream reply + puts 'RESPONSE_CMD_DISCO: ' + reply.inspect + write_to_stream reply + } }.catch { |e| if e.is_a?(Array) && [2, 3].include?(e.length) write_to_stream i.as_error(e[1], e[0], e[2]) @@ -753,8 +757,8 @@ module SGXbwmsgsv2 end command :execute?, node: "set-port-out-pin", sessionid: nil do |iq| - # Ensure user is registered, but discard their credentials because we don't - # need them yet + # Ensure user is registered, but discard their credentials + # because we're just showing them a form. fetch_catapult_cred_for(iq.from).then { |_creds| reply = iq.reply reply.node = 'set-port-out-pin' @@ -787,7 +791,7 @@ module SGXbwmsgsv2 if e.is_a?(Array) && [2, 3].include?(e.length) write_to_stream iq.as_error(e[1], e[0], e[2]) else - EMPromise.reject(e) + EMPromise.reject(e) end }.catch(&method(:panic)) end @@ -824,12 +828,7 @@ module SGXbwmsgsv2 end fetch_catapult_cred_for(iq.from).then { |creds| - user_id, token, secret, phone_num = creds - - # Stripping +1 like this feels janky, but Bandwidth only deals in +1 - # numbers and this is a Bandwidth SGX, so it's fine. - phone_num_local = phone_num.sub(/^\+1/, '') - BandwidthTNOptions.set_port_out_pin(user_id, token, secret, pin, phone_num_local).then { + BandwidthTNOptions.set_port_out_pin(creds, pin).then { reply = iq.reply reply.node = 'set-port-out-pin' reply.sessionid = iq.sessionid