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/>.
38 void serialize(std::string& out) const
40 if (Action == ACT_BAN)
42 else if (Action == ACT_BLOCK)
45 out.append(ConvToStr(Lines)).push_back(':');
46 out.append(ConvToStr(Seconds));
50 out.append(ConvToStr(Diff));
54 out.append(ConvToStr(Backlog));
60 class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
67 RepeatItem(time_t TS, const std::string& Line) : ts(TS), line(Line) { }
70 typedef std::deque<RepeatItem> RepeatItemList;
74 RepeatItemList ItemList;
76 MemberInfo() : Counter(0) {}
81 unsigned int MaxLines;
83 unsigned int MaxBacklog;
85 unsigned int MaxMessageSize;
86 ModuleSettings() : MaxLines(0), MaxSecs(0), MaxBacklog(0), MaxDiff() { }
89 std::vector<unsigned int> mx[2];
92 bool CompareLines(const std::string& message, const std::string& historyline, unsigned int trigger)
94 if (message == historyline)
97 return (Levenshtein(message, historyline) <= trigger);
102 unsigned int Levenshtein(const std::string& s1, const std::string& s2)
104 unsigned int l1 = s1.size();
105 unsigned int l2 = s2.size();
107 for (unsigned int i = 0; i < l2; i++)
109 for (unsigned int i = 0; i < l1; i++)
112 for (unsigned int j = 0; j < l2; j++)
113 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));
121 SimpleExtItem<MemberInfo> MemberInfoExt;
123 RepeatMode(Module* Creator)
124 : ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >(Creator, "repeat", 'E')
125 , MemberInfoExt("repeat_memb", Creator)
129 void OnUnset(User* source, Channel* chan)
131 // Unset the per-membership extension when the mode is removed
132 const UserMembList* users = chan->GetUsers();
133 for (UserMembCIter i = users->begin(); i != users->end(); ++i)
134 MemberInfoExt.unset(i->second);
137 ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
139 ChannelSettings settings;
140 if (!ParseSettings(source, parameter, settings))
142 source->WriteNotice("*** Invalid syntax. Syntax is {[~*]}[lines]:[time]{:[difference]}{:[backlog]}");
143 return MODEACTION_DENY;
146 if ((settings.Backlog > 0) && (settings.Lines > settings.Backlog))
148 source->WriteNotice("*** You can't set needed lines higher than backlog");
149 return MODEACTION_DENY;
152 LocalUser* localsource = IS_LOCAL(source);
153 if ((localsource) && (!ValidateSettings(localsource, settings)))
154 return MODEACTION_DENY;
156 ext.set(channel, settings);
158 return MODEACTION_ALLOW;
161 bool MatchLine(Membership* memb, ChannelSettings* rs, std::string message)
163 // If the message is larger than whatever size it's set to,
164 // let's pretend it isn't. If the first 512 (def. setting) match, it's probably spam.
165 if (message.size() > ms.MaxMessageSize)
166 message.erase(ms.MaxMessageSize);
168 MemberInfo* rp = MemberInfoExt.get(memb);
172 MemberInfoExt.set(memb, rp);
175 unsigned int matches = 0;
177 matches = rp->Counter;
179 RepeatItemList& items = rp->ItemList;
180 const unsigned int trigger = (message.size() * rs->Diff / 100);
181 const time_t now = ServerInstance->Time();
183 std::transform(message.begin(), message.end(), message.begin(), ::tolower);
185 for (std::deque<RepeatItem>::iterator it = items.begin(); it != items.end(); ++it)
189 items.erase(it, items.end());
194 if (CompareLines(message, it->line, trigger))
196 if (++matches >= rs->Lines)
198 if (rs->Action != ChannelSettings::ACT_BLOCK)
203 else if ((ms.MaxBacklog == 0) || (rs->Backlog == 0))
211 unsigned int max_items = (rs->Backlog ? rs->Backlog : 1);
212 if (items.size() >= max_items)
215 items.push_front(RepeatItem(now + rs->Seconds, message));
216 rp->Counter = matches;
220 void Resize(size_t size)
222 size_t newsize = size+1;
223 if (newsize <= mx[0].size())
225 ms.MaxMessageSize = size;
226 mx[0].resize(newsize);
227 mx[1].resize(newsize);
232 ConfigTag* conf = ServerInstance->Config->ConfValue("repeat");
233 ms.MaxLines = conf->getInt("maxlines", 20);
234 ms.MaxBacklog = conf->getInt("maxbacklog", 20);
235 ms.MaxSecs = conf->getInt("maxsecs", 0);
237 ms.MaxDiff = conf->getInt("maxdistance", 50);
238 if (ms.MaxDiff > 100)
241 unsigned int newsize = conf->getInt("size", 512);
242 if (newsize > ServerInstance->Config->Limits.MaxLine)
243 newsize = ServerInstance->Config->Limits.MaxLine;
247 std::string GetModuleSettings() const
249 return ConvToStr(ms.MaxLines) + ":" + ConvToStr(ms.MaxSecs) + ":" + ConvToStr(ms.MaxDiff) + ":" + ConvToStr(ms.MaxBacklog);
252 void SerializeParam(Channel* chan, const ChannelSettings* chset, std::string& out)
254 chset->serialize(out);
258 bool ParseSettings(User* source, std::string& parameter, ChannelSettings& settings)
260 irc::sepstream stream(parameter, ':');
262 if (!stream.GetToken(item))
263 // Required parameter missing
266 if ((item[0] == '*') || (item[0] == '~'))
268 settings.Action = ((item[0] == '*') ? ChannelSettings::ACT_BAN : ChannelSettings::ACT_BLOCK);
269 item.erase(item.begin());
272 settings.Action = ChannelSettings::ACT_KICK;
274 if ((settings.Lines = ConvToInt(item)) == 0)
277 if ((!stream.GetToken(item)) || ((settings.Seconds = InspIRCd::Duration(item)) == 0))
278 // Required parameter missing
281 // The diff and backlog parameters are optional
282 settings.Diff = settings.Backlog = 0;
283 if (stream.GetToken(item))
285 // There is a diff parameter, see if it's valid (> 0)
286 if ((settings.Diff = ConvToInt(item)) == 0)
289 if (stream.GetToken(item))
291 // There is a backlog parameter, see if it's valid
292 if ((settings.Backlog = ConvToInt(item)) == 0)
295 // If there are still tokens, then it's invalid because we allow only 4
296 if (stream.GetToken(item))
304 bool ValidateSettings(LocalUser* source, const ChannelSettings& settings)
306 if (settings.Backlog && !ms.MaxBacklog)
308 source->WriteNotice("*** The server administrator has disabled backlog matching");
314 if (settings.Diff > ms.MaxDiff)
317 source->WriteNotice("*** The server administrator has disabled matching on edit distance");
319 source->WriteNotice("*** The distance you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxDiff));
323 if (ms.MaxLines && settings.Lines > ms.MaxLines)
325 source->WriteNotice("*** The line number you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxLines));
329 if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
331 source->WriteNotice("*** The seconds you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxSecs));
340 class RepeatModule : public Module
345 RepeatModule() : rm(this) {}
347 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
352 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
354 if (target_type != TYPE_CHANNEL || !IS_LOCAL(user))
355 return MOD_RES_PASSTHRU;
357 Channel* chan = reinterpret_cast<Channel*>(dest);
358 ChannelSettings* settings = rm.ext.get(chan);
360 return MOD_RES_PASSTHRU;
362 Membership* memb = chan->GetUser(user);
364 return MOD_RES_PASSTHRU;
366 if (ServerInstance->OnCheckExemption(user, chan, "repeat") == MOD_RES_ALLOW)
367 return MOD_RES_PASSTHRU;
369 if (rm.MatchLine(memb, settings, text))
371 if (settings->Action == ChannelSettings::ACT_BLOCK)
373 user->WriteNotice("*** This line is too similiar to one of your last lines.");
377 if (settings->Action == ChannelSettings::ACT_BAN)
379 std::vector<std::string> parameters;
380 parameters.push_back(memb->chan->name);
381 parameters.push_back("+b");
382 parameters.push_back("*!*@" + user->dhost);
383 ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
386 memb->chan->KickUser(ServerInstance->FakeClient, user, "Repeat flood");
389 return MOD_RES_PASSTHRU;
392 void Prioritize() CXX11_OVERRIDE
394 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
397 Version GetVersion() CXX11_OVERRIDE
399 return Version("Provides the +E channel mode - for blocking of similiar messages", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
403 MODULE_INIT(RepeatModule)