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		query_node = i.children.find { |v| v.element_name == "query" }
 53		if query_node.namespace.href ==
 54			'http://jabber.org/protocol/disco#items'
 55
 56			msg = Blather::Stanza::Iq::DiscoItems.new
 57			msg.id = i.id
 58			msg.to = i.from
 59			msg.type = 'result'
 60
 61			puts "RESPONSE0: #{msg.inspect}"
 62			write_to_stream msg
 63			puts "SENT"
 64		elsif query_node.namespace.href ==
 65			'http://jabber.org/protocol/disco#info'
 66
 67			msg = Blather::Stanza::Iq::DiscoInfo.new
 68			msg.id = i.id
 69			msg.to = i.from
 70			msg.type = 'result'
 71
 72			msg.identities = [{:name =>
 73				'Soprani.ca Gateway to XMPP - Catapult',
 74				:type => 'sms-ctplt', :category => 'gateway'}]
 75			msg.features = ["jabber:iq:register",
 76				"jabber:iq:gateway", "jabber:iq:private",
 77				"http://jabber.org/protocol/disco#info",
 78				"http://jabber.org/protocol/commands",
 79				"http://jabber.org/protocol/muc"]
 80			puts "RESPONSE1: #{msg.inspect}"
 81			write_to_stream msg
 82			puts "SENT"
 83		elsif query_node.namespace.href == 'jabber:iq:register'
 84			orig = Blather::Stanza::Iq.new
 85			orig.id = i.id
 86			orig.to = i.from
 87			orig.type = 'result'
 88
 89			msg = Nokogiri::XML::Node.new 'query',orig.document
 90			msg['xmlns'] = 'jabber:iq:register'
 91			n1 = Nokogiri::XML::Node.new 'instructions',msg.document
 92			n1.content= "Enter the information from your Account " +
 93				"page as well as the Phone Number\nin your " +
 94				"account you want to use (ie. '+12345678901')" +
 95				".\n\nThe source code for this gateway is at " +
 96				"https://github.com/ossguy/sgx-catapult ." +
 97				"\nCopyright (C) 2017  Denver Gingerich, " +
 98				"licensed under AGPLv3+."
 99			n2 = Nokogiri::XML::Node.new 'user_id',msg.document
100			n3 = Nokogiri::XML::Node.new 'api_token',msg.document
101			n4 = Nokogiri::XML::Node.new 'api_secret',msg.document
102			n5 = Nokogiri::XML::Node.new 'phone_number',msg.document
103			msg.add_child(n1)
104			msg.add_child(n2)
105			msg.add_child(n3)
106			msg.add_child(n4)
107			msg.add_child(n5)
108
109			orig.add_child(msg)
110			puts "RESPONSE2: #{orig.inspect}"
111			write_to_stream orig
112			puts "SENT"
113		end
114	end
115
116	subscription(:request?) do |s|
117		# TODO: fix these - they don't actual work; write does not exist
118		#write(s.approve!)
119		#write(s.request!)
120	end
121end
122
123[:INT, :TERM].each do |sig|
124	trap(sig) {
125		puts 'Shutting down gateway...'
126		SGXcatapult.shutdown
127		puts 'Gateway has terminated.'
128
129		EM.stop
130	}
131end
132
133EM.run do
134	SGXcatapult.run
135end