examples/xep0060.lua
changeset 22 6460b020825d
child 24 25552b21d3fb
equal deleted inserted replaced
21:2384ce322282 22:6460b020825d
       
     1 
       
     2 function pubsub_subscribe ( to, node )
       
     3 	local conn = lm.connection.bless ( main.connection () )
       
     4 	local jid = conn:jid():gsub ( '/.*', '' )
       
     5 	conn:send (
       
     6 		lm.message.create { mtype = 'iq-set', to = to, from = conn:jid (),
       
     7 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
       
     8 				subscribe = { node = node, jid = jid },
       
     9 			},
       
    10 		},
       
    11 		function ( conn, mess )
       
    12 			local mtype, smtype = mess:type ()
       
    13 			if smtype == 'result' then
       
    14 				local s = mess:path ( 'pubsub', 'subscription' )
       
    15 				local info
       
    16 				if s then
       
    17 					local id = s:attribute ( 'subid' )
       
    18 					if not id then
       
    19 						id = 'unspecified'
       
    20 					end
       
    21 					info = ('%s is now %s to node %s (id %s)'):format ( s:attribute ( 'jid' ), s:attribute ( 'subscription' ), s:attribute ( 'node' ), id )
       
    22 				else
       
    23 					info = 'Subscription successful, but server supplied no info about it...'
       
    24 				end
       
    25 				main.print_info ( to, info )
       
    26 				return true
       
    27 			elseif smtype == 'error' then
       
    28 				main.print_info ( to, 'Error response for subscription request: ' .. mess:xml () ) -- FIXME
       
    29 				return true
       
    30 			else
       
    31 				print ( 'Weird response for subscription request: ' .. mess:xml () )
       
    32 				return false
       
    33 			end
       
    34 		end )
       
    35 end
       
    36 
       
    37 function pubsub_unsubscribe ( to, node )
       
    38 	local conn = lm.connection.bless ( main.connection () )
       
    39 	local jid = conn:jid():gsub ( '/.*', '' )
       
    40 	conn:send (
       
    41 		lm.message.create { mtype = 'iq-set', to = to,
       
    42 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
       
    43 				unsubscribe = { node = node, jid = jid },
       
    44 			},
       
    45 		},
       
    46 		function ( conn, mess )
       
    47 			local mtype, smtype = mess:type ()
       
    48 			if smtype == 'result' then
       
    49 				main.print_info ( to, 'Unsubscription successful' )
       
    50 				return true
       
    51 			elseif smtype == 'error' then
       
    52 				main.print_info ( to, 'Error response for unsubscription request: ' .. mess:xml () ) -- FIXME
       
    53 				return true
       
    54 			else
       
    55 				print ( 'Weird response for unsubscription request: ' .. mess:xml () )
       
    56 				return false
       
    57 			end
       
    58 		end )
       
    59 end
       
    60 
       
    61 function pubsub_configure_node ( to, node )
       
    62 	local conn = lm.connection.bless ( main.connection () )
       
    63 	local jid = conn:jid():gsub ( '/.*', '' )
       
    64 	conn:send (
       
    65 		lm.message.create { mtype = 'iq-get', to = to,
       
    66 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
    67 				configure = { node = node },
       
    68 			},
       
    69 		},
       
    70 		function ( conn, mess )
       
    71 			local mtype, smtype = mess:type ()
       
    72 			if smtype == 'result' then
       
    73 				local x = mess:path ( 'pubsub', 'configure', 'x' )
       
    74 				if x then
       
    75 					local fid = parse_form ( x )
       
    76 					forms[fid].send =
       
    77 						function ( form )
       
    78 							conn:send (
       
    79 								lm.message.create { mtype = 'iq-set', to = to,
       
    80 									pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
    81 										configure = { node = node,
       
    82 											x = { xmlns = 'jabber:x:data', type = 'submit',
       
    83 												field = form.val,
       
    84 											},
       
    85 										},
       
    86 									},
       
    87 								})
       
    88 						end
       
    89 					forms[fid].status = 'filling'
       
    90 					main.print_info ( to, 'You have new form. To fill it, use /form ' .. fid .. ' fieldname value' )
       
    91 				else
       
    92 					main.print_info ( to, 'Weird, no error and no node configuration form: ' .. mess:xml () )
       
    93 				end
       
    94 				return true
       
    95 			elseif smtype == 'error' then
       
    96 				main.print_info ( to, 'Error response for node configuration request: ' .. mess:xml () ) -- FIXME
       
    97 				return true
       
    98 			else
       
    99 				print ( 'Weird response for node configuration request: ' .. mess:xml () )
       
   100 				return false
       
   101 			end
       
   102 		end )
       
   103 end
       
   104 
       
   105 function pubsub_list_subscriptions ( to, node )
       
   106 	local conn = lm.connection.bless ( main.connection () )
       
   107 	local jid = conn:jid():gsub ( '/.*', '' )
       
   108 	conn:send (
       
   109 		lm.message.create { mtype = 'iq-get', to = to,
       
   110 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
   111 				subscriptions = { node = node },
       
   112 			},
       
   113 		},
       
   114 		function ( conn, mess )
       
   115 			local mtype, smtype = mess:type ()
       
   116 			if smtype == 'result' then
       
   117 				local s = mess:path ( 'pubsub', 'subscriptions' )
       
   118 				if s then
       
   119 					local sub = s:children ()
       
   120 					main.print_info ( to, "List of subscriptions for node " .. node )
       
   121 					while sub do
       
   122 						local subid = sub:attribute ( 'subid' ) or 'unspecified'
       
   123 						main.print_info ( to, ("%s is %s (id %s)"):format ( sub:attribute ( 'jid' ), sub:attribute ( 'subscription' ), subid ) )
       
   124 						sub = sub:next ()
       
   125 					end
       
   126 				else
       
   127 					main.print_info ( to, 'Weird response to node subscription list request: ' .. mess:xml () )
       
   128 				end
       
   129 				return true
       
   130 			elseif smtype == 'error' then
       
   131 				main.print_info ( to, 'Error response for node subscription list request: ' .. mess:xml () ) -- FIXME
       
   132 				return true
       
   133 			else
       
   134 				print ( 'Weird response for node subscription list request: ' .. mess:xml () )
       
   135 				return false
       
   136 			end
       
   137 		end )
       
   138 end
       
   139 
       
   140 function pubsub_modify_subscription ( to, node, who, state, id )
       
   141 	local conn = lm.connection.bless ( main.connection () )
       
   142 	local jid = conn:jid():gsub ( '/.*', '' )
       
   143 	conn:send (
       
   144 		lm.message.create { mtype = 'iq-get', to = to,
       
   145 			pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
       
   146 				subscriptions = { node = node,
       
   147 					subscription = { jid = who, subscription = state, id = id },
       
   148 				},
       
   149 			},
       
   150 		},
       
   151 		function ( conn, mess )
       
   152 			local mtype, smtype = mess:type ()
       
   153 			if smtype == 'result' then
       
   154 				main.print_info ( to, ('Subscription of %s to %s is successfully set to %s (id %s)'):format ( who, node, state, id or 'unspecified' ) )
       
   155 				return true
       
   156 			elseif smtype == 'error' then
       
   157 				main.print_info ( to, 'Error response for node subscription list request: ' .. mess:xml () ) -- FIXME
       
   158 				return true
       
   159 			else
       
   160 				print ( 'Weird response for node subscription list request: ' .. mess:xml () )
       
   161 				return false
       
   162 			end
       
   163 		end )
       
   164 end
       
   165 
       
   166 main.command ( 'subscribe',
       
   167 	function ( args )
       
   168 		pubsub_subscribe ( main.current_buddy (), args )
       
   169 	end )
       
   170 main.command ( 'unsubscribe',
       
   171 	function ( args )
       
   172 		pubsub_unsubscribe ( main.current_buddy (), args )
       
   173 	end )
       
   174 main.command ( 'configure_node',
       
   175 	function ( args )
       
   176 		pubsub_configure_node ( main.current_buddy (), args )
       
   177 	end )
       
   178 main.command ( 'subscriptions',
       
   179 	function ( args )
       
   180 		pubsub_list_subscriptions ( main.current_buddy (), args )
       
   181 	end )
       
   182 main.command ( 'subscription',
       
   183 	function ( args )
       
   184 		local node, jid, state, id = args:match ( '(.-)%s+(.-)%s+(.-)%s+(.+)' )
       
   185 		if not node then
       
   186 			node, jid, state = args:match ( '(.-)%s+(.-)%s+(.+)' )
       
   187 		end
       
   188 		pubsub_modify_subscription ( main.current_buddy (), node, jid, state, id )
       
   189 	end )
       
   190 
       
   191 commands_help['subscribe']      = "node_name\n\nSends pubsub subscription request to specified node of current buddy."
       
   192 commands_help['unsubscribe']    = "node_name\n\nSends pubsub unsubscription request to specified node of current buddy."
       
   193 commands_help['configure_node'] = "node_name\n\nSends pubsub node configuration request to specified node of current buddy."
       
   194 commands_help['subscriptions']  = "node_name\n\nSends pubsub subscription list request to specified node of current buddy."
       
   195 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."
       
   196 
       
   197 -- vim: se ts=4: --