examples/privacy.lua
changeset 68 742878c74b8e
child 99 ed4676536ed9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/privacy.lua	Tue Mar 31 18:35:34 2009 +0300
@@ -0,0 +1,98 @@
+
+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 action = args[1]
+		local conn   = lm.connection.bless ( main.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
+
+hooks_d['hook-post-connect'].privacy =
+	function ( args )
+		privacy.active ( lm.connection.bless ( main.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( main.connection () ):handler ( privacy_handler, 'iq', 'normal' )
+		privacy_handler_registered = true
+		hooks_d['hook-post-connect'].privacy =
+			function ( args )
+				privacy.active ( lm.connection.bless ( main.connection () ), 'mcabber',
+					function ()
+						print ( 'Server blocklist activated' )
+					end,
+					function ()
+						-- list may be absent, so, we will not pollute log with errors
+					end )
+			end
+		hooks_d['hook-quit'].privacy =
+			function ( args )
+				if privacy_handler_registered then
+					lm.connection.bless( main.connection () ):handler ( privacy_handler, 'iq' )
+				end
+			end
+	end
+
+-- vim: se ts=4: --