<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Warfare_Gag"
   author="Abelinc"
   id="a75b9bb6b052924fb8ceb990"
   language="Lua"
   purpose="Gags all wars for which you are not eligible. V1 will just use level, v2 also clan"
   date_written="2011-02-05"
   requires="4.40"
   version="1.0"
   save_state="y"
   >
<description trim="y">
<![CDATA[
Install this plugin to automatically weed out those warfare declarations unless you're
eligible to participate in the war (i.e. like GQ declarations). It isn't 100% silent.
When the war is first declared, it will give a 1-line message stating whether you
qualify for the war or not. This is may be removed in the future, but I wanted to leave
it there for debugging until the plugin is tested by more than just me.

warfare help                  - Displays this text
warfare color <colorname>     - Sets the color for warfare messages

]]>
</description>

</plugin>

<aliases>

  <alias
   name="warfare_help"
   script="warfare_gag_help"
   match="warfare help"
   enabled="y"
   regexp="n"
   ignore_case="y"
   sequence="100"
  >
  </alias>

  <alias
   name="warfare_color"
   script="warfare_gag_color"
   match="warfare color *"
   enabled="y"
   regexp="n"
   ignore_case="y"
   sequence="100"
  >
  </alias>

</aliases>


<triggers>
  <trigger
   enabled="y"
   script="warfare_checklevel"
   match="^WARFARE: (Genocide|A Class war|A Race war|A Clan war) has been declared by [A-Za-z]+ for levels (\d+) to (\d+)!$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   script="warfare_echo"
   match="^WARFARE: The preparation grounds shall close in approx 4 minutes!$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   script="warfare_echo"
   match="^WARFARE: Type 'combat' to join the war. No death penalties!$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   script="warfare_echo"
   match="^WARFARE: The preparation room will close in [1-3] minutes?.$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   script="warfare_echo"
   match="^WARFARE: There are no enemies to fight each other. Peace declared!$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   script="warfare_echo"
   match="^WARFARE: [A-Za-z]+ \(Level [0-9]+ [A-Za-z]+ [a-z]+\) has gone to war!$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   script="warfare_echo"
   match="^WARFARE: [A-Za-z]+ has gone to war to defend the honor of .*?$"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>

</triggers>

<script>
<![CDATA[

require "serialize"
require "commas"
require "gmcphelper"

function OnPluginBroadcast (msg, id, name, text)
   -- Look for GMCP handler.
   if (id == '3e7dedbe37e44942dd46d264') then
      if (text == "char.status") then
         res, gmcparg = CallPlugin("3e7dedbe37e44942dd46d264","gmcpval","char")

         luastmt = "gmcpdata = " .. gmcparg
         assert (loadstring (luastmt or "")) ()
      end
   end
end

function warfare_checklevel (name, line, wildcards)
	if tonumber(wildcards [2]) <= tonumber(gmcpval("status.level")) then
		if tonumber(gmcpval("status.level")) <= tonumber(wildcards [3]) then
			warfare_mylevel = "yes"
			ColourNote (warfare_color_name, "", "I do qualify for this war. My level is " .. gmcpval("status.level"))
			ColourNote (warfare_color_name, "", wildcards [0] )
		else
			warfare_mylevel = "no"
			ColourNote (warfare_color_name, "", "I do NOT qualify for this war. My level is " .. gmcpval("status.level") .. " and it is for " .. wildcards [2] .. " to " .. wildcards [3])
		end
	elseif tonumber(wildcards [2]) >= tonumber(gmcpval("status.level")) then
			warfare_mylevel = "no"
			ColourNote (warfare_color_name, "", "I do NOT qualify for this war. My level is " .. gmcpval("status.level") .. " and it is for " .. wildcards [2] .. " to " .. wildcards [3])
	else
			ColourNote (warfare_color_name, "", "Hmm, something went wrong, and the script didn't figure out whether I qualify or not." .. gmcpval("status.level"))
			ColourNote (warfare_color_name, "", "Contact Abelinc and have him check wars of level " .. wildcards [2] .. " to " .. wildcards [3] .. " at player level " .. gmcpval("status.level"))
	end
end -- warfare_checklevel

function warfare_echo (name, line, wildcards)
	if warfare_mylevel == "yes" then
		ColourNote (warfare_color_name, "", wildcards [0] )
	end
end

function warfare_gag_help (name, line, wildcards)
  ColourNote ("teal", "", world.GetPluginInfo (world.GetPluginID (), 3))
  return true -- done  
end -- warfare_gag_help

function warfare_gag_color (name, line, wildcards)
	warfare_color_name = wildcards [1]
	ColourNote ("yellow", "", "Warfare messages will now be shown in the color ", warfare_color_name, "", wildcards [1] )	
end -- warfare_gag_color

function OnPluginInstall ()
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    check (EnablePlugin(GetPluginID (), false))
    return
  end -- they didn't enable us last time

  OnPluginEnable ()  -- do initialization stuff

end -- OnPluginInstall

function OnPluginEnable ()

	if GetVariable ("warfare_color_name") == nil then 
		SetVariable ("warfare_color_name", "yellow")
	end
	warfare_color_name = GetVariable ("warfare_color_name")

	Send_GMCP_Packet("request char")

end -- OnPluginEnable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
  SetVariable ("warfare_color_name", warfare_color_name)
end -- OnPluginSaveState


]]>
</script>

</muclient>
