<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
   name="ScreenLogger"
   author="Keldar"
   id="2913bdf0ee89bd65f7bb2ddd"
   language="Lua"
   purpose="Logging screen output in HTML"
   date_written="2006-09-26"
   requires="3.80"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Command summary:

  start slog      - start logging
  stop slog       - stop logging
  
]]>
</description>

</plugin>


<aliases>
  <alias
   name="Start_screen_logging"
   script="start_logging"
   match="^\s*start\s+slog\s*$"
   enabled="y"
   regexp="y"
   menu="y"
   sequence="100"
  >
  </alias>
  <alias
   name="Stop_screen_logging"
   script="stop_logging"
   match="^\s*stop\s+slog\s*$"
   enabled="y"
   regexp="y"
   menu="y"
   sequence="100"
  >
  </alias>
</aliases>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Script  -->


<script>
<![CDATA[
require "flc.kutils"


function OnPluginScreendraw(tpe, log, line)

end

font_size = GetOption("output_font_height")
font_name = GetInfo(20)

LOG_START = [[
<!-- ScreenLogger by Keldar -->
<!-- Mushclient - www.mushclient.com -->
<table border="0" cellpadding="5" bgcolor="#000000">
<tr><td>
<pre><code>
]] .. [[<font size="4" face="]] .. font_name .. [[, FixedSys, Lucida Console, Courier New, Courier">]]


LOG_END = [[
</font>
</tr></td>
</pre></code>
</table>
]]

logging = false
log_name = ""


function start_logging()
  -- if already logging then stop here
  if logging then
    kutils.DoNote("A screen logging session is already in progress.")
    return
  end

  local dtable = os.date("*t")
  
  log_name = "SLog-" .. GetInfo(2) .. "-" .. dtable.month .. "-" .. dtable.day .. "-" .. dtable.year .. 
             "-" .. dtable.hour .. "-" .. dtable.min .. "-" .. dtable.sec .. ".html"
  SaveNotepad(log_name, GetInfo(58) .. log_name, true)
  AppendToNotepad(log_name, LOG_START)
  kutils.DoNote("Starting to log.")
  OnPluginScreendraw = log_screen
  logging = true
  
end

function stop_logging()
  -- if not logging then stop here
  if not logging then
    kutils.DoNote("You are not logging currently.")
    return
  end
  
  AppendToNotepad(log_name, LOG_END)
  OnPluginScreendraw = function() end
  SaveNotepad(log_name, GetInfo(58) .. log_name, true)
  CloseNotepad(log_name, false)
  kutils.DoNote("Logging stopped.")
  logging = false
  log_name = ""
  last_tstamp = nil
end

function log_screen (tpe, log, line)
  local timediff
  if last_tstamp then
    timediff = tostring(GetInfo(232) - last_tstamp)
  end
  last_tstamp = GetInfo(232)
  
  local line_number = GetLinesInBufferCount()-1
  local line_info = GetLineInfo(line_number)
  local tooltip = line_info.timestr .. " "
  if line_info.user then
    tooltip = tooltip .. "(command) "
  elseif line_info.note then
    tooltip = tooltip .. "(note) "
  end
  if timediff then
    tooltip = tooltip .. "[" .. timediff .. "]"
  end
    
  
  local styles = {}
  for i=1,line_info.styles do
    table.insert(styles, {text=GetStyleInfo(line_number, i, 1), 
                          fore=GetStyleInfo(line_number, i, 14),
                          back=GetStyleInfo(line_number, i, 15)})
  end
  local line = ""
  for _,style in ipairs(styles) do
    line = line .. 
           [[<span title="]] .. tooltip .. [[" style="color:]] .. RGBColourToName(style.fore) .. 
           [[;background:]] .. RGBColourToName(style.back) ..[[">]] ..
           FixupHTML(style.text) .. [[</span>]]
  end
  line = line .. "\r\n"
  AppendToNotepad(log_name, line)
end -- function



]]>
</script>


<!--  Plugin help  -->

<aliases>
  <alias
   script="OnHelp"
   match="ScreenLogger:help"
   enabled="y"
  >
  </alias>
</aliases>

<script>
<![CDATA[
function OnHelp ()
  world.Note (world.GetPluginInfo (world.GetPluginID (), 3))
end
]]>
</script> 

</muclient>

