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