Update most useful scripts
* new hooks interface
* connection state checking
local lm = require 'lm'
local privacy = require 'lm.privacy'
privacy.handler (
function ( name )
print ( 'Privacy list changed: ' .. name )
end )
main.command ( 'blocklist',
function ( args )
local connection = main.connection ()
if not connection then
print "You are not online!"
return
end
local action = args[1]
local conn = lm.connection.bless ( connection )
local who = main.current_buddy ()
privacy.list ( conn, 'mcabber',
function ( list )
if action == 'add' then
for i, item in ipairs ( list ) do
item.order = nil
end
table.insert ( list, 1, { type = 'jid', value = args[2] or who, action = 'deny' } )
elseif action == 'del' then
for i, item in ipairs ( list ) do
if item.value == ( args[2] or who ) then
table.remove ( list, i )
break
end
end
else
local text = ''
for i, item in ipairs ( list ) do
text = text .. '\n - <' .. ( list.order or '?' ) .. '> ' .. ( list.value or '*' ) .. ' [' .. ( list.type or 'unspecified' ) .. '] ' .. ( list.action or 'unspecified' )
end
if text ~= '' then
print ( 'Blocking list: ' .. text )
end
return
end
privacy.set ( conn, 'mcabber', list,
function ()
print ( 'Blocking list saved' )
end,
function ( mesg )
print ( 'Error saving blocking list: ' .. mesg )
end )
end,
function ( mesg )
if action == 'add' then
privacy.set ( conn, 'mcabber',
{{ type = 'jid', value = args[2] or who, action = 'deny' },
{ action = 'allow' }},
function ()
print ( 'New blocking list created' )
end,
function ( message )
print ( 'Error obtaining/creatieng blocking list: ' .. mesg .. ' / ' .. message )
end )
else
print ( 'Cannot obtain privacy list from server: ' .. mesg )
end
end )
end, true )
commands_help['blocklist'] = '[[add | del] [jid]]\n\nRetrieves list, adds or removes buddies to/from mcabber\'s server blocklist.\nAny stanzas from buddy in list are blocked by server.'
local privacy_handler = lm.message_handler.new ( privacy.iq_handler )
local privacy_handler_registered = false
privacy_pc_handler =
function ( args )
local connection = main.connection ()
if connection then
privacy.active ( lm.connection.bless ( connection ), 'mcabber',
function ()
print ( 'Server blocklist activated' )
end,
function ()
-- list may be absent, so, we will not pollute log with errors
end )
lm.connection.bless( connection ):handler ( privacy_handler, 'iq', 'normal' )
privacy_handler_registered = true
end
end
main.hook ( 'hook-post-connect', privacy_pc_handler )
main.hook ( 'hook-pre-disconnect',
function ( args )
if privacy_handler_registered then
local connection = main.connection ()
if connection then
lm.connection.bless( connection ):handler ( privacy_handler, 'iq' )
end
privacy_handler_registered = false
end
end
-- vim: se ts=4 sw=4: --