examples/evil.lua
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 111 5bcdb71ef2f2
--- a/examples/evil.lua	Wed Mar 31 00:28:04 2010 +0300
+++ b/examples/evil.lua	Wed Mar 31 01:25:19 2010 +0300
@@ -26,7 +26,12 @@
 -- TODO improve interface, check if we sending right thing for offline
 main.command ( 'evil',
 	function ( args )
-		local conn = lm.connection.bless ( main.connection () )
+		local connection = main.connection ()
+		if not connection then
+			print "You are not online!"
+			return
+		end
+		local conn = lm.connection.bless ( connection )
 		if args[1] == 'status' then
 			local text = ''
 			for i, mesg in ipairs ( args ) do
@@ -71,22 +76,39 @@
 local evil_handler = lm.message_handler.new ( evil.stanza_handler )
 local evil_handler_registered = false
 
-hooks_d['hook-post-connect'].evil =
+local evil_pc_handler =
+	function ( args )
+		local connection = main.connection ()
+		if connection then
+			lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq',       'first' )
+			lm.connection.bless( main.connection () ):handler ( evil_handler, 'message',  'first' )
+			lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' )
+			evil_handler_registered = true
+		end
+	end
+main.hook ( 'hook-post-connect', evil_pc_handler )
+main.hook ( 'hook-pre-disconnect',
 	function ( args )
-		lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq',       'first' )
-		lm.connection.bless( main.connection () ):handler ( evil_handler, 'message',  'first' )
-		lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence', 'first' )
-		evil_handler_registered = true
-		hooks_d['hook-post-connect'].evil = nil
-		hooks_d['hook-quit'].evil =
-			function ( args )
-				if evil_handler_registered then
-					lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq'       )
-					lm.connection.bless( main.connection () ):handler ( evil_handler, 'message'  )
-					lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' )
-				end
+		if evil_handler_registered then
+			local connection = main.connection ()
+			if connection then
+				lm.connection.bless( main.connection () ):handler ( evil_handler, 'iq'       )
+				lm.connection.bless( main.connection () ):handler ( evil_handler, 'message'  )
+				lm.connection.bless( main.connection () ):handler ( evil_handler, 'presence' )
 			end
+			evil_handler_registered = false
+		end
+	end )
+
+main.hook ( 'hook-lua-start',
+	function ( args )
+		main.add_feature ( 'http://jabber.org/protocol/evil' )
+		evil_pc_handler ()
 	end
+main.hook ( 'hook-lua-quit',
+	function ( args )
+		main.del_feature ( 'http://jabber.org/protocol/evil' )
+	end )
 
 local char2xmpp = {
 	f = 'chat',
@@ -98,13 +120,14 @@
 }
 
 -- hack, but working ;)
-hooks_d['hook-my-status-change'].evil =
+main.hook ( 'hook-my-status-change',
 	function ( args )
 		if main.yesno ( main.option ( 'lua_evil_mode' ) ) then
-			evil.presence ( lm.connection.bless ( main.connection () ), nil, char2xmpp[args.new_status], args.message )
+			local connection = main.connection ()
+			if connection then
+				evil.presence ( lm.connection.bless ( connection ), nil, char2xmpp[args.new_status], args.message )
+			end
 		end
-	end
+	end )
 
-main.add_feature ( 'http://jabber.org/protocol/evil' )
-
--- vim: se ts=4: --
+-- vim: se ts=4 sw=4: --