Changeset 548

Show
Ignore:
Timestamp:
05/04/06 11:44:39 (3 years ago)
Author:
oxff
Message:

nepenthes-experimental.log-irc: added dynamic log patterns, adjustable via irc, added !help command, added !pattern command, moved setting of connection command

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nepenthes/branches/nepenthes-experimental/modules/log-irc/IrcDialogue.cpp

    r547 r548  
    3131#include <ctype.h> 
    3232#include <string> 
     33#include <string.h> 
    3334#include <vector> 
    3435 
     
    262263                responseMessage += target; 
    263264                 
    264         if(string(message) == "!version"
     265        if(!strcmp(message, "!version")
    265266        { 
    266267                responseMessage += " :nepenthes v" VERSION " compiled on [" __DATE__ " " __TIME__ "] with " MY_COMPILER " " __VERSION__ "\r\n"; 
    267268                m_Socket->doRespond((char *) responseMessage.data(), responseMessage.size()); 
    268269        } 
    269          
    270 
    271  
    272  
    273 // TODO: add on-connect cmd to auth at nickserv 
     270        else if(!strncmp(message, "!pattern ", 9)) 
     271        { 
     272                m_LogIrc->setLogPattern(message + 9); 
     273                 
     274                responseMessage += " :Updated log pattern to \"" + string(message + 9) + "\"\r\n"; 
     275                m_Socket->doRespond((char *) responseMessage.data(), responseMessage.size()); 
     276        } 
     277        else if(!strcmp(message, "!help") && m_NickName == target) 
     278        { 
     279                static const char * helpLines[] = 
     280                { 
     281                        " :nepenthes v" VERSION " log-irc control interface\r\n", 
     282                        " :  !version - print detailed version information\r\n", 
     283                        " :  !pattern <logtags> - set log pattern for this log-irc instance\r\n", 
     284                        " :  !help - display this help message\r\n", 
     285                        " : \r\n", 
     286                        " : <http://nepenthes.mwcollect.org/>\r\n", 
     287                }; 
     288                 
     289                for(uint32_t i = 0; i < sizeof(helpLines) / sizeof(char *); ++i) 
     290                { 
     291                        string responseLine = responseMessage + helpLines[i];                    
     292                        m_Socket->doRespond((char *) responseLine.data(), responseLine.size()); 
     293                } 
     294        } 
     295
     296 
    274297 
    275298void IrcDialogue::loggedOn() 
    276299{ 
     300        m_LogIrc->setDialogue(this); 
     301         
    277302        if(m_LoggedOn) 
    278303                return; 
    279                 
    280         try 
    281         { 
    282                string connectCommand = m_LogIrc->getConnectCommand(); 
     304         
     305        string connectCommand = m_LogIrc->getConnectCommand(); 
     306         
     307        if(!connectCommand.empty()) 
    283308                m_Socket->doRespond((char *) connectCommand.data(), connectCommand.size()); 
    284         } 
    285         catch(...) 
    286         { // no connect command was specified 
    287         } 
    288309 
    289310        string joinCommand = "JOIN " + m_LogIrc->getIrcChannel() + " " + m_LogIrc->getIrcChannelPass() + "\r\n"; 
     
    334355 
    335356 
    336 // TODO: format, translate 
    337  
    338357struct FlagMapping 
    339358{ 
    340         int32_t       m_LogFlag; 
     359        int32_t       m_LogFlag; 
    341360        char    *m_ColorFlag; 
    342361}; 
     
    346365        { 
    347366                l_crit, 
    348                 "\x03\x34" // helles rot 
     367                "\x03\x34" // light red 
    349368        }, 
    350369        { 
    351370                l_warn, 
    352                 "\x03\x36"     // lila 
     371                "\x03\x36" // purple 
    353372        }, 
    354373        { 
    355374                l_debug, 
    356                 "\x03\x31\x33" // pink 
     375                "\x03\x31\x33" // pink 
    357376        }, 
    358377        { 
    359378                l_info, 
    360                 "\x03\x39" // helles gruen  
     379                "\x03\x39" // light green 
    361380        }, 
    362    
     381       
    363382                l_spam, 
    364                 "\x03\x31\x34" // dunkel grau 
    365         } 
     383                "\x03\x31\x34" // dark grey 
     384        }, 
    366385}; 
    367386 
    368387 
    369 // TODO: parse log tags from configuration 
    370  
     388// oxff: made dynamic log tag pattern 
    371389void IrcDialogue::logIrc(uint32_t mask, const char *message) 
    372390{ 
    373         if ( 
    374                 ((mask & l_dl || mask & l_sub) && mask & l_mgr && !(mask & l_spam)  ) || 
    375                 (mask & l_warn || mask & l_crit)  
    376                 ) 
     391        if(m_LogIrc->logMaskMatches(mask)) 
    377392        { 
    378393                if (strlen(message) > 450) 
    379394                        return; 
    380                 string line ="PRIVMSG "; 
    381                 line += m_LogIrc->getIrcChannel(); 
    382                 line += " :"; 
    383  
     395                         
     396                string line ="PRIVMSG " + m_LogIrc->getIrcChannel() + " :"; 
    384397                uint32_t i=0; 
    385398 
    386                 for (i=0;i<sizeof(colors)/sizeof(struct FlagMapping);i++
    387                 { 
    388                         if (mask & colors[i].m_LogFlag) 
     399                for (i=0; i < sizeof(colors) / sizeof(struct FlagMapping); ++i
     400                { 
     401                        if(mask & colors[i].m_LogFlag) 
    389402                        { 
    390403                                line += colors[i].m_ColorFlag; 
     404                                break; 
    391405                        } 
    392406                } 
     
    394408                line += message; 
    395409 
    396                 m_Socket->doRespond((char *)line.c_str(), line.size()); 
     410                m_Socket->doRespond((char *) line.data(), line.size()); 
    397411        } 
    398412}        
  • nepenthes/branches/nepenthes-experimental/modules/log-irc/log-irc.conf.dist

    r547 r548  
    3333        // has to be one IRC command NOT terminated with \r\n 
    3434        // connect-command = "PRIVMSG NickServ :IDENTIFY mypassword"; 
     35         
     36        // set the initial log tag pattern, like on cmdline; defaults sane 
     37        // pattern = "crit,warn,down"; 
    3538    }; 
    3639}; 
  • nepenthes/branches/nepenthes-experimental/modules/log-irc/log-irc.cpp

    r547 r548  
    140140                        m_IrcChannelPass= m_Config->getValString("log-irc.irc.channel.pass"); 
    141141 
    142                 } catch ( ... ) 
     142                } 
     143                catch ( ... ) 
    143144                { 
    144145                        logCrit("Error setting needed vars, check your config\n"); 
    145146                        return false; 
    146147                } 
     148                 
     149                try 
     150                { 
     151                        setLogPattern(m_Config->getValString("log-irc.tag-pattern")); 
     152                } 
     153                catch (...) 
     154                { 
     155                        m_LogPatternNumeric = 0; 
     156                } 
     157                 
     158                try 
     159                { 
     160                        m_ConnectCommand = string(m_Config->getValString("log-irc.irc.connect-command")) + "\r\n"; 
     161                } 
     162                catch (...) 
     163                { 
     164                } 
     165                 
    147166                m_State = LIRC_NULL; 
    148167                doStart(); 
     
    287306{ 
    288307        if (m_IrcDialogue != NULL) 
    289         { 
    290308                m_IrcDialogue->logIrc(mask, message); 
     309} 
     310 
     311string LogIrc::getTorServer() 
     312{ 
     313        return m_TorServer; 
     314} 
     315 
     316string LogIrc::getIrcServer() 
     317{ 
     318        return m_IrcServer; 
     319} 
     320 
     321uint32_t LogIrc::getIrcIP() 
     322{ 
     323        return m_IrcIP; 
     324} 
     325 
     326uint16_t LogIrc::getIrcPort() 
     327{ 
     328        return m_IrcPort; 
     329} 
     330 
     331string LogIrc::getIrcPass() 
     332{ 
     333        return m_IrcPass; 
     334} 
     335 
     336string LogIrc::getIrcNick() 
     337{ 
     338        return m_IrcNick; 
     339} 
     340 
     341string LogIrc::getIrcIdent() 
     342{ 
     343        return m_IrcIdent; 
     344} 
     345 
     346string LogIrc::getIrcUserInfo() 
     347{ 
     348        return m_IrcUserInfo; 
     349} 
     350 
     351string LogIrc::getIrcChannel() 
     352{ 
     353        return m_IrcChannel; 
     354} 
     355 
     356string LogIrc::getIrcChannelPass() 
     357{ 
     358        return m_IrcChannelPass; 
     359} 
     360 
     361string LogIrc::getIrcUserModes() 
     362{ 
     363        return m_IrcUserModes; 
     364} 
     365 
     366string LogIrc::getConnectCommand() 
     367{ 
     368        return m_ConnectCommand; 
     369} 
     370 
     371 
     372bool LogIrc::logMaskMatches(uint32_t mask) 
     373{ 
     374        if(!m_LogPatternNumeric) 
     375        { 
     376                // copied from common's original code 
     377                return ((mask & l_dl || mask & l_sub) && mask & l_mgr && !(mask & l_spam)) || (mask & l_warn || mask & l_crit); 
    291378        } 
    292 
    293  
    294 string LogIrc::getTorServer() 
    295 
    296         return m_TorServer; 
    297 
    298  
    299 string LogIrc::getIrcServer() 
    300 
    301         return m_IrcServer; 
    302 
    303  
    304 uint32_t LogIrc::getIrcIP() 
    305 
    306         return m_IrcIP; 
    307 
    308  
    309 uint16_t LogIrc::getIrcPort() 
    310 
    311         return m_IrcPort; 
    312 
    313  
    314 string LogIrc::getIrcPass() 
    315 
    316         return m_IrcPass; 
    317 
    318  
    319 string LogIrc::getIrcNick() 
    320 
    321         return m_IrcNick; 
    322 
    323  
    324 string LogIrc::getIrcIdent() 
    325 
    326         return m_IrcIdent; 
    327 
    328  
    329 string LogIrc::getIrcUserInfo() 
    330 
    331         return m_IrcUserInfo; 
    332 
    333  
    334 string LogIrc::getIrcChannel() 
    335 
    336         return m_IrcChannel; 
    337 
    338  
    339 string LogIrc::getIrcChannelPass() 
    340 
    341         return m_IrcChannelPass; 
    342 
    343  
    344 string LogIrc::getIrcUserModes() 
    345 
    346         return m_IrcUserModes; 
    347 
    348  
    349 string LogIrc::getConnectCommand() 
    350 
    351         string connectCommand = m_Config->getValString("log-irc.irc.connect-command"); 
    352          
    353         if(connectCommand.empty()) 
    354                 throw (void *) 0; 
    355          
    356         return connectCommand + "\r\n"; 
    357 
    358  
     379        else 
     380        { 
     381                return (mask & m_LogPatternNumeric); 
     382        } 
     383
     384 
     385void LogIrc::setLogPattern(const char * patternString) 
     386{        
     387        // this code just plain sucks, does not detect wrong pattern names... 
     388        m_LogPatternNumeric = g_Nepenthes->getLogMgr()->parseTagString(patternString); 
     389
    359390 
    360391void LogIrc::setDialogue(IrcDialogue *dia) 
  • nepenthes/branches/nepenthes-experimental/modules/log-irc/log-irc.hpp

    r547 r548  
    8989                string getIrcServer(); 
    9090                 
    91                 //! throws exception if no command was specified 
    9291                string getConnectCommand(); 
    9392 
    9493 
    9594                void setDialogue(IrcDialogue *); 
     95                 
     96                bool logMaskMatches(uint32_t mask); 
     97                void setLogPattern(const char * patternString); 
    9698 
    9799                bool useTor(); 
    98  
    99         protected: 
    100  
     100                 
     101        private: 
    101102                log_irc_state m_State; 
    102103 
     
    123124                string m_IrcChannel; 
    124125                string m_IrcChannelPass; 
     126                 
     127                uint32_t m_LogPatternNumeric; 
     128                 
     129                string m_ConnectCommand; 
    125130 
    126131                IrcDialogue *m_IrcDialogue; 
  • nepenthes/branches/nepenthes-experimental/nepenthes-core/include/LogManager.hpp

    r375 r548  
    6060                const char                      *getTagName(uint32_t bit); 
    6161                uint32_t                        getTagId(const char *tag); 
    62                 uint32_t                      parseTagString(const char *tagString); 
     62                virtual uint32_t                      parseTagString(const char *tagString); 
    6363                void                            setColor(bool setting); 
    6464                bool                            getColorSetting();