-- IN-BAND REGISTRATION (XEP-0077)
-- library
require 'lm'
require 'iq'
require 'x_data'
-- public
iq_register = { }
function iq_register.parse ( query )
local form = { xmlns = 'jabber:iq:register', type = 'form' }
local instructions = query:child ( 'instructions' )
if instructions then
form.instructions = instructions:value ()
end
-- XXX how it can be mapped to common form?
-- and needs it be supplied?
if query:child ( 'registered' ) then
form.registered = true
end
local x = query:child ( 'x' )
if x and x:attribute ( 'xmlns' ) == 'jabber:x:data' then
form = x_data.parse ( x )
local format = form.format
form.format =
function ( form, root )
root.query = format ( form, { xmlns = 'jabber:iq:register' } )
return root
end
return form
end
local field = query:children ()
while field do
local name = field:name ()
if name ~= 'instructions' and name ~= 'registered' then
table.insert ( form, { type = 'text-single', var = name, value = field:value () or '' } )
end
field = field:next ()
end
form.format =
function ( form, root )
root.query = { xmlns = 'jabber:iq:register' }
for index, field in ipairs ( form ) do
root.query[field.var] = field.value
end
return root
end
return form
end
function iq_register.register ( conn, to, success, fail )
iq.send ( conn, to, 'get',
{
query = { xmlns = 'jabber:iq:register' },
},
function ( mess )
local query = mess:child ( 'query' )
if query and query:attribute ( 'xmlns' ) == 'jabber:iq:register' then
success ( iq_register.parse ( query ),
function ( form, success, fail )
form.type = 'submit' -- XXX
iq.send ( conn, to, 'set', form.format ( form, { } ), success , fail )
end,
function ( form, success, fail )
success ()
end )
end
end, fail )
end
function iq_register.unregister ( conn, to, success, fail )
iq.send ( conn, to, 'set',
{
query = { xmlns = 'jabber:iq:register',
remove = { },
},
},
function ( mess )
success ()
end,
function ( mesg, mess )
local query = mess:child ( 'query' )
if query and query:attribute ( 'xmlns' ) == 'jabber:iq:register' then
success ( iq_register.parse ( query ),
function ( form, success, fail )
form.type = 'submit' -- XXX
iq.send ( conn, to, 'set', form.format ( form, { } ), success, fail )
end,
function ( form, success, fail )
success ()
end )
else
fail ( mesg )
end
end )
end
-- mcabber
main.command ( 'register',
function ( args )
local who
if args and args ~= '' then
who = args
else
who = main.full_jid ()
end
iq_register.register ( lm.connection.bless ( main.connection () ), who,
function ( form, submit, reject )
local id = #forms + 1
forms[id] = {
form = form,
submit =
function ( form )
submit ( form,
function ()
main.print_info ( who, 'Successfully registered' )
end,
function ( mesg )
main.print_info ( who, 'Registration failed: ' .. mesg )
end )
end,
reject =
function ( form )
reject ( form,
function ()
main.print_info ( who, 'Registration cancelled' )
end,
function ( mesg )
main.print_info ( who, 'Registration cancellation failed: ' .. mesg )
end )
end,
}
print ( 'You have new form ' .. id )
end,
function ( mesg )
main.print_info ( who, 'Registration failed: ' .. mesg )
end )
end, false, 'jid' )
main.command ( 'cancel',
function ( args )
local who
if args and args ~= '' then
who = args
else
who = main.full_jid ()
end
iq_register.unregister ( lm.connection.bless ( main.connection () ), who,
function ( form, submit, reject )
if not form then
main.print_info ( who, 'Successfully unregistered' )
else
local id = #forms + 1
forms[id] = {
form = form,
submit =
function ( form )
submit ( form,
function ()
main.print_info ( who, 'Successfully unregistered' )
end,
function ( mesg )
main.print_info ( who, 'Unregistrering failed: ' .. mesg )
end )
end,
reject =
function ( form )
reject ( form,
function ()
main.print_info ( who, 'Unregistration cancelled' )
end,
function ( mesg )
main.print_info ( who, 'Unregistration cancellation failed: ' .. mesg )
end )
end,
}
print ( 'You have new form ' .. id )
end
end,
function ( mesg )
main.print_info ( who, 'Unregistering failed: ' .. mesg )
end )
end, false, 'jid' )
commands_help['register'] = "[jid]\n\nSends registration request to jid (or current buddy). You, probably, then will need to fill and send some form."
commands_help['cancel'] = "[jid]\n\nSends registration cancellation request to jid (or current buddy). May require a form filling."
-- vim: se ts=4: --