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 unsigned int MaxMessageSize;
53 ModuleSettings() : MaxLines(0), MaxSecs(0), MaxBacklog(0), MaxDiff() { }
56 std::vector<unsigned int> mx[2];
59 bool CompareLines(const std::string& message, const std::string& historyline, unsigned int trigger)
61 if (message == historyline)
64 return (Levenshtein(message, historyline) <= trigger);
69 unsigned int Levenshtein(const std::string& s1, const std::string& s2)
71 unsigned int l1 = s1.size();
72 unsigned int l2 = s2.size();
74 for (unsigned int i = 0; i < l2; i++)
76 for (unsigned int i = 0; i < l1; i++)
79 for (unsigned int j = 0; j < l2; j++)
80 mx[1][j + 1] = std::min(std::min(mx[1][j] + 1, mx[0][j + 1] + 1), mx[0][j] + ((s1[i] == s2[j]) ? 0 : 1));
102 unsigned int Seconds;
104 std::string serialize()
106 std::string ret = ((Action == ACT_BAN) ? "*" : (Action == ACT_BLOCK ? "~" : "")) + ConvToStr(Lines) + ":" + ConvToStr(Seconds);
109 ret += ":" + ConvToStr(Diff);
111 ret += ":" + ConvToStr(Backlog);
117 SimpleExtItem<MemberInfo> MemberInfoExt;
118 SimpleExtItem<ChannelSettings> ChanSet;
120 RepeatMode(Module* Creator)
121 : ModeHandler(Creator, "repeat", 'E', PARAM_SETONLY, MODETYPE_CHANNEL)
122 , MemberInfoExt("repeat_memb", Creator)
123 , ChanSet("repeat", Creator)
127 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
131 if (!channel->IsModeSet(this))
132 return MODEACTION_DENY;
134 // Unset the per-membership extension when the mode is removed
135 const UserMembList* users = channel->GetUsers();
136 for (UserMembCIter i = users->begin(); i != users->end(); ++i)
137 MemberInfoExt.unset(i->second);
139 ChanSet.unset(channel);
140 return MODEACTION_ALLOW;
143 if (channel->GetModeParameter(this) == parameter)
144 return MODEACTION_DENY;
146 ChannelSettings settings;
147 if (!ParseSettings(source, parameter, settings))
149 source->WriteNotice("*** Invalid syntax. Syntax is {[~*]}[lines]:[time]{:[difference]}{:[backlog]}");
150 return MODEACTION_DENY;
153 if ((settings.Backlog > 0) && (settings.Lines > settings.Backlog))
155 source->WriteNotice("*** You can't set needed lines higher than backlog");
156 return MODEACTION_DENY;
159 LocalUser* localsource = IS_LOCAL(source);
160 if ((localsource) && (!ValidateSettings(localsource, settings)))
161 return MODEACTION_DENY;
163 ChanSet.set(channel, settings);
165 return MODEACTION_ALLOW;
168 bool MatchLine(Membership* memb, ChannelSettings* rs, std::string message)
170 // If the message is larger than whatever size it's set to,
171 // let's pretend it isn't. If the first 512 (def. setting) match, it's probably spam.
172 if (message.size() > ms.MaxMessageSize)
173 message.erase(ms.MaxMessageSize);
175 MemberInfo* rp = MemberInfoExt.get(memb);
179 MemberInfoExt.set(memb, rp);
182 unsigned int matches = 0;
184 matches = rp->Counter;
186 RepeatItemList& items = rp->ItemList;
187 const unsigned int trigger = (message.size() * rs->Diff / 100);
188 const time_t now = ServerInstance->Time();
190 std::transform(message.begin(), message.end(), message.begin(), ::tolower);
192 for (std::deque<RepeatItem>::iterator it = items.begin(); it != items.end(); ++it)
196 items.erase(it, items.end());
201 if (CompareLines(message, it->line, trigger))
203 if (++matches >= rs->Lines)
205 if (rs->Action != ACT_BLOCK)
210 else if ((ms.MaxBacklog == 0) || (rs->Backlog == 0))
218 unsigned int max_items = (rs->Backlog ? rs->Backlog : 1);
219 if (items.size() >= max_items)
222 items.push_front(RepeatItem(now + rs->Seconds, message));
223 rp->Counter = matches;
227 void Resize(size_t size)
229 size_t newsize = size+1;
230 if (newsize <= mx[0].size())
232 ms.MaxMessageSize = size;
233 mx[0].resize(newsize);
234 mx[1].resize(newsize);
239 ConfigTag* conf = ServerInstance->Config->ConfValue("repeat");
240 ms.MaxLines = conf->getInt("maxlines", 20);
241 ms.MaxBacklog = conf->getInt("maxbacklog", 20);
242 ms.MaxSecs = conf->getInt("maxsecs", 0);
244 ms.MaxDiff = conf->getInt("maxdistance", 50);
245 if (ms.MaxDiff > 100)
248 unsigned int newsize = conf->getInt("size", 512);
249 if (newsize > ServerInstance->Config->Limits.MaxLine)
250 newsize = ServerInstance->Config->Limits.MaxLine;
254 std::string GetModuleSettings() const
256 return ConvToStr(ms.MaxLines) + ":" + ConvToStr(ms.MaxSecs) + ":" + ConvToStr(ms.MaxDiff) + ":" + ConvToStr(ms.MaxBacklog);
260 bool ParseSettings(User* source, std::string& parameter, ChannelSettings& settings)
262 irc::sepstream stream(parameter, ':');
264 if (!stream.GetToken(item))
265 // Required parameter missing
268 if ((item[0] == '*') || (item[0] == '~'))
270 settings.Action = ((item[0] == '*') ? ACT_BAN : ACT_BLOCK);
271 item.erase(item.begin());
274 settings.Action = ACT_KICK;
276 if ((settings.Lines = ConvToInt(item)) == 0)
279 if ((!stream.GetToken(item)) || ((settings.Seconds = InspIRCd::Duration(item)) == 0))
280 // Required parameter missing
283 // The diff and backlog parameters are optional
284 settings.Diff = settings.Backlog = 0;
285 if (stream.GetToken(item))
287 // There is a diff parameter, see if it's valid (> 0)
288 if ((settings.Diff = ConvToInt(item)) == 0)
291 if (stream.GetToken(item))
293 // There is a backlog parameter, see if it's valid
294 if ((settings.Backlog = ConvToInt(item)) == 0)
297 // If there are still tokens, then it's invalid because we allow only 4
298 if (stream.GetToken(item))
303 parameter = settings.serialize();
307 bool ValidateSettings(LocalUser* source, const ChannelSettings& settings)
309 if (settings.Backlog && !ms.MaxBacklog)
311 source->WriteNotice("*** The server administrator has disabled backlog matching");
317 if (settings.Diff > ms.MaxDiff)
320 source->WriteNotice("*** The server administrator has disabled matching on edit distance");
322 source->WriteNotice("*** The distance you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxDiff));
326 if (ms.MaxLines && settings.Lines > ms.MaxLines)
328 source->WriteNotice("*** The line number you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxLines));
332 if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
334 source->WriteNotice("*** The seconds you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxSecs));
343 class RepeatModule : public Module
348 RepeatModule() : rm(this) {}
350 void init() CXX11_OVERRIDE
352 ServerInstance->Modules->AddService(rm);
353 ServerInstance->Modules->AddService(rm.ChanSet);
354 ServerInstance->Modules->AddService(rm.MemberInfoExt);
358 void OnRehash(User* user) CXX11_OVERRIDE
363 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
365 if (target_type != TYPE_CHANNEL || !IS_LOCAL(user))
366 return MOD_RES_PASSTHRU;
368 Membership* memb = ((Channel*)dest)->GetUser(user);
369 if (!memb || !memb->chan->IsModeSet(&rm))
370 return MOD_RES_PASSTHRU;
372 if (ServerInstance->OnCheckExemption(user, memb->chan, "repeat") == MOD_RES_ALLOW)
373 return MOD_RES_PASSTHRU;
375 RepeatMode::ChannelSettings* settings = rm.ChanSet.get(memb->chan);
377 return MOD_RES_PASSTHRU;
379 if (rm.MatchLine(memb, settings, text))
381 if (settings->Action == RepeatMode::ACT_BLOCK)
383 user->WriteNotice("*** This line is too similiar to one of your last lines.");
387 if (settings->Action == RepeatMode::ACT_BAN)
389 std::vector<std::string> parameters;
390 parameters.push_back(memb->chan->name);
391 parameters.push_back("+b");
392 parameters.push_back("*!*@" + user->dhost);
393 ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
396 memb->chan->KickUser(ServerInstance->FakeClient, user, "Repeat flood");
399 return MOD_RES_PASSTHRU;
402 void Prioritize() CXX11_OVERRIDE
404 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
407 Version GetVersion() CXX11_OVERRIDE
409 return Version("Provides the +E channel mode - for blocking of similiar messages", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
413 MODULE_INIT(RepeatModule)