examples/jobs.lua
changeset 8 fc9060b9b7cc
child 24 25552b21d3fb
equal deleted inserted replaced
7:eb6d89bf1fbf 8:fc9060b9b7cc
       
     1 --- JOBS
       
     2 
       
     3 delayed_jobs = {}
       
     4 
       
     5 -- may fail
       
     6 dopath 'saved_jobs.lua'
       
     7 
       
     8 function save_jobs ()
       
     9 	local h = io.open ( main.config_file ( 'saved_jobs.lua' ), "w" )
       
    10 	if not h then
       
    11 		print ( 'Cannot open jobs file for writing!' )
       
    12 		return
       
    13 	end
       
    14 	h:write ( "-- This is autogenerated file, do not edit it manually\n\ndelayed_jobs = {\n" );
       
    15 	for jid, more in pairs ( delayed_jobs ) do
       
    16 		h:write ( string.format ( "\t[%q] = {\n", jid ) )
       
    17 		for status, action in pairs ( more ) do
       
    18 			if action then -- remove fired jobs
       
    19 				h:write ( string.format ( "\t\t[%q] = %q,\n", status, action ) )
       
    20 			end
       
    21 		end
       
    22 		h:write ( "\t},\n" )
       
    23 	end
       
    24 	h:write ( "}\n" )
       
    25 	h:close ()
       
    26 end
       
    27 
       
    28 main.command ( 'delay',
       
    29 	function ( args )
       
    30 		args = parse_args ( args )
       
    31 		local who
       
    32 		if args.t then
       
    33 			who = args.t
       
    34 			args.t = nil
       
    35 		else
       
    36 			who = main.current_buddy ()
       
    37 		end
       
    38 		local stat = args[1]
       
    39 		args[1] = nil
       
    40 		delayed_jobs[who] = { }
       
    41 		delayed_jobs[who][stat] = 'say_to -q ' .. who .. ' ' .. rebuild_args_string ( args )
       
    42 	end )
       
    43 
       
    44 main.command ( 'job',
       
    45 	function ( args )
       
    46 		local action, jid, stat = args:match ( "(%w+)%s+(%w+)%s+(%w)" )
       
    47 		if action == 'del' then
       
    48 			delayed_jobs[jid][stat] = nil
       
    49 		else
       
    50 			print ( 'List of jobs:' )
       
    51 			for jid, jobs in pairs ( delayed_jobs ) do
       
    52 				for status, job in pairs ( jobs ) do
       
    53 					print ( ' - ' .. jid .. ' -> ' .. status .. ': ' .. job )
       
    54 				end
       
    55 			end
       
    56 		end
       
    57 	end, { "del" } )
       
    58 
       
    59 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."
       
    60 commands_help['job'] = "[del jid status_letter]\n\nLists available jobs or deletes specified one."
       
    61 
       
    62 hooks_d['hook-status-change'].jobs =
       
    63 	function ( args )
       
    64 		if delayed_jobs[args.jid] and delayed_jobs[args.jid][args.new_status] then
       
    65 			main.run ( delayed_jobs[args.jid][args.new_status] )
       
    66 			delayed_jobs[args.jid][args.new_status] = nil
       
    67 		end
       
    68 	end
       
    69 
       
    70 hooks_d['hook-quit'].jobs = save_jobs
       
    71 	
       
    72 
       
    73 -- vim: se ts=4: --