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 = Blather::Stanza::X.new :form, [
155 {:required => true, :type => :"text-single",
156 :label => 'User Id', :var => 'nick'},
157 {:required => true, :type => :"text-single",
158 :label => 'API Token', :var => 'username'},
159 {:required => true, :type => :"text-private",
160 :label => 'API Secret', :var => 'password'},
161 {:required => true, :type => :"text-single",
162 :label => 'Phone Number', :var => 'phone'}
163 ]
164 x.title= 'Register for ' +
165 'Soprani.ca Gateway to XMPP - Catapult'
166 x.instructions= "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 msg.add_child(x)
174
175 orig.add_child(msg)
176 puts "RESPONSE2: #{orig.inspect}"
177 write_to_stream orig
178 puts "SENT"
179 end
180 end
181
182 subscription(:request?) do |s|
183 # TODO: fix these - they don't actual work; write does not exist
184 #write(s.approve!)
185 #write(s.request!)
186 end
187end
188
189[:INT, :TERM].each do |sig|
190 trap(sig) {
191 puts 'Shutting down gateway...'
192 SGXcatapult.shutdown
193 puts 'Gateway has terminated.'
194
195 EM.stop
196 }
197end
198
199EM.run do
200 SGXcatapult.run
201end