<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>

<plugin
 name="Antitheft"
 author="Trevize"
 id="7b29064c82b9454b956fc192"
 language="Lua"
 purpose="preventing theft"
 save_state="y"
 requires="4.40"
 version="1.32">

<description trim="y">
<![CDATA[

Antitheft Manager 1.3.2

This plugin is designed to help you set up your antitheft.

ANTITHEFT HELP shows the commands.

Enjoy!

-Trevize

]]>
</description>

</plugin>


<triggers>
  <trigger
   keep_evaluating="y"
   match="^\d+h(, \d+m)?(, \d+e)?(, \d+w)? [@cexkdb]*(?: Vote)?-"
   enabled="y"
   regexp="y"
   send_to="14"
   sequence="25">
    <send>prompt.data = TriggerStyleRuns</send>
  </trigger>

  <trigger
   enabled="y"
   keep_evaluating="y"
   match="^You get (\d+) gold sovereigns? from (.+)\.$"
   regexp="y"
   send_to="12"
   sequence="100">
    <send>
      if antitheft.enabled.system and antitheft.enabled.container then
        if antitheft.container.desc == "%2" then
          Send("put sovereigns in " .. antitheft.container.num)
        end -- if
      end -- if
    </send>
  </trigger>

  <trigger
   enabled="y"
   keep_evaluating="y"
   match="^You remove (.+)\.$"
   regexp="y"
   send_to="12"
   sequence="100">
    <send>
      if antitheft.enabled.system and antitheft.enabled.rewear then
        for k, v in pairs (antitheft.current) do
          if "%1" == v then Send ("wear " .. k) end
        end -- for
      end -- if
    </send>
  </trigger>
</triggers>


<aliases>
  <alias
   match="^\s*antitheft\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>antitheft.display ("%1")</send>
  </alias>

  <alias
   match="^\s*antitheft\s+(enable|disable)(?:\s+(rewear|container))?\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>antitheft.enable ("%2", "%1")</send>
  </alias>

  <alias
   match="^\s*antitheft\s+add\s+(\d+)\s+(.+?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>antitheft.add ("%1", "%2")</send>
  </alias>

  <alias
   match="^\s*antitheft\s+container\s+(\d+)\s+(.+?)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>antitheft.setcontainer ("%1", "%2")</send>
  </alias>

  <alias
   match="^\s*antitheft\s+remove\s+(\d+)\s*$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100">
    <send>antitheft.remove ("%1", "%2")</send>
  </alias>

  <alias
   match="^\s*antitheft\s.+$"
   enabled="y"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="101">
    <send>antitheft.help ()</send>
  </alias>
</aliases>


<script>
<![CDATA[

-- ============
--  foundation
-- ============

table.save = function (mushvar, luavar, luavarname)
  if type (luavar) == "table" then
    local var = ""
    for k, v in pairs (luavar) do
      if type (v) == "boolean" or type (v) == "number" or type (v) == "string" then
        if type (k) == "string" then k = string.format ("%q", k) end
        local key = "[" .. k .. "]"
        local val
        if type (v) == "string" then val = string.format ("%q", v) else val = tostring (v) end
        var = var .. luavarname .. key .. " = " .. val .. "\r\n"
      elseif type (v) == "table" then
        local keyname = k
        if type (keyname) == "string" then keyname = string.format ("%q", keyname) end
        var = var .. table.save (false, luavar[k], luavarname .. "[" ..keyname .. "]")
      end -- if
    end -- for
    if mushvar then SetVariable (mushvar, var) else return var end
  end -- if
end -- func

table.load = function (name)
  if GetVariable (name) then
    loadstring (GetVariable (name)) ()
    return true
  end -- if
end -- func

ansicolor = function (fore, back)
  local bold = false
  local fore = tonumber (fore) or 7
  if fore >= 0 and fore <= 7 then
    fore = fore + 30
  elseif fore >= 8 and fore <= 15 then
    fore = fore + 22
    bold = true
  else
    fore = 37
  end -- if
  local back = tonumber (back) or 0
  if back > 0 and back <= 7 then
    back = back + 40
  else
    back = 40
  end -- if
  if not bold then
    return ANSI (0, fore, back)
  else
    return ANSI (0, fore, 1, back)
  end -- if
end -- func

prompt = {}
prompt.data = false
prompt.draw = function ()
  if prompt.data then
    for k,v in ipairs (prompt.data) do
      ColourTell (RGBColourToName (v.textcolour),
                  RGBColourToName (v.backcolour),
                  v.text)
    end -- for
  end -- if
  Note ("")
end -- func



-- ===========
--  antitheft
-- ===========

antitheft = {

  enabled = {
    container = true,
    rewear = true,
    system = true,
    },

  current = {},
  container = {num = "pack", desc = "a canvas backpack"},

  display = function ()
      local color = {["true"] = 10, ["false"] = 9}
      local enabled = {["true"] = "enabled", ["false"] = "disabled"}
      local x = tostring(antitheft.enabled.system)
      AnsiNote (ansicolor (15), "Antitheft " .. ansicolor (color[x]), enabled[x])
      AnsiNote (ansicolor (), "")
      x = tostring(antitheft.enabled.container)
      AnsiNote (ansicolor (15), "Container: " .. ansicolor (color[x]), enabled[x])
      AnsiNote (ansicolor (1), " [", ansicolor (15), string.format ("%7s", antitheft.container.num),
                ansicolor (1) , "] ", ansicolor (8), antitheft.container.desc)
      AnsiNote (ansicolor (), "")
      x = tostring(antitheft.enabled.rewear)
      AnsiNote (ansicolor (15), "Rewear " .. ansicolor (color[x]), enabled[x])
      for k, v in pairs (antitheft.current) do
        AnsiNote (ansicolor (1), " [", ansicolor (15), string.format ("%7s", k),
                  ansicolor (1) , "] ", ansicolor (8), v)
      end -- for
      prompt.draw ()
  end, -- func

  add = function (num, item)
    antitheft.current[num] = item
    AnsiNote (ansicolor (), "Item ", ansicolor (15), num,
              ansicolor (8), " (" .. item .. ") ", ansicolor (), "protected.")
    antitheft.save ()
    prompt.draw ()
  end, -- func

  remove = function (num)
    if antitheft.current[num] then
      AnsiNote (ansicolor (), "Item ", ansicolor (15), num,
                ansicolor (8), " (" .. antitheft.current[num] .. ") ", ansicolor (), "no longer protected.")
      antitheft.current[num] = nil
      antitheft.save ()
    else
      AnsiNote (ansicolor (), "Item ", ansicolor (15), num, ansicolor (), " isn't listed.")
    end -- if
    prompt.draw ()
  end, -- func

  setcontainer = function (num, item)
    antitheft.container.num = num
    antitheft.container.desc = item
    AnsiNote (ansicolor (), "Container ", ansicolor (15), num,
              ansicolor (8), " (" .. item .. ") ", ansicolor (), "protected.")
    antitheft.save ()
    prompt.draw ()
  end, -- func

  enable = function (section, textbool)
    section = string.lower (section)
    textbool = string.lower(textbool)
    local bool = (textbool == "enable")
    if section == "" then section = "system" end
    antitheft.enabled[section] = bool
    AnsiNote (ansicolor (), "Antitheft " .. section .. " " .. textbool .. "d.")
    antitheft.save ()
    prompt.draw ()
  end, -- func

  help = function ()
    AnsiNote (ansicolor (), "Antitheft Commands")
    AnsiNote (ansicolor (), "------------------")
    AnsiNote (ansicolor (), "ANTITHEFT - lists items protected and system status")
    AnsiNote (ansicolor (), "ANTITHEFT HELP - displays this file")
    AnsiNote (ansicolor (), "ANTITHEFT CONTAINER <container#> <short desc> - changes protected container")
    AnsiNote (ansicolor (), "ANTITHEFT ADD <item#> <short desc> - adds an item to be protected")
    AnsiNote (ansicolor (), "ANTITHEFT REMOVE <item#> - deletes an item being protected")
    AnsiNote (ansicolor (), "ANTITHEFT ENABLE [CONTAINER|REWEAR] - enables the system or section")
    AnsiNote (ansicolor (), "ANTITHEFT DISABLE [CONTAINER|REWEAR] - disables the system or section")
    prompt.draw ()
  end, -- func

  save = function ()
    local fullvar = table.save (false, antitheft.current, "antitheft.current")
                 .. table.save (false, antitheft.container, "antitheft.container")
                 .. table.save (false, antitheft.enabled, "antitheft.enabled")
    SetVariable ("antitheft", fullvar)
  end, -- func
  }

table.load ("antitheft")

]]>
</script>

</muclient>

