block load
{
  es_xdoblock corelib/keyfilter/keyfilter_register
}

// keyfilter <keygroup> <key> <not/only> <part-of-value-name> [value]

block keyfilter_register
{
  // variables that keyfilter uses
  es_xsetinfo _keyfilter_argc 0
  es_xsetinfo _keyfilter_arg1 0
  es_xsetinfo _keyfilter_arg2 0
  es_xsetinfo _keyfilter_arg3 0
  es_xsetinfo _keyfilter_arg4 0
  es_xsetinfo _keyfilter_arg5 0
  es_xsetinfo _keyfilter_loop 0
  es_xsetinfo _keyfilter_loopval 0
  // check to see if keyfilter exists
  es es_xexists _exists command keyfilter
  if (server_var(_exists) = 0) do
  {
    es_xregcmd keyfilter corelib/keyfilter/keyfilter
  }
}

block keyfilter
{
  es_xgetargc _keyfilter_argc
  es es_xif (server_var(_keyfilter_argc) > 4) do
  {
    es_xgetargv _keyfilter_arg1 1
    es es_xexists _tempcore keygroup server_var(_keyfilter_arg1)
    es es_xif (server_var(_tempcore) = 1) do
    {
      es_xgetargv _keyfilter_arg2 2
      es es_xexists _tempcore key server_var(_keyfilter_arg1) server_var(_keyfilter_arg2)
      es es_xif (server_var(_tempcore) = 1) do
      {
        es_xgetargv _keyfilter_arg3 3
        es_xgetargv _keyfilter_arg4 4
        es_xdoblock corelib/keyfilter/filter
      }
      else do
      {
        es es_xdbgmsg 0 keyfilter: Invalid key provided for keygroup server_var(_keyfilter_arg1): server_var(_keyfilter_arg2)
      }
    }
    else do
    {
      es es_xdbgmsg 0 keyfilter: Invalid keygroup provided: server_var(_keyfilter_arg1)
    }
  }
  else do
  {
    es_xdbgmsg 0 keyfilter: Not enough arguments. Syntax: keyfilter <keygroup> <key> <not/only> <part-of-value-name> [value]
  }
}

block filter
{
  es_xgetargc _keyfilter_argc
  es_xgetargv _keyfilter_arg4 4
  es_xgetargv _keyfilter_arg5 5
  es_xkeygroupcreate _keyfilter_tempgroup
  es_xkeycreate _keyfilter_tempgroup _keyfilter_tempkey
  es es_xif (server_var(_keyfilter_arg3) = not) do
  {
    es es_xforeachval _keyfilter_loop in server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) "es_xdoblock corelib/keyfilter/copy_not"
    es es_xkeydelete server_var(_keyfilter_arg1) server_var(_keyfilter_arg2)
    es es_xkeycreate server_var(_keyfilter_arg1) server_var(_keyfilter_arg2)
    es es_xforeachval _keyfilter_loop in _keyfilter_tempgroup _keyfilter_tempkey "es_xdoblock corelib/keyfilter/paste"
  }
  else do
  {
    es es_xif (server_var(_keyfilter_arg3) = only) do
    {
      es es_xforeachval _keyfilter_loop in server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) "es_xdoblock corelib/keyfilter/copy_only"
      es es_xkeydelete server_var(_keyfilter_arg1) server_var(_keyfilter_arg2)
      es es_xkeycreate server_var(_keyfilter_arg1) server_var(_keyfilter_arg2)
      es es_xforeachval _keyfilter_loop in _keyfilter_tempgroup _keyfilter_tempkey "es_xdoblock corelib/keyfilter/paste"
    }
    else do
    {
      es es_xdbgmsg 0 keyfilter: Invalid action provided. Syntax: keyfilter <keygroup> <key> <not/only> <part-of-value-name> [value]
    }
  }
  es_xkeygroupdelete _keyfilter_tempgroup
}

block copy_not
{
  es es_xif (server_var(_keyfilter_argc) >= 6) do
  {
    es es_xkeygetvalue _keyfilter_loopval server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) server_var(_keyfilter_loop)
    es es_xif (server_var(_keyfilter_arg4) notin server_var(_keyfilter_loop)) do
    {
      es es_xkeysetvalue _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
    }
    else do
    {
      es es_xif (server_var(_keyfilter_arg5) != server_var(_keyfilter_loopval)) do
      {
        es es_xkeysetvalue _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
      }
    }
  }
  else do
  {
    es es_xkeygetvalue _keyfilter_loopval server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) server_var(_keyfilter_loop)
    es es_xif (server_var(_keyfilter_arg4) notin server_var(_keyfilter_loop)) do
    {
      es es_xkeysetvalue _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
    }
  }
}

block copy_only
{
  es es_xif (server_var(_keyfilter_argc) >= 6) do
  {
    es es_xkeygetvalue _keyfilter_loopval server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) server_var(_keyfilter_loop)
    es es_xif (server_var(_keyfilter_arg4) in server_var(_keyfilter_loop)) do
    {
      es es_xkeysetvalue _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
    }
    else do
    {
      es es_xif (server_var(_keyfilter_arg5) != server_var(_keyfilter_loopval)) do
      {
        es es_xkeysetvalue _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
      }
    }
  }
  else do
  {
    es es_xkeygetvalue _keyfilter_loopval server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) server_var(_keyfilter_loop)
    es es_xif (server_var(_keyfilter_arg4) in server_var(_keyfilter_loop)) do
    {
      es es_xkeysetvalue _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
    }
  }
}

block paste
{
  es es_xkeygetvalue _keyfilter_loopval _keyfilter_tempgroup _keyfilter_tempkey server_var(_keyfilter_loop)
  es es_xkeysetvalue server_var(_keyfilter_arg1) server_var(_keyfilter_arg2) server_var(_keyfilter_loop) server_var(_keyfilter_loopval)
}
