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
423# Commands that just pass through to the SGX
424{
425 "configure-calls" => ["Configure Calls"]
426}.each do |node, args|
427 Command.new(node, *args) {
428 Command.customer.then do |customer|
429 customer.stanza_from(Command.execution.iq)
430 end
431 }.register(self, guards: [node: node]).then(&CommandList.method(:register))
432end
433
434Command.new(
435 "ogm",
436 "Record Voicemail Greeting",
437 list_for: ->(fwd: nil, **) { !!fwd },
438 customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
439) {
440 Command.customer.then do |customer|
441 BANDWIDTH_VOICE.create_call(
442 CONFIG[:creds][:account],
443 body: customer.fwd.create_call_request do |cc|
444 cc.from = customer.registered?.phone
445 cc.application_id = CONFIG[:bandwidth_app]
446 cc.answer_url = "#{CONFIG[:web_root]}/ogm/start?" \
447 "customer_id=#{customer.customer_id}"
448 end
449 )
450 Command.finish("You will now receive a call.")
451 end
452}.register(self).then(&CommandList.method(:register))
453
454Command.new(
455 "credit cards",
456 "Credit Card Settings and Management"
457) {
458 Command.customer.then do |customer|
459 url = CONFIG[:credit_card_url].call(
460 customer.jid.to_s.gsub("\\", "%5C"),
461 customer.customer_id
462 )
463 desc = "Manage credits cards and settings"
464 Command.finish("#{desc}: #{url}") do |reply|
465 oob = OOB.find_or_create(reply.command)
466 oob.url = url
467 oob.desc = desc
468 end
469 end
470}.register(self).then(&CommandList.method(:register))
471
472Command.new(
473 "top up",
474 "Buy Account Credit by Credit Card",
475 list_for: ->(payment_methods: [], **) { !payment_methods.empty? },
476 format_error: ->(e) { "Failed to buy credit, system said: #{e.message}" }
477) {
478 Command.customer.then { |customer|
479 BuyAccountCreditForm.for(customer).then do |credit_form|
480 Command.reply { |reply|
481 reply.allowed_actions = [:complete]
482 credit_form.add_to_form(reply.form)
483 }.then do |iq|
484 Transaction.sale(customer, **credit_form.parse(iq.form))
485 end
486 end
487 }.then { |transaction|
488 transaction.insert.then do
489 Command.finish("#{transaction} added to your account balance.")
490 end
491 }.catch_only(BuyAccountCreditForm::AmountValidationError) do |e|
492 Command.finish(e.message, type: :error)
493 end
494}.register(self).then(&CommandList.method(:register))
495
496Command.new(
497 "alt top up",
498 "Buy Account Credit by Bitcoin, Mail, or Interac eTransfer",
499 list_for: ->(customer:, **) { !!customer&.currency }
500) {
501 Command.customer.then { |customer|
502 EMPromise.all([AltTopUpForm.for(customer), customer])
503 }.then do |(alt_form, customer)|
504 Command.reply { |reply|
505 reply.allowed_actions = [:complete]
506 reply.command << alt_form.form
507 }.then do |iq|
508 AddBitcoinAddress.for(iq, alt_form, customer).write
509 end
510 end
511}.register(self).then(&CommandList.method(:register))
512
513Command.new(
514 "reset sip account",
515 "Create or Reset SIP Account"
516) {
517 Command.customer.then(&:reset_sip_account).then do |sip_account|
518 Command.finish do |reply|
519 reply.command << sip_account.form
520 end
521 end
522}.register(self).then(&CommandList.method(:register))
523
524Command.new(
525 "usage",
526 "Show Monthly Usage"
527) {
528 report_for = (Date.today..(Date.today << 1))
529
530 Command.customer.then { |customer|
531 customer.usage_report(report_for)
532 }.then do |usage_report|
533 Command.finish do |reply|
534 reply.command << usage_report.form
535 end
536 end
537}.register(self).then(&CommandList.method(:register))
538
539Command.new(
540 "invite codes",
541 "Refer a friend for free credit"
542) {
543 Command.customer.then(&:unused_invites).then do |invites|
544 if invites.empty?
545 Command.finish("You have no more invites right now, try again later.")
546 else
547 Command.finish do |reply|
548 reply.form.type = :result
549 reply.form.title = "Unused Invite Codes"
550 reply.form.instructions =
551 "Each of these codes is single use and gives the person using " \
552 "them a free month of JMP service. You will receive credit " \
553 "equivalent to one month of free service if they later become " \
554 "a paying customer."
555 FormTable.new(
556 invites.map { |i| [i] },
557 code: "Invite Code"
558 ).add_to_form(reply.form)
559 end
560 end
561 end
562}.register(self).then(&CommandList.method(:register))
563
564Command.new(
565 "info",
566 "Show Account Info",
567 list_for: ->(*) { true },
568 customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
569) {
570 Command.customer.then(&:info).then do |info|
571 Command.finish do |reply|
572 form = Blather::Stanza::X.new(:result)
573 form.title = "Account Info"
574 form.fields = info.fields
575 reply.command << form
576 end
577 end
578}.register(self).then(&CommandList.method(:register))
579
580Command.new(
581 "customer info",
582 "Show Customer Info",
583 list_for: ->(customer: nil, **) { customer&.admin? }
584) {
585 Command.customer.then do |customer|
586 raise AuthError, "You are not an admin" unless customer&.admin?
587
588 customer_info = CustomerInfoForm.new
589 Command.reply { |reply|
590 reply.allowed_actions = [:next]
591 reply.command << customer_info.picker_form
592 }.then { |response|
593 customer_info.find_customer(response)
594 }.then do |target_customer|
595 target_customer.admin_info.then do |info|
596 Command.finish do |reply|
597 form = Blather::Stanza::X.new(:result)
598 form.title = "Customer Info"
599 form.fields = info.fields
600 reply.command << form
601 end
602 end
603 end
604 end
605}.register(self).then(&CommandList.method(:register))
606
607Command.new(
608 "migrate billing",
609 "Switch from PayPal or expired trial to new billing",
610 list_for: ->(tel:, customer:, **) { tel && !customer&.currency },
611 customer_repo: CustomerRepo.new(sgx_repo: Bwmsgsv2Repo.new)
612) {
613 EMPromise.all([
614 Command.customer,
615 Command.reply do |reply|
616 reply.allowed_actions = [:next]
617 reply.command << FormTemplate.render("migrate_billing")
618 end
619 ]).then do |(customer, iq)|
620 Registration::Payment.for(
621 iq, customer, customer.registered?.phone,
622 final_message: PaypalDone::MESSAGE,
623 finish: PaypalDone
624 ).then(&:write).catch_only(Command::Execution::FinalStanza) do |s|
625 BLATHER.join(CONFIG[:notify_admin], "sgx-jmp")
626 BLATHER.say(
627 CONFIG[:notify_admin],
628 "#{customer.customer_id} migrated to #{customer.currency}",
629 :groupchat
630 )
631 EMPromise.reject(s)
632 end
633 end
634}.register(self).then(&CommandList.method(:register))
635
636Command.new(
637 "lnp",
638 "Port in your number from another carrier",
639 list_for: ->(**) { true }
640) {
641 using FormToH
642
643 EMPromise.all([
644 Command.customer,
645 Command.reply do |reply|
646 reply.allowed_actions = [:next]
647 reply.command << FormTemplate.render("lnp")
648 end
649 ]).then do |(customer, iq)|
650 order = PortInOrder.new(iq.form.to_h.slice(
651 "BillingTelephoneNumber", "Subscriber", "WirelessInfo"
652 ).merge("CustomerOrderId" => customer.customer_id))
653 order_id = BandwidthIris::PortIn.create(order.to_h)[:order_id]
654 url = "https://dashboard.bandwidth.com/portal/r/a/" \
655 "#{CONFIG[:creds][:account]}/orders/portIn/#{order_id}"
656 BLATHER.join(CONFIG[:notify_admin], "sgx-jmp")
657 BLATHER.say(
658 CONFIG[:notify_admin],
659 "New port-in request for #{customer.customer_id}: #{url}",
660 :groupchat
661 )
662 Command.finish(
663 "Your port-in request has been accepted, " \
664 "support will contact you with next steps"
665 )
666 end
667}.register(self).then(&CommandList.method(:register))
668
669command :execute?, node: "web-register" do |iq|
670 StatsD.increment("command", tags: ["node:#{iq.node}"])
671
672 sentry_hub = new_sentry_hub(iq, name: iq.node)
673
674 begin
675 jid = iq.form.field("jid")&.value.to_s.strip
676 tel = iq.form.field("tel")&.value.to_s.strip
677 sentry_hub.current_scope.set_user(jid: jid, tel: tel)
678 if iq.from.stripped != CONFIG[:web_register][:from]
679 BLATHER << iq.as_error("forbidden", :auth)
680 elsif jid == "" || tel !~ /\A\+\d+\Z/
681 reply_with_note(iq, "Invalid JID or telephone number.", type: :error)
682 else
683 IQ_MANAGER.write(Blather::Stanza::Iq::Command.new.tap { |cmd|
684 cmd.to = CONFIG[:web_register][:to]
685 cmd.node = "push-register"
686 cmd.form.fields = [var: "to", value: jid]
687 cmd.form.type = "submit"
688 }).then { |result|
689 TEL_SELECTIONS.set(result.form.field("from")&.value.to_s.strip, tel)
690 }.then {
691 BLATHER << iq.reply.tap { |reply| reply.status = :completed }
692 }.catch { |e| panic(e, sentry_hub) }
693 end
694 rescue StandardError => e
695 sentry_hub.capture_exception(e)
696 end
697end
698
699command sessionid: /./ do |iq|
700 COMMAND_MANAGER.fulfill(iq)
701end
702
703iq type: [:result, :error] do |iq|
704 IQ_MANAGER.fulfill(iq)
705end
706
707iq type: [:get, :set] do |iq|
708 StatsD.increment("unknown_iq")
709
710 self << Blather::StanzaError.new(iq, "feature-not-implemented", :cancel)
711end