examples/iq_register.lua
changeset 49 95f3bf77c598
child 53 2162188b20cf
equal deleted inserted replaced
48:d31ae73038f7 49:95f3bf77c598
       
     1 
       
     2 -- IN-BAND REGISTRATION (XEP-0077)
       
     3 
       
     4 -- FIXME this is not yet finished, as format of form is undecided yet
       
     5 
       
     6 -- library
       
     7 
       
     8 require 'lm'
       
     9 require 'iq'
       
    10 require 'x_data'
       
    11 
       
    12 -- public
       
    13 
       
    14 iq_register = { }
       
    15 
       
    16 function iq_register.parse ( query )
       
    17 	local form  = { xmlns = 'jabber:iq:register', type = 'form' }
       
    18 
       
    19 	local instructions = query:child ( 'instructions' )
       
    20 	if instructions then
       
    21 		form.instructions = instructions:value ()
       
    22 	end
       
    23 	-- XXX how it can be mapped to common form?
       
    24 	--     and needs it be supplied?
       
    25 	if query:child ( 'registered' ) then
       
    26 		form.registered = true
       
    27 	end
       
    28 
       
    29 	local x = query:child ( 'x' )
       
    30 	if x:attribute ( 'xmlns' ) == 'jabber:x:data' then
       
    31 		form = form.parse ( x )
       
    32 		local format = form.format
       
    33 		form.format =
       
    34 			function ( form, root )
       
    35 					root.query = format ( form, { xmlns = 'jabber:iq:register' } )
       
    36 					return root
       
    37 			end
       
    38 		return form
       
    39 	end
       
    40 
       
    41 	local field = query:children ()
       
    42 	while field do
       
    43 		local name  = field:name ()
       
    44 		if name ~= 'instructions' and name ~= 'registered' then
       
    45 			table.insert ( form, { type = 'text-single', var = name, value = { field:value () or '' } } )
       
    46 		end
       
    47 		field = field:next ()
       
    48 	end
       
    49 	form.format =
       
    50 		function ( form, root )
       
    51 			root.query = { xmlns = 'jabber:iq:register' }
       
    52 			for index, field in ipairs ( form ) do
       
    53 				root.query[field.var] = field.value
       
    54 			end
       
    55 			return root
       
    56 		end
       
    57 	return form
       
    58 end
       
    59 
       
    60 function iq_register.register ( conn, to, success, fail )
       
    61 	iq.send ( conn, to, 'get',
       
    62 		{
       
    63 			query = { xmlns = 'jabber:iq:register' },
       
    64 		},
       
    65 		function ( mess )
       
    66 			local query = mess:child ( 'query' )
       
    67 			if query and query:attribute ( 'xmlns' ) == 'jabber:iq:register' then
       
    68 				success ( iq_register.parse ( query ),
       
    69 					function ( form, success, fail )
       
    70 						form.type = 'submit' -- XXX
       
    71 						iq.send ( conn, to, 'set', form.format ( form, { } ), success , fail )
       
    72 					end,
       
    73 					function ( form, success, fail )
       
    74 						success ()
       
    75 					end )
       
    76 			end
       
    77 		end, fail )
       
    78 end
       
    79 
       
    80 function iq_register.unregister ( conn, to, success, fail )
       
    81 	iq.send ( conn, to, 'set',
       
    82 		{
       
    83 			query = { xmlns = 'jabber:iq:register',
       
    84 				remove = { },
       
    85 			},
       
    86 		},
       
    87 		function ( mess )
       
    88 			success ()
       
    89 		end,
       
    90 		function ( mesg, mess )
       
    91 			local query = mess:child ( 'query' )
       
    92 			if query and query:attribute ( 'xmlns' ) == 'jabber:iq:register' then
       
    93 				success ( iq_register.parse ( query ),
       
    94 					function ( form, success, fail )
       
    95 						form.type = 'submit' -- XXX
       
    96 						iq.send ( conn, to, 'set', form.format ( form, { } ), success, fail )
       
    97 					end,
       
    98 					function ( form, success, fail )
       
    99 						success ()
       
   100 					end )
       
   101 			else
       
   102 				fail ( mesg )
       
   103 			end
       
   104 		end )
       
   105 end
       
   106 
       
   107 -- mcabber
       
   108 
       
   109 main.command ( 'register',
       
   110 	function ( args )
       
   111 		local who
       
   112 		if args and args ~= '' then
       
   113 			who = args
       
   114 		else
       
   115 			who = main.full_jid ()
       
   116 		end
       
   117 		iq_register.register ( lm.connection.bless ( main.connection () ), who,
       
   118 			function ( form, submit, reject )
       
   119 				local id = #forms + 1
       
   120 				forms[id] = {
       
   121 					form   = form,
       
   122 					submit =
       
   123 						function ( form )
       
   124 							submit ( form,
       
   125 								function ()
       
   126 									main.print_info ( who, 'Successfully registered' )
       
   127 								end,
       
   128 								function ( mesg )
       
   129 									main.print_info ( who, 'Registration failed: ' .. mesg )
       
   130 								end )
       
   131 						end,
       
   132 					reject =
       
   133 						function ( form )
       
   134 							reject ( form,
       
   135 								function ()
       
   136 									main.print_info ( who, 'Registration cancelled' )
       
   137 								end,
       
   138 								function ( mesg )
       
   139 									main.print_info ( who, 'Registration cancellation failed: ' .. mesg )
       
   140 								end )
       
   141 						end,
       
   142 				}
       
   143 				print ( 'You have new form ' .. id )
       
   144 			end,
       
   145 			function ( mesg )
       
   146 				main.print_info ( who, 'Registration failed: ' .. mesg )
       
   147 			end )
       
   148 	end, false, 'jid' )
       
   149 main.command ( 'cancel',
       
   150 	function ( args )
       
   151 		local who
       
   152 		if args and args ~= '' then
       
   153 			who = args
       
   154 		else
       
   155 			who = main.full_jid ()
       
   156 		end
       
   157 		iq_register.unregister ( lm.connection.bless ( main.connection () ), who,
       
   158 			function ( form, submit, reject )
       
   159 				if not form then
       
   160 					main.print_info ( who, 'Successfully unregistered' )
       
   161 				else
       
   162 					local id = #forms + 1
       
   163 					forms[id] = {
       
   164 						form   = form,
       
   165 						submit =
       
   166 							function ( form )
       
   167 								submit ( form,
       
   168 									function ()
       
   169 										main.print_info ( who, 'Successfully unregistered' )
       
   170 									end,
       
   171 									function ( mesg )
       
   172 										main.print_info ( who, 'Unregistrering failed: ' .. mesg )
       
   173 									end )
       
   174 							end,
       
   175 						reject =
       
   176 							function ( form )
       
   177 								reject ( form,
       
   178 									function ()
       
   179 										main.print_info ( who, 'Unregistration cancelled' )
       
   180 									end,
       
   181 									function ( mesg )
       
   182 										main.print_info ( who, 'Unregistration cancellation failed: ' .. mesg )
       
   183 									end )
       
   184 							end,
       
   185 					}
       
   186 					print ( 'You have new form ' .. id )
       
   187 				end
       
   188 			end,
       
   189 			function ( mesg )
       
   190 				main.print_info ( who, 'Unregistering failed: ' .. mesg )
       
   191 			end )
       
   192 	end, false, 'jid' )
       
   193 
       
   194 commands_help['register'] = "[jid]\n\nSends registration request to jid (or current buddy). You, probably, then will need to fill and send some form."
       
   195 commands_help['cancel'] = "[jid]\n\nSends registration cancellation request to jid (or current buddy). May require a form filling."
       
   196 
       
   197 -- vim: se ts=4: --