examples/mc_pubsub.lua
changeset 66 542f61e113cb
child 67 d33ca5572e91
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
       
     1 
       
     2 local lm     = require 'lm'
       
     3 local pubsub = require 'pubsub'
       
     4 
       
     5 main.command ( 'node',
       
     6 	function ( args )
       
     7 		local who, action, node = args.t, args[1], args[2]
       
     8 		local conn = lm.connection.bless ( main.connection () )
       
     9 		if not who then
       
    10 			who = main.current_buddy ()
       
    11 		end
       
    12 		if action == 'subscribe' then
       
    13 			pubsub.subscribe ( conn, who, node,
       
    14 				function ( id )
       
    15 					if id then
       
    16 						main.print_info ( who, 'Subscription succeeds with id ' .. id )
       
    17 					else
       
    18 						main.print_info ( who, 'Subscription successful' )
       
    19 					end
       
    20 				end,
       
    21 				function ( mesg )
       
    22 					main.print_info ( who, 'Subscription unsuccessful: ' .. mesg )
       
    23 				end )
       
    24 		elseif action == 'unsubscribe' then
       
    25 			pubsub.unsubscribe ( conn, who, node,
       
    26 				function ()
       
    27 					main.print_info ( who, 'Unubscription successful' )
       
    28 				end,
       
    29 				function ( mesg )
       
    30 					main.print_info ( who, 'Unsubscription unsuccessful: ' .. mesg )
       
    31 				end )
       
    32 		elseif action == 'retrieve' or action == 'items' or action == 'get' then
       
    33 			pubsub.retrieve ( conn, who, node,
       
    34 				function ( from, node, item )
       
    35 					main.print_info ( who, 'Item from ' .. from .. ', node ' .. node .. ':\n' .. item:xml () ) 
       
    36 				end,
       
    37 				function ( mesg )
       
    38 					main.print_info ( who, 'Retrieval failed: ' .. mesg )
       
    39 				end, args.m )
       
    40 		elseif action == 'create' or action == 'new' then
       
    41 			pubsub.create_node ( conn, who, node,
       
    42 				function ( node )
       
    43 					if node then
       
    44 						main.print_info ( who, 'Node ' .. node .. ' successfully created' )
       
    45 					else
       
    46 						main.print_info ( who, 'Node successfully created' )
       
    47 					end
       
    48 				end,
       
    49 				function ( mesg )
       
    50 					main.print_info ( who, 'Creation failed: ' .. mesg )
       
    51 				end )
       
    52 		elseif action == 'delete' or action == 'del' then
       
    53 			pubsub.delete_node ( conn, who, node,
       
    54 				function ()
       
    55 					main.print_info ( who, 'Node deleted' )
       
    56 				end,
       
    57 				function ( mesg )
       
    58 					main.print_info ( who, 'Node deletion failed: ' .. mesg )
       
    59 				end )
       
    60 		elseif action == 'purge' or action == 'del_items' then
       
    61 			pubsub.purge_node ( conn, who, node,
       
    62 				function ()
       
    63 					main.print_info ( who, 'Node purged' )
       
    64 				end,
       
    65 				function ( mesg )
       
    66 					main.print_info ( who, 'Node purge failed: ' .. mesg )
       
    67 				end )
       
    68 		elseif action:sub ( 1, 4 ) == 'conf' then
       
    69 			pubsub.configure_node ( conn, who, node,
       
    70 				function ( form, submit, reject )
       
    71 					local id = #forms + 1
       
    72 					forms[id] = {
       
    73 						form = form,
       
    74 						submit =
       
    75 							function ( form )
       
    76 								submit ( form,
       
    77 									function ()
       
    78 										main.print_info ( who, 'Node configuration completed' )
       
    79 									end,
       
    80 									function ( mesg )
       
    81 										main.print_info ( who, 'Node configuration failed: ' .. mesg )
       
    82 									end )
       
    83 							end,
       
    84 						reject =
       
    85 							function ( form )
       
    86 								reject ( form,
       
    87 									function ()
       
    88 										main.print_info ( who, 'Node configuration cancelled' )
       
    89 									end,
       
    90 									function ( mesg )
       
    91 										main.print_info ( who, 'Node configuration cancellation failed: ' .. mesg )
       
    92 									end )
       
    93 							end,
       
    94 					}
       
    95 					print ( 'You have new form ' .. id )
       
    96 				end,
       
    97 				function ( mesg )
       
    98 					main.print_info ( who, 'Node configuration failed: ' .. mesg )
       
    99 				end )
       
   100 		elseif action == 'subscriptions' or action == 'subscribers' then
       
   101 			pubsub.list_subscriptions ( conn, who, node,
       
   102 				function ( s )
       
   103 					local text = ''
       
   104 					for i, v in ipairs ( s ) do
       
   105 						local subid = v.subid
       
   106 						if subid then
       
   107 							subid = '(id ' .. subid .. ')'
       
   108 						else
       
   109 							subid = ''
       
   110 						end
       
   111 						text = text .. ('\n- [%s] %s %s'):format ( v.subscription, v.jid, subid )
       
   112 					end
       
   113 					if text ~= '' then
       
   114 						main.print_info ( who, 'Node subscriptions:' .. text )
       
   115 					else
       
   116 						main.print_info ( who, 'No subscriptions' )
       
   117 					end
       
   118 				end,
       
   119 				function ( mesg )
       
   120 					main.print_info ( who, 'Node subscriptions retrieval failed: ' .. mesg )
       
   121 				end )
       
   122 		elseif action == 'subscription' or action == 'modify' then -- XXX
       
   123 			pubsub.modify_subscription ( conn, args.t or main.current_buddy (), node, args[3], args[4],
       
   124 				function ()
       
   125 					main.print_info ( who, 'Subscription modified' )
       
   126 				end,
       
   127 				function ( mesg )
       
   128 					main.print_info ( who, 'Subsrciption modification failed: ' .. mesg )
       
   129 				end, args[5] )
       
   130 		else
       
   131 			print ( 'Error: unknown action' )
       
   132 		end
       
   133 	end, true )
       
   134 
       
   135 -- FIXME
       
   136 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?)"
       
   137 --[[
       
   138 commands_help['subscribe']      = "[-t jid] node_name\n\nSends pubsub subscription request to specified node of jid or current buddy."
       
   139 commands_help['unsubscribe']    = "[-t jid] node_name\n\nSends pubsub unsubscription request to specified node of jid or current buddy."
       
   140 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."
       
   141 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."
       
   142 commands_help['configure_node'] = "[-t jid] node_name\n\nSends pubsub node configuration request to specified node of jid or current buddy."
       
   143 commands_help['delete_node']    = "[-t jid] node_name\n\nSends pubsub node deletion request to specified node of jid or current buddy."
       
   144 commands_help['purge_node']     = "[-t jid] node_name\n\nSends pubsub node items purging request to specified node of jid or current buddy."
       
   145 commands_help['subscriptions']  = "[-t jid] node_name\n\nSends pubsub subscription list request to specified node of jid or current buddy."
       
   146 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."
       
   147 --]]
       
   148 
       
   149 local pubsub_handler = lm.message_handler.new ( pubsub.message_handler )
       
   150 
       
   151 local pubsub_handler_registered = false
       
   152 
       
   153 hooks_d['hook-post-connect'].pubsub =
       
   154 	function ( args )
       
   155 		lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message', 'normal' )
       
   156 		pubsub_handler_registered = true
       
   157 		hooks_d['hook-post-connect'].pubsub = nil
       
   158 		hooks_d['hook-quit'].pubsub =
       
   159 			function ( args )
       
   160 				if pubsub_handler_registered then
       
   161 					lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message' )
       
   162 					pubsub_handler_registered = false
       
   163 				end
       
   164 			end
       
   165 	end
       
   166 
       
   167 -- vim: se ts=4: --