RestrictedEnvironment.lua

RestrictedEnvironment.lua (9.1.0.39617; unchanged since 9.0.1.36230)
  1. -- RestrictedEnvironment.lua (Part of the new Secure Headers implementation)
  2. --
  3. -- This file defines the environment available to restricted code. The
  4. -- 'base' API functions (everything that's safe to use from the core
  5. -- WoW lua API), and functions which provide the same degree of game state
  6. -- as macro conditionals.
  7. --
  8. -- Nevin Flanagan
  9. -- Daniel Stephens
  10. ---------------------------------------------------------------------------
  11. local tostring = tostring;
  12. local GetFrameHandleFrame = GetFrameHandleFrame;
  13. local IsGamePadEnabled = C_GamePad.IsEnabled;
  14. local GetGamePadState = C_GamePad.GetDeviceMappedState;
  15. -- The bare minimum functions that should exist in order to be
  16. -- useful without being ridiculously restrictive.
  17. RESTRICTED_FUNCTIONS_SCOPE = {
  18. math = math;
  19. string = string;
  20. -- table is provided elsewhere, as direct tables are not allowed
  21. select = select;
  22. tonumber = tonumber;
  23. tostring = tostring;
  24. print = print;
  25. -- String methods
  26. format = format;
  27. gmatch = gmatch;
  28. gsub = gsub; -- Restricted table aware rtgsub is added later
  29. strbyte = strbyte;
  30. strchar = strchar;
  31. strconcat = strconcat;
  32. strfind = strfind;
  33. strjoin = strjoin;
  34. strlen = strlen;
  35. strlower = strlower;
  36. strmatch = strmatch;
  37. strrep = strrep;
  38. strrev = strrev;
  39. strsplit = strsplit;
  40. strsub = strsub;
  41. strtrim = strtrim;
  42. strupper = strupper;
  43. -- Math functions
  44. abs = abs;
  45. acos = acos;
  46. asin = asin;
  47. atan = atan;
  48. atan2 = atan2;
  49. ceil = ceil;
  50. cos = cos;
  51. deg = deg;
  52. exp = exp;
  53. floor = floor;
  54. frexp = frexp;
  55. ldexp = ldexp;
  56. log = log;
  57. log10 = log10;
  58. max = max;
  59. min = min;
  60. mod = mod;
  61. rad = rad;
  62. random = random;
  63. sin = sin;
  64. tan = tan;
  65. };
  66. -- Initialize directly available functions so they can be copied into the
  67. -- table
  68. local DIRECT_MACRO_CONDITIONAL_NAMES = {
  69. "SecureCmdOptionParse",
  70. "GetShapeshiftForm", "IsStealthed",
  71. "UnitExists", "UnitIsDead", "UnitIsGhost",
  72. "UnitPlayerOrPetInParty", "UnitPlayerOrPetInRaid",
  73. "IsRightAltKeyDown", "IsLeftAltKeyDown", "IsAltKeyDown",
  74. "IsRightControlKeyDown", "IsLeftControlKeyDown", "IsControlKeyDown",
  75. "IsLeftShiftKeyDown", "IsRightShiftKeyDown", "IsShiftKeyDown",
  76. "IsModifierKeyDown", "IsModifiedClick",
  77. "GetMouseButtonClicked", "GetActionBarPage", "GetBonusBarOffset",
  78. "IsMounted", "IsSwimming", "IsSubmerged", "IsFlying", "IsFlyableArea",
  79. "IsIndoors", "IsOutdoors",
  80. "HasVehicleActionBar", "HasOverrideActionBar", "HasTempShapeshiftActionBar",
  81. "HasBonusActionBar", "GetBonusBarIndex", "GetVehicleBarIndex", "GetOverrideBarIndex",
  82. "HasExtraActionBar", "GetTempShapeshiftBarIndex", "CanExitVehicle"
  83. };
  84. local OTHER_SAFE_FUNCTION_NAMES = {
  85. "GetBindingKey", "HasAction",
  86. "IsHarmfulSpell", "IsHarmfulItem", "IsHelpfulSpell", "IsHelpfulItem",
  87. "GetMultiCastTotemSpells", "FindSpellBookSlotBySpellID", "UnitTargetsVehicleInRaidUI"
  88. };
  89. -- Copy the direct functions into the table
  90. for _, name in ipairs( DIRECT_MACRO_CONDITIONAL_NAMES ) do
  91. RESTRICTED_FUNCTIONS_SCOPE[name] = _G[name];
  92. end
  93. -- Copy the other safe functions into the table
  94. for _, name in ipairs( OTHER_SAFE_FUNCTION_NAMES ) do
  95. RESTRICTED_FUNCTIONS_SCOPE[name] = _G[name];
  96. end
  97. -- Now create the remainder (ENV is just an alias for brevity)
  98. local ENV = RESTRICTED_FUNCTIONS_SCOPE;
  99. function ENV.PlayerCanAttack( unit )
  100. return UnitCanAttack( "player", unit )
  101. end
  102. function ENV.PlayerCanAssist( unit )
  103. return UnitCanAssist( "player", unit )
  104. end
  105. function ENV.PlayerIsChanneling()
  106. return (UnitChannelInfo( "player" ) ~= nil)
  107. end
  108. function ENV.PlayerPetSummary()
  109. return UnitCreatureFamily( "pet" ), (UnitName( "pet" ))
  110. end
  111. function ENV.PlayerInCombat()
  112. return UnitAffectingCombat( "player" ) or UnitAffectingCombat( "pet" )
  113. end
  114. function ENV.PlayerInGroup()
  115. return ( IsInRaid() and "raid" )
  116. or ( IsInGroup() and "party" )
  117. end
  118. function ENV.UnitHasVehicleUI(unit)
  119. unit = tostring(unit);
  120. return UnitHasVehicleUI(unit) and
  121. (UnitCanAssist("player", unit:gsub("(%D+)(%d*)", "%1pet%2")) and true) or
  122. (UnitCanAssist("player", unit) and false);
  123. end
  124. function ENV.RegisterStateDriver(frameHandle, ...)
  125. return RegisterStateDriver(GetFrameHandleFrame(frameHandle), ...);
  126. end
  127. function ENV.UnregisterStateDriver(frameHandle, ...)
  128. return UnregisterStateDriver(GetFrameHandleFrame(frameHandle), ...);
  129. end
  130. function ENV.RegisterAttributeDriver(frameHandle, ...)
  131. return RegisterAttributeDriver(GetFrameHandleFrame(frameHandle), ...);
  132. end
  133. function ENV.UnregisterAttributeDriver(frameHandle, ...)
  134. return UnregisterAttributeDriver(GetFrameHandleFrame(frameHandle), ...);
  135. end
  136. function ENV.RegisterUnitWatch(frameHandle, ...)
  137. return RegisterUnitWatch(GetFrameHandleFrame(frameHandle), ...);
  138. end
  139. function ENV.UnregisterUnitWatch(frameHandle, ...)
  140. return UnregisterUnitWatch(GetFrameHandleFrame(frameHandle), ...);
  141. end
  142. function ENV.UnitWatchRegistered(frameHandle, ...)
  143. return UnitWatchRegistered(GetFrameHandleFrame(frameHandle), ...);
  144. end
  145. local safeActionTypes = {["spell"] = true, ["companion"] = true, ["item"] = true, ["macro"] = true, ["flyout"] = true}
  146. local function scrubActionInfo(actionType, ...)
  147. if ( safeActionTypes[actionType]) then
  148. return actionType, ...
  149. else
  150. return actionType
  151. end
  152. end
  153. function ENV.GetActionInfo(...)
  154. return scrubActionInfo(GetActionInfo(...));
  155. end
  156. function ENV.IsGamePadEnabled()
  157. return IsGamePadEnabled();
  158. end
  159. function ENV.GetGamePadState()
  160. return GetGamePadState();
  161. end
  162. ENV = nil;