sgx-catapult.rb

  1#!/usr/bin/env ruby
  2#
  3# Copyright (C) 2017  Denver Gingerich <denver@ossguy.com>
  4#
  5# This file is part of sgx-catapult.
  6#
  7# sgx-catapult is free software: you can redistribute it and/or modify it under
  8# the terms of the GNU Affero General Public License as published by the Free
  9# Software Foundation, either version 3 of the License, or (at your option) any
 10# later version.
 11#
 12# sgx-catapult is distributed in the hope that it will be useful, but WITHOUT
 13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 14# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 15# details.
 16#
 17# You should have received a copy of the GNU Affero General Public License along
 18# with sgx-catapult.  If not, see <http://www.gnu.org/licenses/>.
 19
 20require 'blather/client/dsl'
 21require 'json'
 22require 'net/http'
 23require 'redis/connection/hiredis'
 24require 'uri'
 25
 26if ARGV.size != 6 then
 27	puts "Usage: sgx-catapult.rb <component_jid> <component_password> " +
 28		"<server_hostname> <server_port> <redis_hostname> <redis_port>"
 29	exit 0
 30end
 31
 32module SGXcatapult
 33	extend Blather::DSL
 34
 35	def self.run
 36		client.run
 37	end
 38
 39	def self.error_msg(orig, query_node, type, name, text = nil)
 40		orig.add_child(query_node)
 41		orig.type = :error
 42
 43		error = Nokogiri::XML::Node.new 'error', orig.document
 44		error['type'] = type
 45		orig.add_child(error)
 46
 47		suberr = Nokogiri::XML::Node.new name, orig.document
 48		suberr['xmlns'] = 'urn:ietf:params:xml:ns:xmpp-stanzas'
 49		error.add_child(suberr)
 50
 51		# TODO: add some explanatory xml:lang='en' text (see text param)
 52		puts "RESPONSE3: #{orig.inspect}"
 53		return orig
 54	end
 55
 56	setup ARGV[0], ARGV[1], ARGV[2], ARGV[3]
 57
 58	message :chat?, :body do |m|
 59		begin
 60			puts "#{m.from.to_s} -> #{m.to.to_s} #{m.body}"
 61			msg = Blather::Stanza::Message.new(m.from, 'thx for "' +
 62				m.body + '"')
 63			msg.from = m.to
 64			write_to_stream msg
 65		rescue => e
 66			# TODO: do something better with this info
 67			say m.from, e.inspect
 68		end
 69	end
 70
 71	iq '/iq/ns:query', :ns =>
 72		'http://jabber.org/protocol/disco#items' do |i, xpath_result|
 73
 74		write_to_stream i.reply
 75	end
 76
 77	iq '/iq/ns:query', :ns =>
 78		'http://jabber.org/protocol/disco#info' do |i, xpath_result|
 79
 80		msg = i.reply
 81		msg.identities = [{:name =>
 82			'Soprani.ca Gateway to XMPP - Catapult',
 83			:type => 'sms-ctplt', :category => 'gateway'}]
 84		msg.features = ["jabber:iq:register",
 85			"jabber:iq:gateway", "jabber:iq:private",
 86			"http://jabber.org/protocol/disco#info",
 87			"http://jabber.org/protocol/commands",
 88			"http://jabber.org/protocol/muc"]
 89		write_to_stream msg
 90	end
 91
 92	iq '/iq/ns:query', :ns => 'jabber:iq:register' do |i, qn|
 93		puts "IQ: #{i.inspect}"
 94
 95		if i.type == :set
 96			xn = qn.children.find { |v| v.element_name == "x" }
 97
 98			user_id = ''
 99			api_token = ''
100			api_secret = ''
101			phone_num = ''
102
103			if xn.nil?
104				user_id = qn.children.find {
105					|v| v.element_name == "nick" }
106				api_token = qn.children.find {
107					|v| v.element_name == "username" }
108				api_secret = qn.children.find {
109					|v| v.element_name == "password" }
110				phone_num = qn.children.find {
111					|v| v.element_name == "phone" }
112			else
113				for field in xn.children
114					if field.element_name == "field"
115						val = field.children.find { |v|
116						v.element_name == "value" }
117
118						case field['var']
119						when 'nick'
120							user_id = val.text
121						when 'username'
122							api_token = val.text
123						when 'password'
124							api_secret = val.text
125						when 'phone'
126							phone_num = val.text
127						else
128							# TODO: error
129							puts "?: " +field['var']
130						end
131					end
132				end
133			end
134
135			if phone_num[0] != '+'
136				# TODO: add text re number not (yet) supported
137				write_to_stream error_msg(i.reply, qn, :modify,
138					'policy-violation')
139				next
140			end
141
142			uri = URI.parse('https://api.catapult.inetwork.com')
143			http = Net::HTTP.new(uri.host, uri.port)
144			http.use_ssl = true
145			request = Net::HTTP::Get.new('/v1/users/' + user_id +
146				'/phoneNumbers/' + phone_num)
147			request.basic_auth api_token, api_secret
148			response = http.request(request)
149
150			puts 'API response: ' + response.to_s + ' with code ' +
151				response.code + ', body "' + response.body + '"'
152
153			if response.code == '200'
154				params = JSON.parse response.body
155				if params['numberState'] == 'enabled'
156					num_key = "catapult_num-" + phone_num
157
158					bare_jid = i.from.to_s.split('/', 2)[0]
159					cred_key = "catapult_cred-" + bare_jid
160
161					# TODO: pre-validate ARGV[5] is integer
162					conn = Hiredis::Connection.new
163					conn.connect(ARGV[4], ARGV[5].to_i)
164
165					conn.write ["EXISTS", num_key]
166					if conn.read == 1
167						# TODO: add txt re num exists
168						write_to_stream error_msg(
169							i.reply, qn, :cancel,
170							'conflict')
171						next
172					end
173
174					conn.write ["EXISTS", cred_key]
175					if conn.read == 1
176						# TODO: add txt re already exist
177						write_to_stream error_msg(
178							i.reply, qn, :cancel,
179							'conflict')
180						next
181					end
182
183					conn.write ["RPUSH",num_key,bare_jid]
184					if conn.read != 1
185						# TODO: catch/relay RuntimeError
186						# TODO: add txt re push failure
187						write_to_stream error_msg(
188							i.reply, qn, :cancel,
189							'internal-server-error')
190						next
191					end
192
193					conn.write ["RPUSH",cred_key,user_id]
194					conn.write ["RPUSH",cred_key,api_token]
195					conn.write ["RPUSH",cred_key,api_secret]
196					conn.write ["RPUSH",cred_key,phone_num]
197
198					for n in 1..4 do
199						# TODO: catch/relay RuntimeError
200						result = conn.read
201						if result != n
202							write_to_stream(
203							error_msg(
204							i.reply, qn, :cancel,
205							'internal-server-error')
206							)
207							next
208						end
209					end
210
211					write_to_stream i.reply
212				else
213					# TODO: add text re number disabled
214					write_to_stream error_msg(i.reply, qn,
215						:modify, 'not-acceptable')
216				end
217			elsif response.code == '401'
218				# TODO: add text re bad credentials
219				write_to_stream error_msg(i.reply, qn, :auth,
220					'not-authorized')
221			elsif response.code == '404'
222				# TODO: add text re number not found or disabled
223				write_to_stream error_msg(i.reply, qn, :cancel,
224					'item-not-found')
225			else
226				# TODO: add text re misc error, and mention code
227				write_to_stream error_msg(i.reply, qn, :modify,
228					'not-acceptable')
229			end
230
231		elsif i.type == :get
232			orig = i.reply
233
234			msg = Nokogiri::XML::Node.new 'query',orig.document
235			msg['xmlns'] = 'jabber:iq:register'
236			n1 = Nokogiri::XML::Node.new 'instructions',msg.document
237			n1.content= "Enter the information from your Account " +
238				"page as well as the Phone Number\nin your " +
239				"account you want to use (ie. '+12345678901')" +
240				".\nUser Id is nick, API Token is username, " +
241				"API Secret is password, Phone Number is phone"+
242				".\n\nThe source code for this gateway is at " +
243				"https://github.com/ossguy/sgx-catapult ." +
244				"\nCopyright (C) 2017  Denver Gingerich, " +
245				"licensed under AGPLv3+."
246			n2 = Nokogiri::XML::Node.new 'nick',msg.document
247			n3 = Nokogiri::XML::Node.new 'username',msg.document
248			n4 = Nokogiri::XML::Node.new 'password',msg.document
249			n5 = Nokogiri::XML::Node.new 'phone',msg.document
250			msg.add_child(n1)
251			msg.add_child(n2)
252			msg.add_child(n3)
253			msg.add_child(n4)
254			msg.add_child(n5)
255
256			x = Blather::Stanza::X.new :form, [
257				{:required => true, :type => :"text-single",
258				:label => 'User Id', :var => 'nick'},
259				{:required => true, :type => :"text-single",
260				:label => 'API Token', :var => 'username'},
261				{:required => true, :type => :"text-private",
262				:label => 'API Secret', :var => 'password'},
263				{:required => true, :type => :"text-single",
264				:label => 'Phone Number', :var => 'phone'}
265			]
266			x.title= 'Register for ' +
267				'Soprani.ca Gateway to XMPP - Catapult'
268			x.instructions= "Enter the details from your Account " +
269				"page as well as the Phone Number\nin your " +
270				"account you want to use (ie. '+12345678901')" +
271				".\n\nThe source code for this gateway is at " +
272				"https://github.com/ossguy/sgx-catapult ." +
273				"\nCopyright (C) 2017  Denver Gingerich, " +
274				"licensed under AGPLv3+."
275			msg.add_child(x)
276
277			orig.add_child(msg)
278			puts "RESPONSE2: #{orig.inspect}"
279			write_to_stream orig
280			puts "SENT"
281		end
282	end
283
284	subscription(:request?) do |s|
285		# TODO: are these the best to return?  really need '!' here?
286		#write_to_stream s.approve!
287		#write_to_stream s.request!
288	end
289end
290
291[:INT, :TERM].each do |sig|
292	trap(sig) {
293		puts 'Shutting down gateway...'
294		SGXcatapult.shutdown
295		puts 'Gateway has terminated.'
296
297		EM.stop
298	}
299end
300
301EM.run do
302	SGXcatapult.run
303end