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 '/iq/ns:query', :ns =>
 51		'http://jabber.org/protocol/disco#items' do |i, xpath_result|
 52
 53		puts "IQ: #{i.inspect}"
 54
 55		msg = i.reply
 56
 57		puts "RESPONSE0: #{msg.inspect}"
 58		write_to_stream msg
 59		puts "SENT"
 60	end
 61
 62	iq '/iq/ns:query', :ns =>
 63		'http://jabber.org/protocol/disco#info' do |i, xpath_result|
 64
 65		puts "IQ: #{i.inspect}"
 66
 67		msg = i.reply
 68		msg.identities = [{:name =>
 69			'Soprani.ca Gateway to XMPP - Catapult',
 70			:type => 'sms-ctplt', :category => 'gateway'}]
 71		msg.features = ["jabber:iq:register",
 72			"jabber:iq:gateway", "jabber:iq:private",
 73			"http://jabber.org/protocol/disco#info",
 74			"http://jabber.org/protocol/commands",
 75			"http://jabber.org/protocol/muc"]
 76		puts "RESPONSE1: #{msg.inspect}"
 77		write_to_stream msg
 78		puts "SENT"
 79	end
 80
 81	iq '/iq/ns:query', :ns => 'jabber:iq:register' do |i, qn|
 82		puts "IQ: #{i.inspect}"
 83
 84		if i.type == :set
 85			xn = qn.children.find { |v| v.element_name == "x" }
 86
 87			if xn.nil?
 88				puts "id: " + qn.children.find {
 89					|v| v.element_name == "nick" }
 90				puts "token: " + qn.children.find {
 91					|v| v.element_name == "username" }
 92				puts "secret: " + qn.children.find {
 93					|v| v.element_name == "password" }
 94				puts "phone num: " + qn.children.find {
 95					|v| v.element_name == "phone" }
 96				next
 97			end
 98
 99			for field in xn.children
100				if field.element_name == "field"
101					val = field.children.find { |v|
102						v.element_name == "value" }
103
104					case field['var']
105					when 'nick'
106						puts "id: " + val.text
107					when 'username'
108						puts "token: " + val.text
109					when 'password'
110						puts "secret: " + val.text
111					when 'phone'
112						puts "phone num: " + val.text
113					when 'FORM_TYPE'
114						puts "FORM_TYPE: " + val.text
115					else
116						# TODO: error
117						puts "weird var: " +field['var']
118					end
119				end
120			end
121
122			# success (for now)
123			msg = i.reply
124
125			puts "RESPONSE3: #{msg.inspect}"
126			write_to_stream msg
127			puts "SENT"
128
129		elsif i.type == :get
130			orig = i.reply
131
132			msg = Nokogiri::XML::Node.new 'query',orig.document
133			msg['xmlns'] = 'jabber:iq:register'
134			n1 = Nokogiri::XML::Node.new 'instructions',msg.document
135			n1.content= "Enter the information from your Account " +
136				"page as well as the Phone Number\nin your " +
137				"account you want to use (ie. '+12345678901')" +
138				".\nUser Id is nick, API Token is username, " +
139				"API Secret is password, Phone Number is phone"+
140				".\n\nThe source code for this gateway is at " +
141				"https://github.com/ossguy/sgx-catapult ." +
142				"\nCopyright (C) 2017  Denver Gingerich, " +
143				"licensed under AGPLv3+."
144			n2 = Nokogiri::XML::Node.new 'nick',msg.document
145			n3 = Nokogiri::XML::Node.new 'username',msg.document
146			n4 = Nokogiri::XML::Node.new 'password',msg.document
147			n5 = Nokogiri::XML::Node.new 'phone',msg.document
148			msg.add_child(n1)
149			msg.add_child(n2)
150			msg.add_child(n3)
151			msg.add_child(n4)
152			msg.add_child(n5)
153
154			x = Nokogiri::XML::Node.new 'x',orig.document
155			x['xmlns'] = 'jabber:x:data'
156			x['type'] = 'form'
157			msg.add_child(x)
158
159			title = Nokogiri::XML::Node.new 'title',orig.document
160			title.content= 'Register for ' +
161				'Soprani.ca Gateway to XMPP - Catapult'
162			x.add_child(title)
163
164			instr = Nokogiri::XML::Node.new 'instructions',
165				orig.document
166			instr.content= "Enter the details from your Account " +
167				"page as well as the Phone Number\nin your " +
168				"account you want to use (ie. '+12345678901')" +
169				".\n\nThe source code for this gateway is at " +
170				"https://github.com/ossguy/sgx-catapult ." +
171				"\nCopyright (C) 2017  Denver Gingerich, " +
172				"licensed under AGPLv3+."
173			x.add_child(instr)
174
175			f1 = Nokogiri::XML::Node.new 'field',orig.document
176			f1['type'] = 'hidden'
177			f1['var'] = 'FORM_TYPE'
178			v1 = Nokogiri::XML::Node.new 'value',orig.document
179			v1.content= 'jabber:iq:register'
180			f1.add_child(v1)
181			x.add_child(f1)
182
183			f2 = Nokogiri::XML::Node.new 'field',orig.document
184			f2['type'] = 'text-single'
185			f2['label'] = 'User Id'
186			f2['var'] = 'nick'
187			v2 = Nokogiri::XML::Node.new 'required',orig.document
188			f2.add_child(v2)
189			x.add_child(f2)
190
191			f3 = Nokogiri::XML::Node.new 'field',orig.document
192			f3['type'] = 'text-single'
193			f3['label'] = 'API Token'
194			f3['var'] = 'username'
195			v3 = Nokogiri::XML::Node.new 'required',orig.document
196			f3.add_child(v3)
197			x.add_child(f3)
198
199			f4 = Nokogiri::XML::Node.new 'field',orig.document
200			f4['type'] = 'text-private'
201			f4['label'] = 'API Secret'
202			f4['var'] = 'password'
203			v4 = Nokogiri::XML::Node.new 'required',orig.document
204			f4.add_child(v4)
205			x.add_child(f4)
206
207			f5 = Nokogiri::XML::Node.new 'field',orig.document
208			f5['type'] = 'text-single'
209			f5['label'] = 'Phone Number'
210			f5['var'] = 'phone'
211			v5 = Nokogiri::XML::Node.new 'required',orig.document
212			f5.add_child(v5)
213			x.add_child(f5)
214
215			orig.add_child(msg)
216			puts "RESPONSE2: #{orig.inspect}"
217			write_to_stream orig
218			puts "SENT"
219		end
220	end
221
222	subscription(:request?) do |s|
223		# TODO: fix these - they don't actual work; write does not exist
224		#write(s.approve!)
225		#write(s.request!)
226	end
227end
228
229[:INT, :TERM].each do |sig|
230	trap(sig) {
231		puts 'Shutting down gateway...'
232		SGXcatapult.shutdown
233		puts 'Gateway has terminated.'
234
235		EM.stop
236	}
237end
238
239EM.run do
240	SGXcatapult.run
241end