<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="DelayedAction"
   author="Keldar"
   id="82d1da5383014849474d726c"
   language="Lua"
   purpose="Suppressing all input so that some actions can finish executing"
   date_written="2006-09-20"
   requires="3.80"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Suspends all input until specified time expires.

Commands:

  flc_pause <time> <action>   - <time> is the time interval 
                                (in seconds) to wait for;
                                
                                <action> is whatever text to send
                                to the MUD before starting to suspend;
                                
  flc_defer <time>            - starts deferring any sent commands,
                                so they are sent at intervals equal to
                                <time> (seconds);
                                
  flc_resume                  - stop suspending or deferring output 
                                and resume normal processing. 

The <action> text will be passed through Execute, so you can use an 
alias when suspending input.

Example of using the suspend function in a script:

  Execute("flc_pause 1.8 cure aeon")
  
That will  execute "cure aeon", which is hopefully your alias for
curing that affliction.

When the plugin starts suspending input, it sets the "flc_delayed" flag
true, so you can test whether input is being suspended by checking this
flag's value with the klib.GetFlag() function supplied by the flc package.

When the timeout expires, processing will be resumed automatically, and
the "flc_resume" alias will be Executed for any other interested parties 
to hear and take necessary action.
]]>
</description>

</plugin>


<aliases>
  <alias
   name="flc_pause"
   script="DoPause"
   match="^flc_pause\s+(?P&lt;timeout&gt;\d+(?:\.\d+)?)\s+?(?P&lt;send&gt;.+)$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>

<aliases>
  <alias
   name="flc_defer"
   script="DoDefer"
   match="^flc_defer\s+(?P&lt;timeout&gt;\d+(?:\.\d+)?)$"
   enabled="y"
   regexp="y"
   sequence="100"
  >
  </alias>
</aliases>

<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Script  -->


<script>
<![CDATA[
require "flc.kwait"
require "flc.klib"
require "flc.kutils"
require "flc.events"

local end_token = "delay_4f3660abb"
local placeholder = "defer_4f3660abb"

local default_delay = GetSpeedWalkDelay()

local redo_defer = false
local defer_delay = 0

function OnResume(evt)
  if redo_defer then
    klib.SetFlag("flc_deferred", true)
    redo_defer = false
    DiscardQueue()
    SetSpeedWalkDelay(defer_delay)
    Queue(placeholder)
    defer_delay = 0
    return
  end
  DiscardQueue()
  klib.SetFlag("flc_delayed", false)
  klib.SetFlag("flc_deferred", false)
  SetSpeedWalkDelay(default_delay)
  kutils.DoNote("Resuming normal command processing.")
end

OnResume()

events.SetEvent(events.FLC_RESUME, OnResume)

function OnPluginSend(data)
  if string.find(data, "^" .. end_token) then
    Execute(events.FLC_RESUME)
    return false
  elseif klib.GetFlag("flc_deferred") then
    if not GetQueue() then
      Queue(placeholder)
    else
      local q = "(" .. table.concat(GetQueue(), ")(") .. ")"
      DiscardQueue()
      Queue(EvaluateSpeedwalk(q))
    end
    if data == placeholder then
      return false
    end
  end
  return true
end


function convert_seconds(seconds)
  return seconds * 1000
end

function DoPause(name, outp, wildcs)
  if klib.GetFlag("flc_delayed") then
    kutils.DoNote("Input is already delayed.")
    return
  elseif klib.GetFlag("flc_deferred") then
    redo_defer = true
    klib.SetFlag("flc_deferred", false)
    defer_delay = GetSpeedWalkDelay()
  end
  
  local delay = convert_seconds(tonumber(wildcs.timeout))
  local tosend = wildcs.send
  
  DiscardQueue()
  SetSpeedWalkDelay(delay)
  Execute(tosend)
  Queue(end_token)
  kutils.DoNote("Command input supressed for " .. wildcs.timeout .. " seconds.")
  klib.SetFlag("flc_delayed", true)
  
end


function DoDefer(name, outp, wildcs)
  if klib.GetFlag("flc_delayed") then
    kutils.DoNote("Can't defer - input is currently suspended.")
    return
  end
  
  local delay = convert_seconds(tonumber(wildcs.timeout))
  DiscardQueue()
  SetSpeedWalkDelay(delay)
  Queue(placeholder)
  klib.SetFlag("flc_deferred", true)
  kutils.DoNote("Commands will be deferred by " .. wildcs.timeout .. " seconds.")
end


]]>
</script>


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="DelayedAction:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp ()
  world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script> 

</muclient>
