examples/pubsub.lua
changeset 51 a95a3a73482c
parent 47 aba4bbe32cba
child 53 2162188b20cf
equal deleted inserted replaced
50:12d8dd774fcc 51:a95a3a73482c
     3 
     3 
     4 -- library
     4 -- library
     5 
     5 
     6 require 'lm'
     6 require 'lm'
     7 require 'iq'
     7 require 'iq'
       
     8 require 'x_data'
     8 
     9 
     9 -- public
    10 -- public
    10 
    11 
    11 pubsub = { }
    12 pubsub = { }
    12 
    13 
    66 				local item = items:children ()
    67 				local item = items:children ()
    67 				while item do
    68 				while item do
    68 					success ( from, node, item ) -- XXX use registered xmlns handlers for that?
    69 					success ( from, node, item ) -- XXX use registered xmlns handlers for that?
    69 					item = item:next ()
    70 					item = item:next ()
    70 				end
    71 				end
       
    72 			else
       
    73 				-- XXX
    71 			end
    74 			end
    72 		end, fail )
    75 		end, fail )
    73 end
    76 end
    74 
    77 
    75 -- OWNER USE CASES
    78 -- OWNER USE CASES
   119 		{
   122 		{
   120 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   123 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   121 				configure = { node = node },
   124 				configure = { node = node },
   122 			},
   125 			},
   123 		},
   126 		},
   124 		function ( mess ) -- FIXME
   127 		function ( mess )
   125 			local x = mess:path ( 'pubsub', 'configure', 'x' )
   128 			local x = mess:path ( 'pubsub', 'configure', 'x' )
   126 			if x then
   129 			if x then
   127 				local fid = parse_form ( x )
   130 				success ( x_data.parse ( x ),
   128 				forms[fid].send =
   131 					function ( form, success, fail )
   129 					function ( form )
   132 						iq.send ( conn, to, 'set',
       
   133 							{
       
   134 								pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
   135 									configure = form.format ( form, { node = node } ),
       
   136 								},
       
   137 							}, success, fail )
       
   138 					end,
       
   139 					function ( form, success, fail )
   130 						iq.send ( conn, to, 'set',
   140 						iq.send ( conn, to, 'set',
   131 							{
   141 							{
   132 								pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   142 								pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   133 									configure = { node = node,
   143 									configure = { node = node,
   134 										x = { xmlns = 'jabber:x:data', type = 'submit',
   144 										x = { xmlns = 'jabber:x:data', type = 'cancel' }, -- FIXME
   135 											field = form.val,
       
   136 										},
       
   137 									},
   145 									},
   138 								},
   146 								},
   139 							},
   147 							}, success, fail )
   140 							function ( mess )
   148 					end )
   141 								success ()
   149 			else
   142 								main.print_info ( to, 'Now you can run /form del ' .. fid .. ' to delete form from list' )
   150 				fail ( mess:xml () ) -- XXX
   143 								form.status = 'acquired'
       
   144 							end,
       
   145 							function ( mesg )
       
   146 								if mesg then
       
   147 									form.status = 'rejected'
       
   148 								else
       
   149 									form.status = 'unknown'
       
   150 								end
       
   151 								fail ( mesg )
       
   152 							end )
       
   153 						form.status = 'sent'
       
   154 					end
       
   155 				forms[fid].status = 'filling'
       
   156 				main.print_info ( to, 'You have new form. To fill it, use /form ' .. fid .. ' fieldname value' )
       
   157 			else
       
   158 				main.print_info ( to, 'Weird, no error and no node configuration form: ' .. mess:xml () )
       
   159 			end
   151 			end
   160 		end, fail )
   152 		end, fail )
   161 end
   153 end
   162 
   154 
   163 function pubsub.list_subscriptions ( conn, to, node, success, fail )
   155 function pubsub.list_subscriptions ( conn, to, node, success, fail )
   259 				function ( mesg )
   251 				function ( mesg )
   260 					main.print_info ( who, 'Node purge failed: ' .. mesg )
   252 					main.print_info ( who, 'Node purge failed: ' .. mesg )
   261 				end )
   253 				end )
   262 		elseif action:sub ( 1, 4 ) == 'conf' then
   254 		elseif action:sub ( 1, 4 ) == 'conf' then
   263 			pubsub.configure_node ( conn, who, node,
   255 			pubsub.configure_node ( conn, who, node,
   264 				function ()
   256 				function ( form, submit, reject )
   265 					main.print_info ( who, 'Node configuration accepted' )
   257 					local id = #forms + 1
       
   258 					forms[id] = {
       
   259 						form = form,
       
   260 						submit =
       
   261 							function ( form )
       
   262 								submit ( form,
       
   263 									function ()
       
   264 										main.print_info ( who, 'Node configuration completed' )
       
   265 									end,
       
   266 									function ( mesg )
       
   267 										main.print_info ( who, 'Node configuration failed: ' .. mesg )
       
   268 									end )
       
   269 							end,
       
   270 						reject =
       
   271 							function ( form )
       
   272 								reject ( form,
       
   273 									function ()
       
   274 										main.print_info ( who, 'Node configuration cancelled' )
       
   275 									end,
       
   276 									function ( mesg )
       
   277 										main.print_info ( who, 'Node configuration cancellation failed: ' .. mesg )
       
   278 									end )
       
   279 							end,
       
   280 					}
       
   281 					print ( 'You have new form ' .. id )
   266 				end,
   282 				end,
   267 				function ( mesg )
   283 				function ( mesg )
   268 					main.print_info ( who, 'Node configuration failed: ' .. mesg )
   284 					main.print_info ( who, 'Node configuration failed: ' .. mesg )
   269 				end )
   285 				end )
   270 		elseif action == 'subscriptions' or action == 'subscribers' then
   286 		elseif action == 'subscriptions' or action == 'subscribers' then