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'
 21
 22if ARGV.size != 4 then
 23	puts "Usage: sgx-catapult.rb <component_jid> <component_password> " +
 24		"<server_hostname> <server_port>"
 25	exit 0
 26end
 27
 28module SGXcatapult
 29	extend Blather::DSL
 30
 31	def self.run
 32		client.run
 33	end
 34
 35	setup ARGV[0], ARGV[1], ARGV[2], ARGV[3]
 36
 37	message :chat?, :body do |m|
 38		begin
 39			puts "#{m.from.to_s} -> #{m.to.to_s} #{m.body}"
 40			msg = Blather::Stanza::Message.new(m.from, 'thx for "' +
 41				m.body + '"')
 42			msg.from = m.to
 43			write_to_stream msg
 44		rescue => e
 45			# TODO: do something better with this info
 46			say m.from, e.inspect
 47		end
 48	end
 49
 50	iq do |i|
 51		puts "IQ: #{i.inspect}"
 52
 53		if i.type == :set
 54			puts "received set, likely for jabber:iq:register"
 55
 56			# TODO: resilient error handling; what if no query node?
 57
 58			qn = i.children.find { |v| v.element_name == "query" }
 59			# TODO: add below check - as-written has unmatched end
 60	i		#if qn.namespace.href != 'jabber:iq:register'
 61			#	# TODO: error
 62			#	puts "weird xmlns: " + qn.namespace.href
 63			#	next
 64			#end
 65
 66			xn = qn.children.find { |v| v.element_name == "x" }
 67			for field in xn.children
 68				if field.element_name == "field"
 69					val = field.children.find { |v|
 70						v.element_name == "value" }
 71
 72					case field['var']
 73					when 'nick'
 74						puts "id: " + val.text
 75					when 'username'
 76						puts "token: " + val.text
 77					when 'password'
 78						puts "secret: " + val.text
 79					when 'phone'
 80						puts "phone num: " + val.text
 81					when 'FORM_TYPE'
 82						puts "FORM_TYPE: " + val.text
 83					else
 84						# TODO: error
 85						puts "weird var: " +field['var']
 86					end
 87				end
 88			end
 89
 90			# success (for now)
 91			msg = Blather::Stanza::Iq.new
 92			msg.id = i.id
 93			msg.to = i.from
 94			msg.type = 'result'
 95
 96			puts "RESPONSE3: #{msg.inspect}"
 97			write_to_stream msg
 98			puts "SENT"
 99
100			# TODO: implement this (verify/save data, return result)
101			next
102		end
103
104		query_node = i.children.find { |v| v.element_name == "query" }
105		if query_node.namespace.href ==
106			'http://jabber.org/protocol/disco#items'
107
108			msg = Blather::Stanza::Iq::DiscoItems.new
109			msg.id = i.id
110			msg.to = i.from
111			msg.type = 'result'
112
113			puts "RESPONSE0: #{msg.inspect}"
114			write_to_stream msg
115			puts "SENT"
116		elsif query_node.namespace.href ==
117			'http://jabber.org/protocol/disco#info'
118
119			msg = Blather::Stanza::Iq::DiscoInfo.new
120			msg.id = i.id
121			msg.to = i.from
122			msg.type = 'result'
123
124			msg.identities = [{:name =>
125				'Soprani.ca Gateway to XMPP - Catapult',
126				:type => 'sms-ctplt', :category => 'gateway'}]
127			msg.features = ["jabber:iq:register",
128				"jabber:iq:gateway", "jabber:iq:private",
129				"http://jabber.org/protocol/disco#info",
130				"http://jabber.org/protocol/commands",
131				"http://jabber.org/protocol/muc"]
132			puts "RESPONSE1: #{msg.inspect}"
133			write_to_stream msg
134			puts "SENT"
135		elsif query_node.namespace.href == 'jabber:iq:register'
136			orig = Blather::Stanza::Iq.new
137			orig.id = i.id
138			orig.to = i.from
139			orig.type = 'result'
140
141			msg = Nokogiri::XML::Node.new 'query',orig.document
142			msg['xmlns'] = 'jabber:iq:register'
143			n1 = Nokogiri::XML::Node.new 'instructions',msg.document
144			n1.content= "Enter the information from your Account " +
145				"page as well as the Phone Number\nin your " +
146				"account you want to use (ie. '+12345678901')" +
147				".\n\nThe source code for this gateway is at " +
148				"https://github.com/ossguy/sgx-catapult ." +
149				"\nCopyright (C) 2017  Denver Gingerich, " +
150				"licensed under AGPLv3+."
151			n2 = Nokogiri::XML::Node.new 'nick',msg.document
152			n3 = Nokogiri::XML::Node.new 'username',msg.document
153			n4 = Nokogiri::XML::Node.new 'password',msg.document
154			n5 = Nokogiri::XML::Node.new 'phone',msg.document
155			msg.add_child(n1)
156			msg.add_child(n2)
157			msg.add_child(n3)
158			msg.add_child(n4)
159			msg.add_child(n5)
160
161			x = Nokogiri::XML::Node.new 'x',orig.document
162			x['xmlns'] = 'jabber:x:data'
163			x['type'] = 'form'
164			msg.add_child(x)
165
166			title = Nokogiri::XML::Node.new 'title',orig.document
167			title.content= 'Register for ' +
168				'Soprani.ca Gateway to XMPP - Catapult'
169			x.add_child(title)
170
171			instr = Nokogiri::XML::Node.new 'instructions',
172				orig.document
173			instr.content= "Enter the details from your Account " +
174				"page as well as the Phone Number\nin your " +
175				"account you want to use (ie. '+12345678901')" +
176				".\n\nThe source code for this gateway is at " +
177				"https://github.com/ossguy/sgx-catapult ." +
178				"\nCopyright (C) 2017  Denver Gingerich, " +
179				"licensed under AGPLv3+."
180			x.add_child(instr)
181
182			f1 = Nokogiri::XML::Node.new 'field',orig.document
183			f1['type'] = 'hidden'
184			f1['var'] = 'FORM_TYPE'
185			v1 = Nokogiri::XML::Node.new 'value',orig.document
186			v1.content= 'jabber:iq:register'
187			f1.add_child(v1)
188			x.add_child(f1)
189
190			f2 = Nokogiri::XML::Node.new 'field',orig.document
191			f2['type'] = 'text-single'
192			f2['label'] = 'User Id'
193			f2['var'] = 'nick'
194			v2 = Nokogiri::XML::Node.new 'required',orig.document
195			f2.add_child(v2)
196			x.add_child(f2)
197
198			f3 = Nokogiri::XML::Node.new 'field',orig.document
199			f3['type'] = 'text-single'
200			f3['label'] = 'API Token'
201			f3['var'] = 'username'
202			v3 = Nokogiri::XML::Node.new 'required',orig.document
203			f3.add_child(v3)
204			x.add_child(f3)
205
206			f4 = Nokogiri::XML::Node.new 'field',orig.document
207			f4['type'] = 'text-private'
208			f4['label'] = 'API Secret'
209			f4['var'] = 'password'
210			v4 = Nokogiri::XML::Node.new 'required',orig.document
211			f4.add_child(v4)
212			x.add_child(f4)
213
214			f5 = Nokogiri::XML::Node.new 'field',orig.document
215			f5['type'] = 'text-single'
216			f5['label'] = 'Phone Number'
217			f5['var'] = 'phone'
218			v5 = Nokogiri::XML::Node.new 'required',orig.document
219			f5.add_child(v5)
220			x.add_child(f5)
221
222			orig.add_child(msg)
223			puts "RESPONSE2: #{orig.inspect}"
224			write_to_stream orig
225			puts "SENT"
226		end
227	end
228
229	subscription(:request?) do |s|
230		# TODO: fix these - they don't actual work; write does not exist
231		#write(s.approve!)
232		#write(s.request!)
233	end
234end
235
236[:INT, :TERM].each do |sig|
237	trap(sig) {
238		puts 'Shutting down gateway...'
239		SGXcatapult.shutdown
240		puts 'Gateway has terminated.'
241
242		EM.stop
243	}
244end
245
246EM.run do
247	SGXcatapult.run
248end