]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_silence.cpp
Only valid targets for encap are now server ids
[user/henk/code/inspircd.git] / src / modules / m_silence.cpp
index cb7ac29938dae711fdb2cfb4d1fa7d774f3674c2..9361b9a67f1c032df15fd7853f9c10a1d798ffdc 100644 (file)
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
- *                       E-mail:
- *                <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
 
-#include <stdio.h>
-#include <string>
-#include <vector>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
+#include "inspircd.h"
+#include "wildcard.h"
 
 /* $ModDesc: Provides support for the /SILENCE command */
 
-
 // This typedef holds a silence list. Each user may or may not have a
 // silencelist, if a silence list is empty for a user, he/she does not
 // have one of these structures associated with their user record.
-typedef std::vector<std::string> silencelist;
+typedef std::map<irc::string, time_t> silencelist;
 
-
-void handle_silence(char **parameters, int pcnt, userrec *user)
+class CommandSilence : public Command
 {
-       if (!pcnt)
+       unsigned int& maxsilence;
+ public:
+       CommandSilence (InspIRCd* Instance, unsigned int &max) : Command(Instance,"SILENCE", 0, 0), maxsilence(max)
        {
-               // no parameters, show the current silence list.
-               // Use Extensible::GetExt to fetch the silence list
-               silencelist* sl = (silencelist*)user->GetExt("silence_list");
-               // if the user has a silence list associated with their user record, show it
-               if (sl)
-               {
-                       for (silencelist::const_iterator c = sl->begin(); c < sl->end(); c++)
-                       {
-                               WriteServ(user->fd,"271 %s %s %s!*@*",user->nick, user->nick,c->c_str());
-                       }
-               }
-               WriteServ(user->fd,"272 %s :End of Silence List",user->nick);
+               this->source = "m_silence.so";
+               syntax = "{[+|-]<mask>}";
+               TRANSLATE2(TR_TEXT, TR_END);
        }
-       else if (pcnt > 0)
+
+       CmdResult Handle (const char* const* parameters, int pcnt, User *user)
        {
-               // one or more parameters, add or delete entry from the list (only the first parameter is used)
-               char *nick = parameters[0];
-               if (nick[0] == '-')
+               if (!pcnt)
                {
-                       // removing an item from the list
-                       nick++;
-                       // fetch their silence list
-                       silencelist* sl = (silencelist*)user->GetExt("silence_list");
-                       // does it contain any entries and does it exist?
+                       // no parameters, show the current silence list.
+                       // Use Extensible::GetExt to fetch the silence list
+                       silencelist* sl;
+                       user->GetExt("silence_list", sl);
+                       // if the user has a silence list associated with their user record, show it
                        if (sl)
                        {
-                               if (sl->size())
-                               {
-                                       for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
-                                               {
-                                               // search through for the item
-                                                       if (!strcasecmp(i->c_str(),nick))
-                                                       {
-                                                               sl->erase(i);
-                                                       WriteServ(user->fd,"950 %s %s :Removed %s!*@* from silence list",user->nick, user->nick,nick);
-                                                       // we have modified the vector from within a loop, we must now bail out
-                                                       return;
-                                                       }
-                                               }
-                               }
-                               if (!sl->size())
+                               for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
                                {
-                                       // tidy up -- if a user's list is empty, theres no use having it
-                                       // hanging around in the user record.
-                                       delete sl;
-                                       user->Shrink("silence_list");
+                                       user->WriteServ("271 %s %s %s :%lu",user->nick, user->nick, c->first.c_str(), (unsigned long)c->second);
                                }
                        }
+                       user->WriteServ("272 %s :End of Silence List",user->nick);
+
+                       return CMD_SUCCESS;
                }
-               else if (nick[0] == '+')
+               else if (pcnt > 0)
                {
-                       nick++;
-                       // fetch the user's current silence list
-                       silencelist* sl = (silencelist*)user->GetExt("silence_list");
-                       // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
-                       if (!sl)
+                       // one or more parameters, add or delete entry from the list (only the first parameter is used)
+                       std::string mask = parameters[0] + 1;
+                       char action = *parameters[0];
+                       
+                       if (!mask.length())
                        {
-                               sl = new silencelist;
-                               user->Extend(std::string("silence_list"),(char*)sl);
+                               // 'SILENCE +' or 'SILENCE -', assume *!*@*
+                               mask = "*!*@*";
                        }
-                       // add the nick to it -- silence only takes nicks for some reason even though its list shows masks
-                       for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
+                       
+                       ModeParser::CleanMask(mask);
+
+                       if (action == '-')
                        {
-                               if (!strcasecmp(n->c_str(),nick))
+                               // fetch their silence list
+                               silencelist* sl;
+                               user->GetExt("silence_list", sl);
+                               // does it contain any entries and does it exist?
+                               if (sl)
                                {
-                                       WriteServ(user->fd,"952 %s %s :%s is already on your silence list",user->nick, user->nick,nick);
-                                       return;
+                                       silencelist::iterator i = sl->find(mask.c_str());
+                                       if (i != sl->end())
+                                       {
+                                               sl->erase(i);
+                                               user->WriteServ("950 %s %s :Removed %s from silence list",user->nick, user->nick, mask.c_str());
+                                               if (!sl->size())
+                                               {
+                                                       // tidy up -- if a user's list is empty, theres no use having it
+                                                       // hanging around in the user record.
+                                                       delete sl;
+                                                       user->Shrink("silence_list");
+                                               }
+                                       }
+                                       else
+                                               user->WriteServ("952 %s %s :%s does not exist on your silence list",user->nick, user->nick, mask.c_str());
+                               }
+                       }
+                       else if (action == '+')
+                       {
+                               // fetch the user's current silence list
+                               silencelist* sl;
+                               user->GetExt("silence_list", sl);
+                               // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
+                               if (!sl)
+                               {
+                                       sl = new silencelist;
+                                       user->Extend("silence_list", sl);
                                }
+                               silencelist::iterator n = sl->find(mask.c_str());
+                               if (n != sl->end())
+                               {
+                                       user->WriteServ("952 %s %s :%s is already on your silence list",user->nick, user->nick, mask.c_str());
+                                       return CMD_FAILURE;
+                               }
+                               if (sl->size() >= maxsilence)
+                               {
+                                       user->WriteServ("952 %s %s :Your silence list is full",user->nick, user->nick, mask.c_str());
+                                       return CMD_FAILURE;
+                               }
+                               sl->insert(std::make_pair<irc::string, time_t>(mask.c_str(), ServerInstance->Time()));
+                               user->WriteServ("951 %s %s :Added %s to silence list",user->nick, user->nick, mask.c_str());
+                               return CMD_SUCCESS;
                        }
-                       sl->push_back(std::string(nick));
-                       WriteServ(user->fd,"951 %s %s :Added %s!*@* to silence list",user->nick, user->nick,nick);
-                       return;
                }
+               return CMD_SUCCESS;
        }
-       return;
-}
-
+};
 
 class ModuleSilence : public Module
 {
-       Server *Srv;
+       
+       CommandSilence* mycommand;
+       unsigned int maxsilence;
  public:
  
-       ModuleSilence()
+       ModuleSilence(InspIRCd* Me)
+               : Module(Me), maxsilence(32)
+       {
+               OnRehash(NULL, "");
+               mycommand = new CommandSilence(ServerInstance, maxsilence);
+               ServerInstance->AddCommand(mycommand);
+               Implementation eventlist[] = { I_OnRehash, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
+               ServerInstance->Modules->Attach(eventlist, this, 5);
+       }
+
+
+       virtual void OnRehash(User* user, const std::string &parameter)
        {
-               Srv = new Server;
-               Srv->AddCommand("SILENCE",handle_silence,0,0,"m_silence.so");
+               ConfigReader Conf(ServerInstance);
+               maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
+               if (!maxsilence)
+                       maxsilence = 32;
        }
 
-       virtual void OnUserQuit(userrec* user)
+       virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
        {
                // when the user quits tidy up any silence list they might have just to keep things tidy
                // and to prevent a HONKING BIG MEMORY LEAK!
-               silencelist* sl = (silencelist*)user->GetExt("silence_list");
+               silencelist* sl;
+               user->GetExt("silence_list", sl);
                if (sl)
                {
                        delete sl;
@@ -138,24 +164,25 @@ class ModuleSilence : public Module
        virtual void On005Numeric(std::string &output)
        {
                // we don't really have a limit...
-               output = output + " SILENCE=999";
+               output = output + " SILENCE=" + ConvToStr(maxsilence);
        }
        
-       virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
+       virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
        {
                // im not sure how unreal's silence operates but ours is sensible. It blocks notices and
                // privmsgs from people on the silence list, directed privately at the user.
                // channel messages are unaffected (ever tried to follow the flow of conversation in
                // a channel when you've set an ignore on the two most talkative people?)
-               if (target_type == TYPE_USER)
+               if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
                {
-                       userrec* u = (userrec*)dest;
-                       silencelist* sl = (silencelist*)u->GetExt("silence_list");
+                       User* u = (User*)dest;
+                       silencelist* sl;
+                       u->GetExt("silence_list", sl);
                        if (sl)
                        {
                                for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
                                {
-                                       if (!strcasecmp(c->c_str(),user->nick))
+                                       if (match(user->GetFullHost(), c->first.c_str()))
                                        {
                                                return 1;
                                        }
@@ -165,59 +192,19 @@ class ModuleSilence : public Module
                return 0;
        }
 
-        virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
-        {
-                if (target_type == TYPE_USER)
-                {
-                        userrec* u = (userrec*)dest;
-                        silencelist* sl = (silencelist*)u->GetExt("silence_list");
-                        if (sl)
-                        {
-                                for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
-                                {
-                                        if (!strcasecmp(c->c_str(),user->nick))
-                                        {
-                                                return 1;
-                                        }
-                                }
-                        }
-                }
-                return 0;
-        }
-
-       virtual ~ModuleSilence()
-       {
-               delete Srv;
-       }
-       
-       virtual Version GetVersion()
+       virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
        {
-               return Version(1,0,0,0,0);
+               return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
        }
-};
 
-
-class ModuleSilenceFactory : public ModuleFactory
-{
- public:
-       ModuleSilenceFactory()
-       {
-       }
-       
-       ~ModuleSilenceFactory()
+       virtual ~ModuleSilence()
        {
        }
        
-       virtual Module * CreateModule()
+       virtual Version GetVersion()
        {
-               return new ModuleSilence;
+               return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
        }
-       
 };
 
-
-extern "C" void * init_module( void )
-{
-       return new ModuleSilenceFactory;
-}
-
+MODULE_INIT(ModuleSilence)