action.lua (2214B)
1 local Actions = { } 2 local PreActions = { } 3 local AnsiActions = { } 4 local AnsiPreActions = { } 5 6 local ChatActions = { } 7 local ChatPreActions = { } 8 local ChatAnsiActions = { } 9 local ChatAnsiPreActions = { } 10 11 local doActions 12 local doPreActions 13 local doAnsiActions 14 local doAnsiPreActions 15 16 local doChatActions 17 local doChatPreActions 18 local doChatAnsiActions 19 local doChatAnsiPreActions 20 21 local function genericActions( actions ) 22 return 23 function( pattern, callback, disabled ) 24 enforce( pattern, "pattern", "string" ) 25 enforce( callback, "callback", "function", "string" ) 26 27 if type( callback ) == "string" then 28 local command = callback 29 30 callback = function() 31 mud.input( command, true ) 32 end 33 end 34 35 local action = { 36 pattern = pattern, 37 callback = callback, 38 39 enabled = not disabled, 40 41 enable = function( self ) 42 self.enabled = true 43 end, 44 disable = function( self ) 45 self.enabled = false 46 end, 47 } 48 49 table.insert( actions, action ) 50 51 return action 52 end, 53 54 function( line ) 55 for i = 1, #actions do 56 local action = actions[ i ] 57 58 if action.enabled then 59 local ok, err = pcall( string.gsub, line, action.pattern, action.callback ) 60 61 if not ok then 62 mud.print( debug.traceback( "\n#s> action callback failed: %s" % err ) ) 63 end 64 end 65 end 66 end 67 end 68 69 mud.action, doActions = genericActions( Actions ) 70 mud.preAction, doPreActions = genericActions( PreActions ) 71 mud.ansiAction, doAnsiActions = genericActions( AnsiActions ) 72 mud.ansiPreAction, doAnsiPreActions = genericActions( AnsiPreActions ) 73 74 mud.chatAction, doChatActions = genericActions( ChatActions ) 75 mud.preChatAction, doChatPreActions = genericActions( ChatPreActions ) 76 mud.ansiChatAction, doChatAnsiActions = genericActions( ChatAnsiActions ) 77 mud.ansiPreChatAction, doChatAnsiPreActions = genericActions( ChatAnsiPreActions ) 78 79 return { 80 doActions = doActions, 81 doPreActions = doPreActions, 82 doAnsiActions = doAnsiActions, 83 doAnsiPreActions = doAnsiPreActions, 84 85 doChatActions = doChatActions, 86 doChatPreActions = doChatPreActions, 87 doChatAnsiActions = doChatAnsiActions, 88 doChatAnsiPreActions = doChatAnsiPreActions, 89 }