WoW:Hooking outgoing chat messages: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
No edit summary
(The previous code was very outdated and caused tainting)
Line 1: Line 1:
This HOWTO uses the [[Sea]] Cosmos Library, please be sure you have it installed and listed as a dependency for any addon using this HOWTO.
To modify outgoing chat messages we are going to hook the ChatEdit_ParseText function. However, this function also handles macros; so to avoid tainting we must use [[API_hooksecurefunc|hooksecurefunc]]() to hook the function properly. Otherwise, your macros will no longer work.


To modify outgoing chat messages we are going to hook the ChatEdit_ParseText function.
  -- Place this code in the OnLoad function of your addon
  hooksecurefunc('ChatEdit_ParseText',MyAddon_ParseText);


You probably want to do this in an OnLoad function. Sea screws up the normal function hooking process, so you need to do it differently depending on whether Sea is installed or not.
MyAddon_ParseText will now be called after ChatEdit_ParseText has had a chance to handle macros and slash commands.
  -- Make these variable global to the whole lua file
  local oldChatEdit_ParseText;
  local seaPresent = (Sea ~= nil);


   -- Place this code in the OnLoad function
   function MyAddon_ParseText(chatEntry, send)
  if (seaPresent) then
      Sea.util.hook("ChatEdit_ParseText","My_ParseText","after");
  else
      oldChatEdit_ParseText = ChatEdit_ParseText;
      ChatEdit_ParseText = My_ParseText;
  end;
 
My_ParseTest is going to get called now instead of ChatEdit_ParseText. It should look something like:
 
  function My_ParseText(chatEntry, send)
      -- if Sea is not installed, call the original function first. If Sea
      -- is installed it does this step for you. The original function will
      -- process any slash commands for you, so all that's left is chat messages
      if (~seaPresent) then
        oldChatEdit_ParseText(chatEntry, send);
      end
      --
       -- This function actually gets called every time the user hits a key. But the
       -- This function actually gets called every time the user hits a key. But the
       -- send flag will only be set when he hits return to send the message.
       -- send flag will only be set when he hits return to send the message.

Revision as of 07:11, 29 June 2008

To modify outgoing chat messages we are going to hook the ChatEdit_ParseText function. However, this function also handles macros; so to avoid tainting we must use hooksecurefunc() to hook the function properly. Otherwise, your macros will no longer work.

  -- Place this code in the OnLoad function of your addon
  hooksecurefunc('ChatEdit_ParseText',MyAddon_ParseText);

MyAddon_ParseText will now be called after ChatEdit_ParseText has had a chance to handle macros and slash commands.

  function MyAddon_ParseText(chatEntry, send)
     -- This function actually gets called every time the user hits a key. But the
     -- send flag will only be set when he hits return to send the message.
     if (send == 1) then
        local text = chatEntry:GetText(); -- Here's how you get the original text
        local newText = text;             -- here's where you can modify the text to your liking
        chatEntry:SetText( newText );     -- send the new text back to the UI
     end
  end