<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Saturday, June 30, 2007, 10:48  -->
<!-- MuClient version 4.13 -->

<!-- Plugin "Chat_Redirector" generated by Plugin Wizard -->

<!--
Edit plugin and change "chat_world" variable to be the name of the 
world you want chats to go to.
-->

<muclient>
<plugin
   name="Aardwolf_Chat"
   author="Nick Gammon"
   id="c46cf2efaba34c42b3dd17bf"
   language="Lua"
   purpose="Redirects Aardwolf chat messages"
   date_written="2008-06-18"
   requires="4.08"
   version="1.0"
   save_state="y"
   >
<description trim="y">
<![CDATA[
Redirects chats, tells and channels to the "Chats" world window.

chats echo on   --> also echo in the main window
chats echo off  --> only show in the other window
chats echo      --> toggle echoing (switch from on to off or vice-versa)

]]>
</description>

</plugin>

<!--  Triggers  -->

<triggers>

  <trigger
   enabled="y"
   match="^{say}(?<msg>.*)"
   regexp="y"
   script="chat_redirect"
   omit_from_output="y"
   sequence="100"
  >
  </trigger>

<trigger
   enabled="y"
   match="^{chan ch=(?<channel>\w+)}(?<msg>.*)"
   regexp="y"
   script="channel_redirect"
   omit_from_output="y"
   sequence="100"
  >
  </trigger>

 <trigger
   enabled="y"
   match="^Remort Auction:.+$"
   regexp="y"
   script="channel_redirect"
   omit_from_output="y"
   sequence="100"
  >
  </trigger>

 <trigger
   enabled="y"
   match="^Global Quest:.+$"
   regexp="y"
   script="channel_redirect"
   omit_from_output="y"
   sequence="100"
  >
  </trigger>

 <trigger
   enabled="y"
   match="^INFO:.+$"
   regexp="y"
   script="channel_redirect"
   omit_from_output="y"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="y"
   match="^{tell}(?<msg>.*)"
   script="chat_redirect"
   omit_from_output="y"
   regexp="y"
   sequence="100"
  >
  </trigger>
 

</triggers>

<aliases>
  <alias
   script="chat_echo"
   match="^chats echo( on| off)?$"
   enabled="y"
   regexp="y"
   sequence="100"
   ignore_case="y"
  >
  </alias>
</aliases>


<!--  Script  -->


<script>
<![CDATA[
chat_world = "Chats"
folder = "Aardwolf"

require "getworld"
require "checkplugin"
require "utils"

echo = false

function hasmore(text)
  _place = string.find(text, '}')
  _stlen = string.len(text)
  if _place == _stlen then
    return false
  else
    return true
  end
end

-- chat redirector
function chat_redirect (name, line, wildcards, styles)

  local w = get_a_world (chat_world, folder)

  if w == nil then
     colourNote("red","","Chat world file not available.")
     return
  end

  if not hasmore(styles[1].text) then
    table.remove (styles, 1)  -- get rid of tag
  else
    ttext = styles[1].text
    _place = string.find(ttext, '}')
    if _place then
      styles[1].text = string.sub(ttext, _place + 1)
    end
  end

  -- this stuff to echo in this world as well
  if echo then
    for i, v in ipairs (styles) do
      ColourTell (RGBColourToName (v.textcolour), 
                    RGBColourToName (v.backcolour), 
                    v.text)  
    end
    Note ("")  -- wrap up line
  end
  
  
  -- send to other world 
  send_to_world (chat_world, styles)
  
    
end -- function chat_redirect 

function channel_redirect (name, line, wildcards, styles)

  -- note, we could filter on channel name (wildcards.channel)
  
  chat_redirect (name, line, wildcards, styles)
  
end -- function channel_redirect 

function chat_echo (name, line, wildcards)
  if wildcards [1] == false then
    echo = not echo
  elseif wildcards [1]:lower () == " on" then
    echo = true
  elseif wildcards [1]:lower () == " off" then
    echo = false
  end -- if
  
  if echo then
    ColourNote ("yellow", "", "Echoing chats in main window enabled.")
  else
    ColourNote ("yellow", "", "Echoing chats in main window DISABLED.")
  end -- if  
    
end -- chat_echo

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

-- pull in telnet option handling
dofile (GetPluginInfo (GetPluginID (), 20) .. "telnet_options.lua")

function OnPluginConnect ()
  -- first time after connect, get tags right
  
  TelnetOptionOn (TELOPT_CHANNELS)
  TelnetOptionOn (TELOPT_TELLS)
  TelnetOptionOn (TELOPT_SAYS)
 
  echo = false
            
end -- function OnPluginConnect

function OnPluginClose ()
  -- if enabled
  if GetPluginInfo (GetPluginID (), 17) then
    TelnetOptionOff (TELOPT_CHANNELS)
    TelnetOptionOff (TELOPT_TELLS)
    TelnetOptionOff (TELOPT_SAYS)
  end -- if enabled         
end -- OnPluginClose

function OnPluginEnable ()
  -- ensure world file exists
  w = get_a_world (chat_world, folder)

  if w == nil then
     CallPlugin ("35dfdbf3afc8cbf60c91277c", "CreateWorldFile", folder .. "," .. chat_world)
     w = get_a_world (chat_world, folder)
  end
   
  if w then
    os.setlocale ("", "time")
    w:Note (os.date ("Chats will appear here - started %c."))
    w:SetOption ("do_not_show_outstanding_lines", 1)
    w:SetCommandWindowHeight (0)  -- no command window
    w:SetWorldWindowStatus (3) -- restore it 
  end -- no world

  -- if we are connected when the plugin loads, it must have been reloaded whilst playing
  if IsConnected () then
    OnPluginConnect ()
  end -- if already connected
  
end -- OnPluginEnable

function OnPluginDisable ()
  TelnetOptionOff (TELOPT_CHANNELS)
  TelnetOptionOff (TELOPT_TELLS)
  TelnetOptionOff (TELOPT_SAYS)

  if not w then return end

  local w = get_a_world (chat_world, folder)
  if w then
    w:SetWorldWindowStatus (2) -- minimize it on disable
  end -- if 
  Activate ()
end -- OnPluginDisable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
end -- OnPluginSaveState


]]>
</script>
</muclient>
