00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015
00016
00017
00020 class joinfloodsettings : public classbase
00021 {
00022 public:
00023
00024 int secs;
00025 int joins;
00026 time_t reset;
00027 time_t unlocktime;
00028 int counter;
00029 bool locked;
00030 InspIRCd* ServerInstance;
00031
00032 joinfloodsettings() : secs(0), joins(0) {};
00033
00034 joinfloodsettings(int b, int c) : secs(b), joins(c)
00035 {
00036 reset = ServerInstance->Time() + secs;
00037 counter = 0;
00038 locked = false;
00039 };
00040
00041 void addjoin()
00042 {
00043 counter++;
00044 if (ServerInstance->Time() > reset)
00045 {
00046 counter = 0;
00047 reset = ServerInstance->Time() + secs;
00048 }
00049 }
00050
00051 bool shouldlock()
00052 {
00053 return (counter >= this->joins);
00054 }
00055
00056 void clear()
00057 {
00058 counter = 0;
00059 }
00060
00061 bool islocked()
00062 {
00063 if (locked)
00064 {
00065 if (ServerInstance->Time() > 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 = ServerInstance->Time() + 60;
00082 }
00083
00084 };
00085
00088 class JoinFlood : public ModeHandler
00089 {
00090 public:
00091 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
00092
00093 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
00094 {
00095 joinfloodsettings* x;
00096 if (channel->GetExt("joinflood",x))
00097 return std::make_pair(true, ConvToStr(x->joins)+":"+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
00105 return (their_param < our_param);
00106 }
00107
00108 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
00109 {
00110 joinfloodsettings* dummy;
00111
00112 if (adding)
00113 {
00114 char ndata[MAXBUF];
00115 char* data = ndata;
00116 strlcpy(ndata,parameter.c_str(),MAXBUF);
00117 char* joins = 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
00134 int njoins = atoi(joins);
00135 int nsecs = atoi(secs);
00136 if ((njoins<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("joinflood", dummy))
00145 {
00146 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
00147 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
00148 channel->Extend("joinflood", f);
00149 channel->SetMode('j', true);
00150 channel->SetModeParam('j', parameter.c_str(), true);
00151 return MODEACTION_ALLOW;
00152 }
00153 else
00154 {
00155 std::string cur_param = channel->GetModeParameter('j');
00156 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
00157 if (cur_param == parameter)
00158 {
00159
00160 return MODEACTION_DENY;
00161 }
00162 else
00163 {
00164
00165 if ((nsecs > 0) && (njoins > 0))
00166 {
00167 joinfloodsettings* f;
00168 channel->GetExt("joinflood", f);
00169 delete f;
00170 f = new joinfloodsettings(nsecs,njoins);
00171 channel->Shrink("joinflood");
00172 channel->Extend("joinflood", f);
00173 channel->SetModeParam('j', cur_param.c_str(), false);
00174 channel->SetModeParam('j', 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("joinflood", dummy))
00194 {
00195 joinfloodsettings *f;
00196 channel->GetExt("joinflood", f);
00197 delete f;
00198 channel->Shrink("joinflood");
00199 channel->SetMode('j', false);
00200 return MODEACTION_ALLOW;
00201 }
00202 }
00203 return MODEACTION_DENY;
00204 }
00205 };
00206
00207 class ModuleJoinFlood : public Module
00208 {
00209
00210 JoinFlood* jf;
00211
00212 public:
00213
00214 ModuleJoinFlood(InspIRCd* Me)
00215 : Module(Me)
00216 {
00217
00218 jf = new JoinFlood(ServerInstance);
00219 if (!ServerInstance->Modes->AddMode(jf))
00220 throw ModuleException("Could not add new modes!");
00221 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
00222 ServerInstance->Modules->Attach(eventlist, this, 3);
00223 }
00224
00225 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
00226 {
00227 if (chan)
00228 {
00229 joinfloodsettings *f;
00230 if (chan->GetExt("joinflood", f))
00231 {
00232 if (f->islocked())
00233 {
00234 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
00235 return 1;
00236 }
00237 }
00238 }
00239 return 0;
00240 }
00241
00242 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
00243 {
00244 joinfloodsettings *f;
00245
00246
00247 if (sync)
00248 return;
00249
00250
00251 if (channel->GetExt("joinflood",f))
00252 {
00253 f->addjoin();
00254 if (f->shouldlock())
00255 {
00256 f->clear();
00257 f->lock();
00258 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", channel->name.c_str(), f->joins, f->secs);
00259 }
00260 }
00261 }
00262
00263 void OnChannelDelete(Channel* chan)
00264 {
00265 joinfloodsettings *f;
00266 if (chan->GetExt("joinflood",f))
00267 {
00268 delete f;
00269 chan->Shrink("joinflood");
00270 }
00271 }
00272
00273
00274 virtual ~ModuleJoinFlood()
00275 {
00276 ServerInstance->Modes->DelMode(jf);
00277 delete jf;
00278 }
00279
00280 virtual Version GetVersion()
00281 {
00282 return Version("$Id: m_joinflood.cpp 10783 2008-11-01 23:02:23Z w00t $", VF_COMMON | VF_VENDOR, API_VERSION);
00283 }
00284 };
00285
00286 MODULE_INIT(ModuleJoinFlood)