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


--- JOBS

delayed_jobs = {}

-- may fail
dofile ( main.fileoption 'lua_jobs_file' )

function save_jobs ()
	local h = io.open ( main.fileoption 'lua_jobs_file', "w" )
	if not h then
		print ( 'Cannot open jobs file for writing!' )
		return
	end
	h:write ( "-- This is autogenerated file, do not edit it manually\n\ndelayed_jobs = {\n" );
	for jid, more in pairs ( delayed_jobs ) do
		h:write ( string.format ( "\t[%q] = {\n", jid ) )
		for status, action in pairs ( more ) do
			if action then -- remove fired jobs
				h:write ( string.format ( "\t\t[%q] = %q,\n", status, action ) )
			end
		end
		h:write ( "\t},\n" )
	end
	h:write ( "}\n" )
	h:close ()
end

main.command ( 'delay',
	function ( args )
		local who
		if args.t then
			who = args.t
		else
			who = main.current_buddy ()
		end
		local stat = args[1]
		local mess = args[2]
		delayed_jobs[who] = { }
		delayed_jobs[who][stat] = 'say_to -q ' .. who .. ' ' .. mess
	end, true )

main.command ( 'job',
	function ( args )
		local action, jid, stat = args[1], args[2], args[3]
		if action == 'del' then
			delayed_jobs[jid][stat] = nil
		else
			local text = ''
			for jid, jobs in pairs ( delayed_jobs ) do
				for status, job in pairs ( jobs ) do
					text = text .. '\n - ' .. jid .. ' -> ' .. status .. ': ' .. job
				end
			end
			if text ~= '' then
				print ( 'List of jobs:' .. text )
			else
				print ( 'No jobs' )
			end
		end
	end, true, { "del" } )

commands_help['delay'] = "[-t target_jid] status_letter message\n\nDelays sending a message to target jid (or current buddy) until it switches to specified status."
commands_help['job'] = "[del jid status_letter]\n\nLists available jobs or deletes specified one."

main.hook ( 'hook-status-change',
	function ( args )
		if delayed_jobs[args.jid] and delayed_jobs[args.jid][args.new_status] then
			main.run ( delayed_jobs[args.jid][args.new_status] )
			delayed_jobs[args.jid][args.new_status] = nil
		end
	end )

main.hook ( 'hook-lua-quit', save_jobs )

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