--- a/examples/iq_register.lua Fri Mar 27 12:06:19 2009 +0200
+++ b/examples/iq_register.lua Sat Mar 28 19:43:12 2009 +0200
@@ -3,60 +3,127 @@
-- library
-local lm = require 'lm'
-local iq = require 'iq'
---local x_data =
-require 'x_data'
+local iq = require 'iq'
+local x_data = require 'x_data'
+local form_field = require 'form_field'
+
+--
+
+local F = { }
+local M = { }
+M.__index = M
--- public
-
-iq_register = { }
+function F.new ( args )
+ local form = {
+ xmlns = 'jabber:iq:register',
+ ftype = args.type or 'form',
+ registered = args.registered,
+ -- XXX title
+ instructions = args.instructions,
+ x_data = args.x_data,
+ f = { },
+ }
+ setmetatable ( form, M )
+ return form
+end
-function iq_register.parse ( query )
- local form = { xmlns = 'jabber:iq:register', type = 'form' }
-
+function F.parse ( query )
local instructions = query:child ( 'instructions' )
if instructions then
- form.instructions = instructions:value ()
+ instructions = instructions:value ()
end
- -- XXX how it can be mapped to common form?
- -- and needs it be supplied?
+
+ local registered
if query:child ( 'registered' ) then
- form.registered = true
+ 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
+ return F.new { type = 'form', registered = registered, instructions = instructions, x_data = x_data.parse ( x ) }
end
+ local form = F.new { type = 'form', registered = registered, instructions = instructions }
+
local field = query:child ()
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 '' } )
+ form:add ( name, { type = 'text-single', 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 )
+function M.type ( form )
+ return form.ftype
+end
+
+function M.desc ( form )
+ if form.x_data then
+ return form.x_data:desc ()
+ else
+ return form.title, form.instructions
+ end
+end
+
+function M.format ( form, root, format_as )
+ local ft = format_as or form:type ()
+
+ if form.x_data then
+ root.query = form.x_data:format ( { xmlns = 'jabber:iq:register' }, ft )
+ else
+ root.query = { xmlns = 'jabber:iq:register' }
+ for index, field in form:fields () do
+ root.query[field:name ()] = field:value ()
+ end
+ end
+
+ if ft == 'form' then
+ local title, instructions = form:desc ()
+ if instructions then
+ root.query.instructions = { instructions }
+ end
+ if form.registered then
+ root.query.registered = { }
+ end
+ end
+
+ return root
+end
+
+function M.fields ( form )
+ if form.x_data then
+ return form.x_data:fields ()
+ else
+ return ipairs ( form.f )
+ end
+end
+
+function M.add ( form, name, fld )
+ if form.x_data then
+ return form.x_data:add ( name, fld )
+ else
+ fld.var = name
+ fld.index = #form.f + 1
+ local obj = form_field.new ( fld )
+ table.insert ( form.f, obj )
+ form.f[name] = obj
+ return obj
+ end
+end
+
+function M.field ( form, name )
+ if form.x_data then
+ return form.x_data:field ( name )
+ else
+ return form.f[name]
+ end
+end
+
+function F.register ( conn, to, success, fail )
iq.send ( conn, to, 'get',
{
query = { xmlns = 'jabber:iq:register' },
@@ -64,10 +131,9 @@
function ( mess )
local query = mess:child ( 'query' )
if query and query:attribute ( 'xmlns' ) == 'jabber:iq:register' then
- success ( iq_register.parse ( query ),
+ success ( F.parse ( query ),
function ( form, success, fail )
- form.type = 'submit' -- XXX
- iq.send ( conn, to, 'set', form.format ( form, { } ), success , fail )
+ iq.send ( conn, to, 'set', form:format ( form, { }, 'submit' ), success , fail )
end,
function ( form, success, fail )
success ()
@@ -76,7 +142,7 @@
end, fail )
end
-function iq_register.unregister ( conn, to, success, fail )
+function F.unregister ( conn, to, success, fail )
iq.send ( conn, to, 'set',
{
query = { xmlns = 'jabber:iq:register',
@@ -89,10 +155,9 @@
function ( mesg, mess )
local query = mess:child ( 'query' )
if query and query:attribute ( 'xmlns' ) == 'jabber:iq:register' then
- success ( iq_register.parse ( query ),
+ success ( F.parse ( query ),
function ( form, success, fail )
- form.type = 'submit' -- XXX
- iq.send ( conn, to, 'set', form.format ( form, { } ), success, fail )
+ iq.send ( conn, to, 'set', form:format ( form, { }, 'submit' ), success, fail )
end,
function ( form, success, fail )
success ()
@@ -103,94 +168,6 @@
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."
+return F
-- vim: se ts=4: --