-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollecta_web.rb
246 lines (218 loc) · 7.02 KB
/
collecta_web.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env ruby
require 'rubygems'
require 'digest/sha1'
require 'eventmachine'
require 'xmpp4r-simple'
require 'sinatra'
require 'json'
require 'httpclient'
require 'collecta'
begin
require 'system_timer'
MyTimer = SystemTimer
rescue
require 'timeout'
MyTimer = Timeout
end
class String
def valid_jid?
return false if self.length < 2 or self.length > 64 # 2-16 chars
return false if not self.include?('@') # @ means full JID
return false if self =~ /\d+/ # not only digits
true
end
end
module Collecta
class Search
attr_accessor :queries, :callbacks, :service, :jid, :noxmpp
def initialize(jid, apikey, noxmpp)
@jid = jid
@noxmpp = noxmpp
@queries = []
@callbacks = []
@service = Collecta::Client.new(apikey)
@service.anonymous_connect
end
def subscribed?(query); @queries.include?(query) end
def hooked?(url); @callbacks.include?(url) end
def connected?; @service == nil end
def subscribe(query)
unless @queries.include?(query)
@queries << query
@service.subscribe(query)
end
end
def unsubscribe
@service.unsubscribe
@queries = []
@callbacks = []
end
def hook(url)
return unless url and not url.empty?
@callbacks << url unless @callbacks.include?(url)
end
def to_s
msg = "<h2>JID: #{@jid}</h2><pre>\n"
msg += "HTTP Only?: #{@noxmpp ? 'yes' : 'no'}\n"
msg += "Queries: #{@queries.inspect}\n"
msg += "Callbacks: #{@callbacks.inspect}\n"
msg += "<pre>\n"
msg
end
end
class App < Sinatra::Default
set :sessions, false
set :run, false
set :environment, ENV['RACK_ENV']
configure do
API_VERSION = "1.2"
DB = {}
CFG = YAML.load(File.read("config.yml"))
XMPP = Jabber::Simple.new(CFG['bot.jid'], CFG['bot.password'])
end
helpers do
def protected!
response['WWW-Authenticate'] = %(Basic realm="Protected Area") and \
throw(:halt, [401, "Not authorized\n"]) and \
return unless authorized?
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials &&
@auth.credentials == ['admin', CFG['web.password']]
end
# post the search results to a callback url(s) for some JID
def do_post(jid, payload)
DB[jid].callbacks.each do |cb|
begin
MyTimer.timeout(CFG['web.giveup'].to_i) do
params = { :apikey => CFG['web.apikey'],
:jid => jid,
:meta => payload.meta,
:category => payload.category,
:title => payload.title,
:body => payload.body }
if payload.links
link = Array(payload.links)[0]
link = link['href'] if link.kind_of?(Hash)
link = link[1] if link.kind_of?(Array) and link[0] == 'href'
params[:links] = link
end
HTTPClient.post(cb, params)
end
rescue Exception => e
case e
when Timeout::Error
p "Timeout: #{cb}"
else
p "[E] do_post: #{e.to_s}"
end
next
end
end
end
def do_subscribe(jid, query, callback = "", noxmpp = false)
unless DB[jid]
DB[jid] = Collecta::Search.new(jid, CFG['collecta.apikey'], noxmpp)
DB[jid].service.add_message_callback do |msg|
payload = Collecta::Payload.new(msg)
text = "[#{payload.meta}] #{payload.category}: #{payload.title}"
XMPP.deliver(jid, "#{text}\n#{payload.body}") unless DB[jid].noxmpp == true
# post the results also to the verified webhook
do_post(jid, payload) unless DB[jid].callbacks.empty?
end
end
DB[jid].noxmpp = noxmpp
DB[jid].subscribe(query)
DB[jid].hook(callback)
end
end
get '/' do
return "API v#{API_VERSION}"
end
get '/1/?' do
return "API v#{API_VERSION}"
end
post '/1/sub/?' do
begin
jid = params[:jid]
query = params[:q]
callback = params[:callback]
noxmpp = params[:noxmpp] ? true : false
raise "Missing or wrong parameter" unless jid and jid.valid_jid? and query
sig = Digest::SHA1.hexdigest("--#{CFG['web.apikey']}--#{jid}")
raise "Non authorized" unless params[:sig] == sig
do_subscribe(jid, query, callback, noxmpp)
rescue Exception => e
throw :halt, [400, "Bad request: #{e.to_s}"]
end
throw :halt, [200, "OK"]
end
post '/1/unsub/?' do
begin
jid = params[:jid]
raise "Invalid JID '#{jid}'" unless jid and jid.valid_jid? and DB[jid]
sig = Digest::SHA1.hexdigest("--#{CFG['web.apikey']}--#{jid}")
raise "Non authorized" unless params[:sig] == sig
DB[jid].unsubscribe
rescue Exception => e
throw :halt, [400, "Bad request: #{e.to_s}"]
end
throw :halt, [200, "OK"]
end
get '/1/list/?' do
begin
jid = params[:jid]
raise "Invalid JID '#{jid}'" unless jid and jid.valid_jid? and DB[jid]
sig = Digest::SHA1.hexdigest("--#{CFG['web.apikey']}--#{jid}")
raise "Non authorized" unless params[:sig] == sig
content_type 'application/json; charset=utf-8'
js = DB[jid].queries.to_json
# Allow 'abc' and 'abc.def' but not '.abc' or 'abc.'
if params[:callback] and params[:callback].match(/^\w+(\.\w+)*$/)
js = "#{params[:callback]}(#{js})"
end
return js
rescue Exception => e
throw :halt, [400, "Bad request: #{e.to_s}"]
end
end
# Debug subscribe
get '/1/admin/?' do
protected!
erb :admin
end
post '/1/?' do
jid = params[:jid]
mode = params[:mode]
throw :halt, [400, "Bad request"] unless mode and jid
raise "Non authorized" unless params[:apikey] == CFG['web.apikey']
if mode == 'dump'
text = ""
if jid == "__all__"
DB.each do |j|
text += "#{j.to_s}<br/>\n"
end
else
text = DB[jid].to_s
end
text += '<br/><br/><a href="/1/admin/#" onClick="history.go(-1)">Back</a>'
throw :halt, [200, text]
elsif mode == 'subscribe'
query = params[:query]
throw :halt, [400, "Bad request"] unless query
callback = params[:callback]
noxmpp = (params[:noxmpp] == 'yes' and callback and not callback.empty?) ? true : false
do_subscribe(jid, query, params[:callback], noxmpp)
elsif mode == 'unsubscribe'
DB[jid].unsubscribe
else
throw :halt, [400, "Bad request, unknown 'mode' parameter"]
end
throw :halt, [200, DB[jid].to_s]
end
end
end
if __FILE__ == $0
Collecta::App.run!
end