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/>.
21 #include "modules/exemption.h"
39 void serialize(std::string& out) const
41 if (Action == ACT_BAN)
43 else if (Action == ACT_BLOCK)
46 out.append(ConvToStr(Lines)).push_back(':');
47 out.append(ConvToStr(Seconds));
51 out.append(ConvToStr(Diff));
55 out.append(ConvToStr(Backlog));
61 class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
68 RepeatItem(time_t TS, const std::string& Line) : ts(TS), line(Line) { }
71 typedef std::deque<RepeatItem> RepeatItemList;
75 RepeatItemList ItemList;
77 MemberInfo() : Counter(0) {}
82 unsigned int MaxLines;
84 unsigned int MaxBacklog;
86 unsigned int MaxMessageSize;
87 ModuleSettings() : MaxLines(0), MaxSecs(0), MaxBacklog(0), MaxDiff() { }
90 std::vector<unsigned int> mx[2];
93 bool CompareLines(const std::string& message, const std::string& historyline, unsigned int trigger)
95 if (message == historyline)
98 return (Levenshtein(message, historyline) <= trigger);
103 unsigned int Levenshtein(const std::string& s1, const std::string& s2)
105 unsigned int l1 = s1.size();
106 unsigned int l2 = s2.size();
108 for (unsigned int i = 0; i < l2; i++)
110 for (unsigned int i = 0; i < l1; i++)
113 for (unsigned int j = 0; j < l2; j++)
114 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));
122 SimpleExtItem<MemberInfo> MemberInfoExt;
124 RepeatMode(Module* Creator)
125 : ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >(Creator, "repeat", 'E')
126 , MemberInfoExt("repeat_memb", ExtensionItem::EXT_MEMBERSHIP, Creator)
130 void OnUnset(User* source, Channel* chan)
132 // Unset the per-membership extension when the mode is removed
133 const Channel::MemberMap& users = chan->GetUsers();
134 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
135 MemberInfoExt.unset(i->second);
138 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
140 ChannelSettings settings;
141 if (!ParseSettings(source, parameter, settings))
143 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
144 "Invalid repeat syntax. Syntax is {[~*]}[lines]:[time]{:[difference]}{:[backlog]}."));
145 return MODEACTION_DENY;
148 if ((settings.Backlog > 0) && (settings.Lines > settings.Backlog))
150 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
151 "Invalid repeat syntax. You can't set needed lines higher than backlog."));
152 return MODEACTION_DENY;
155 LocalUser* localsource = IS_LOCAL(source);
156 if ((localsource) && (!ValidateSettings(localsource, channel, parameter, settings)))
157 return MODEACTION_DENY;
159 ext.set(channel, settings);
161 return MODEACTION_ALLOW;
164 bool MatchLine(Membership* memb, ChannelSettings* rs, std::string message)
166 // If the message is larger than whatever size it's set to,
167 // let's pretend it isn't. If the first 512 (def. setting) match, it's probably spam.
168 if (message.size() > ms.MaxMessageSize)
169 message.erase(ms.MaxMessageSize);
171 MemberInfo* rp = MemberInfoExt.get(memb);
175 MemberInfoExt.set(memb, rp);
178 unsigned int matches = 0;
180 matches = rp->Counter;
182 RepeatItemList& items = rp->ItemList;
183 const unsigned int trigger = (message.size() * rs->Diff / 100);
184 const time_t now = ServerInstance->Time();
186 std::transform(message.begin(), message.end(), message.begin(), ::tolower);
188 for (std::deque<RepeatItem>::iterator it = items.begin(); it != items.end(); ++it)
192 items.erase(it, items.end());
197 if (CompareLines(message, it->line, trigger))
199 if (++matches >= rs->Lines)
201 if (rs->Action != ChannelSettings::ACT_BLOCK)
206 else if ((ms.MaxBacklog == 0) || (rs->Backlog == 0))
214 unsigned int max_items = (rs->Backlog ? rs->Backlog : 1);
215 if (items.size() >= max_items)
218 items.push_front(RepeatItem(now + rs->Seconds, message));
219 rp->Counter = matches;
223 void Resize(size_t size)
225 size_t newsize = size+1;
226 if (newsize <= mx[0].size())
228 ms.MaxMessageSize = size;
229 mx[0].resize(newsize);
230 mx[1].resize(newsize);
235 ConfigTag* conf = ServerInstance->Config->ConfValue("repeat");
236 ms.MaxLines = conf->getUInt("maxlines", 20);
237 ms.MaxBacklog = conf->getUInt("maxbacklog", 20);
238 ms.MaxSecs = conf->getDuration("maxtime", conf->getDuration("maxsecs", 0));
240 ms.MaxDiff = conf->getUInt("maxdistance", 50);
241 if (ms.MaxDiff > 100)
244 unsigned int newsize = conf->getUInt("size", 512);
245 if (newsize > ServerInstance->Config->Limits.MaxLine)
246 newsize = ServerInstance->Config->Limits.MaxLine;
250 std::string GetModuleSettings() const
252 return ConvToStr(ms.MaxLines) + ":" + ConvToStr(ms.MaxSecs) + ":" + ConvToStr(ms.MaxDiff) + ":" + ConvToStr(ms.MaxBacklog);
255 void SerializeParam(Channel* chan, const ChannelSettings* chset, std::string& out)
257 chset->serialize(out);
261 bool ParseSettings(User* source, std::string& parameter, ChannelSettings& settings)
263 irc::sepstream stream(parameter, ':');
265 if (!stream.GetToken(item))
266 // Required parameter missing
269 if ((item[0] == '*') || (item[0] == '~'))
271 settings.Action = ((item[0] == '*') ? ChannelSettings::ACT_BAN : ChannelSettings::ACT_BLOCK);
272 item.erase(item.begin());
275 settings.Action = ChannelSettings::ACT_KICK;
277 if ((settings.Lines = ConvToInt(item)) == 0)
280 if ((!stream.GetToken(item)) || ((settings.Seconds = InspIRCd::Duration(item)) == 0))
281 // Required parameter missing
284 // The diff and backlog parameters are optional
285 settings.Diff = settings.Backlog = 0;
286 if (stream.GetToken(item))
288 // There is a diff parameter, see if it's valid (> 0)
289 if ((settings.Diff = ConvToInt(item)) == 0)
292 if (stream.GetToken(item))
294 // There is a backlog parameter, see if it's valid
295 if ((settings.Backlog = ConvToInt(item)) == 0)
298 // If there are still tokens, then it's invalid because we allow only 4
299 if (stream.GetToken(item))
307 bool ValidateSettings(LocalUser* source, Channel* channel, const std::string& parameter, const ChannelSettings& settings)
309 if (ms.MaxLines && settings.Lines > ms.MaxLines)
311 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
312 "Invalid repeat parameter. The line number you specified is too great. Maximum allowed is %u.", ms.MaxLines)));
316 if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
318 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
319 "Invalid repeat parameter. The seconds you specified is too great. Maximum allowed is %u.", ms.MaxSecs)));
323 if (settings.Diff && settings.Diff > ms.MaxDiff)
326 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
327 "Invalid repeat parameter. The server administrator has disabled matching on edit distance."));
329 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
330 "Invalid repeat parameter. The distance you specified is too great. Maximum allowed is %u.", ms.MaxDiff)));
334 if (settings.Backlog && settings.Backlog > ms.MaxBacklog)
336 if (ms.MaxBacklog == 0)
337 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
338 "Invalid repeat parameter. The server administrator has disabled backlog matching."));
340 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
341 "Invalid repeat paramter. The backlog you specified is too great. Maximum allowed is %u.", ms.MaxBacklog)));
349 class RepeatModule : public Module
351 CheckExemption::EventProvider exemptionprov;
356 : exemptionprov(this)
361 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
366 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
368 if (target.type != MessageTarget::TYPE_CHANNEL || !IS_LOCAL(user))
369 return MOD_RES_PASSTHRU;
371 Channel* chan = target.Get<Channel>();
372 ChannelSettings* settings = rm.ext.get(chan);
374 return MOD_RES_PASSTHRU;
376 Membership* memb = chan->GetUser(user);
378 return MOD_RES_PASSTHRU;
380 ModResult res = CheckExemption::Call(exemptionprov, user, chan, "repeat");
381 if (res == MOD_RES_ALLOW)
382 return MOD_RES_PASSTHRU;
384 if (rm.MatchLine(memb, settings, details.text))
386 if (settings->Action == ChannelSettings::ACT_BLOCK)
388 user->WriteNotice("*** This line is too similar to one of your last lines.");
392 if (settings->Action == ChannelSettings::ACT_BAN)
394 Modes::ChangeList changelist;
395 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), "*!*@" + user->GetDisplayedHost());
396 ServerInstance->Modes->Process(ServerInstance->FakeClient, chan, NULL, changelist);
399 memb->chan->KickUser(ServerInstance->FakeClient, user, "Repeat flood");
402 return MOD_RES_PASSTHRU;
405 void Prioritize() CXX11_OVERRIDE
407 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
410 Version GetVersion() CXX11_OVERRIDE
412 return Version("Provides the +E channel mode - for blocking of similar messages", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
416 MODULE_INIT(RepeatModule)