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_nickflood.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 channel mode +F (nick flood protection) */
00017 
00020 class nickfloodsettings : public classbase
00021 {
00022  public:
00023 
00024         int secs;
00025         int nicks;
00026         time_t reset;
00027         time_t unlocktime;
00028         int counter;
00029         bool locked;
00030         InspIRCd* ServerInstance;
00031 
00032         nickfloodsettings() : secs(0), nicks(0) {};
00033 
00034         nickfloodsettings(int b, int c) : secs(b), nicks(c)
00035         {
00036                 reset = time(NULL) + secs;
00037                 counter = 0;
00038                 locked = false;
00039         };
00040 
00041         void addnick()
00042         {
00043                 counter++;
00044                 if (time(NULL) > reset)
00045                 {
00046                         counter = 0;
00047                         reset = time(NULL) + secs;
00048                 }
00049         }
00050 
00051         bool shouldlock()
00052         {
00053                 return (counter >= this->nicks);
00054         }
00055 
00056         void clear()
00057         {
00058                 counter = 0;
00059         }
00060 
00061         bool islocked()
00062         {
00063                 if (locked)
00064                 {
00065                         if (time(NULL) > unlocktime)
00066                         {
00067                                 locked = false;
00068                                 return false;
00069                         }
00070                         else
00071                         {
00072                                 return true;
00073                         }
00074                 }
00075                 return false;
00076         }
00077 
00078         void lock()
00079         {
00080                 locked = true;
00081                 unlocktime = time(NULL) + 60;
00082         }
00083 
00084 };
00085 
00088 class NickFlood : public ModeHandler
00089 {
00090  public:
00091         NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
00092 
00093         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
00094         {
00095                 nickfloodsettings* x;
00096                 if (channel->GetExt("nickflood",x))
00097                         return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
00098                 else
00099                         return std::make_pair(false, parameter);
00100         }
00101 
00102         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
00103         {
00104                 /* When TS is equal, the alphabetically later one wins */
00105                 return (their_param < our_param);
00106         }
00107 
00108         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
00109         {
00110                 nickfloodsettings* dummy;
00111 
00112                 if (adding)
00113                 {
00114                         char ndata[MAXBUF];
00115                         char* data = ndata;
00116                         strlcpy(ndata,parameter.c_str(),MAXBUF);
00117                         char* nicks = data;
00118                         char* secs = NULL;
00119                         while (*data)
00120                         {
00121                                 if (*data == ':')
00122                                 {
00123                                         *data = 0;
00124                                         data++;
00125                                         secs = data;
00126                                         break;
00127                                 }
00128                                 else data++;
00129                         }
00130                         if (secs)
00131 
00132                         {
00133                                 /* Set up the flood parameters for this channel */
00134                                 int nnicks = atoi(nicks);
00135                                 int nsecs = atoi(secs);
00136                                 if ((nnicks<1) || (nsecs<1))
00137                                 {
00138                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
00139                                         parameter.clear();
00140                                         return MODEACTION_DENY;
00141                                 }
00142                                 else
00143                                 {
00144                                         if (!channel->GetExt("nickflood", dummy))
00145                                         {
00146                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
00147                                                 nickfloodsettings *f = new nickfloodsettings(nsecs,nnicks);
00148                                                 channel->Extend("nickflood", f);
00149                                                 channel->SetMode('F', true);
00150                                                 channel->SetModeParam('F', parameter.c_str(), true);
00151                                                 return MODEACTION_ALLOW;
00152                                         }
00153                                         else
00154                                         {
00155                                                 std::string cur_param = channel->GetModeParameter('F');
00156                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
00157                                                 if (cur_param == parameter)
00158                                                 {
00159                                                         // mode params match
00160                                                         return MODEACTION_DENY;
00161                                                 }
00162                                                 else
00163                                                 {
00164                                                         // new mode param, replace old with new
00165                                                         if ((nsecs > 0) && (nnicks > 0))
00166                                                         {
00167                                                                 nickfloodsettings* f;
00168                                                                 channel->GetExt("nickflood", f);
00169                                                                 delete f;
00170                                                                 f = new nickfloodsettings(nsecs, nnicks);
00171                                                                 channel->Shrink("nickflood");
00172                                                                 channel->Extend("nickflood", f);
00173                                                                 channel->SetModeParam('F', cur_param.c_str(), false);
00174                                                                 channel->SetModeParam('F', parameter.c_str(), true);
00175                                                                 return MODEACTION_ALLOW;
00176                                                         }
00177                                                         else
00178                                                         {
00179                                                                 return MODEACTION_DENY;
00180                                                         }
00181                                                 }
00182                                         }
00183                                 }
00184                         }
00185                         else
00186                         {
00187                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
00188                                 return MODEACTION_DENY;
00189                         }
00190                 }
00191                 else
00192                 {
00193                         if (channel->GetExt("nickflood", dummy))
00194                         {
00195                                 nickfloodsettings *f;
00196                                 channel->GetExt("nickflood", f);
00197                                 delete f;
00198                                 channel->Shrink("nickflood");
00199                                 channel->SetMode('F', false);
00200                                 return MODEACTION_ALLOW;
00201                         }
00202                 }
00203                 return MODEACTION_DENY;
00204         }
00205 };
00206 
00207 class ModuleNickFlood : public Module
00208 {
00209         NickFlood* jf;
00210 
00211  public:
00212 
00213         ModuleNickFlood(InspIRCd* Me)
00214                 : Module(Me)
00215         {
00216 
00217                 jf = new NickFlood(ServerInstance);
00218                 if (!ServerInstance->Modes->AddMode(jf))
00219                         throw ModuleException("Could not add new modes!");
00220                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick };
00221                 ServerInstance->Modules->Attach(eventlist, this, 2);
00222         }
00223 
00224         virtual int OnUserPreNick(User* user, const std::string &newnick)
00225         {
00226                 if (isdigit(newnick[0])) /* allow switches to UID */
00227                         return 0;
00228 
00229                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
00230                 {
00231                         Channel *channel = i->first;
00232 
00233                         nickfloodsettings *f;
00234                         if (channel->GetExt("nickflood", f))
00235                         {
00236                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
00237                                         continue;
00238 
00239                                 if (f->islocked())
00240                                 {
00241                                         user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %d nick changes in %d seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs);
00242                                         return 1;
00243                                 }
00244 
00245                                 f->addnick();
00246                                 if (f->shouldlock())
00247                                 {
00248                                         f->clear();
00249                                         f->lock();
00250                                         channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %d nick changes in %d seconds.", channel->name.c_str(), f->nicks, f->secs);
00251                                         return 1;
00252                                 }
00253                         }
00254                 }
00255 
00256                 return 0;
00257         }
00258 
00259         void OnChannelDelete(Channel* chan)
00260         {
00261                 nickfloodsettings *f;
00262                 if (chan->GetExt("nickflood",f))
00263                 {
00264                         delete f;
00265                         chan->Shrink("nickflood");
00266                 }
00267         }
00268 
00269 
00270         virtual ~ModuleNickFlood()
00271         {
00272                 ServerInstance->Modes->DelMode(jf);
00273                 delete jf;
00274         }
00275 
00276         virtual Version GetVersion()
00277         {
00278                 return Version("$Id: m_nickflood.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_COMMON | VF_VENDOR, API_VERSION);
00279         }
00280 };
00281 
00282 MODULE_INIT(ModuleNickFlood)