The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_silence.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 
00016 /* $ModDesc: Provides support for the /SILENCE command */
00017 
00018 /* Improved drop-in replacement for the /SILENCE command
00019  * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
00020  *
00021  * example that blocks all except private messages
00022  *  /SILENCE +*!*@* a
00023  *  /SILENCE +*!*@* px
00024  *
00025  * example that blocks all invites except from channel services
00026  *  /SILENCE +*!*@* i
00027  *  /SILENCE +chanserv!services@chatters.net ix
00028  *
00029  * example that blocks some bad dude from private, notice and inviting you
00030  *  /SILENCE +*!kiddie@lamerz.net pin
00031  *
00032  * TODO: possibly have add and remove check for existing host and only modify flags according to
00033  *       what's been changed instead of having to remove first, then add if you want to change
00034  *       an entry.
00035  */
00036 
00037 // pair of hostmask and flags
00038 typedef std::pair<std::string, int> silenceset;
00039 
00040 // deque list of pairs
00041 typedef std::deque<silenceset> silencelist;
00042 
00043 // intmasks for flags
00044 static int SILENCE_PRIVATE      = 0x0001; /* p  private messages      */
00045 static int SILENCE_CHANNEL      = 0x0002; /* c  channel messages      */
00046 static int SILENCE_INVITE       = 0x0004; /* i  invites               */
00047 static int SILENCE_NOTICE       = 0x0008; /* n  notices               */
00048 static int SILENCE_CNOTICE      = 0x0010; /* t  channel notices       */
00049 static int SILENCE_ALL          = 0x0020; /* a  all, (pcint)          */
00050 static int SILENCE_EXCLUDE      = 0x0040; /* x  exclude this pattern  */
00051 
00052 
00053 class CommandSVSSilence : public Command
00054 {
00055  public:
00056         CommandSVSSilence(InspIRCd* Instance) : Command(Instance,"SVSSILENCE", 0, 2)
00057         {
00058                 this->source = "m_silence.so";
00059                 syntax = "<target> {[+|-]<mask> <p|c|i|n|t|a|x>}";
00060                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
00061         }
00062 
00063         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
00064         {
00065                 /*
00066                  * XXX: thought occurs to me
00067                  * We may want to change the syntax of this command to
00068                  * SVSSILENCE <flagsora+> +<nick> -<nick> +<nick>
00069                  * style command so services can modify lots of entries at once.
00070                  * leaving it backwards compatible for now as it's late. -- w
00071                  */
00072                 if (!ServerInstance->ULine(user->server))
00073                         return CMD_FAILURE;
00074 
00075                 User *u = ServerInstance->FindNick(parameters[0]);
00076                 if (!u)
00077                         return CMD_FAILURE;
00078 
00079                 if (IS_LOCAL(u))
00080                 {
00081                         ServerInstance->Parser->CallHandler("SILENCE", std::vector<std::string>(++parameters.begin(), parameters.end()), u);
00082                 }
00083 
00084                 return CMD_SUCCESS;
00085         }
00086 };
00087 
00088 class CommandSilence : public Command
00089 {
00090         unsigned int& maxsilence;
00091  public:
00092         CommandSilence (InspIRCd* Instance, unsigned int &max) : Command(Instance,"SILENCE", 0, 0), maxsilence(max)
00093         {
00094                 this->source = "m_silence_ext.so";
00095                 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
00096                 TRANSLATE3(TR_TEXT, TR_TEXT, TR_END);
00097         }
00098 
00099         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
00100         {
00101                 if (!parameters.size())
00102                 {
00103                         // no parameters, show the current silence list.
00104                         // Use Extensible::GetExt to fetch the silence list
00105                         silencelist* sl;
00106                         user->GetExt("silence_list", sl);
00107                         // if the user has a silence list associated with their user record, show it
00108                         if (sl)
00109                         {
00110                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
00111                                 {
00112                                         user->WriteNumeric(271, "%s %s %s %s",user->nick.c_str(), user->nick.c_str(),c->first.c_str(), DecompPattern(c->second).c_str());
00113                                 }
00114                         }
00115                         user->WriteNumeric(272, "%s :End of Silence List",user->nick.c_str());
00116 
00117                         return CMD_LOCALONLY;
00118                 }
00119                 else if (parameters.size() > 0)
00120                 {
00121                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
00122                         std::string mask = parameters[0].substr(1);
00123                         char action = parameters[0][0];
00124                         // Default is private and notice so clients do not break
00125                         int pattern = CompilePattern("pn");
00126 
00127                         // if pattern supplied, use it
00128                         if (parameters.size() > 1) {
00129                                 pattern = CompilePattern(parameters[1].c_str());
00130                         }
00131 
00132                         if (!mask.length())
00133                         {
00134                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
00135                                 mask = "*!*@*";
00136                         }
00137 
00138                         ModeParser::CleanMask(mask);
00139 
00140                         if (action == '-')
00141                         {
00142                                 // fetch their silence list
00143                                 silencelist* sl;
00144                                 user->GetExt("silence_list", sl);
00145                                 // does it contain any entries and does it exist?
00146                                 if (sl)
00147                                 {
00148                                         for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
00149                                         {
00150                                                 // search through for the item
00151                                                 irc::string listitem = i->first.c_str();
00152                                                 if (listitem == mask && i->second == pattern)
00153                                                 {
00154                                                         sl->erase(i);
00155                                                         user->WriteNumeric(950, "%s %s :Removed %s %s from silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
00156                                                         if (!sl->size())
00157                                                         {
00158                                                                 delete sl;
00159                                                                 user->Shrink("silence_list");
00160                                                         }
00161                                                         return CMD_SUCCESS;
00162                                                 }
00163                                         }
00164                                 }
00165                                 user->WriteNumeric(952, "%s %s :%s %s does not exist on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
00166                         }
00167                         else if (action == '+')
00168                         {
00169                                 // fetch the user's current silence list
00170                                 silencelist* sl;
00171                                 user->GetExt("silence_list", sl);
00172                                 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
00173                                 if (!sl)
00174                                 {
00175                                         sl = new silencelist;
00176                                         user->Extend("silence_list", sl);
00177                                 }
00178                                 if (sl->size() > maxsilence)
00179                                 {
00180                                         user->WriteNumeric(952, "%s %s :Your silence list is full",user->nick.c_str(), user->nick.c_str());
00181                                         return CMD_FAILURE;
00182                                 }
00183                                 for (silencelist::iterator n = sl->begin(); n != sl->end();  n++)
00184                                 {
00185                                         irc::string listitem = n->first.c_str();
00186                                         if (listitem == mask && n->second == pattern)
00187                                         {
00188                                                 user->WriteNumeric(952, "%s %s :%s %s is already on your silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
00189                                                 return CMD_FAILURE;
00190                                         }
00191                                 }
00192                                 if (((pattern & SILENCE_EXCLUDE) > 0))
00193                                 {
00194                                         sl->push_front(silenceset(mask,pattern));
00195                                 }
00196                                 else
00197                                 {
00198                                         sl->push_back(silenceset(mask,pattern));
00199                                 }
00200                                 user->WriteNumeric(951, "%s %s :Added %s %s to silence list",user->nick.c_str(), user->nick.c_str(), mask.c_str(), DecompPattern(pattern).c_str());
00201                                 return CMD_SUCCESS;
00202                         }
00203                 }
00204                 return CMD_LOCALONLY;
00205         }
00206 
00207         /* turn the nice human readable pattern into a mask */
00208         int CompilePattern(const char* pattern)
00209         {
00210                 int p = 0;
00211                 for (const char* n = pattern; *n; n++)
00212                 {
00213                         switch (*n)
00214                         {
00215                                 case 'p':
00216                                         p |= SILENCE_PRIVATE;
00217                                         break;
00218                                 case 'c':
00219                                         p |= SILENCE_CHANNEL;
00220                                         break;
00221                                 case 'i':
00222                                         p |= SILENCE_INVITE;
00223                                         break;
00224                                 case 'n':
00225                                         p |= SILENCE_NOTICE;
00226                                         break;
00227                                 case 't':
00228                                         p |= SILENCE_CNOTICE;
00229                                         break;
00230                                 case 'a':
00231                                 case '*':
00232                                         p |= SILENCE_ALL;
00233                                         break;
00234                                 case 'x':
00235                                         p |= SILENCE_EXCLUDE;
00236                                         break;
00237                                 default:
00238                                         break;
00239                         }
00240                 }
00241                 return p;
00242         }
00243 
00244         /* turn the mask into a nice human readable format */
00245         std::string DecompPattern (const int pattern)
00246         {
00247                 std::string out;
00248                 if ((pattern & SILENCE_PRIVATE) > 0)
00249                         out += ",privatemessages";
00250                 if ((pattern & SILENCE_CHANNEL) > 0)
00251                         out += ",channelmessages";
00252                 if ((pattern & SILENCE_INVITE) > 0)
00253                         out += ",invites";
00254                 if ((pattern & SILENCE_NOTICE) > 0)
00255                         out += ",privatenotices";
00256                 if ((pattern & SILENCE_CNOTICE) > 0)
00257                         out += ",channelnotices";
00258                 if ((pattern & SILENCE_ALL) > 0)
00259                         out = ",all";
00260                 if ((pattern & SILENCE_EXCLUDE) > 0)
00261                         out += ",exclude";
00262                 return "<" + out.substr(1) + ">";
00263         }
00264 
00265 };
00266 
00267 class ModuleSilence : public Module
00268 {
00269         CommandSilence* cmdsilence;
00270         CommandSVSSilence *cmdsvssilence;
00271         unsigned int maxsilence;
00272  public:
00273 
00274         ModuleSilence(InspIRCd* Me)
00275                 : Module(Me), maxsilence(32)
00276         {
00277                 OnRehash(NULL, "");
00278                 cmdsilence = new CommandSilence(ServerInstance,maxsilence);
00279                 cmdsvssilence = new CommandSVSSilence(ServerInstance);
00280                 ServerInstance->AddCommand(cmdsilence);
00281                 ServerInstance->AddCommand(cmdsvssilence);
00282 
00283                 Implementation eventlist[] = { I_OnRehash, I_OnBuildExemptList, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnUserPreInvite };
00284                 ServerInstance->Modules->Attach(eventlist, this, 7);
00285         }
00286 
00287         virtual void OnRehash(User* user, const std::string &parameter)
00288         {
00289                 ConfigReader Conf(ServerInstance);
00290                 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
00291                 if (!maxsilence)
00292                         maxsilence = 32;
00293         }
00294 
00295 
00296         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
00297         {
00298                 // when the user quits tidy up any silence list they might have just to keep things tidy
00299                 silencelist* sl;
00300                 user->GetExt("silence_list", sl);
00301                 if (sl)
00302                 {
00303                         delete sl;
00304                         user->Shrink("silence_list");
00305                 }
00306         }
00307 
00308         virtual void On005Numeric(std::string &output)
00309         {
00310                 // we don't really have a limit...
00311                 output = output + " ESILENCE SILENCE=" + ConvToStr(maxsilence);
00312         }
00313 
00314         virtual void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
00315         {
00316                 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
00317                 CUList *ulist;
00318                 switch (status)
00319                 {
00320                         case '@':
00321                                 ulist = chan->GetOppedUsers();
00322                                 break;
00323                         case '%':
00324                                 ulist = chan->GetHalfoppedUsers();
00325                                 break;
00326                         case '+':
00327                                 ulist = chan->GetVoicedUsers();
00328                                 break;
00329                         default:
00330                                 ulist = chan->GetUsers();
00331                                 break;
00332                 }
00333 
00334                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
00335                 {
00336                         if (IS_LOCAL(i->first))
00337                         {
00338                                 if (MatchPattern(i->first, sender, public_silence) == 1)
00339                                 {
00340                                         exempt_list[i->first] = i->first->nick;
00341                                 }
00342                         }
00343                 }
00344         }
00345 
00346         virtual int PreText(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
00347         {
00348                 if (!IS_LOCAL(user))
00349                         return 0;
00350 
00351                 if (target_type == TYPE_USER)
00352                 {
00353                         return MatchPattern((User*)dest, user, silence_type);
00354                 }
00355                 else if (target_type == TYPE_CHANNEL)
00356                 {
00357                         Channel* chan = (Channel*)dest;
00358                         if (chan)
00359                         {
00360                                 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list, "");
00361                         }
00362                 }
00363                 return 0;
00364         }
00365 
00366         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
00367         {
00368                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
00369         }
00370 
00371         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
00372         {
00373                 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
00374         }
00375 
00376         virtual int OnUserPreInvite(User* source,User* dest,Channel* channel, time_t timeout)
00377         {
00378                 return MatchPattern(dest, source, SILENCE_INVITE);
00379         }
00380 
00381         int MatchPattern(User* dest, User* source, int pattern)
00382         {
00383                 /* Server source */
00384                 if (!source || !dest)
00385                         return 1;
00386 
00387                 silencelist* sl;
00388                 dest->GetExt("silence_list", sl);
00389                 if (sl)
00390                 {
00391                         for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
00392                         {
00393                                 if (((((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0)) && (InspIRCd::Match(source->GetFullHost(), c->first)))
00394                                         return !(((c->second & SILENCE_EXCLUDE) > 0));
00395                         }
00396                 }
00397                 return 0;
00398         }
00399 
00400         virtual ~ModuleSilence()
00401         {
00402         }
00403 
00404         virtual Version GetVersion()
00405         {
00406                 return Version("$Id: m_silence.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_COMMON | VF_VENDOR, API_VERSION);
00407         }
00408 };
00409 
00410 MODULE_INIT(ModuleSilence)