examples/iq_register.lua
changeset 49 95f3bf77c598
child 53 2162188b20cf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/iq_register.lua	Sun Mar 22 04:14:36 2009 +0200
@@ -0,0 +1,197 @@
+
+-- IN-BAND REGISTRATION (XEP-0077)
+
+-- FIXME this is not yet finished, as format of form is undecided yet
+
+-- 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:attribute ( 'xmlns' ) == 'jabber:x:data' then
+		form = form.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: --