2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Robby <robby@chatbelgie.be>
5 * Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class ManagerInternal;
33 typedef std::vector<Entry*> WatchedList;
34 typedef std::vector<LocalUser*> WatcherList;
38 struct IRCv3::Monitor::Entry
43 void SetNick(const std::string& Nick)
46 // We may show this string to other users so do not leak the casing
47 std::transform(Nick.begin(), Nick.end(), std::back_inserter(nick), ::tolower);
50 const std::string& GetNick() const { return nick; }
53 class IRCv3::Monitor::Manager
60 class ExtItem : public ExtensionItem
65 ExtItem(Module* mod, const std::string& extname, Manager& managerref)
66 : ExtensionItem(extname, ExtensionItem::EXT_USER, mod)
71 ExtData* get(Extensible* container, bool create = false)
73 ExtData* extdata = static_cast<ExtData*>(get_raw(container));
74 if ((!extdata) && (create))
76 extdata = new ExtData;
77 set_raw(container, extdata);
82 void unset(Extensible* container)
84 free(container, unset_raw(container));
87 std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE
90 const ExtData* extdata = static_cast<ExtData*>(item);
91 for (WatchedList::const_iterator i = extdata->list.begin(); i != extdata->list.end(); ++i)
93 const Entry* entry = *i;
94 ret.append(entry->GetNick()).push_back(' ');
97 ret.erase(ret.size()-1);
101 void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
103 void free(Extensible* container, void* item) CXX11_OVERRIDE
105 delete static_cast<ExtData*>(item);
110 Manager(Module* mod, const std::string& extname)
111 : ext(mod, extname, *this)
123 WatchResult Watch(LocalUser* user, const std::string& nick, unsigned int maxwatch)
125 if (!ServerInstance->IsNick(nick))
126 return WR_INVALIDNICK;
128 WatchedList* watched = GetWatchedPriv(user, true);
129 if (watched->size() >= maxwatch)
132 Entry* entry = AddWatcher(nick, user);
133 if (stdalgo::isin(*watched, entry))
134 return WR_ALREADYWATCHING;
136 entry->watchers.push_back(user);
137 watched->push_back(entry);
141 bool Unwatch(LocalUser* user, const std::string& nick)
143 WatchedList* list = GetWatchedPriv(user);
147 bool ret = RemoveWatcher(nick, user, *list);
148 // If no longer watching any nick unset ext
154 const WatchedList& GetWatched(LocalUser* user)
156 WatchedList* list = GetWatchedPriv(user);
159 return emptywatchedlist;
162 void UnwatchAll(LocalUser* user)
164 WatchedList* list = GetWatchedPriv(user);
168 while (!list->empty())
170 Entry* entry = list->front();
171 RemoveWatcher(entry->GetNick(), user, *list);
176 WatcherList* GetWatcherList(const std::string& nick)
178 Entry* entry = Find(nick);
180 return &entry->watchers;
184 static User* FindNick(const std::string& nick)
186 User* user = ServerInstance->FindNickOnly(nick);
187 if ((user) && (user->registered == REG_ALL))
193 typedef TR1NS::unordered_map<std::string, Entry, irc::insensitive, irc::StrHashComp> NickHash;
195 Entry* Find(const std::string& nick)
197 NickHash::iterator it = nicks.find(nick);
198 if (it != nicks.end())
203 Entry* AddWatcher(const std::string& nick, LocalUser* user)
205 std::pair<NickHash::iterator, bool> ret = nicks.insert(std::make_pair(nick, Entry()));
206 Entry& entry = ret.first->second;
212 bool RemoveWatcher(const std::string& nick, LocalUser* user, WatchedList& watchedlist)
214 NickHash::iterator it = nicks.find(nick);
215 // If nobody is watching this nick the user trying to remove it isn't watching it for sure
216 if (it == nicks.end())
219 Entry& entry = it->second;
220 // Erase from the user's list of watched nicks
221 if (!stdalgo::vector::swaperase(watchedlist, &entry))
222 return false; // User is not watching this nick
224 // Erase from the nick's list of watching users
225 stdalgo::vector::swaperase(entry.watchers, user);
227 // If nobody else is watching the nick remove map entry
228 if (entry.watchers.empty())
234 WatchedList* GetWatchedPriv(LocalUser* user, bool create = false)
236 ExtData* extdata = ext.get(user, create);
239 return &extdata->list;
244 WatchedList emptywatchedlist;
247 void IRCv3::Monitor::Manager::ExtItem::FromInternal(Extensible* container, const std::string& value)
249 irc::spacesepstream ss(value);
250 for (std::string nick; ss.GetToken(nick); )
251 manager.Watch(static_cast<LocalUser*>(container), nick, UINT_MAX);
254 #ifndef INSPIRCD_MONITOR_MANAGER_ONLY
259 RPL_MONOFFLINE = 731,
261 RPL_ENDOFMONLIST = 733,
262 ERR_MONLISTFULL = 734
265 class CommandMonitor : public SplitCommand
267 typedef Numeric::Builder<> ReplyBuilder;
268 // Additional penalty for the /MONITOR L and /MONITOR S commands that request a list from the server
269 static const unsigned int ListPenalty = 3000;
271 IRCv3::Monitor::Manager& manager;
273 void HandlePlus(LocalUser* user, const std::string& input)
275 ReplyBuilder online(user, RPL_MONONLINE);
276 ReplyBuilder offline(user, RPL_MONOFFLINE);
277 irc::commasepstream ss(input);
278 for (std::string nick; ss.GetToken(nick); )
280 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxmonitor);
281 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
283 // List is full, send error which includes the remaining nicks that were not processed
284 user->WriteNumeric(ERR_MONLISTFULL, maxmonitor, InspIRCd::Format("%s%s%s", nick.c_str(), (ss.StreamEnd() ? "" : ","), ss.GetRemaining().c_str()), "Monitor list is full");
287 else if (result != IRCv3::Monitor::Manager::WR_OK)
288 continue; // Already added or invalid nick
290 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(nick) ? online : offline);
298 void HandleMinus(LocalUser* user, const std::string& input)
300 irc::commasepstream ss(input);
301 for (std::string nick; ss.GetToken(nick); )
302 manager.Unwatch(user, nick);
306 unsigned int maxmonitor;
308 CommandMonitor(Module* mod, IRCv3::Monitor::Manager& managerref)
309 : SplitCommand(mod, "MONITOR", 1)
310 , manager(managerref)
313 allow_empty_last_param = false;
314 syntax = "C|L|S|(+|-) <nick>[,<nick>]+";
317 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
319 char subcmd = toupper(parameters[0][0]);
322 if (parameters.size() > 1)
323 HandlePlus(user, parameters[1]);
325 else if (subcmd == '-')
327 if (parameters.size() > 1)
328 HandleMinus(user, parameters[1]);
330 else if (subcmd == 'C')
332 manager.UnwatchAll(user);
334 else if (subcmd == 'L')
336 user->CommandFloodPenalty += ListPenalty;
337 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
338 ReplyBuilder out(user, RPL_MONLIST);
339 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
341 IRCv3::Monitor::Entry* entry = *i;
342 out.Add(entry->GetNick());
345 user->WriteNumeric(RPL_ENDOFMONLIST, "End of MONITOR list");
347 else if (subcmd == 'S')
349 user->CommandFloodPenalty += ListPenalty;
351 ReplyBuilder online(user, RPL_MONONLINE);
352 ReplyBuilder offline(user, RPL_MONOFFLINE);
354 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
355 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
357 IRCv3::Monitor::Entry* entry = *i;
358 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(entry->GetNick()) ? online : offline);
359 out.Add(entry->GetNick());
372 class ModuleMonitor : public Module
374 IRCv3::Monitor::Manager manager;
377 void SendAlert(unsigned int numeric, const std::string& nick)
379 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
383 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
385 LocalUser* curr = *i;
386 curr->WriteNumeric(numeric, nick);
392 : manager(this, "monitor")
397 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
399 ConfigTag* tag = ServerInstance->Config->ConfValue("monitor");
400 cmd.maxmonitor = tag->getUInt("maxentries", 30, 1);
403 void OnPostConnect(User* user) CXX11_OVERRIDE
405 SendAlert(RPL_MONONLINE, user->nick);
408 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
410 // Detect and ignore nickname case change
411 if (ServerInstance->FindNickOnly(oldnick) == user)
414 SendAlert(RPL_MONOFFLINE, oldnick);
415 SendAlert(RPL_MONONLINE, user->nick);
418 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
420 LocalUser* localuser = IS_LOCAL(user);
422 manager.UnwatchAll(localuser);
423 SendAlert(RPL_MONOFFLINE, user->nick);
426 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
428 tokens["MONITOR"] = ConvToStr(cmd.maxmonitor);
431 Version GetVersion() CXX11_OVERRIDE
433 return Version("Provides MONITOR support", VF_VENDOR);
437 MODULE_INIT(ModuleMonitor)