correct_duplicate_addrs

 1#!/usr/bin/ruby
 2# frozen_string_literal: true
 3
 4# This is meant to be run with the output of detect_duplicate_addrs on stdin
 5# The assumption is that some logging will dump that, and then someone will
 6# run this after looking into why
 7# Theoretically they could be piped together directly for automated fixing
 8
 9require "redis"
10
11redis = Redis.new
12
13customer_id = ENV.fetch("DEFAULT_CUSTOMER_ID", nil)
14unless customer_id
15	puts "The env-var DEFAULT_CUSTOMER_ID must be set to the ID " \
16	     "of the customer who will receive the duplicated addrs, preferably " \
17	     "a support customer or something linked to notifications when " \
18	     "stray money is sent to these addresses"
19	exit 1
20end
21
22$stdin.each_line do |line|
23	match = line.match(/^(\w+) is used by the following \d+ keys: (.*)/)
24	unless match
25		puts "The following line can't be understood and is being ignored"
26		puts "  #{line}"
27		next
28	end
29
30	addr = match[1]
31	keys = match[2].split
32
33	# This is the customer ID of the support chat
34	# All duplicates are moved to the support addr so we still hear when people
35	# send money there
36	redis.sadd("jmp_customer_btc_addresses-#{customer_id}", addr)
37
38	keys.each do |key|
39		redis.srem(key, addr)
40	end
41end