sgx_jmp.rb

  1# frozen_string_literal: true
  2
  3require "pg/em/connection_pool"
  4require "bandwidth"
  5require "bigdecimal"
  6require "blather/client/dsl" # Require this first to not auto-include
  7require "blather/client"
  8require "braintree"
  9require "date"
 10require "dhall"
 11require "em-hiredis"
 12require "em_promise"
 13require "ougai"
 14require "ruby-bandwidth-iris"
 15require "sentry-ruby"
 16require "statsd-instrument"
 17
 18$stdout.sync = true
 19LOG = Ougai::Logger.new($stdout)
 20LOG.level = ENV.fetch("LOG_LEVEL", "info")
 21LOG.formatter = Ougai::Formatters::Readable.new(
 22	nil,
 23	nil,
 24	plain: !$stdout.isatty
 25)
 26Blather.logger = LOG
 27EM::Hiredis.logger = LOG
 28StatsD.logger = LOG
 29LOG.info "Starting"
 30
 31Sentry.init do |config|
 32	config.logger = LOG
 33	config.breadcrumbs_logger = [:sentry_logger]
 34end
 35
 36module SentryOugai
 37	class SentryLogger
 38		include Sentry::Breadcrumb::SentryLogger
 39		include Singleton
 40	end
 41
 42	def _log(severity, message=nil, ex=nil, data=nil, &block)
 43		super
 44		SentryLogger.instance.add_breadcrumb(severity, message || ex.to_s, &block)
 45	end
 46end
 47LOG.extend SentryOugai
 48
 49CONFIG =
 50	Dhall::Coder
 51	.new(safe: Dhall::Coder::JSON_LIKE + [Symbol, Proc])
 52	.load(
 53		"(#{ARGV[0]}) : #{__dir__}/config-schema.dhall",
 54		transform_keys: ->(k) { k&.to_sym }
 55	)
 56WEB_LISTEN =
 57	if CONFIG[:web].is_a?(Hash)
 58		[CONFIG[:web][:interface], CONFIG[:web][:port]]
 59	else
 60		[CONFIG[:web]]
 61	end
 62
 63singleton_class.class_eval do
 64	include Blather::DSL
 65	Blather::DSL.append_features(self)
 66end
 67
 68require_relative "lib/polyfill"
 69require_relative "lib/alt_top_up_form"
 70require_relative "lib/add_bitcoin_address"
 71require_relative "lib/backend_sgx"
 72require_relative "lib/bwmsgsv2_repo"
 73require_relative "lib/bandwidth_tn_order"
 74require_relative "lib/btc_sell_prices"
 75require_relative "lib/buy_account_credit_form"
 76require_relative "lib/command"
 77require_relative "lib/command_list"
 78require_relative "lib/customer"
 79require_relative "lib/customer_info_form"
 80require_relative "lib/customer_repo"
 81require_relative "lib/electrum"
 82require_relative "lib/expiring_lock"
 83require_relative "lib/em"
 84require_relative "lib/form_to_h"
 85require_relative "lib/low_balance"
 86require_relative "lib/port_in_order"
 87require_relative "lib/payment_methods"
 88require_relative "lib/paypal_done"
 89require_relative "lib/registration"
 90require_relative "lib/transaction"
 91require_relative "lib/tel_selections"
 92require_relative "lib/session_manager"
 93require_relative "lib/statsd"
 94require_relative "web"
 95
 96ELECTRUM = Electrum.new(**CONFIG[:electrum])
 97EM::Hiredis::Client.load_scripts_from("./redis_lua")
 98
 99Faraday.default_adapter = :em_synchrony
100BandwidthIris::Client.global_options = {
101	account_id: CONFIG[:creds][:account],
102	username: CONFIG[:creds][:username],
103	password: CONFIG[:creds][:password]
104}
105BANDWIDTH_VOICE = Bandwidth::Client.new(
106	voice_basic_auth_user_name: CONFIG[:creds][:username],
107	voice_basic_auth_password: CONFIG[:creds][:password]
108).voice_client.client
109
110def new_sentry_hub(stanza, name: nil)
111	hub = Sentry.get_current_hub&.new_from_top
112	raise "Sentry.init has not been called" unless hub
113
114	hub.push_scope
115	hub.current_scope.clear_breadcrumbs
116	hub.current_scope.set_transaction_name(name) if name
117	hub.current_scope.set_user(jid: stanza.from.stripped.to_s)
118	hub
119end
120
121class AuthError < StandardError; end
122
123# Braintree is not async, so wrap in EM.defer for now
124class AsyncBraintree
125	def initialize(environment:, merchant_id:, public_key:, private_key:, **)
126		@gateway = Braintree::Gateway.new(
127			environment: environment,
128			merchant_id: merchant_id,
129			public_key: public_key,
130			private_key: private_key
131		)
132		@gateway.config.logger = LOG
133	end
134
135	def respond_to_missing?(m, *)
136		@gateway.respond_to?(m)
137	end
138
139	def method_missing(m, *args)
140		return super unless respond_to_missing?(m, *args)
141
142		EM.promise_defer(klass: PromiseChain) do
143			@gateway.public_send(m, *args)
144		end
145	end
146
147	class PromiseChain < EMPromise
148		def respond_to_missing?(*)
149			false # We don't actually know what we respond to...
150		end
151
152		def method_missing(m, *args)
153			return super if respond_to_missing?(m, *args)
154			self.then { |o| o.public_send(m, *args) }
155		end
156	end
157end
158
159BRAINTREE = AsyncBraintree.new(**CONFIG[:braintree])
160
161def panic(e, hub=nil)
162	(Thread.current[:log] || LOG).fatal(
163		"Error raised during event loop: #{e.class}",
164		e
165	)
166	if e.is_a?(::Exception)
167		(hub || Sentry).capture_exception(e, hint: { background: false })
168	else
169		(hub || Sentry).capture_message(e.to_s, hint: { background: false })
170	end
171	exit 1
172end
173
174EM.error_handler(&method(:panic))
175
176def poll_for_notify(db)
177	db.wait_for_notify_defer.then { |notify|
178		CustomerRepo.new.find(notify[:extra])
179	}.then(&LowBalance.method(:for)).then(&:notify!).then {
180		poll_for_notify(db)
181	}.catch(&method(:panic))
182end
183
184when_ready do
185	LOG.info "Ready"
186	BLATHER = self
187	REDIS = EM::Hiredis.connect
188	TEL_SELECTIONS = TelSelections.new
189	BTC_SELL_PRICES = BTCSellPrices.new(REDIS, CONFIG[:oxr_app_id])
190	DB = PG::EM::ConnectionPool.new(dbname: "jmp") do |conn|
191		conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn)
192		conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn)
193	end
194
195	DB.hold do |conn|
196		conn.query("LISTEN low_balance")
197		conn.query("SELECT customer_id FROM balances WHERE balance < 5").each do |c|
198			conn.query("SELECT pg_notify('low_balance', $1)", c.values)
199		end
200		poll_for_notify(conn)
201	end
202
203	EM.add_periodic_timer(3600) do
204		ping = Blather::Stanza::Iq::Ping.new(:get, CONFIG[:server][:host])
205		ping.from = CONFIG[:component][:jid]
206		self << ping
207	end
208
209	Web.run(LOG.child, *WEB_LISTEN)
210end
211
212# workqueue_count MUST be 0 or else Blather uses threads!
213setup(
214	CONFIG[:component][:jid],
215	CONFIG[:component][:secret],
216	CONFIG[:server][:host],
217	CONFIG[:server][:port],
218	nil,
219	nil,
220	workqueue_count: 0
221)
222
223message to: /\Aaccount@/, body: /./ do |m|
224	StatsD.increment("deprecated_account_bot")
225
226	self << m.reply.tap do |out|
227		out.body = "This bot is deprecated. Please talk to xmpp:cheogram.com"
228	end
229end
230
231before(
232	:iq,
233	type: [:error, :result],
234	to: /\Acustomer_/,
235	from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/
236) { |iq| halt if IQ_MANAGER.fulfill(iq) }
237
238before nil, to: /\Acustomer_/, from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/ do |s|
239	StatsD.increment("stanza_customer")
240
241	sentry_hub = new_sentry_hub(s, name: "stanza_customer")
242	CustomerRepo.new.find(
243		s.to.node.delete_prefix("customer_")
244	).then { |customer|
245		sentry_hub.current_scope.set_user(
246			id: customer.customer_id,
247			jid: s.from.stripped.to_s
248		)
249		customer.stanza_to(s)
250	}.catch { |e| panic(e, sentry_hub) }
251	halt
252end
253
254ADDRESSES_NS = "http://jabber.org/protocol/address"
255message(
256	to: /\A#{CONFIG[:component][:jid]}\Z/,
257	from: /(\A|@)#{CONFIG[:sgx]}(\/|\Z)/
258) do |m|
259	StatsD.increment("inbound_group_text")
260	sentry_hub = new_sentry_hub(m, name: "message")
261
262	address = m.find("ns:addresses", ns: ADDRESSES_NS).first
263		&.find("ns:address", ns: ADDRESSES_NS)
264		&.find { |el| el["jid"].to_s.start_with?("customer_") }
265	pass unless address
266
267	CustomerRepo.new.find_by_jid(address["jid"]).then { |customer|
268		m.from = m.from.with(domain: CONFIG[:component][:jid])
269		m.to = m.to.with(domain: customer.jid.domain)
270		address["jid"] = customer.jid.to_s
271		BLATHER << m
272	}.catch { |e| panic(e, sentry_hub) }
273end
274
275# Ignore groupchat messages
276# Especially if we have the component join MUC for notifications
277message(type: :groupchat) { true }
278
279def billable_message(m)
280	(m.body && !m.body.empty?) || m.find("ns:x", ns: OOB.registered_ns).first
281end
282
283message do |m|
284	StatsD.increment("message")
285
286	sentry_hub = new_sentry_hub(m, name: "message")
287	today = Time.now.utc.to_date
288	CustomerRepo.new.find_by_jid(m.from.stripped).then { |customer|
289		sentry_hub.current_scope.set_user(
290			id: customer.customer_id, jid: m.from.stripped.to_s
291		)
292		EMPromise.all([
293			(customer.incr_message_usage if billable_message(m)),
294			customer.stanza_from(m)
295		]).then { customer }
296	}.then { |customer|
297		customer.message_usage((today..(today - 30))).then do |usage|
298			next unless usage > 500
299
300			ExpiringLock.new("jmp_usage_notify-#{customer.customer_id}").with do
301				BLATHER.join(CONFIG[:notify_admin], "sgx-jmp")
302				BLATHER.say(
303					CONFIG[:notify_admin],
304					"#{customer.customer_id} has used #{usage} messages since #{today - 30}",
305					:groupchat
306				)
307			end
308		end
309	}.catch { |e| panic(e, sentry_hub) }
310end
311
312message :error? do |m|
313	StatsD.increment("message_error")
314
315	LOG.error "MESSAGE ERROR", stanza: m
316end
317
318IQ_MANAGER = SessionManager.new(self, :id)
319COMMAND_MANAGER = SessionManager.new(
320	self,
321	:sessionid,
322	timeout: 60 * 60,
323	error_if: ->(s) { s.cancel? }
324)
325
326disco_info to: Blather::JID.new(CONFIG[:component][:jid]) do |iq|
327	reply = iq.reply
328	reply.identities = [{
329		name: "JMP.chat",
330		type: "sms",
331		category: "gateway"
332	}]
333	reply.features = [
334		"http://jabber.org/protocol/disco#info",
335		"http://jabber.org/protocol/commands"
336	]
337	form = Blather::Stanza::X.find_or_create(reply.query)
338	form.type = "result"
339	form.fields = [
340		{
341			var: "FORM_TYPE",
342			type: "hidden",
343			value: "http://jabber.org/network/serverinfo"
344		}
345	] + CONFIG[:xep0157]
346	self << reply
347end
348
349disco_info do |iq|
350	reply = iq.reply
351	reply.identities = [{
352		name: "JMP.chat",
353		type: "sms",
354		category: "client"
355	}]
356	reply.features = [
357		"urn:xmpp:receipts"
358	]
359	self << reply
360end
361
362disco_items node: "http://jabber.org/protocol/commands" do |iq|
363	StatsD.increment("command_list")
364
365	sentry_hub = new_sentry_hub(iq, name: iq.node)
366	reply = iq.reply
367	reply.node = "http://jabber.org/protocol/commands"
368
369	CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new).find_by_jid(
370		iq.from.stripped
371	).catch {
372		nil
373	}.then { |customer|
374		CommandList.for(customer)
375	}.then { |list|
376		reply.items = list.map do |item|
377			Blather::Stanza::DiscoItems::Item.new(
378				iq.to,
379				item[:node],
380				item[:name]
381			)
382		end
383		self << reply
384	}.catch { |e| panic(e, sentry_hub) }
385end
386
387iq "/iq/ns:services", ns: "urn:xmpp:extdisco:2" do |iq|
388	StatsD.increment("extdisco")
389
390	reply = iq.reply
391	reply << Nokogiri::XML::Builder.new {
392		services(xmlns: "urn:xmpp:extdisco:2") do
393			service(
394				type: "sip",
395				host: CONFIG[:sip_host]
396			)
397		end
398	}.doc.root
399
400	self << reply
401end
402
403Command.new(
404	"jabber:iq:register",
405	"Register",
406	list_for: ->(*) { true },
407	customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
408) {
409	Command.customer.catch {
410		Sentry.add_breadcrumb(Sentry::Breadcrumb.new(message: "Customer.create"))
411		Command.execution.customer_repo.create(Command.execution.iq.from.stripped)
412	}.then { |customer|
413		Sentry.add_breadcrumb(Sentry::Breadcrumb.new(message: "Registration.for"))
414		Registration.for(customer, TEL_SELECTIONS).then(&:write)
415	}.then {
416		StatsD.increment("registration.completed")
417	}.catch_only(Command::Execution::FinalStanza) do |e|
418		StatsD.increment("registration.completed")
419		EMPromise.reject(e)
420	end
421}.register(self).then(&CommandList.method(:register))
422
423Command.new(
424	"info",
425	"Show Account Info",
426	list_for: ->(*) { true },
427	customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
428) {
429	Command.customer.then(&:info).then do |info|
430		Command.finish do |reply|
431			form = Blather::Stanza::X.new(:result)
432			form.title = "Account Info"
433			form.fields = info.fields
434			reply.command << form
435		end
436	end
437}.register(self).then(&CommandList.method(:register))
438
439Command.new(
440	"usage",
441	"Show Monthly Usage"
442) {
443	report_for = (Date.today..(Date.today << 1))
444
445	Command.customer.then { |customer|
446		customer.usage_report(report_for)
447	}.then do |usage_report|
448		Command.finish do |reply|
449			reply.command << usage_report.form
450		end
451	end
452}.register(self).then(&CommandList.method(:register))
453
454# Commands that just pass through to the SGX
455{
456	"configure-calls" => ["Configure Calls"]
457}.each do |node, args|
458	Command.new(node, *args) {
459		Command.customer.then do |customer|
460			customer.stanza_from(Command.execution.iq)
461		end
462	}.register(self, guards: [node: node]).then(&CommandList.method(:register))
463end
464
465Command.new(
466	"ogm",
467	"Record Voicemail Greeting",
468	list_for: ->(fwd: nil, **) { !!fwd },
469	customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
470) {
471	Command.customer.then do |customer|
472		BANDWIDTH_VOICE.create_call(
473			CONFIG[:creds][:account],
474			body: customer.fwd.create_call_request do |cc|
475				cc.from = customer.registered?.phone
476				cc.application_id = CONFIG[:bandwidth_app]
477				cc.answer_url = "#{CONFIG[:web_root]}/ogm/start?" \
478				                "customer_id=#{customer.customer_id}"
479			end
480		)
481		Command.finish("You will now receive a call.")
482	end
483}.register(self).then(&CommandList.method(:register))
484
485Command.new(
486	"migrate billing",
487	"Switch from PayPal or expired trial to new billing",
488	list_for: ->(tel:, customer:, **) { tel && !customer&.currency },
489	customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
490) {
491	EMPromise.all([
492		Command.customer,
493		Command.reply do |reply|
494			reply.allowed_actions = [:next]
495			reply.command << FormTemplate.render("migrate_billing")
496		end
497	]).then do |(customer, iq)|
498		Registration::Payment.for(
499			iq, customer, customer.registered?.phone,
500			final_message: PaypalDone::MESSAGE,
501			finish: PaypalDone
502		).then(&:write).catch_only(Command::Execution::FinalStanza) do |s|
503			BLATHER.join(CONFIG[:notify_admin], "sgx-jmp")
504			BLATHER.say(
505				CONFIG[:notify_admin],
506				"#{customer.customer_id} migrated to #{customer.currency}",
507				:groupchat
508			)
509			EMPromise.reject(s)
510		end
511	end
512}.register(self).then(&CommandList.method(:register))
513
514Command.new(
515	"credit cards",
516	"Credit Card Settings and Management"
517) {
518	Command.customer.then do |customer|
519		url = CONFIG[:credit_card_url].call(
520			customer.jid.to_s.gsub("\\", "%5C"),
521			customer.customer_id
522		)
523		desc = "Manage credits cards and settings"
524		Command.finish("#{desc}: #{url}") do |reply|
525			oob = OOB.find_or_create(reply.command)
526			oob.url = url
527			oob.desc = desc
528		end
529	end
530}.register(self).then(&CommandList.method(:register))
531
532Command.new(
533	"top up",
534	"Buy Account Credit by Credit Card",
535	list_for: ->(payment_methods: [], **) { !payment_methods.empty? },
536	format_error: ->(e) { "Failed to buy credit, system said: #{e.message}" }
537) {
538	Command.customer.then { |customer|
539		BuyAccountCreditForm.for(customer).then do |credit_form|
540			Command.reply { |reply|
541				reply.allowed_actions = [:complete]
542				credit_form.add_to_form(reply.form)
543			}.then do |iq|
544				Transaction.sale(customer, **credit_form.parse(iq.form))
545			end
546		end
547	}.then { |transaction|
548		transaction.insert.then do
549			Command.finish("#{transaction} added to your account balance.")
550		end
551	}.catch_only(BuyAccountCreditForm::AmountValidationError) do |e|
552		Command.finish(e.message, type: :error)
553	end
554}.register(self).then(&CommandList.method(:register))
555
556Command.new(
557	"alt top up",
558	"Buy Account Credit by Bitcoin, Mail, or Interac eTransfer",
559	list_for: ->(customer:, **) { !!customer&.currency }
560) {
561	Command.customer.then { |customer|
562		EMPromise.all([AltTopUpForm.for(customer), customer])
563	}.then do |(alt_form, customer)|
564		Command.reply { |reply|
565			reply.allowed_actions = [:complete]
566			reply.command << alt_form.form
567		}.then do |iq|
568			AddBitcoinAddress.for(iq, alt_form, customer).write
569		end
570	end
571}.register(self).then(&CommandList.method(:register))
572
573Command.new(
574	"referral codes",
575	"Refer a friend for free credit"
576) {
577	Command.customer.then(&:unused_invites).then do |invites|
578		if invites.empty?
579			Command.finish("You have no more invites right now, try again later.")
580		else
581			Command.finish do |reply|
582				reply.form.type = :result
583				reply.form.title = "Unused Invite Codes"
584				reply.form.instructions =
585					"Each of these codes is single use and gives the person using " \
586					"them a free month of JMP service. You will receive credit " \
587					"equivalent to one month of free service if they later become " \
588					"a paying customer."
589				FormTable.new(
590					invites.map { |i| [i] },
591					code: "Invite Code"
592				).add_to_form(reply.form)
593			end
594		end
595	end
596}.register(self).then(&CommandList.method(:register))
597
598Command.new(
599	"reset sip account",
600	"Create or Reset SIP Account"
601) {
602	Command.customer.then(&:reset_sip_account).then do |sip_account|
603		Command.finish do |reply|
604			reply.command << sip_account.form
605		end
606	end
607}.register(self).then(&CommandList.method(:register))
608
609Command.new(
610	"lnp",
611	"Port in your number from another carrier",
612	list_for: ->(**) { true }
613) {
614	using FormToH
615
616	EMPromise.all([
617		Command.customer,
618		Command.reply do |reply|
619			reply.allowed_actions = [:next]
620			reply.command << FormTemplate.render("lnp")
621		end
622	]).then do |(customer, iq)|
623		order = PortInOrder.new(iq.form.to_h.slice(
624			"BillingTelephoneNumber", "Subscriber", "WirelessInfo"
625		).merge("CustomerOrderId" => customer.customer_id))
626		order_id = BandwidthIris::PortIn.create(order.to_h)[:order_id]
627		url = "https://dashboard.bandwidth.com/portal/r/a/" \
628		      "#{CONFIG[:creds][:account]}/orders/portIn/#{order_id}"
629		BLATHER.join(CONFIG[:notify_admin], "sgx-jmp")
630		BLATHER.say(
631			CONFIG[:notify_admin],
632			"New port-in request for #{customer.customer_id}: #{url}",
633			:groupchat
634		)
635		Command.finish(
636			"Your port-in request has been accepted, " \
637			"support will contact you with next steps"
638		)
639	end
640}.register(self).then(&CommandList.method(:register))
641
642Command.new(
643	"customer info",
644	"Show Customer Info",
645	list_for: ->(customer: nil, **) { customer&.admin? }
646) {
647	Command.customer.then do |customer|
648		raise AuthError, "You are not an admin" unless customer&.admin?
649
650		customer_info = CustomerInfoForm.new
651		Command.reply { |reply|
652			reply.allowed_actions = [:next]
653			reply.command << customer_info.picker_form
654		}.then { |response|
655			customer_info.find_customer(response)
656		}.then do |target_customer|
657			target_customer.admin_info.then do |info|
658				Command.finish do |reply|
659					form = Blather::Stanza::X.new(:result)
660					form.title = "Customer Info"
661					form.fields = info.fields
662					reply.command << form
663				end
664			end
665		end
666	end
667}.register(self).then(&CommandList.method(:register))
668
669def reply_with_note(iq, text, type: :info)
670	reply = iq.reply
671	reply.status = :completed
672	reply.note_type = type
673	reply.note_text = text
674
675	self << reply
676end
677
678command :execute?, node: "web-register" do |iq|
679	StatsD.increment("command", tags: ["node:#{iq.node}"])
680
681	sentry_hub = new_sentry_hub(iq, name: iq.node)
682
683	begin
684		jid = iq.form.field("jid")&.value.to_s.strip
685		tel = iq.form.field("tel")&.value.to_s.strip
686		sentry_hub.current_scope.set_user(jid: jid, tel: tel)
687		if iq.from.stripped != CONFIG[:web_register][:from]
688			BLATHER << iq.as_error("forbidden", :auth)
689		elsif jid == "" || tel !~ /\A\+\d+\Z/
690			reply_with_note(iq, "Invalid JID or telephone number.", type: :error)
691		else
692			IQ_MANAGER.write(Blather::Stanza::Iq::Command.new.tap { |cmd|
693				cmd.to = CONFIG[:web_register][:to]
694				cmd.node = "push-register"
695				cmd.form.fields = [var: "to", value: jid]
696				cmd.form.type = "submit"
697			}).then { |result|
698				TEL_SELECTIONS.set(result.form.field("from")&.value.to_s.strip, tel)
699			}.then {
700				BLATHER << iq.reply.tap { |reply| reply.status = :completed }
701			}.catch { |e| panic(e, sentry_hub) }
702		end
703	rescue StandardError => e
704		sentry_hub.capture_exception(e)
705	end
706end
707
708command sessionid: /./ do |iq|
709	COMMAND_MANAGER.fulfill(iq)
710end
711
712iq type: [:result, :error] do |iq|
713	IQ_MANAGER.fulfill(iq)
714end
715
716iq type: [:get, :set] do |iq|
717	StatsD.increment("unknown_iq")
718
719	self << Blather::StanzaError.new(iq, "feature-not-implemented", :cancel)
720end