examples/vcard.lua
changeset 67 d33ca5572e91
parent 66 542f61e113cb
child 68 742878c74b8e
--- a/examples/vcard.lua	Fri Mar 27 12:06:19 2009 +0200
+++ b/examples/vcard.lua	Sat Mar 28 19:43:12 2009 +0200
@@ -3,48 +3,86 @@
 
 -- library
 
-local lm = require 'lm'
-local iq = require 'iq'
-require 'x_data'
+local iq         = require 'iq'
+local form_field = require 'form_field'
+
+--
+
+local F = { }
+local M = { }
+M.__index = M
 
--- public
+function F.new ( args )
+	local form = {
+		xmlns        = 'vcard-temp',
+		ftype        = args.type or 'form',
+		title        = args.title,
+		instructions = args.instructions,
+		f            = { },
+	}
+	setmetatable ( form, M )
+	return form
+end
 
-vcard = { }
-
-local function vcard_parse ( node, path, ret )
+local function vcard_parse ( node, path, form )
 	local item = node:child ()
 	if item then
 		while item do
-			vcard_parse ( item, ( path and path .. '/' or '' ) .. item:name (), ret )
+			vcard_parse ( item, ( path and path .. '/' or '' ) .. item:name (), form )
 			item = item:next ()
 		end
 	elseif path then
-		table.insert ( ret, { var = path, type = 'text-single', value = node:value () } )
+		form:add ( path, { type = 'text-single', value = node:value () } )
 	end
 end
 
-function vcard.parse ( card )
-	local form = { xmlns = 'vcard-temp', type = 'form' }
+function F.parse ( card )
+	local form = F.new { type = 'form' }
 	vcard_parse ( card, nil, form )
-	form.format =
-		function ( form, root )
-			root.vCard = { xmlns = 'vcard-temp' }
-			for k, field in ipairs ( form ) do
-				local el = root.vCard
-				for k in field.var:gmatch ( '[^/]+' ) do
-					if not el[k] then
-						el[k] = { }
-					end
-					el = el[k]
-				end
-				el[1] = field.value
-			end
-			return root
-		end
 	return form
 end
 
-function vcard.get ( conn, from, success, fail )
+function M.type ( form )
+	return form.ftype
+end
+
+function M.desc ( form )
+	return form.title, form.instructions
+end
+
+function M.format ( form, root, format_as )
+	root.vCard = { xmlns = 'vcard-temp' }
+	for k, field in form:fields () do
+		local el = root.vCard
+		for k in field:name():gmatch ( '[^/]+' ) do
+			if not el[k] then
+				el[k] = { }
+			end
+			el = el[k]
+		end
+		el[1] = field:value ()
+	end
+	return root
+end
+
+function M.fields ( form )
+	return ipairs ( form.f )
+end
+
+function M.add ( form, name, fld )
+	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
+
+function M.field ( form, name )
+	return form.f[name]
+end
+
+function F.retrieve ( conn, from, success, fail )
 	iq.send ( conn, from, 'get',
 		{
 			vCard = { xmlns = 'vcard-temp' },
@@ -52,10 +90,9 @@
 		function ( mess )
 			local card = mess:child ( 'vCard' )
 			if card and card:attribute ( 'xmlns' ) == 'vcard-temp' then
-				success ( vcard.parse ( mess:child ( 'vCard' ) ),
+				success ( F.parse ( mess:child ( 'vCard' ) ),
 					function ( form, success, fail )
-						form.type = 'submit' -- :)
-						iq.send ( conn, from, 'set', form.format ( form, { } ), success, fail )
+						iq.send ( conn, from, 'set', form:format ( form, { }, 'submit' ), success, fail )
 					end,
 					function ( form, success, fail )
 						success ()
@@ -66,41 +103,6 @@
 		end, fail )
 end
 
--- mcabber
-
-main.command ( 'vcard-temp',
-	function ( args )
-		vcard.get ( lm.connection.bless ( main.connection () ), args[1],
-			function ( form, submit, reject )
-				local id = #forms + 1
-				forms[id] = {
-					form = form,
-					submit =
-						function ( form )
-							submit ( form,
-								function ()
-									print ( 'Vcard changed' )
-								end,
-								function ( mesg )
-									print ( 'Vcard changing error: ' .. mesg )
-								end )
-						end,
-					reject =
-						function ( form )
-							reject ( form,
-								function ()
-									print ( 'Vcard changing cancelled' )
-								end,
-								function ( mesg )
-									print ( 'Vcard changing cancellation error: ' .. mesg )
-								end )
-						end,
-				}
-				print ( 'You have new form ' .. id )
-			end,
-			function ( mesg )
-				print ( 'Vcard obtaining error: ' .. mesg )
-			end )
-	end, true, 'jid' )
+return F
 
 -- vim: se ts=4: --