scripts/xep0077.lua
changeset 4 bca17e4a9851
equal deleted inserted replaced
3:a5f864d4207f 4:bca17e4a9851
       
     1 
       
     2 function parse_iq_register ( node )
       
     3 	local form = { title = 'jabber:iq:register', exp = '', val = { } }
       
     4 	if node:child ( 'instructions' ) then
       
     5 		form.exp = form.exp .. 'Instructions: ' .. node:child( 'instructions' ):value () .. '\n'
       
     6 	end
       
     7 	if node:child ( 'registered' ) then
       
     8 		form.exp = form.exp .. 'Registered: yes\n'
       
     9 	end
       
    10 	form.exp = form.exp .. 'Fields:\n'
       
    11 	local field = node:children ()
       
    12 	while field do
       
    13 		local name = field:name ()
       
    14 		if name ~= 'instructions' and name ~= 'registered' then
       
    15 			form.exp = form.exp .. ' - ' .. name .. '\n'
       
    16 			if field:value () and field:value () ~= '' then
       
    17 				form.exp = form.exp .. '   Default value: ' .. field:value () .. '\n'
       
    18 			end
       
    19 			table.insert ( form.val, { type = 'text-single', var = name, value = { field:value () or '' } } )
       
    20 		end
       
    21 		field = field:next ()
       
    22 	end
       
    23 	table.insert ( forms, form )
       
    24 	return #forms
       
    25 end
       
    26 
       
    27 function format_iq_register ( fields )
       
    28 	local result = { xmlns = 'jabber:iq:register' }
       
    29 	for index, field in ipairs ( fields ) do
       
    30 		result[field.var] = field.value
       
    31 	end
       
    32 	return result
       
    33 end
       
    34 
       
    35 function register_to ( who )
       
    36 	lm.connection.bless( main.connection () ):send (
       
    37 		lm.message.create { mtype = 'iq-get', to = who,
       
    38 			query = { xmlns = 'jabber:iq:register' }
       
    39 		},
       
    40 		function ( conn, mess )
       
    41 			local node = mess:child ( 'query' )
       
    42 			if node and node:attribute ( 'xmlns' ) == 'jabber:iq:register' then
       
    43 				main.print_info ( who, 'D: Got:\n' .. mess:xml () )
       
    44 				local x = node:child ( 'x' )
       
    45 				local id
       
    46 				local finalizer =
       
    47 					function ( conn, mess )
       
    48 						local mt, st = mess:type ()
       
    49 						if st == 'result' then
       
    50 							main.print_info ( who, 'Now you can run /form del ' .. id .. ' to delete form from list' )
       
    51 							forms[id].status = 'acquired'
       
    52 						else
       
    53 							main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
       
    54 							forms[id].status = 'rejected'
       
    55 						end
       
    56 						return true
       
    57 					end
       
    58 				if x and x:attribute ( 'xmlns' ) == 'jabber:x:data' then
       
    59 					id = parse_form ( x )
       
    60 					-- local sid = mess:child( 'command' ):attribute ( 'sessionid' )
       
    61 					forms[id].send =
       
    62 						function ( form )
       
    63 							conn:send (
       
    64 								lm.message.create { mtype = 'iq-set', to = who,
       
    65 									query = { xmlns = 'jabber:iq:register',
       
    66 										x = { xmlns = 'jabber:x:data', type = 'submit',
       
    67 											field = form.val,
       
    68 										},
       
    69 									},
       
    70 								}, finalizer )
       
    71 						end
       
    72 					forms[id].status = 'filling'
       
    73 					main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
       
    74 				else
       
    75 					id = parse_iq_register ( mess:child ( 'query' ) )
       
    76 					forms[id].send =
       
    77 						function ( form )
       
    78 							conn:send (
       
    79 								lm.message.create { mtype = 'iq-set', to = who,
       
    80 									query = format_iq_register ( form.val ),
       
    81 								}, finalizer )
       
    82 						end
       
    83 					forms[id].status = 'filling'
       
    84 					main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
       
    85 				end
       
    86 			else
       
    87 				main.print_info ( who, 'Error response:\n' .. mess:xml () )
       
    88 			end
       
    89 			return true
       
    90 		end )
       
    91 end
       
    92 
       
    93 function unregister_from ( who )
       
    94 	lm.connection.bless( main.connection () ):send (
       
    95 		lm.message.create { mtype = 'iq-set', to = who,
       
    96 			query = { xmlns = 'jabber:iq:register',
       
    97 				remove = { },
       
    98 			},
       
    99 		},
       
   100 		function ( conn, mess )
       
   101 			local mt, st = mess:type ()
       
   102 			if st == 'result' then
       
   103 				main.print_info ( who, 'Registration cancelled' )
       
   104 			elseif st == 'error' then
       
   105 				if mess:child ( 'error' ) then
       
   106 					main.print_info ( who, 'Error response: ' .. mess:child( 'error' ):children():name () )
       
   107 				else
       
   108 					local query = mess:child ( 'query' )
       
   109 					if query and query:child ( 'x' ) then
       
   110 						local id = parse_form ( query:child ( 'x' ) )
       
   111 						forms[id].send =
       
   112 							function ( form )
       
   113 								conn:send (
       
   114 									lm.message.create { mtype = 'iq-set', to = who,
       
   115 										query = { xmlns = 'jabber:iq:register',
       
   116 											x = { xmlns = 'jabber:x:data', type = 'submit',
       
   117 												field = form.val,
       
   118 											},
       
   119 										},
       
   120 									},
       
   121 									function ( conn, mess )
       
   122 										local mt, st = mess:type ()
       
   123 										if st == 'result' then
       
   124 											main.print_info ( who, 'Now you can run /form del ' .. id .. ' to delete form from list' )
       
   125 											forms[id].status = 'acquired'
       
   126 										else
       
   127 											main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
       
   128 											forms[id].status = 'rejected'
       
   129 										end
       
   130 										return true
       
   131 									end )
       
   132 							end
       
   133 						forms[id].status = 'filling'
       
   134 						main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
       
   135 					else
       
   136 						main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
       
   137 					end
       
   138 				end
       
   139 			else
       
   140 				main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
       
   141 			end
       
   142 			return true
       
   143 		end )
       
   144 end
       
   145 
       
   146 main.add_command ( 'register',
       
   147 	function ( args )
       
   148 		local who
       
   149 		if args[1] then
       
   150 			who = rebuild_args_string ( args )
       
   151 		else
       
   152 			who = full_current_jid ()
       
   153 		end
       
   154 		register_to ( who )
       
   155 	end )
       
   156 
       
   157 main.add_command ( 'cancel',
       
   158 	function ( args )
       
   159 		local who
       
   160 		if args and args ~= '' then
       
   161 			who = args
       
   162 		else
       
   163 			who = full_current_jid ()
       
   164 		end
       
   165 		unregister_from ( who )
       
   166 	end )
       
   167 
       
   168 commands_help['register'] = "[jid]\n\nSends registration request to jid (or current buddy). You, probably, then will need to fill and send some form."
       
   169 commands_help['cancel'] = "[jid]\n\nSends registration cancellation request to jid (or current buddy). May require a form filling."
       
   170 
       
   171 -- vim: se ts=4: --