mudgangster

Log | Files | Refs

commit c36561bf6995c945960318640ac733caa0cfbbe2
parent bb745abb48f995f607edcc8a0d9a5d071bfcd671
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri, 17 Oct 2014 09:29:03 +0100

Add mud.intercept

Diffstat:
handlers.lua | 3+++
intercept.lua | 43+++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/handlers.lua b/handlers.lua @@ -1,5 +1,6 @@ local action = require( "action" ) local alias = require( "alias" ) +local intercept = require( "intercept" ) local gag = require( "gag" ) local macro = require( "macro" ) local sub = require( "sub" ) @@ -216,6 +217,8 @@ local function handleCommand( input, hide ) return end + intercept.doIntercept( input ) + if not hide and input ~= "" then local toShow = ( showInput and input or ( "*" ):rep( input:len() ) ) .. "\n" diff --git a/intercept.lua b/intercept.lua @@ -0,0 +1,43 @@ +local Intercepts = { } + +local function doIntercept( command ) + for i = 1, #Intercepts do + local intercept = Intercepts[ i ] + + if intercept.enabled then + local ok, err = pcall( string.gsub, command, intercept.pattern, intercept.callback ) + + if not ok then + mud.print( debug.traceback( "\n#s> intercept callback failed: %s" % err ) ) + end + end + end +end + +function mud.intercept( pattern, callback, disabled ) + enforce( pattern, "pattern", "string" ) + enforce( callback, "callback", "function" ) + enforce( disabled, "disabled", "boolean", "nil" ) + + local intercept = { + pattern = pattern, + callback = callback, + + enabled = not disabled, + + enable = function( self ) + self.enabled = true + end, + disable = function( self ) + self.enabled = false + end, + } + + table.insert( Intercepts, intercept ) + + return intercept +end + +return { + doIntercept = doIntercept, +}