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,
},
},
},
},
function ( conn, mess )
local mt, st = mess:type ()
if st == 'result' then
main.print_info ( to, 'Now you can run /form del ' .. fid .. ' to delete form from list' )
form.status = 'acquired'
elseif st == 'error' then
main.print_info ( to, 'Got non-successful response to form:\n' .. mess:xml () )
form.status = 'rejected'
else
print ( 'Weird response to submitted form:\n' .. mess:xml () )
form.status = 'unknown'
return false
end
return true
end )
form.status = 'sent'
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 )
args = parse_args ( args )
pubsub_subscribe ( args.t or main.current_buddy (), args )
end )
main.command ( 'unsubscribe',
function ( args )
args = parse_args ( args )
pubsub_unsubscribe ( args.t or main.current_buddy (), args )
end )
main.command ( 'configure_node',
function ( args )
args = parse_args ( args )
pubsub_configure_node ( args.t or main.current_buddy (), args )
end )
main.command ( 'subscriptions',
function ( args )
args = parse_args ( args )
pubsub_list_subscriptions ( args.t or main.current_buddy (), args )
end )
main.command ( 'subscription',
function ( args )
args = parse_args ( args )
local node, jid, state, id = args[1], args[2], args[3], args[4]
pubsub_modify_subscription ( args.t or 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: --