examples/xep0004.lua
changeset 24 25552b21d3fb
parent 20 fd96da2c3acc
child 27 92b254b64360
equal deleted inserted replaced
23:e441162b1386 24:25552b21d3fb
     1 
     1 
     2 forms = { }
     2 forms = { }
     3 form_cid = 0
     3 form_cid = main.add_category { 'del', 'send' }
     4 
     4 
     5 function insert_form ( form )
     5 function insert_form ( form )
     6 	table.insert ( forms, form )
     6 	table.insert ( forms, form )
     7 	main.add_completion ( form_cid, tostring(#forms) )
     7 	main.add_completion ( form_cid, tostring(#forms) )
     8 	return #forms
     8 	return #forms
   103 			end
   103 			end
   104 		end
   104 		end
   105 	end
   105 	end
   106 end
   106 end
   107 
   107 
   108 form_cid = main.command ( 'form',
   108 main.command ( 'form',
   109 	function ( args )
   109 	function ( args )
   110 		args = parse_args ( args )
   110 		args = parse_args ( args )
   111 		local action = args[1]
   111 		local action = args[1]
   112 		local id = tonumber (args[1])
   112 		local id = tonumber (args[1])
   113 		if forms[id] then
   113 		if forms[id] then
   114 			if args[2] then
   114 			local field = args[2]
   115 				local field = args[2]
   115 			if field then
   116 				if args[3] == 'clear' then
   116 				local value = args[3]
   117 					form_set ( id, field, nil )
   117 				if value == 'clear' then
   118 				else
   118 					value = nil
   119 					args[1] = nil
   119 				elseif value == 'set' then
   120 					args[2] = nil
   120 					value = args[4]
   121 					if args[3] == 'set' then
       
   122 						args[3] = nil
       
   123 					end
       
   124 					form_set ( id, field, rebuild_args_string ( args ) )
       
   125 				end
   121 				end
       
   122 				form_set ( id, field, value )
   126 			else
   123 			else
   127 				print ( 'Form: ' .. ( forms[id].title or '' ) .. '\n' .. forms[id].exp )
   124 				print ( 'Form: ' .. ( forms[id].title or '' ) .. '\n' .. forms[id].exp )
   128 				print ( 'Fields:' )
   125 				local text = 'Fields:'
   129 				for index, field in ipairs ( forms[id].val ) do -- this should not be here, but setting up callback just for this...
   126 				for index, field in ipairs ( forms[id].val ) do -- this should not be here, but setting up callback just for this...
   130 					if field.type == 'jid-multi' or field.type == 'list-multi' or field.type == 'text-multi' then
   127 					if field.type == 'jid-multi' or field.type == 'list-multi' or field.type == 'text-multi' then
   131 						print ( ' - ' .. field.var .. ' [' .. field.type .. ']:' )
   128 						text = text .. '\n - ' .. field.var .. ' [' .. field.type .. ']:'
   132 						for vin, value in ipairs ( field.value ) do
   129 						for vin, value in ipairs ( field.value ) do
   133 							print ( '    * ' .. value[1] )
   130 							text = text .. '\n    * ' .. value[1]
   134 						end
   131 						end
   135 					else
   132 					else
   136 						print ( ' - ' .. field.var .. ' [' .. field.type .. ']: ' .. field.value[1] )
   133 						text = text .. '\n - ' .. field.var .. ' [' .. field.type .. ']: ' .. field.value[1]
   137 					end
   134 					end
   138 				end
   135 				end
       
   136 				print ( text )
   139 			end
   137 			end
   140 		elseif action == 'del' then
   138 		elseif action == 'del' then
   141 			forms[tonumber(args[2])] = nil
   139 			forms[tonumber(args[2])] = nil
   142 			main.del_completion ( form_cid, tostring(args[2]) )
   140 			main.del_completion ( form_cid, tostring(args[2]) )
   143 		elseif action == 'send' then
   141 		elseif action == 'send' then
   144 			local form = forms[tonumber(args[2])]
   142 			local form = forms[tonumber(args[2])]
   145 			if form then
   143 			if form then
   146 				form.send ( form )
   144 				form.send ( form )
   147 			end
   145 			end
   148 		else
   146 		else
   149 			print ( 'Forms list:' )
   147 			local text = ''
   150 			for id, form in ipairs ( forms ) do
   148 			for id, form in pairs ( forms ) do
   151 				print ( ' - ' .. tostring(id) .. ' ' .. form.title .. ' [' .. ( form.status or 'unknown' ) .. ']' )
   149 				text = text .. '\n - ' .. id .. ' ' .. form.title .. ' [' .. ( form.status or 'unknown' ) .. ']'
       
   150 			end
       
   151 			if text ~= '' then
       
   152 				print ( 'Forms list:' .. text )
       
   153 			else
       
   154 				print ( 'No forms' )
   152 			end
   155 			end
   153 		end
   156 		end
   154 	end, { "del", "send" } )
   157 	end, form_cid )
   155 
   158 
   156 commands_help['form'] = "[del form_id | send form_id | form_id [field_name {clear | [set] value}]\n\nWith bare form id prints info on that form.\nWith field name sets or clears field value. Set subcommand is optional, just to allow values, starting with 'clear'.\nWithout arguments prints form list."
   159 commands_help['form'] = "[del form_id | send form_id | form_id [field_name {clear | [set] value}]\n\nWith bare form id prints info on that form.\nWith field name sets or clears field value. Set subcommand is optional, just to allow values, starting with 'clear'.\nWithout arguments prints form list."
   157 
   160 
   158 -- vim: se ts=4: --
   161 -- vim: se ts=4: --