<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Global_Option_Updater"
   author="Nick Gammon"
   id="1204316574ebc1d41d7011b3"
   language="Lua"
   purpose="Shows / changes global options"
   date_written="2010-09-17 08:28:51"
   date_modified="2018-03-28 11:06"
   requires="4.50"
   version="2.0"
   >
<description trim="y">
<![CDATA[
Usage:

list_global_options       --> list all valid option names
show_global_option x      --> displays value of option
change_global_option x y  --> changes value of option x to y


eg.

show_global_option FixedPitchFont

change_global_option FixedPitchFont Dina

After changing an option you should exit the client and re-open it, otherwise
if you use the GUI interface (global preferences) it may put the option back again.

]]>
</description>

</plugin>


<!--  Aliases  -->

<aliases>
  <alias
   match="change_global_option * *"
   enabled="y"
   sequence="100"
   script="ChangeOption"
  >
  </alias>

  <alias
   match="show_global_option *"
   enabled="y"
   sequence="100"
   script="ShowOption"
  >
  </alias>

 <alias
   match="list_global_options"
   enabled="y"
   sequence="100"
   script="ListOptions"
  >
  </alias>

</aliases>

<script>

function ListOptions (name, line, wildcards)

  ColourNote ("cyan", "", "Global Options")
  ColourNote ("cyan", "", string.rep ("-", 40))

  -- show all

  local t = GetGlobalOptionList ()
  table.sort (t)

  for k, v in ipairs (t) do
   ColourNote ("cyan", "", v)
  end  -- for

  ColourNote ("cyan", "", string.rep ("-", 40))
end -- ListOptions

function ShowOption (name, line, wildcards)

  local whichOption = wildcards [1]

  -- find all possible keys
  local t = { }
  for k, v in ipairs (GetGlobalOptionList ()) do
    t [v] = true
  end -- for

  if not t [whichOption] then
    ColourNote ("red", "", "Item '" .. whichOption .. "' is not a global option. Try: list_global_options")
    ColourNote ("deepskyblue", "", "Option names are case-sensitive.")
    return
  end -- if option not found

  -- open preferences database
  local db = assert (sqlite3.open (GetInfo (82)))
  local value = nil

  -- find the item
  for a in db:nrows ("SELECT * FROM prefs WHERE name = '" .. whichOption .. "'") do
    value = a.value
  end  -- for

  -- if found, display it
  if value then
    ColourTell ("cyan", "", "Item '" .. whichOption .. "' has value '")
    ColourTell ("orange", "", value)
    ColourNote ("cyan", "", "'")
  else
    ColourNote ("red", "", "Item '" .. whichOption .. "' is not in preferences database")
  end -- does not exist

  db:close()

end -- ShowOption

function ChangeOption (name, line, wildcards)

  local whichOption = wildcards [1]
  local newValue = wildcards [2]

  -- find all possible keys
  local t = { }
  for k, v in ipairs (GetGlobalOptionList ()) do
    t [v] = true
  end -- for

  if not t [whichOption] then
    ColourNote ("red", "", "Item '" .. whichOption .. "' is not a global option. Try: list_global_options")
    ColourNote ("deepskyblue", "", "Option names are case-sensitive.")
    return
  end -- if

  -- open preferences database
  local db = assert (sqlite3.open (GetInfo (82)))
  local value = '(nothing)'

  -- see if this key already exists and if so, what its value is
  for a in db:nrows ("SELECT * FROM prefs WHERE name = '" .. whichOption .. "'") do
    value = a.value
  end  -- for

  -- fix up quotes
  newValueFixed = string.gsub (newValue, "'", "''")

  if db:execute ("INSERT OR REPLACE INTO prefs " ..
                 "(name, value) VALUES ('" .. whichOption .. "', '"
                 .. newValue .. "')") ~= sqlite3.OK then
    ColourNote ("red", "", "Unable to modify preferences: " .. db:errmsg ())
  else
    if value ~= "%2" then
      ColourNote ("cyan", "", "Item '" .. whichOption .. "' changed from '"
                  .. value .. "' to '" .. newValue .. "'")
      ColourNote ("orange", "", [[
After changing an option you should exit the client and re-open it, otherwise
if you use the GUI interface (global preferences) it may put the option back again
to its old value because the options are kept in memory.]])

    else
      ColourNote ("cyan", "", "No change required, value of '"
                  .. whichOption .. "' is already '" .. newValue .. "'")
    end -- if
  end -- if updated OK

  db:close()

end -- ChangeOption

-- show help
ColourNote ("cyan", "", GetPluginInfo (GetPluginID (), 3))

</script>

</muclient>
