intercept.lua (874B)
1 local Intercepts = { } 2 3 local function doIntercept( command ) 4 for i = 1, #Intercepts do 5 local intercept = Intercepts[ i ] 6 7 if intercept.enabled then 8 local ok, err = pcall( string.gsub, command, intercept.pattern, intercept.callback ) 9 10 if not ok then 11 mud.print( debug.traceback( "\n#s> intercept callback failed: %s" % err ) ) 12 end 13 end 14 end 15 end 16 17 function mud.intercept( pattern, callback, disabled ) 18 enforce( pattern, "pattern", "string" ) 19 enforce( callback, "callback", "function" ) 20 enforce( disabled, "disabled", "boolean", "nil" ) 21 22 local intercept = { 23 pattern = pattern, 24 callback = callback, 25 26 enabled = not disabled, 27 28 enable = function( self ) 29 self.enabled = true 30 end, 31 disable = function( self ) 32 self.enabled = false 33 end, 34 } 35 36 table.insert( Intercepts, intercept ) 37 38 return intercept 39 end 40 41 return { 42 doIntercept = doIntercept, 43 }