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/>.
22 class RepeatMode : public ModeHandler
29 RepeatItem(time_t TS, const std::string& Line) : ts(TS), line(Line) { }
32 typedef std::deque<RepeatItem> RepeatItemList;
36 RepeatItemList ItemList;
38 MemberInfo() : Counter(0) {}
43 unsigned int MaxLines;
45 unsigned int MaxBacklog;
47 unsigned int MaxMessageSize;
48 ModuleSettings() : MaxLines(0), MaxSecs(0), MaxBacklog(0), MaxDiff() { }
51 std::vector<unsigned int> mx[2];
54 bool CompareLines(const std::string& message, const std::string& historyline, unsigned int trigger)
56 if (message == historyline)
59 return (Levenshtein(message, historyline) <= trigger);
64 unsigned int Levenshtein(const std::string& s1, const std::string& s2)
66 unsigned int l1 = s1.size();
67 unsigned int l2 = s2.size();
69 for (unsigned int i = 0; i < l2; i++)
71 for (unsigned int i = 0; i < l1; i++)
74 for (unsigned int j = 0; j < l2; j++)
75 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));
99 std::string serialize()
101 std::string ret = ((Action == ACT_BAN) ? "*" : (Action == ACT_BLOCK ? "~" : "")) + ConvToStr(Lines) + ":" + ConvToStr(Seconds);
104 ret += ":" + ConvToStr(Diff);
106 ret += ":" + ConvToStr(Backlog);
112 SimpleExtItem<MemberInfo> MemberInfoExt;
113 SimpleExtItem<ChannelSettings> ChanSet;
115 RepeatMode(Module* Creator)
116 : ModeHandler(Creator, "repeat", 'E', PARAM_SETONLY, MODETYPE_CHANNEL)
117 , MemberInfoExt("repeat_memb", Creator)
118 , ChanSet("repeat", Creator)
122 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
126 if (!channel->IsModeSet(this))
127 return MODEACTION_DENY;
129 // Unset the per-membership extension when the mode is removed
130 const UserMembList* users = channel->GetUsers();
131 for (UserMembCIter i = users->begin(); i != users->end(); ++i)
132 MemberInfoExt.unset(i->second);
134 ChanSet.unset(channel);
135 return MODEACTION_ALLOW;
138 if (channel->GetModeParameter(this) == parameter)
139 return MODEACTION_DENY;
141 ChannelSettings settings;
142 if (!ParseSettings(source, parameter, settings))
144 source->WriteNotice("*** Invalid syntax. Syntax is {[~*]}[lines]:[time]{:[difference]}{:[backlog]}");
145 return MODEACTION_DENY;
148 if ((settings.Backlog > 0) && (settings.Lines > settings.Backlog))
150 source->WriteNotice("*** You can't set needed lines higher than backlog");
151 return MODEACTION_DENY;
154 LocalUser* localsource = IS_LOCAL(source);
155 if ((localsource) && (!ValidateSettings(localsource, settings)))
156 return MODEACTION_DENY;
158 ChanSet.set(channel, settings);
160 return MODEACTION_ALLOW;
163 bool MatchLine(Membership* memb, ChannelSettings* rs, std::string message)
165 // If the message is larger than whatever size it's set to,
166 // let's pretend it isn't. If the first 512 (def. setting) match, it's probably spam.
167 if (message.size() > ms.MaxMessageSize)
168 message.erase(ms.MaxMessageSize);
170 MemberInfo* rp = MemberInfoExt.get(memb);
174 MemberInfoExt.set(memb, rp);
177 unsigned int matches = 0;
179 matches = rp->Counter;
181 RepeatItemList& items = rp->ItemList;
182 const unsigned int trigger = (message.size() * rs->Diff / 100);
183 const time_t now = ServerInstance->Time();
185 std::transform(message.begin(), message.end(), message.begin(), ::tolower);
187 for (std::deque<RepeatItem>::iterator it = items.begin(); it != items.end(); ++it)
191 items.erase(it, items.end());
196 if (CompareLines(message, it->line, trigger))
198 if (++matches >= rs->Lines)
200 if (rs->Action != ACT_BLOCK)
205 else if ((ms.MaxBacklog == 0) || (rs->Backlog == 0))
213 unsigned int max_items = (rs->Backlog ? rs->Backlog : 1);
214 if (items.size() >= max_items)
217 items.push_front(RepeatItem(now + rs->Seconds, message));
218 rp->Counter = matches;
222 void Resize(size_t size)
224 size_t newsize = size+1;
225 if (newsize <= mx[0].size())
227 ms.MaxMessageSize = size;
228 mx[0].resize(newsize);
229 mx[1].resize(newsize);
234 ConfigTag* conf = ServerInstance->Config->ConfValue("repeat");
235 ms.MaxLines = conf->getInt("maxlines", 20);
236 ms.MaxBacklog = conf->getInt("maxbacklog", 20);
237 ms.MaxSecs = conf->getInt("maxsecs", 0);
239 ms.MaxDiff = conf->getInt("maxdistance", 50);
240 if (ms.MaxDiff > 100)
243 unsigned int newsize = conf->getInt("size", 512);
244 if (newsize > ServerInstance->Config->Limits.MaxLine)
245 newsize = ServerInstance->Config->Limits.MaxLine;
249 std::string GetModuleSettings() const
251 return ConvToStr(ms.MaxLines) + ":" + ConvToStr(ms.MaxSecs) + ":" + ConvToStr(ms.MaxDiff) + ":" + ConvToStr(ms.MaxBacklog);
255 bool ParseSettings(User* source, std::string& parameter, ChannelSettings& settings)
257 irc::sepstream stream(parameter, ':');
259 if (!stream.GetToken(item))
260 // Required parameter missing
263 if ((item[0] == '*') || (item[0] == '~'))
265 settings.Action = ((item[0] == '*') ? ACT_BAN : ACT_BLOCK);
266 item.erase(item.begin());
269 settings.Action = ACT_KICK;
271 if ((settings.Lines = ConvToInt(item)) == 0)
274 if ((!stream.GetToken(item)) || ((settings.Seconds = InspIRCd::Duration(item)) == 0))
275 // Required parameter missing
278 // The diff and backlog parameters are optional
279 settings.Diff = settings.Backlog = 0;
280 if (stream.GetToken(item))
282 // There is a diff parameter, see if it's valid (> 0)
283 if ((settings.Diff = ConvToInt(item)) == 0)
286 if (stream.GetToken(item))
288 // There is a backlog parameter, see if it's valid
289 if ((settings.Backlog = ConvToInt(item)) == 0)
292 // If there are still tokens, then it's invalid because we allow only 4
293 if (stream.GetToken(item))
298 parameter = settings.serialize();
302 bool ValidateSettings(LocalUser* source, const ChannelSettings& settings)
304 if (settings.Backlog && !ms.MaxBacklog)
306 source->WriteNotice("*** The server administrator has disabled backlog matching");
312 if (settings.Diff > ms.MaxDiff)
315 source->WriteNotice("*** The server administrator has disabled matching on edit distance");
317 source->WriteNotice("*** The distance you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxDiff));
321 if (ms.MaxLines && settings.Lines > ms.MaxLines)
323 source->WriteNotice("*** The line number you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxLines));
327 if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
329 source->WriteNotice("*** The seconds you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxSecs));
338 class RepeatModule : public Module
343 RepeatModule() : rm(this) {}
345 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
350 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
352 if (target_type != TYPE_CHANNEL || !IS_LOCAL(user))
353 return MOD_RES_PASSTHRU;
355 Membership* memb = ((Channel*)dest)->GetUser(user);
356 if (!memb || !memb->chan->IsModeSet(&rm))
357 return MOD_RES_PASSTHRU;
359 if (ServerInstance->OnCheckExemption(user, memb->chan, "repeat") == MOD_RES_ALLOW)
360 return MOD_RES_PASSTHRU;
362 RepeatMode::ChannelSettings* settings = rm.ChanSet.get(memb->chan);
364 return MOD_RES_PASSTHRU;
366 if (rm.MatchLine(memb, settings, text))
368 if (settings->Action == RepeatMode::ACT_BLOCK)
370 user->WriteNotice("*** This line is too similiar to one of your last lines.");
374 if (settings->Action == RepeatMode::ACT_BAN)
376 std::vector<std::string> parameters;
377 parameters.push_back(memb->chan->name);
378 parameters.push_back("+b");
379 parameters.push_back("*!*@" + user->dhost);
380 ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
383 memb->chan->KickUser(ServerInstance->FakeClient, user, "Repeat flood");
386 return MOD_RES_PASSTHRU;
389 void Prioritize() CXX11_OVERRIDE
391 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
394 Version GetVersion() CXX11_OVERRIDE
396 return Version("Provides the +E channel mode - for blocking of similiar messages", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
400 MODULE_INIT(RepeatModule)