examples/xep0060.lua
changeset 26 fc83934f9b8d
parent 25 38c68c285e41
child 27 92b254b64360
equal deleted inserted replaced
25:38c68c285e41 26:fc83934f9b8d
       
     1 
       
     2 -- SUBSCRIBER USE CASES
     1 
     3 
     2 function pubsub_subscribe ( to, node )
     4 function pubsub_subscribe ( to, node )
     3 	local conn = lm.connection.bless ( main.connection () )
     5 	local conn = lm.connection.bless ( main.connection () )
     4 	local jid = conn:jid():gsub ( '/.*', '' )
     6 	local jid = conn:jid():gsub ( '/.*', '' )
     5 	conn:send (
     7 	conn:send (
     6 		lm.message.create { mtype = 'iq-set', to = to, from = conn:jid (),
     8 		lm.message.create { mtype = 'iq-set', to = to, -- from = conn:jid (),
     7 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
     9 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
     8 				subscribe = { node = node, jid = jid },
    10 				subscribe = { node = node, jid = jid },
     9 			},
    11 			},
    10 		},
    12 		},
    11 		function ( conn, mess )
    13 		function ( conn, mess )
    24 				end
    26 				end
    25 				main.print_info ( to, info )
    27 				main.print_info ( to, info )
    26 				return true
    28 				return true
    27 			elseif smtype == 'error' then
    29 			elseif smtype == 'error' then
    28 				main.print_info ( to, 'Error response for subscription request: ' .. mess:xml () ) -- FIXME
    30 				main.print_info ( to, 'Error response for subscription request: ' .. mess:xml () ) -- FIXME
       
    31 				-- XXX: handle configuration forms?
    29 				return true
    32 				return true
    30 			else
    33 			else
    31 				print ( 'Weird response for subscription request: ' .. mess:xml () )
    34 				print ( 'Weird response for subscription request: ' .. mess:xml () )
    32 				return false
    35 				return false
    33 			end
    36 			end
    56 				return false
    59 				return false
    57 			end
    60 			end
    58 		end )
    61 		end )
    59 end
    62 end
    60 
    63 
       
    64 -- I found no servers with subscription options support thus it is not implemented.
       
    65 
       
    66 -- untested :(
       
    67 function pubsub_retrieve ( to, node, handler, max, ids )
       
    68 	local items = { node = node, max_items = max }
       
    69 	if ids then
       
    70 		items.item = { }
       
    71 		for k, id in pairs ( ids ) do
       
    72 			table.insert ( items.item, { id = id } )
       
    73 		end
       
    74 	end
       
    75 	local conn = lm.connection.bless ( main.connection () )
       
    76 	conn:send (
       
    77 		lm.message.create { mtype = 'iq-set', to = to,
       
    78 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
       
    79 				items = items,
       
    80 			},
       
    81 		},
       
    82 		function ( conn, mess )
       
    83 			local mtype, smtype = mess:type ()
       
    84 			if smtype == 'result' then
       
    85 				local items = mess:path ( 'pubsub', 'items' )
       
    86 				if items then
       
    87 					local item = items:children ()
       
    88 					while item do
       
    89 						-- we cannot know, what item contains
       
    90 						handler ( item )
       
    91 						item = item:next ()
       
    92 					end
       
    93 				end
       
    94 				return true
       
    95 			elseif smtype == 'error' then
       
    96 				main.print_info ( to, 'Error response for items retrieval request: ' .. mess:xml () ) -- FIXME
       
    97 				return true
       
    98 			else
       
    99 				print ( 'Weird response for items retrieval request: ' .. mess:xml () )
       
   100 				return false
       
   101 			end
       
   102 		end )
       
   103 end
       
   104 
       
   105 -- OWNER USE CASES
       
   106 
       
   107 -- node may be nil
       
   108 function pubsub_create_node ( to, node )
       
   109 	local conn = lm.connection.bless ( main.connection () )
       
   110 	conn:send (
       
   111 		lm.message.create { mtype = 'iq-set', to = to,
       
   112 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
       
   113 				create = { node = node },
       
   114 			},
       
   115 		},
       
   116 		function ( conn, mess )
       
   117 			local mtype, smtype = mess:type ()
       
   118 			if smtype == 'result' then
       
   119 				if node then
       
   120 					main.print_info ( to, 'Node ' .. node .. ' successfully created' )
       
   121 				else
       
   122 					local create = mess:path ( 'pubsub', 'create' )
       
   123 					if create then
       
   124 						local nid = create:attribute ( 'node' )
       
   125 						main.print_info ( to, 'Node created, id ' .. (nid or 'unspecified') )
       
   126 					else
       
   127 						main.print_info ( to, 'Node created, but no id provieded' )
       
   128 					end
       
   129 				end
       
   130 				return true
       
   131 			elseif smtype == 'error' then
       
   132 				main.print_info ( to, 'Error response for node creation request: ' .. mess:xml () ) -- FIXME
       
   133 				return true
       
   134 			else
       
   135 				print ( 'Weird response for node creation request: ' .. mess:xml () )
       
   136 				return false
       
   137 			end
       
   138 		end )
       
   139 end
       
   140 
       
   141 function pubsub_delete_node ( to, node )
       
   142 	local conn = lm.connection.bless ( main.connection () )
       
   143 	conn:send (
       
   144 		lm.message.create { mtype = 'iq-set', to = to,
       
   145 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
   146 				delete = { node = node },
       
   147 			},
       
   148 		},
       
   149 		function ( conn, mess )
       
   150 			local mtype, smtype = mess:type ()
       
   151 			if smtype == 'result' then
       
   152 				main.print_info ( to, 'Node ' .. node .. ' successfully deleted' )
       
   153 				return true
       
   154 			elseif smtype == 'error' then
       
   155 				main.print_info ( to, 'Error response for node deletion request: ' .. mess:xml () ) -- FIXME
       
   156 				return true
       
   157 			else
       
   158 				print ( 'Weird response for node deletion request: ' .. mess:xml () )
       
   159 				return false
       
   160 			end
       
   161 		end )
       
   162 end
       
   163 
       
   164 function pubsub_purge_node ( to, node )
       
   165 	local conn = lm.connection.bless ( main.connection () )
       
   166 	conn:send (
       
   167 		lm.message.create { mtype = 'iq-set', to = to,
       
   168 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
   169 				purge = { node = node },
       
   170 			},
       
   171 		},
       
   172 		function ( conn, mess )
       
   173 			local mtype, smtype = mess:type ()
       
   174 			if smtype == 'result' then
       
   175 				main.print_info ( to, 'Node ' .. node .. ' items successfully purged' )
       
   176 				return true
       
   177 			elseif smtype == 'error' then
       
   178 				main.print_info ( to, 'Error response for node items purging request: ' .. mess:xml () ) -- FIXME
       
   179 				return true
       
   180 			else
       
   181 				print ( 'Weird response for node items purging request: ' .. mess:xml () )
       
   182 				return false
       
   183 			end
       
   184 		end )
       
   185 end
       
   186 
    61 function pubsub_configure_node ( to, node )
   187 function pubsub_configure_node ( to, node )
    62 	local conn = lm.connection.bless ( main.connection () )
   188 	local conn = lm.connection.bless ( main.connection () )
    63 	local jid = conn:jid():gsub ( '/.*', '' )
       
    64 	conn:send (
   189 	conn:send (
    65 		lm.message.create { mtype = 'iq-get', to = to,
   190 		lm.message.create { mtype = 'iq-get', to = to,
    66 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   191 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
    67 				configure = { node = node },
   192 				configure = { node = node },
    68 			},
   193 			},
   118 		end )
   243 		end )
   119 end
   244 end
   120 
   245 
   121 function pubsub_list_subscriptions ( to, node )
   246 function pubsub_list_subscriptions ( to, node )
   122 	local conn = lm.connection.bless ( main.connection () )
   247 	local conn = lm.connection.bless ( main.connection () )
   123 	local jid = conn:jid():gsub ( '/.*', '' )
       
   124 	conn:send (
   248 	conn:send (
   125 		lm.message.create { mtype = 'iq-get', to = to,
   249 		lm.message.create { mtype = 'iq-get', to = to,
   126 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   250 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   127 				subscriptions = { node = node },
   251 				subscriptions = { node = node },
   128 			},
   252 			},
   153 		end )
   277 		end )
   154 end
   278 end
   155 
   279 
   156 function pubsub_modify_subscription ( to, node, who, state, id )
   280 function pubsub_modify_subscription ( to, node, who, state, id )
   157 	local conn = lm.connection.bless ( main.connection () )
   281 	local conn = lm.connection.bless ( main.connection () )
   158 	local jid = conn:jid():gsub ( '/.*', '' )
       
   159 	conn:send (
   282 	conn:send (
   160 		lm.message.create { mtype = 'iq-get', to = to,
   283 		lm.message.create { mtype = 'iq-get', to = to,
   161 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   284 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
   162 				subscriptions = { node = node,
   285 				subscriptions = { node = node,
   163 					subscription = { jid = who, subscription = state, id = id },
   286 					subscription = { jid = who, subscription = state, id = id },
   177 				return false
   300 				return false
   178 			end
   301 			end
   179 		end )
   302 		end )
   180 end
   303 end
   181 
   304 
   182 main.command ( 'subscribe',
   305 -- INTERFACE
       
   306 
       
   307 main.command ( 'node',
   183 	function ( args )
   308 	function ( args )
   184 		args = parse_args ( args )
   309 		args = parse_args ( args )
   185 		pubsub_subscribe ( args.t or main.current_buddy (), args )
   310 		local who, action, node = args.t, args[1], args[2]
       
   311 		if not who then
       
   312 			who = main.current_buddy ()
       
   313 		end
       
   314 		if action == 'subscribe' then
       
   315 			pubsub_subscribe ( who, node )
       
   316 		elseif action == 'unsubscribe' then
       
   317 			pubsub_unsubscribe ( who, node )
       
   318 		elseif action == 'retrieve' or action == 'items' or action == 'get' then
       
   319 			pubsub_retrieve ( who, node,
       
   320 				function ( item )
       
   321 					main.print_to ( who, item:xml () ) 
       
   322 				end, args.m )
       
   323 		elseif action == 'create' or action == 'new' then
       
   324 			pubsub_create_node ( who, node )
       
   325 		elseif action == 'delete' or action == 'del' then
       
   326 			pubsub_delete_node ( who, node )
       
   327 		elseif action == 'purge' or action == 'del_items' then
       
   328 			pubsub_purge_node ( who, node )
       
   329 		elseif action:sub ( 1, 4 ) == 'conf' then
       
   330 			pubsub_configure_node ( who, node )
       
   331 		elseif action == 'subscriptions' or action == 'subscribers' then
       
   332 			pubsub_list_subscriptions ( who, node )
       
   333 		elseif action == 'subscription' or action == 'modify' then -- XXX
       
   334 			pubsub_modify_subscription ( args.t or main.current_buddy (), node, args[3], args[4], args[5] )
       
   335 		else
       
   336 			print ( 'Error: unknown action' )
       
   337 		end
   186 	end )
   338 	end )
   187 main.command ( 'unsubscribe',
   339 
   188 	function ( args )
   340 -- FIXME
   189 		args = parse_args ( args )
   341 commands_help['node']           = "[-t jid] [-m max_items] action [node_name]\n\nAction can be subscribe, unsubscribe, retrieve (items, get), create (new), delete (del), purge (del_items), configure (conf*), subscriptions (subscribers), subscription (modify?)"
   190 		pubsub_unsubscribe ( args.t or main.current_buddy (), args )
   342 --[[
   191 	end )
   343 commands_help['subscribe']      = "[-t jid] node_name\n\nSends pubsub subscription request to specified node of jid or current buddy."
   192 main.command ( 'configure_node',
   344 commands_help['unsubscribe']    = "[-t jid] node_name\n\nSends pubsub unsubscription request to specified node of jid or current buddy."
   193 	function ( args )
   345 commands_help['retrieve']       = "[-t jid] [-m max_items] node_name\n\nSends pubsub items retrieval request to specified node of jid or current buddy.\nNote, that we cannot know, how to deal with these itemss, so, raw xml will be printed as a result."
   194 		args = parse_args ( args )
   346 commands_help['create_node']    = "[-t jid] [node_name]\n\nSends pubsub node creation request to specified node of jid or current buddy. If node name is not specified, server can generate unique id for it, if supported."
   195 		pubsub_configure_node ( args.t or main.current_buddy (), args )
   347 commands_help['configure_node'] = "[-t jid] node_name\n\nSends pubsub node configuration request to specified node of jid or current buddy."
   196 	end )
   348 commands_help['delete_node']    = "[-t jid] node_name\n\nSends pubsub node deletion request to specified node of jid or current buddy."
   197 main.command ( 'subscriptions',
   349 commands_help['purge_node']     = "[-t jid] node_name\n\nSends pubsub node items purging request to specified node of jid or current buddy."
   198 	function ( args )
   350 commands_help['subscriptions']  = "[-t jid] node_name\n\nSends pubsub subscription list request to specified node of jid or current buddy."
   199 		args = parse_args ( args )
   351 commands_help['subscription']   = "[-t jid] node_name subscriber_jid state [subscription_id]\n\nSends pubsub subscription modification request to change subscription state of 'subscriber_jid' to 'state'. Optional id is used when multiple subscriptions for one jid are available."
   200 		pubsub_list_subscriptions ( args.t or main.current_buddy (), args )
   352 --]]
   201 	end )
       
   202 main.command ( 'subscription',
       
   203 	function ( args )
       
   204 		args = parse_args ( args )
       
   205 		local node, jid, state, id = args[1], args[2], args[3], args[4]
       
   206 		pubsub_modify_subscription ( args.t or main.current_buddy (), node, jid, state, id )
       
   207 	end )
       
   208 
       
   209 commands_help['subscribe']      = "node_name\n\nSends pubsub subscription request to specified node of current buddy."
       
   210 commands_help['unsubscribe']    = "node_name\n\nSends pubsub unsubscription request to specified node of current buddy."
       
   211 commands_help['configure_node'] = "node_name\n\nSends pubsub node configuration request to specified node of current buddy."
       
   212 commands_help['subscriptions']  = "node_name\n\nSends pubsub subscription list request to specified node of current buddy."
       
   213 commands_help['subscription']  = "node_name subscriber_jid state [subscription_id]\n\nSends pubsub subscription modification request to change subscription state of 'jid' to 'state'. Optional id is used when multiple subscriptions for one jid are available."
       
   214 
   353 
   215 -- vim: se ts=4: --
   354 -- vim: se ts=4: --