MUSHClient script function compatibility

Registered by PenquinCoder

Porting a few of the most widely used MUSHClient script functions, as well as ensuring compatibilty with MUSHClient.

Blueprint information

Status:
Started
Approver:
None
Priority:
Undefined
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
Discussion
Series goal:
None
Implementation:
Good progress
Milestone target:
None
Started by
PenquinCoder

Related branches

Sprints

Whiteboard

---
Is this still being worked on? Also, is there interest to be supporting this long-term?
---

API for MUSHclient - http://www.gammon.com.au/scripts/function.php

Most used function of MUSHClient, and the Mudlet equivalent
##Naftali note - some functions in the second group would be greatly improved with a function to delete GUI triggers, etc.

mushenv = {
 GetVariable = function (var) return _G[var] end,
 GetVariableList = function () return _G end,
 SetVariable = function (var,what) _G[var] = what end,
 Simulate = feedTriggers,
 Execute = expandAlias,
 DeleteVariable = function (var) _G[var] = nil end,
 DeleteTrigger = disableTrigger,
 DeleteTriggerGroup = disableTrigger,
 EnableTrigger = function (triggername,tf) if tf then enableTrigger(triggername) else disableTrigger(triggername) end end,
 DeleteTimer = disableTimer,
 DeleteTimerGroup = disableTimer,
 EnableTimer = function (timername,tf) if tf then enableTimer(timername) else disableTimer(timername) end end,
 IsConnected = function () return true end,
 prepmatches = function () table.remove(matches,1) return matches or {} end,
 Send = function (...)
   local str,args = "", assert({ ... })
   for i,v in ipairs(args) do str = str .. v end
   send(str)
  end,
 colors = {
  darkcyan = {0,139,139},
  cyan = {0,255,255},
  silver = {192,192,192},
  lime = {0,255,0},
  darkred = {139,0,0},
  dimgray = {105,105,105},
  lightgreen = {144,238,144},
  darkgreen = {0,100,0},
  normal = {192, 192, 192},
  darkmagenta = {139,0,139},
  olive = {128,128,0},
  darkblue = {0,0,139},
  },
 ColourTell = function (...)
   local args,fg,bg = assert({ ... })
   for i = 1,table.maxn(args) do args[i] = args[i] or "black" end

   if math.floor( #args / 3 ) ~= ( #args / 3 ) then debug("Args must be in groups of three") return end
   for i = 1, #args, 3 do
    fg = (mushenv.colors[args[ i ]] or color_table[args[ i ]])
    bg = (mushenv.colors[args[ i+1 ]] or color_table[args[ i+1 ]])
    setFgColor(unpack(fg))
    setBgColor(unpack(bg))
    echo(args[ i+2 ])
   end
   resetFormat()
  end,
 Tell = function (...)
   local str,args = "", assert({ ... })
   for i,v in ipairs(args) do str = str .. v end
   echo(str)
  end,
 ColourNote = function (...)
   local str,args,fg,bg = "", assert({ ... })
   for i = 1,table.maxn(args) do args[i] = args[i] or "black" end

   if math.floor( #args / 3 ) ~= ( #args / 3 ) then debug("Args must be in groups of three") return end
   for i = 1, #args,3 do
    fg = (mushenv.colors[args[ i ]] or color_table[args[ i ]])
    bg = (mushenv.colors[args[ i+1 ]] or color_table[args[ i+1 ]])
    str = str .. "<" .. fg[1] .. "," .. fg[2] .. "," .. fg[3] .. ":" .. bg[1] .. "," .. bg[2] .. "," .. bg[3] .. ">" .. args[ i+3 ]
   end
   decho(str)
  end,
 Note = function (...)
   local str,args = "", assert({ ... })
   for i,v in ipairs(args) do str = str .. v end
   str = str .. "\n"
   echo(str)
  end,
 IsTimer = function (id)
   if not tostring(id) then return "eInvalidObjectLabel" end
   id = mushtimers[id]
   if not id or ( not isActive(id,"timer") and not ( enableTimer(id) and disableTimer(id) ) ) then return "eTimerNotFound" end
   return 0,"eOK"
  end,
 AddTimer = function (timername,hour,minute,second,response_text,flags,scriptname)
   local eEnabled,eAtTime,eOneShot,eTimerSpeedWalk,eTimerNote,eActiveWhenClosed,eReplace,eTemporary
   local flagtypes = { "eEnabled","eAtTime","eOneShot","eTimerSpeedWalk","eTimerNote","eActiveWhenClosed","eReplace","eTemporary" }
   local script,result,time = ""
   for i,flag in ipairs(flagtypes) do
    if flags < 2^(i-1) or math.fmod(flags,2^i) ~= 2^(i-1) then result = "false" else result = "true" end
    assert(loadstring(flag .. " = " .. result))()
    if assert(loadstring("return " .. flag))() then flags = flags - 2^(i-1) end
   end
   if eReplace then killTimer(timername) end

   if eAtTime then
    hour = math.max(hour - tonumber(getTime(true,'h')),0)
    minute = math.max(minute - tonumber(getTime(true,'m')),0)
    second = math.max(second - ( tonumber(getTime(true,'s')) + ( tonumber(getTime(true,'z')) * 0.001 ) ),0)
   end
   time = (hour * 3600) + (minute * 60) + second

   if response_text then script = script .. "send(" .. response_text .. ") " end

   if scriptname then script = script .. scriptname end

   if eOneShot then script = script .. " killTimer(" .. timername .. ")"
   elseif eAtTime then script = script .. " killTimer(" .. timername .. ") permTimer(" .. timername .. ",nil,86400," .. script .. ")"
   end

   permTimer(timername,nil,time,script)

   if not eEnabled then disableTimer(timername) end
  end,
 DoAfter = function (time,command) tempTimer(time,function () send(command) end) end,
 DoAfterNote = function (time,message) tempTimer(time,function () echo(message .. "\n") end) end,
 sendto = {
  world = 0,
  command = 1,
  output = 2,
  status = 3,
  notepad = 4,
  notepadappend = 5,
  logfile = 6,
  notepadreplace = 7,
  commandqueue = 8,
  variable = 9,
  execute = 10,
  speedwalk = 11,
  script = 12,
  immediate = 13,
  scriptafteromit = 14
 },
 DoAfterSpecial = function (time,thing,where)
  local functable = {
   ["0"] = send,
   ["2"] = function (stuff) echo(stuff .. "\n") end,
   ["10"] = expandAlias,
   ["12"] = function (stuff) assert(loadstring(stuff))() end,
   ["14"] = functable["12"]
  }
  tempTimer(time,function () functable[tostring(where)](thing) end)
 end,
 EnableAlias = function (aliasname,tf) if tf then enableAlias(aliasname) else disableAlias(aliasname) end end,
 OpenBrowser = openUrl,
 Hyperlink = function (action, text, hint, fgcolor, bgcolor,url)
  --Note - no support for text formating as of right now
  if url then action = [[openUrl(]] .. action .. [[)]]
  else action = [[expandAlias(]] .. action .. [[)]]
  end
  echoLink(text,action,hint)
 end,
 string = string,
 table = table,
 pairs = pairs,
 ipairs = ipairs
}
setfenv(1,mushenv)

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.