Added missing files

This commit is contained in:
annelin 2019-05-22 13:41:43 +03:00
parent 3ce6edb6c9
commit 711534b909
2 changed files with 138 additions and 0 deletions

68
inc/xmppgateway.rb Normal file
View file

@ -0,0 +1,68 @@
module Jabber
module Gateway
NS_GATEWAY = 'jabber:iq:gateway'
class Responder
attr_accessor :description
attr_accessor :prompt
def initialize(stream, &func)
@stream = stream
@func = func
@stream.add_iq_callback() do |iq|
if iq.query.kind_of?(IqQueryGateway)
if iq.type == :get
# Client is requesting fields
answer = iq.answer(false)
answer.type = :result
query = answer.add(IqQueryGateway.new)
query.desc = @description || ''
query.prompt = @prompt || ''
@stream.send(answer)
elsif iq.type == :set
# Client is requesting full JID
query = iq.query.prompt
jid = @func.call(iq, query)
answer = iq.answer(false)
answer.type = :result
query = answer.add(IqQueryGateway.new)
query.jid = jid
@stream.send(answer)
end
end
end
end
end
class IqQueryGateway < IqQuery
name_xmlns 'query', Jabber::Gateway::NS_GATEWAY
def desc
first_element_text('desc')
end
def desc=(new_desc)
replace_element_text('desc', new_desc)
end
def prompt
first_element_text('prompt')
end
def prompt=(new_prompt)
replace_element_text('prompt', new_prompt)
end
def jid
first_element_text('jid')
end
def jid=(new_prompt)
replace_element_text('jid', new_prompt)
end
end
end
end

70
inc/xmppregister.rb Normal file
View file

@ -0,0 +1,70 @@
module Jabber
module Register
NS_REGISTER = 'jabber:iq:register'
class Responder
attr_accessor :instructions
def initialize(stream)
@stream = stream
@fields = []
@registered_callbacks = []
@stream.add_iq_callback() do |iq|
if iq.query.kind_of?(IqQueryRegister) then
if iq.type == :get then # Registration request
answer = iq.answer(false)
answer.type = :result
query = answer.add(IqQueryRegister.new)
query.add(Field.new(:instructions, @instructions)) unless instructions.nil?
@fields.each do |field| query.add(Field.new(field[0])) end
@stream.send(answer)
elsif iq.type == :set then # Registration response
iq.query.each do |field|
validator = @fields.assoc(field.name.to_sym)[2]
if !validator.call(iq.from, field.text) then
puts "- - n0t acceptable here - -"
answer = iq.answer(true)
answer.type = :error
answer.add(Jabber::ErrorResponse.new('not-acceptable'))
@stream.send(answer)
end
end
# let them know that all looks good!
answer = iq.answer(false)
answer.type = :result
@stream.send(answer)
# Fire off callbacks
@registered_callbacks.each do |cb|
cb.call(iq.from)
end
end
end
end
end
def add_field(name, required, &validator)
@fields << [ name, required, validator ]
end
def add_registered_callback(&cb)
@registered_callbacks << cb
end
end
end
class IqQueryRegister < IqQuery
name_xmlns 'query', Jabber::Register::NS_REGISTER
end
class Field < REXML::Element
def initialize(name, value=nil)
super(name.to_s)
self.text = value
end
end
end