examples/pubsub.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 68 742878c74b8e
permissions -rw-r--r--
Fix module loading problem


local lm     = require 'lm'
local pubsub = require 'lm.pubsub'

main.command ( 'node',
	function ( args )
		local who, action, node = args.t, args[1], args[2]
		local conn = lm.connection.bless ( main.connection () )
		if not who then
			who = main.current_buddy ()
		end
		if action == 'subscribe' then
			pubsub.subscribe ( conn, who, node,
				function ( id )
					if id then
						main.print_info ( who, 'Subscription succeeds with id ' .. id )
					else
						main.print_info ( who, 'Subscription successful' )
					end
				end,
				function ( mesg )
					main.print_info ( who, 'Subscription unsuccessful: ' .. mesg )
				end )
		elseif action == 'unsubscribe' then
			pubsub.unsubscribe ( conn, who, node,
				function ()
					main.print_info ( who, 'Unubscription successful' )
				end,
				function ( mesg )
					main.print_info ( who, 'Unsubscription unsuccessful: ' .. mesg )
				end )
		elseif action == 'retrieve' or action == 'items' or action == 'get' then
			pubsub.retrieve ( conn, who, node,
				function ( from, node, item )
					main.print_info ( who, 'Item from ' .. from .. ', node ' .. node .. ':\n' .. item:xml () ) 
				end,
				function ( mesg )
					main.print_info ( who, 'Retrieval failed: ' .. mesg )
				end, args.m )
		elseif action == 'create' or action == 'new' then
			pubsub.create_node ( conn, who, node,
				function ( node )
					if node then
						main.print_info ( who, 'Node ' .. node .. ' successfully created' )
					else
						main.print_info ( who, 'Node successfully created' )
					end
				end,
				function ( mesg )
					main.print_info ( who, 'Creation failed: ' .. mesg )
				end )
		elseif action == 'delete' or action == 'del' then
			pubsub.delete_node ( conn, who, node,
				function ()
					main.print_info ( who, 'Node deleted' )
				end,
				function ( mesg )
					main.print_info ( who, 'Node deletion failed: ' .. mesg )
				end )
		elseif action == 'purge' or action == 'del_items' then
			pubsub.purge_node ( conn, who, node,
				function ()
					main.print_info ( who, 'Node purged' )
				end,
				function ( mesg )
					main.print_info ( who, 'Node purge failed: ' .. mesg )
				end )
		elseif action:sub ( 1, 4 ) == 'conf' then
			pubsub.configure_node ( conn, who, node,
				function ( form, submit, reject )
					insert_form ( form,
						function ( form )
							submit ( form,
								function ()
									main.print_info ( who, 'Node configuration completed' )
								end,
								function ( mesg )
									main.print_info ( who, 'Node configuration failed: ' .. mesg )
								end )
						end,
						function ( form )
							reject ( form,
								function ()
									main.print_info ( who, 'Node configuration cancelled' )
								end,
								function ( mesg )
									main.print_info ( who, 'Node configuration cancellation failed: ' .. mesg )
								end )
						end )
				end,
				function ( mesg )
					main.print_info ( who, 'Node configuration failed: ' .. mesg )
				end )
		elseif action == 'subscriptions' or action == 'subscribers' then
			pubsub.list_subscriptions ( conn, who, node,
				function ( s )
					local text = ''
					for i, v in ipairs ( s ) do
						local subid = v.subid
						if subid then
							subid = '(id ' .. subid .. ')'
						else
							subid = ''
						end
						text = text .. ('\n- [%s] %s %s'):format ( v.subscription, v.jid, subid )
					end
					if text ~= '' then
						main.print_info ( who, 'Node subscriptions:' .. text )
					else
						main.print_info ( who, 'No subscriptions' )
					end
				end,
				function ( mesg )
					main.print_info ( who, 'Node subscriptions retrieval failed: ' .. mesg )
				end )
		elseif action == 'subscription' or action == 'modify' then -- XXX
			pubsub.modify_subscription ( conn, args.t or main.current_buddy (), node, args[3], args[4],
				function ()
					main.print_info ( who, 'Subscription modified' )
				end,
				function ( mesg )
					main.print_info ( who, 'Subsrciption modification failed: ' .. mesg )
				end, args[5] )
		else
			print ( 'Error: unknown action' )
		end
	end, true )

-- FIXME
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?)"
--[[
commands_help['subscribe']      = "[-t jid] node_name\n\nSends pubsub subscription request to specified node of jid or current buddy."
commands_help['unsubscribe']    = "[-t jid] node_name\n\nSends pubsub unsubscription request to specified node of jid or current buddy."
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."
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."
commands_help['configure_node'] = "[-t jid] node_name\n\nSends pubsub node configuration request to specified node of jid or current buddy."
commands_help['delete_node']    = "[-t jid] node_name\n\nSends pubsub node deletion request to specified node of jid or current buddy."
commands_help['purge_node']     = "[-t jid] node_name\n\nSends pubsub node items purging request to specified node of jid or current buddy."
commands_help['subscriptions']  = "[-t jid] node_name\n\nSends pubsub subscription list request to specified node of jid or current buddy."
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."
--]]

local pubsub_handler = lm.message_handler.new ( pubsub.message_handler )

local pubsub_handler_registered = false

hooks_d['hook-post-connect'].pubsub =
	function ( args )
		lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message', 'normal' )
		pubsub_handler_registered = true
		hooks_d['hook-post-connect'].pubsub = nil
		hooks_d['hook-quit'].pubsub =
			function ( args )
				if pubsub_handler_registered then
					lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message' )
					pubsub_handler_registered = false
				end
			end
	end

-- vim: se ts=4: --