2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 // windows.h defines this
27 class RepeatMode : public ModeHandler
34 RepeatItem(time_t TS, const std::string& Line) : ts(TS), line(Line) { }
37 typedef std::deque<RepeatItem> RepeatItemList;
41 RepeatItemList ItemList;
43 MemberInfo() : Counter(0) {}
48 unsigned int MaxLines;
50 unsigned int MaxBacklog;
52 ModuleSettings() : MaxLines(0), MaxSecs(0), MaxBacklog(0), MaxDiff() { }
55 std::vector<std::vector<unsigned int> > mx;
58 bool CompareLines(const std::string& message, const std::string& historyline, unsigned int trigger)
61 return (Levenshtein(message, historyline) <= trigger);
63 return (message == historyline);
66 unsigned int Levenshtein(const std::string& s1, const std::string& s2)
68 unsigned int l1 = s1.size();
69 unsigned int l2 = s2.size();
71 for (unsigned int i = 0; i <= l1; i++)
73 for (unsigned int i = 0; i <= l2; i++)
75 for (unsigned int i = 1; i <= l1; i++)
76 for (unsigned int j = 1; j <= l2; j++)
77 mx[i][j] = std::min(std::min(mx[i - 1][j] + 1, mx[i][j - 1] + 1), mx[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1));
98 std::string serialize()
100 std::string ret = ((Action == ACT_BAN) ? "*" : (Action == ACT_BLOCK ? "~" : "")) + ConvToStr(Lines) + ":" + ConvToStr(Seconds);
103 ret += ":" + ConvToStr(Diff);
105 ret += ":" + ConvToStr(Backlog);
111 SimpleExtItem<MemberInfo> MemberInfoExt;
112 SimpleExtItem<ChannelSettings> ChanSet;
114 RepeatMode(Module* Creator)
115 : ModeHandler(Creator, "repeat", 'E', PARAM_SETONLY, MODETYPE_CHANNEL)
116 , MemberInfoExt("repeat_memb", Creator)
117 , ChanSet("repeat", Creator)
121 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
125 if (!channel->IsModeSet(this))
126 return MODEACTION_DENY;
128 // Unset the per-membership extension when the mode is removed
129 const UserMembList* users = channel->GetUsers();
130 for (UserMembCIter i = users->begin(); i != users->end(); ++i)
131 MemberInfoExt.unset(i->second);
133 ChanSet.unset(channel);
134 return MODEACTION_ALLOW;
137 if (channel->GetModeParameter(this) == parameter)
138 return MODEACTION_DENY;
140 ChannelSettings settings;
141 if (!ParseSettings(source, parameter, settings))
143 source->WriteNotice("*** Invalid syntax. Syntax is {[~*]}[lines]:[time]{:[difference]}{:[backlog]}");
144 return MODEACTION_DENY;
147 if ((settings.Backlog > 0) && (settings.Lines > settings.Backlog))
149 source->WriteNotice("*** You can't set needed lines higher than backlog");
150 return MODEACTION_DENY;
153 LocalUser* localsource = IS_LOCAL(source);
154 if ((localsource) && (!ValidateSettings(localsource, settings)))
155 return MODEACTION_DENY;
157 ChanSet.set(channel, settings);
159 return MODEACTION_ALLOW;
162 bool MatchLine(Membership* memb, ChannelSettings* rs, std::string message)
164 // If the message is larger than whatever size it's set to,
165 // let's pretend it isn't. If the first 512 (def. setting) match, it's probably spam.
166 if (message.size() > mx.size())
167 message.erase(mx.size());
169 MemberInfo* rp = MemberInfoExt.get(memb);
173 MemberInfoExt.set(memb, rp);
176 unsigned int matches = 0;
178 matches = rp->Counter;
180 RepeatItemList& items = rp->ItemList;
181 const unsigned int trigger = (message.size() * rs->Diff / 100);
182 const time_t now = ServerInstance->Time();
184 std::transform(message.begin(), message.end(), message.begin(), ::tolower);
186 for (std::deque<RepeatItem>::iterator it = items.begin(); it != items.end(); ++it)
190 items.erase(it, items.end());
195 if (CompareLines(message, it->line, trigger))
197 if (++matches >= rs->Lines)
199 if (rs->Action != ACT_BLOCK)
204 else if ((ms.MaxBacklog == 0) || (rs->Backlog == 0))
212 unsigned int max_items = (rs->Backlog ? rs->Backlog : 1);
213 if (items.size() >= max_items)
216 items.push_front(RepeatItem(now + rs->Seconds, message));
217 rp->Counter = matches;
221 void Resize(size_t size)
223 if (size == mx.size())
227 if (mx.size() > size)
230 for (unsigned int i = 0; i < mx.size(); i++)
235 for (unsigned int i = 0; i < mx.size(); i++)
238 std::vector<unsigned int>(mx[i]).swap(mx[i]);
240 std::vector<std::vector<unsigned int> >(mx).swap(mx);
246 ConfigTag* conf = ServerInstance->Config->ConfValue("repeat");
247 ms.MaxLines = conf->getInt("maxlines", 20);
248 ms.MaxBacklog = conf->getInt("maxbacklog", 20);
249 ms.MaxSecs = conf->getInt("maxsecs", 0);
251 ms.MaxDiff = conf->getInt("maxdistance", 50);
252 if (ms.MaxDiff > 100)
255 unsigned int newsize = conf->getInt("size", 512);
256 if (newsize > ServerInstance->Config->Limits.MaxLine)
257 newsize = ServerInstance->Config->Limits.MaxLine;
261 std::string GetModuleSettings() const
263 return ConvToStr(ms.MaxLines) + ":" + ConvToStr(ms.MaxSecs) + ":" + ConvToStr(ms.MaxDiff) + ":" + ConvToStr(ms.MaxBacklog);
267 bool ParseSettings(User* source, std::string& parameter, ChannelSettings& settings)
269 irc::sepstream stream(parameter, ':');
271 if (!stream.GetToken(item))
272 // Required parameter missing
275 if ((item[0] == '*') || (item[0] == '~'))
277 settings.Action = ((item[0] == '*') ? ACT_BAN : ACT_BLOCK);
278 item.erase(item.begin());
281 settings.Action = ACT_KICK;
283 if ((settings.Lines = ConvToInt(item)) == 0)
286 if ((!stream.GetToken(item)) || ((settings.Seconds = InspIRCd::Duration(item)) == 0))
287 // Required parameter missing
290 // The diff and backlog parameters are optional
291 settings.Diff = settings.Backlog = 0;
292 if (stream.GetToken(item))
294 // There is a diff parameter, see if it's valid (> 0)
295 if ((settings.Diff = ConvToInt(item)) == 0)
298 if (stream.GetToken(item))
300 // There is a backlog parameter, see if it's valid
301 if ((settings.Backlog = ConvToInt(item)) == 0)
304 // If there are still tokens, then it's invalid because we allow only 4
305 if (stream.GetToken(item))
310 parameter = settings.serialize();
314 bool ValidateSettings(LocalUser* source, const ChannelSettings& settings)
316 if (settings.Backlog && !ms.MaxBacklog)
318 source->WriteNotice("*** The server administrator has disabled backlog matching");
324 if (settings.Diff > ms.MaxDiff)
327 source->WriteNotice("*** The server administrator has disabled matching on edit distance");
329 source->WriteNotice("*** The distance you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxDiff));
333 if (ms.MaxLines && settings.Lines > ms.MaxLines)
335 source->WriteNotice("*** The line number you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxLines));
339 if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
341 source->WriteNotice("*** The seconds you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxSecs));
350 class RepeatModule : public Module
355 RepeatModule() : rm(this) {}
357 void init() CXX11_OVERRIDE
359 ServerInstance->Modules->AddService(rm);
360 ServerInstance->Modules->AddService(rm.ChanSet);
361 ServerInstance->Modules->AddService(rm.MemberInfoExt);
362 Implementation eventlist[] = { I_OnUserPreMessage, I_OnRehash };
363 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
367 void OnRehash(User* user) CXX11_OVERRIDE
372 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
374 if (target_type != TYPE_CHANNEL || !IS_LOCAL(user))
375 return MOD_RES_PASSTHRU;
377 Membership* memb = ((Channel*)dest)->GetUser(user);
378 if (!memb || !memb->chan->IsModeSet(&rm))
379 return MOD_RES_PASSTHRU;
381 if (ServerInstance->OnCheckExemption(user, memb->chan, "repeat") == MOD_RES_ALLOW)
382 return MOD_RES_PASSTHRU;
384 RepeatMode::ChannelSettings* settings = rm.ChanSet.get(memb->chan);
386 return MOD_RES_PASSTHRU;
388 if (rm.MatchLine(memb, settings, text))
390 if (settings->Action == RepeatMode::ACT_BLOCK)
392 user->WriteNotice("*** This line is too similiar to one of your last lines.");
396 if (settings->Action == RepeatMode::ACT_BAN)
398 std::vector<std::string> parameters;
399 parameters.push_back(memb->chan->name);
400 parameters.push_back("+b");
401 parameters.push_back("*!*@" + user->dhost);
402 ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
405 memb->chan->KickUser(ServerInstance->FakeClient, user, "Repeat flood");
408 return MOD_RES_PASSTHRU;
411 void Prioritize() CXX11_OVERRIDE
413 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
416 Version GetVersion() CXX11_OVERRIDE
418 return Version("Provides the +E channel mode - for blocking of similiar messages", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
422 MODULE_INIT(RepeatModule)