Orukxu / SpamThrottle

Vanilla WoW addon to remove unwanted chat messages
GNU General Public License v3.0
19 stars 13 forks source link

Enhancement: Delaying the hook of ChatFrame_OnEvent to hook after other addons to be the "master" #1

Closed Logonz closed 8 years ago

Logonz commented 8 years ago

Here is a not tested code snippet you can check out

--============================
--= This replaces the default ChatFrame handler with our own.
--= Implementation could conflict with other chat handling programs, but the API here is really old,
--= and was from before Chat Filters were implemented.
--============================

local OldChatFrame_OnEvent;

local SpamThrottle_ChatFrame_OnEvent;

local UpdateFrame = CreateFrame("Frame", nil);
UpdateFrame:SetScript("OnUpdate",OverHookEvents);
UpdateFrame:Show();
local StartTime = time();
local Initialized;
function OverHookEvents()
   if(time() - StartTime > 10 and Initialized == nil) then
      OldChatFrame_OnEvent = ChatFrame_OnEvent;
      ChatFrame_OnEvent = SpamThrottle_ChatFrame_OnEvent;
      StartTime = nil;
      UpdateFrame:SetScript("OnUpdate", nil);
      UpdateFrame = nil;
      Initialized = true;
   end
end
--ChatFrame_OnEvent
function SpamThrottle_ChatFrame_OnEvent(event)
-- arg1 is the actual message
-- arg2 is the player name
-- arg4 is the composite channel name (e.g. "3. global")
-- arg8 is the channel number (e.g. "3")
-- arg9 is the channel name (e.g. "global")

   local hideColor = "|cFF5C5C5C";
   local oppFacColor = "|cA0A00000";
   local theColor = hideColor;

   if SpamThrottle_Config == nil then SpamThrottle_init(); end

   if not SpamThrottle_Config.STActive then
      OldChatFrame_OnEvent(event);
      return;
   end;

   if (SpamThrottle_Config.STCtrlMsgs) then -- Remove the left/joined channel spam and a few other notification messages
      if (event == "CHAT_MSG_CHANNEL_JOIN" or event == "CHAT_MSG_CHANNEL_LEAVE" or event == "CHAT_MSG_CHANNEL_NOTICE" or event == "CHAT_MSG_CHANNEL_NOTICE_USER") then      
         return;
      end
   end

   if arg2 then -- if this is not a server message
      if (event == "CHAT_MSG_CHANNEL" or (event == "CHAT_MSG_YELL" and SpamThrottle_Config.STYellMsgs) or (event == "CHAT_MSG_SAY" and SpamThrottle_Config.STSayMsgs) or (event == "CHAT_MSG_WHISPER" and SpamThrottle_Config.STWispMsgs)) then

         -- Code to handle message goes here. Just return if we are going to ignore it.

         if arg1 and arg2 then   -- only execute this code once although event handler is called many times per message
            local NormalizedMessage = SpamThrottle_strNorm(arg1, arg2);
            if time() == MessageLatestTime[NormalizedMessage] then return end;
         end

         local BlockType = SpamThrottle_ShouldBlock(arg1,arg2,event,arg9);
         SpamThrottle_RecordMessage(arg1,arg2);

         if SpamThrottle_Config.STWispBack and event == "CHAT_MSG_WHISPER" and not SpamThrottle_Config.STReverse then
            if BlockType == 1 or BlockType == 2 then
               SendChatMessage(SpamThrottleChatMsg.WhisperBack, "WHISPER", nil, arg2);
            end
         end

         if BlockType == 2 then
            return;
         end

         if BlockType == 3 then
            theColor = oppFacColor;
         end

         if BlockType == 1 or BlockType == 3 then
            local CleanText = "";
            CleanText = string.gsub(arg1,"|c%x%x%x%x%x%x%x%x", "");
            CleanText = string.gsub(CleanText,"|r", "");
            CleanText = string.gsub(CleanText,"|H.-|h", "");
            CleanText = string.gsub(CleanText,"|h", "");

            if event == "CHAT_MSG_YELL" then
               CleanText = theColor .. "[" .. arg2 .. "] yells: " .. CleanText .. "|r";
            else
               if event == "CHAT_MSG_SAY" then
                  CleanText = theColor .. "[" .. arg2 .. "] says: " .. CleanText .. "|r";
               else
                  if event == "CHAT_MSG_WHISPER" then
                     CleanText = theColor .. "[" .. arg2 .. "] whispers: " .. CleanText .. "|r";
                  else
                     CleanText = theColor .. "[" .. arg4 .. "] [" .. arg2 .. "]: " .. CleanText .. "|r";
                  end
               end
            end

            DEFAULT_CHAT_FRAME:AddMessage(CleanText);
            return;
         end
      end
   end

   local theStatusValue = string.format("%7d",table.length(MessageList));
   SpamThrottleStatusValue5:SetText(theStatusValue);

   theStatusValue = string.format("%7d",FilteredCount);
   SpamThrottleStatusValue6:SetText(theStatusValue);

   OldChatFrame_OnEvent(event);
end
Orukxu commented 8 years ago

Thanks Logonz, it took a while, but I finally implemented and tested an overhook. So now SpamThrottle waits 10 seconds using an update frame. Hopefully this makes it more compatible with Prat; I've been testing it with Prat and haven't found any problems so far, but I'm not a Prat expert.