--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/xep0060.lua Sun Mar 15 02:40:32 2009 +0200
@@ -0,0 +1,197 @@
+
+function pubsub_subscribe ( to, node )
+ local conn = lm.connection.bless ( main.connection () )
+ local jid = conn:jid():gsub ( '/.*', '' )
+ conn:send (
+ lm.message.create { mtype = 'iq-set', to = to, from = conn:jid (),
+ pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
+ subscribe = { node = node, jid = jid },
+ },
+ },
+ function ( conn, mess )
+ local mtype, smtype = mess:type ()
+ if smtype == 'result' then
+ local s = mess:path ( 'pubsub', 'subscription' )
+ local info
+ if s then
+ local id = s:attribute ( 'subid' )
+ if not id then
+ id = 'unspecified'
+ end
+ info = ('%s is now %s to node %s (id %s)'):format ( s:attribute ( 'jid' ), s:attribute ( 'subscription' ), s:attribute ( 'node' ), id )
+ else
+ info = 'Subscription successful, but server supplied no info about it...'
+ end
+ main.print_info ( to, info )
+ return true
+ elseif smtype == 'error' then
+ main.print_info ( to, 'Error response for subscription request: ' .. mess:xml () ) -- FIXME
+ return true
+ else
+ print ( 'Weird response for subscription request: ' .. mess:xml () )
+ return false
+ end
+ end )
+end
+
+function pubsub_unsubscribe ( to, node )
+ local conn = lm.connection.bless ( main.connection () )
+ local jid = conn:jid():gsub ( '/.*', '' )
+ conn:send (
+ lm.message.create { mtype = 'iq-set', to = to,
+ pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
+ unsubscribe = { node = node, jid = jid },
+ },
+ },
+ function ( conn, mess )
+ local mtype, smtype = mess:type ()
+ if smtype == 'result' then
+ main.print_info ( to, 'Unsubscription successful' )
+ return true
+ elseif smtype == 'error' then
+ main.print_info ( to, 'Error response for unsubscription request: ' .. mess:xml () ) -- FIXME
+ return true
+ else
+ print ( 'Weird response for unsubscription request: ' .. mess:xml () )
+ return false
+ end
+ end )
+end
+
+function pubsub_configure_node ( to, node )
+ local conn = lm.connection.bless ( main.connection () )
+ local jid = conn:jid():gsub ( '/.*', '' )
+ conn:send (
+ lm.message.create { mtype = 'iq-get', to = to,
+ pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
+ configure = { node = node },
+ },
+ },
+ function ( conn, mess )
+ local mtype, smtype = mess:type ()
+ if smtype == 'result' then
+ local x = mess:path ( 'pubsub', 'configure', 'x' )
+ if x then
+ local fid = parse_form ( x )
+ forms[fid].send =
+ function ( form )
+ conn:send (
+ lm.message.create { mtype = 'iq-set', to = to,
+ pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
+ configure = { node = node,
+ x = { xmlns = 'jabber:x:data', type = 'submit',
+ field = form.val,
+ },
+ },
+ },
+ })
+ end
+ forms[fid].status = 'filling'
+ main.print_info ( to, 'You have new form. To fill it, use /form ' .. fid .. ' fieldname value' )
+ else
+ main.print_info ( to, 'Weird, no error and no node configuration form: ' .. mess:xml () )
+ end
+ return true
+ elseif smtype == 'error' then
+ main.print_info ( to, 'Error response for node configuration request: ' .. mess:xml () ) -- FIXME
+ return true
+ else
+ print ( 'Weird response for node configuration request: ' .. mess:xml () )
+ return false
+ end
+ end )
+end
+
+function pubsub_list_subscriptions ( to, node )
+ local conn = lm.connection.bless ( main.connection () )
+ local jid = conn:jid():gsub ( '/.*', '' )
+ conn:send (
+ lm.message.create { mtype = 'iq-get', to = to,
+ pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
+ subscriptions = { node = node },
+ },
+ },
+ function ( conn, mess )
+ local mtype, smtype = mess:type ()
+ if smtype == 'result' then
+ local s = mess:path ( 'pubsub', 'subscriptions' )
+ if s then
+ local sub = s:children ()
+ main.print_info ( to, "List of subscriptions for node " .. node )
+ while sub do
+ local subid = sub:attribute ( 'subid' ) or 'unspecified'
+ main.print_info ( to, ("%s is %s (id %s)"):format ( sub:attribute ( 'jid' ), sub:attribute ( 'subscription' ), subid ) )
+ sub = sub:next ()
+ end
+ else
+ main.print_info ( to, 'Weird response to node subscription list request: ' .. mess:xml () )
+ end
+ return true
+ elseif smtype == 'error' then
+ main.print_info ( to, 'Error response for node subscription list request: ' .. mess:xml () ) -- FIXME
+ return true
+ else
+ print ( 'Weird response for node subscription list request: ' .. mess:xml () )
+ return false
+ end
+ end )
+end
+
+function pubsub_modify_subscription ( to, node, who, state, id )
+ local conn = lm.connection.bless ( main.connection () )
+ local jid = conn:jid():gsub ( '/.*', '' )
+ conn:send (
+ lm.message.create { mtype = 'iq-get', to = to,
+ pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner',
+ subscriptions = { node = node,
+ subscription = { jid = who, subscription = state, id = id },
+ },
+ },
+ },
+ function ( conn, mess )
+ local mtype, smtype = mess:type ()
+ if smtype == 'result' then
+ main.print_info ( to, ('Subscription of %s to %s is successfully set to %s (id %s)'):format ( who, node, state, id or 'unspecified' ) )
+ return true
+ elseif smtype == 'error' then
+ main.print_info ( to, 'Error response for node subscription list request: ' .. mess:xml () ) -- FIXME
+ return true
+ else
+ print ( 'Weird response for node subscription list request: ' .. mess:xml () )
+ return false
+ end
+ end )
+end
+
+main.command ( 'subscribe',
+ function ( args )
+ pubsub_subscribe ( main.current_buddy (), args )
+ end )
+main.command ( 'unsubscribe',
+ function ( args )
+ pubsub_unsubscribe ( main.current_buddy (), args )
+ end )
+main.command ( 'configure_node',
+ function ( args )
+ pubsub_configure_node ( main.current_buddy (), args )
+ end )
+main.command ( 'subscriptions',
+ function ( args )
+ pubsub_list_subscriptions ( main.current_buddy (), args )
+ end )
+main.command ( 'subscription',
+ function ( args )
+ local node, jid, state, id = args:match ( '(.-)%s+(.-)%s+(.-)%s+(.+)' )
+ if not node then
+ node, jid, state = args:match ( '(.-)%s+(.-)%s+(.+)' )
+ end
+ pubsub_modify_subscription ( main.current_buddy (), node, jid, state, id )
+ end )
+
+commands_help['subscribe'] = "node_name\n\nSends pubsub subscription request to specified node of current buddy."
+commands_help['unsubscribe'] = "node_name\n\nSends pubsub unsubscription request to specified node of current buddy."
+commands_help['configure_node'] = "node_name\n\nSends pubsub node configuration request to specified node of current buddy."
+commands_help['subscriptions'] = "node_name\n\nSends pubsub subscription list request to specified node of current buddy."
+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."
+
+-- vim: se ts=4: --