examples/privacy.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 121 75a7d595817c
permissions -rw-r--r--
Fix module loading problem


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 )

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
privacy_pd_handler = 
	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
main.hook ( 'hook-post-connect',   privacy_pc_handler )
main.hook ( 'hook-pre-disconnect', privacy_pd_handler )
main.hook ( 'hook-lua-quit',       privacy_pd_handler )

-- vim: se ts=4 sw=4: --