]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_censor.cpp
Works with the m_testclient test program/suite!
[user/henk/code/inspircd.git] / src / modules / m_censor.cpp
index 03b4f0f9dfc8d2726dac90b43000468050921238..69db8bbe6936d9c304be62c53e6dd161b0c14673 100644 (file)
@@ -3,7 +3,7 @@
  *       +------------------------------------+
  *
  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                       E-mail:
+ *                                    E-mail:
  *                <brain@chatspike.net>
  *               <Craig@chatspike.net>
  *     
@@ -36,11 +36,73 @@ class CensorException : public ModuleException
        }
 };
 
+class CensorUser : public ModeHandler
+{
+ public:
+       CensorUser() : ModeHandler('G', 0, 0, false, MODETYPE_USER, false) { }
+
+       ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
+       {
+               /* Only opers can change other users modes */
+               if ((source != dest) && (!*source->oper))
+                       return MODEACTION_DENY;
+
+               if (adding)
+               {
+                       if (!dest->IsModeSet('G'))
+                       {
+                               dest->SetMode('G',true);
+                               return MODEACTION_ALLOW;
+                       }
+               }
+               else
+               {
+                       if (dest->IsModeSet('G'))
+                       {
+                               dest->SetMode('G',false);
+                               return MODEACTION_ALLOW;
+                       }
+               }
+
+               return MODEACTION_DENY;
+       }
+};
+
+class CensorChannel : public ModeHandler
+{
+ public:
+       CensorChannel() : ModeHandler('G', 0, 0, false, MODETYPE_CHANNEL, false) { }
+
+       ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
+       {
+               if (adding)
+               {
+                       if (!channel->IsModeSet('G'))
+                       {
+                               channel->SetMode('G',false);
+                               return MODEACTION_ALLOW;
+                       }
+               }
+               else
+               {
+                       if (channel->IsModeSet('G'))
+                       {
+                               channel->SetMode('G',false);
+                               return MODEACTION_ALLOW;
+                       }
+               }
+
+               return MODEACTION_ALLOW;
+       }
+};
+
 class ModuleCensor : public Module
 {
 
        Server *Srv;
        censor_t censors;
+       CensorUser *cu;
+       CensorChannel *cc;
  
  public:
        ModuleCensor(Server* Me)
@@ -60,37 +122,27 @@ class ModuleCensor : public Module
                 */
                Srv = Me;
                OnRehash("");
-               Srv->AddExtendedMode('G',MT_CHANNEL,false,0,0);
-               Srv->AddExtendedMode('G',MT_CLIENT,false,0,0);
+               cu = new CensorUser;
+               cc = new CensorChannel;
+               Srv->AddMode(cu, 'G');
+               Srv->AddMode(cc, 'G');
        }
 
        void Implements(char* List)
        {
-               List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnExtendedMode] = 1;
+               List[I_OnRehash] = List[I_On005Numeric] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
        }
 
 
-        virtual void On005Numeric(std::string &output)
-        {
-               InsertMode(output,"G",4);
-        }
-
-
-       virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
+       virtual void On005Numeric(std::string &output)
        {
-               // check if this is our mode character...
-               if (modechar == 'G')
-               {
-                       return 1;
-               }
-               else
-               {
-                       return 0;
-               }
+               InsertMode(output,"G",4);
        }
-       
        virtual ~ModuleCensor()
        {
+               DELETE(cu);
+               DELETE(cc);
        }
        
        virtual void ReplaceLine(irc::string &text, irc::string pattern, irc::string replace)
@@ -119,12 +171,12 @@ class ModuleCensor : public Module
                                if (target_type == TYPE_USER)
                                {
                                        userrec* t = (userrec*)dest;
-                                       active = (strchr(t->modes,'G') > 0);
+                                       active = t->IsModeSet('G');
                                }
                                else if (target_type == TYPE_CHANNEL)
                                {
                                        chanrec* t = (chanrec*)dest;
-                                       active = (t->IsModeSet('G'));
+                                       active = t->IsModeSet('G');
                                }
                                
                                if (active)
@@ -158,14 +210,14 @@ class ModuleCensor : public Module
                        throw(e);
                }
                censors.clear();
-                for (int index = 0; index < MyConf->Enumerate("badword"); index++)
-                {
-                        irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
-                        irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
+               for (int index = 0; index < MyConf->Enumerate("badword"); index++)
+               {
+                       irc::string pattern = (MyConf->ReadValue("badword","text",index)).c_str();
+                       irc::string replace = (MyConf->ReadValue("badword","replace",index)).c_str();
                        censors[pattern] = replace;
                }
-               delete Conf;
-               delete MyConf;
+               DELETE(Conf);
+               DELETE(MyConf);
        }
        
        virtual Version GetVersion()
@@ -201,4 +253,3 @@ extern "C" void * init_module( void )
 {
        return new ModuleCensorFactory;
 }
-