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 write_to_stream i.reply
54 end
55
56 iq '/iq/ns:query', :ns =>
57 'http://jabber.org/protocol/disco#info' do |i, xpath_result|
58
59 msg = i.reply
60 msg.identities = [{:name =>
61 'Soprani.ca Gateway to XMPP - Catapult',
62 :type => 'sms-ctplt', :category => 'gateway'}]
63 msg.features = ["jabber:iq:register",
64 "jabber:iq:gateway", "jabber:iq:private",
65 "http://jabber.org/protocol/disco#info",
66 "http://jabber.org/protocol/commands",
67 "http://jabber.org/protocol/muc"]
68 write_to_stream msg
69 end
70
71 iq '/iq/ns:query', :ns => 'jabber:iq:register' do |i, qn|
72 puts "IQ: #{i.inspect}"
73
74 if i.type == :set
75 xn = qn.children.find { |v| v.element_name == "x" }
76
77 if xn.nil?
78 puts "id: " + qn.children.find {
79 |v| v.element_name == "nick" }
80 puts "token: " + qn.children.find {
81 |v| v.element_name == "username" }
82 puts "secret: " + qn.children.find {
83 |v| v.element_name == "password" }
84 puts "phone num: " + qn.children.find {
85 |v| v.element_name == "phone" }
86 next
87 end
88
89 for field in xn.children
90 if field.element_name == "field"
91 val = field.children.find { |v|
92 v.element_name == "value" }
93
94 case field['var']
95 when 'nick'
96 puts "id: " + val.text
97 when 'username'
98 puts "token: " + val.text
99 when 'password'
100 puts "secret: " + val.text
101 when 'phone'
102 puts "phone num: " + val.text
103 when 'FORM_TYPE'
104 puts "FORM_TYPE: " + val.text
105 else
106 # TODO: error
107 puts "weird var: " +field['var']
108 end
109 end
110 end
111
112 # success (for now)
113 write_to_stream i.reply
114
115 elsif i.type == :get
116 orig = i.reply
117
118 msg = Nokogiri::XML::Node.new 'query',orig.document
119 msg['xmlns'] = 'jabber:iq:register'
120 n1 = Nokogiri::XML::Node.new 'instructions',msg.document
121 n1.content= "Enter the information from your Account " +
122 "page as well as the Phone Number\nin your " +
123 "account you want to use (ie. '+12345678901')" +
124 ".\nUser Id is nick, API Token is username, " +
125 "API Secret is password, Phone Number is phone"+
126 ".\n\nThe source code for this gateway is at " +
127 "https://github.com/ossguy/sgx-catapult ." +
128 "\nCopyright (C) 2017 Denver Gingerich, " +
129 "licensed under AGPLv3+."
130 n2 = Nokogiri::XML::Node.new 'nick',msg.document
131 n3 = Nokogiri::XML::Node.new 'username',msg.document
132 n4 = Nokogiri::XML::Node.new 'password',msg.document
133 n5 = Nokogiri::XML::Node.new 'phone',msg.document
134 msg.add_child(n1)
135 msg.add_child(n2)
136 msg.add_child(n3)
137 msg.add_child(n4)
138 msg.add_child(n5)
139
140 x = Blather::Stanza::X.new :form, [
141 {:required => true, :type => :"text-single",
142 :label => 'User Id', :var => 'nick'},
143 {:required => true, :type => :"text-single",
144 :label => 'API Token', :var => 'username'},
145 {:required => true, :type => :"text-private",
146 :label => 'API Secret', :var => 'password'},
147 {:required => true, :type => :"text-single",
148 :label => 'Phone Number', :var => 'phone'}
149 ]
150 x.title= 'Register for ' +
151 'Soprani.ca Gateway to XMPP - Catapult'
152 x.instructions= "Enter the details from your Account " +
153 "page as well as the Phone Number\nin your " +
154 "account you want to use (ie. '+12345678901')" +
155 ".\n\nThe source code for this gateway is at " +
156 "https://github.com/ossguy/sgx-catapult ." +
157 "\nCopyright (C) 2017 Denver Gingerich, " +
158 "licensed under AGPLv3+."
159 msg.add_child(x)
160
161 orig.add_child(msg)
162 puts "RESPONSE2: #{orig.inspect}"
163 write_to_stream orig
164 puts "SENT"
165 end
166 end
167
168 subscription(:request?) do |s|
169 # TODO: are these the best to return? really need '!' here?
170 #write_to_stream s.approve!
171 #write_to_stream s.request!
172 end
173end
174
175[:INT, :TERM].each do |sig|
176 trap(sig) {
177 puts 'Shutting down gateway...'
178 SGXcatapult.shutdown
179 puts 'Gateway has terminated.'
180
181 EM.stop
182 }
183end
184
185EM.run do
186 SGXcatapult.run
187end