function parse_iq_register ( node )
local form = { title = 'jabber:iq:register', exp = '', val = { } }
if node:child ( 'instructions' ) then
form.exp = form.exp .. 'Instructions: ' .. node:child( 'instructions' ):value () .. '\n'
end
if node:child ( 'registered' ) then
form.exp = form.exp .. 'Registered: yes\n'
end
form.exp = form.exp .. 'Fields:\n'
local field = node:children ()
while field do
local name = field:name ()
if name ~= 'instructions' and name ~= 'registered' then
form.exp = form.exp .. ' - ' .. name .. '\n'
if field:value () and field:value () ~= '' then
form.exp = form.exp .. ' Default value: ' .. field:value () .. '\n'
end
table.insert ( form.val, { type = 'text-single', var = name, value = { field:value () or '' } } )
end
field = field:next ()
end
return insert_form ( form )
end
function format_iq_register ( fields )
local result = { xmlns = 'jabber:iq:register' }
for index, field in ipairs ( fields ) do
result[field.var] = field.value
end
return result
end
function register_to ( who )
lm.connection.bless( main.connection () ):send (
lm.message.create { mtype = 'iq-get', to = who,
query = { xmlns = 'jabber:iq:register' }
},
function ( conn, mess )
local node = mess:child ( 'query' )
if node and node:attribute ( 'xmlns' ) == 'jabber:iq:register' then
main.print_info ( who, 'D: Got:\n' .. mess:xml () )
local x = node:child ( 'x' )
local id
local finalizer =
function ( conn, mess )
local mt, st = mess:type ()
if st == 'result' then
main.print_info ( who, 'Now you can run /form del ' .. id .. ' to delete form from list' )
forms[id].status = 'acquired'
else
main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
forms[id].status = 'rejected'
end
return true
end
if x and x:attribute ( 'xmlns' ) == 'jabber:x:data' then
id = parse_form ( x )
-- local sid = mess:child( 'command' ):attribute ( 'sessionid' )
forms[id].send =
function ( form )
conn:send (
lm.message.create { mtype = 'iq-set', to = who,
query = { xmlns = 'jabber:iq:register',
x = { xmlns = 'jabber:x:data', type = 'submit',
field = form.val,
},
},
}, finalizer )
end
forms[id].status = 'filling'
main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
else
id = parse_iq_register ( mess:child ( 'query' ) )
forms[id].send =
function ( form )
conn:send (
lm.message.create { mtype = 'iq-set', to = who,
query = format_iq_register ( form.val ),
}, finalizer )
end
forms[id].status = 'filling'
main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
end
else
main.print_info ( who, 'Error response:\n' .. mess:xml () )
end
return true
end )
end
function unregister_from ( who )
lm.connection.bless( main.connection () ):send (
lm.message.create { mtype = 'iq-set', to = who,
query = { xmlns = 'jabber:iq:register',
remove = { },
},
},
function ( conn, mess )
local mt, st = mess:type ()
if st == 'result' then
main.print_info ( who, 'Registration cancelled' )
elseif st == 'error' then
if mess:child ( 'error' ) then
main.print_info ( who, 'Error response: ' .. mess:child( 'error' ):children():name () )
else
local query = mess:child ( 'query' )
if query and query:child ( 'x' ) then
local id = parse_form ( query:child ( 'x' ) )
forms[id].send =
function ( form )
conn:send (
lm.message.create { mtype = 'iq-set', to = who,
query = { xmlns = 'jabber:iq:register',
x = { xmlns = 'jabber:x:data', type = 'submit',
field = form.val,
},
},
},
function ( conn, mess )
local mt, st = mess:type ()
if st == 'result' then
main.print_info ( who, 'Now you can run /form del ' .. id .. ' to delete form from list' )
forms[id].status = 'acquired'
else
main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
forms[id].status = 'rejected'
end
return true
end )
end
forms[id].status = 'filling'
main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
else
main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
end
end
else
main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
end
return true
end )
end
main.command ( 'register',
function ( args )
local who
if args and args ~= '' then
who = args
else
who = main.full_jid ()
end
register_to ( who )
end, 'jid' )
main.command ( 'cancel',
function ( args )
local who
if args and args ~= '' then
who = args
else
who = main.full_jid ()
end
unregister_from ( who )
end, '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: --