WoWInterface SVN ccUnspammer

[/] [trunk/] [ccUnspammer.lua] - Rev 2

Compare with Previous | Blame | View Log

CCU = {
        defaults                = {
                cache_maxAge            = 120,
                cache_purgeTime = 180,
                chat_unignore           = {}
        },
        
        settings                = {
        },
        
        handlers                = {
                onLoad  = function() 
                        CCU.settings = CCU.copyTable(CCU.defaults)

                        CCU.handlers.Original_ChatFrame_MessageEventHandler = ChatFrame_MessageEventHandler
                        ChatFrame_MessageEventHandler = CCU.handlers.ChatFrame_MessageEventHandler
                        
                        SLASH_CCUNSPAMMER1, SLASH_CCUNSPAMMER2, SLASH_CCUNSPAMMER3, SLASH_CCUNSPAMMER4, SLASH_CCUNSPAMMER5 = '/ccus', '/unspam', '/ccunspammer', '/ccu';
                        SlashCmdList['CCUNSPAMMER'] = CCU.handlers.slashCommand
                        
                        CCU.frame:UnregisterEvent('PLAYER_ENTERING_WORLD')
                end,
                
                slashCommand = function(msg, editbox)
                        local command, rest = msg:match("^(%S*)%s*(.-)$");
                        
                        command = strlower(command)
                        if command == "set" and rest ~= "" then
                                local setting, value = rest:match("^(%S*)%s*(.-)$")
                                if value == '' or not CCU.inList(setting, {'cache_maxAge','cache_purgeTime', 'chat_unignore'}) then
                                        print("Syntax: /ccu set setting [value] value="..value);
                                elseif setting == 'chat_unignore' then
                                        table.insert(CCU.settings.chat_unignore, value)
                                else
                                        CCU.settings[setting] = value
                                end;
                        elseif command == "reset" then
                                if rest ~= '' then
                                        -- reset only a specific one
                                        if CCU.inList(setting, {'cache_maxAge','cache_purgeTime', 'chat_unignore'}) then
                                                print('ccUnspammer: unknown setting provided')
                                        else
                                                CCU.settings[rest] = CCU.defaults[rest]
                                        end;
                                else
                                        CCU.settings = CCU.copyTable(CCU.defaults)
                                end
                        elseif command == 'unignore' and rest ~= '' then
                                table.insert(CCU.settings.chat_unignore, rest);
                                print('ccUnspammer: showing all messages for '..rest);
                        elseif command == 'list' then
                                if rest ~= '' then
                                        print('CCU: ['..rest..'] = '..CCU.concatTable(CCU.settings[rest]))
                                else
                                        print('CCU: listing settings');
                                        for k,v in pairs(CCU.settings) do
                                                if type(v) == 'table' then
                                                        v = '['..CCU.concatTable(v)..']'
                                                end;
                                                print('  ['..k..'] = '..v)
                                        end;
                                end
                        else
                                -- |cffffff00 = yellow
                                -- |cff00ff00 = green
                                print("|cff00ff00cc_Unspammer|r v2.0.1");
                                print("Syntax: |cff00ff00/ccu|r");
                                print("  |cffffff00set|r    |cff00ff00foo bar|r - sets setting |cff00ff00foo|r to value |cff00ff00bar|r");
                                print("  |cffffff00reset|r [|cff00ff00foo|r]    - resets setting |cff00ff00foo|r, if none provided does a full reset of all settings");
                                print("  |cffffff00list|r  [|cff00ff00foo|r]    - shows the value for setting |cff00ff00foo|r, if none provided shows values for all settings");
                        end;
                end,

                ChatFrame_MessageEventHandler = function(self, event, ...)
                        if event == 'CHAT_MSG_CHANNEL' then
                                if not CCU.cache[self:GetName()] then
                                        CCU.cache[self:GetName()] = {}
                                end

                                -- do some preprocessing
                                local timeCheck = (time() - CCU.settings.cache_maxAge)
                                local msg, author, _, channel = ...
                                if not CCU.inList(author, CCU.settings.chat_unignore) then
                                        if CCU.cache[self:GetName()][author] then
                                                -- search the list to see if the message has been posted < 2 min ago
                                                for k,v in pairs(CCU.cache[self:GetName()][author]) do
                                                        if v['msg'] == msg and v['channel'] == channel and v['time'] > timeCheck then
                                                                -- spam, let's skip it
                                                                return
                                                        end
                                                end
                                        else
                                                CCU.cache[self:GetName()][author] = {}
                                        end

                                        -- cache the entry so we won't miss it next time
                                        table.insert(CCU.cache[self:GetName()][author], 
                                                {
                                                        msg=msg,
                                                        time=time(),
                                                        channel=channel
                                                }
                                        )
                                end
                        end

                        CCU.handlers.Original_ChatFrame_MessageEventHandler(self, event, ...)
                end
        },
        
        inList = function(needle, haystack)
                if haystack == nil then
                        return false
                end
                
           for k,v in pairs(haystack) do
              if v == needle then
                 return true
              end;
           end;
           return false;
        end,
        
        copyTable = function(t,deep)
          if deep == nil then
             deep = true
          end;
          local t2 = {}
          for k,v in pairs(t) do
                 if type(v) == 'table' and deep then
                        v = CCU.copyTable(v,true)
                 end;
                 t2[k] = v
          end
          return t2
        end,
        
        concatTable = function(t)
                if type(t) ~= 'table' then
                        return t
                end;
                
                rStr = ''
                for k,v in pairs(t) do
                   if type(v) == 'table' then
                      rStr = rStr..'['..CCU.concatTable(v)..'], '
                   else
                      rStr = rStr..v..', '
                   end
                end
                return rStr
        end,

        purge   = function()
                local timeCheck = (time() - CCU.settings.cache_maxAge)
                local cleanCache = {}
                for frame,chatList in pairs(CCU.cache) do
                        for author,chat in pairs(chatList) do
                                local tempCache = {}
                                local i = 0
                                for k,v in pairs(chat) do
                                        if v['time'] > timeCheck then
                                                table.insert(tempCache, v)
                                                i = i+1
                                        end
                                end
                                if i > 0 then
                                        cleanCache[author] = tempCache
                                end
                        end
                        CCU.cache[frame] = cleanCache
                end
                
                CCU.purged = 0
        end,
        
        cache           = {},
        purged  = 0,
        frame           = CreateFrame('Frame'),
}

CCU.frame:RegisterEvent('PLAYER_ENTERING_WORLD')
CCU.frame:SetScript('OnEvent', function(self, event, ...) CCU.handlers.onLoad() end)
CCU.frame:SetScript('OnUpdate', function(self,elapsed)
        if CCU.purged + elapsed > CCU.settings.cache_purgeTime then
                CCU.purge()
        end
        CCU.purged = CCU.purged + elapsed
end)

Compare with Previous | Blame