<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Todo"
   author="Trevize"
   id="091e0018c5777fe2436663b2"
   language="Lua"
   purpose="Todo List"
   save_state="y"
   requires="4.40"
   version="1.0"
   >

<description trim="y">
<![CDATA[

Todo list.

Enjoy!

-Trevize

]]>
</description>

</plugin>


<aliases>
  <alias match="^\s*todo(?:\s+(add|remove|replace|insert|help)?\s*(.*))?$" enabled="y" group="todo" regexp="y" send_to="12" ignore_case="y" sequence="100">
    <send>todo.parse ("%1", "%2")</send>
  </alias>
</aliases>

<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>
</triggers>


<script>
<![CDATA[

Z = {
  tbl = {
    save = function (name, t)
      if type (t) == "table" then
        local var = ""
        for k, v in pairs (t) do
          if (type (k) == "number" or type (k) == "string") and
             (type (v) == "boolean" or type (v) == "number" or type (v) == "string") then
            local key
            if type (k) == "number" then
              key = '[' .. k .. ']'
            elseif type (k) == "string" then
              key = '["' .. k .. '"]'
            end -- if
            local val
            if type (v) == "boolean" or type (v) == "number" then
              val = tostring (v)
            elseif type (v) == "string" then
              val = string.format("%q", v)
            end -- if
            var = var .. name .. key .. " = " .. val .. "\r\n"
          end -- if
        end -- for
        return var
      end -- if
    end, -- func

    load = function (name)
      if GetVariable (name) then
        loadstring (GetVariable (name)) ()
      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

  trim = function (x)
    return (string.gsub(x, "^%s*(.-)%s*$", "%1"))
  end, -- func

  singlespace = function (x)
    while string.find (x, "  ") do
      x = string.gsub (x, "  ", " ")
    end -- do
    return x
  end, -- func
  }
ansicolor = Z.ansicolor
string.trim = Z.trim
string.singlespace = Z.singlespace

prompt = {}
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




todo = {
  list = {},

  parse = function (cmd, txt)
    cmd = string.lower(string.singlespace(string.trim(cmd)))
    txt = string.singlespace(string.trim(txt))
    if cmd == "" then
      todo.run ()
    elseif cmd == "add" then
      if txt == "" then
        AnsiNote (ansicolor (), "What do you want to add to the todo list?")
      else
        table.insert (todo.list, txt)
        todo.save ()
        todo.run ()
      end -- if
    elseif cmd == "insert" then
      txt = utils.split (txt, " ", 2)
      txt[1] = tonumber(txt[1])
      if not txt[1] then
        AnsiNote (ansicolor (), "What position do you want to insert the item at?")
      elseif txt[1] > (1 + #todo.list) then
        AnsiNote (ansicolor (), "You can't insert an item beyond one past the list's last item.")
      elseif not txt[2] then
        AnsiNote (ansicolor (), "What do you want to insert into the todo list?")
      else
        table.insert (todo.list, txt[1], txt[2])
        todo.save ()
        todo.run ()
      end -- if
    elseif cmd == "remove" then
      txt = tonumber(txt)
      if not txt then
        AnsiNote (ansicolor (), "Which item do you want to remove from the todo list?\n")
      elseif not todo.list[txt] then
        AnsiNote (ansicolor (), "No todo item \"" .. txt .. "\" found.\n")
      else
        AnsiNote (ansicolor (), "Removed:")
        AnsiNote (ansicolor (), txt .. ". " .. todo.list[txt] .. "\n")
        table.remove (todo.list, txt)
        todo.save ()
      end -- if
      todo.run ()
    elseif cmd == "replace" then
      txt = utils.split (txt, " ", 2)
      txt[1] = tonumber(txt[1])
      if not txt[1] then
        AnsiNote (ansicolor (), "What item do you want to replace?")
      elseif not todo.list[txt[1]] then
        AnsiNote (ansicolor (), "There is no item at that position.")
      elseif not txt[2] then
        AnsiNote (ansicolor (), "What do you want to replace item " .. txt[1] .. " with?")
      else
        AnsiNote (ansicolor (), "Replaced:")
        AnsiNote (ansicolor (), txt[1] .. ". " .. todo.list[txt[1]] .. "\n")
        todo.list[txt[1]] = txt[2]
        todo.save ()
        todo.run ()
      end -- if
    elseif cmd == "help" then
      todo.help ()
    end -- if
    prompt.draw ()
  end, -- func

  run = function ()
    AnsiNote (ansicolor (), "To Do List:\n")
    for k, v in ipairs (todo.list) do
      AnsiNote (ansicolor (), k .. ". " .. v)
    end -- for
  end, -- func

  help = function ()
    AnsiNote (ansicolor (), "\nTodo Commands\n-------------")
    AnsiNote (ansicolor (), "TODO - shows the todo list")
    AnsiNote (ansicolor (), "TODO ADD <todo note> - adds an item to the todo list")
    AnsiNote (ansicolor (), "TODO REMOVE <#> - removes an item from the todo list")
    AnsiNote (ansicolor (), "TODO REPLACE <#> <todo note> - replaces a todo note")
    AnsiNote (ansicolor (), "TODO INSERT <#> <todo note> - inserts a todo note at the location")
  end, -- func

  save = function ()
    SetVariable ("todo", Z.tbl.save ("todo.list", todo.list))
  end, -- func
  }

Z.tbl.load ("todo")
]]>
</script>

</muclient>