2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
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/>.
29 class ManagerInternal;
31 typedef std::vector<Entry*> WatchedList;
32 typedef std::vector<LocalUser*> WatcherList;
36 struct IRCv3::Monitor::Entry
41 void SetNick(const std::string& Nick)
44 // We may show this string to other users so do not leak the casing
45 std::transform(Nick.begin(), Nick.end(), std::back_inserter(nick), ::tolower);
48 const std::string& GetNick() const { return nick; }
51 class IRCv3::Monitor::Manager
58 class ExtItem : public ExtensionItem
63 ExtItem(Module* mod, const std::string& extname, Manager& managerref)
64 : ExtensionItem(extname, ExtensionItem::EXT_USER, mod)
69 ExtData* get(Extensible* container, bool create = false)
71 ExtData* extdata = static_cast<ExtData*>(get_raw(container));
72 if ((!extdata) && (create))
74 extdata = new ExtData;
75 set_raw(container, extdata);
80 void unset(Extensible* container)
82 free(unset_raw(container));
85 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
88 if (format == FORMAT_NETWORK)
91 const ExtData* extdata = static_cast<ExtData*>(item);
92 for (WatchedList::const_iterator i = extdata->list.begin(); i != extdata->list.end(); ++i)
94 const Entry* entry = *i;
95 ret.append(entry->GetNick()).push_back(' ');
98 ret.erase(ret.size()-1);
102 void unserialize(SerializeFormat format, Extensible* container, const std::string& value);
104 void free(void* item)
106 delete static_cast<ExtData*>(item);
111 Manager(Module* mod, const std::string& extname)
112 : ext(mod, extname, *this)
124 WatchResult Watch(LocalUser* user, const std::string& nick, unsigned int maxwatch)
126 if (!ServerInstance->IsNick(nick))
127 return WR_INVALIDNICK;
129 WatchedList* watched = GetWatchedPriv(user, true);
130 if (watched->size() >= maxwatch)
133 Entry* entry = AddWatcher(nick, user);
134 if (stdalgo::isin(*watched, entry))
135 return WR_ALREADYWATCHING;
137 entry->watchers.push_back(user);
138 watched->push_back(entry);
142 bool Unwatch(LocalUser* user, const std::string& nick)
144 WatchedList* list = GetWatchedPriv(user);
148 bool ret = RemoveWatcher(nick, user, *list);
149 // If no longer watching any nick unset ext
155 const WatchedList& GetWatched(LocalUser* user)
157 WatchedList* list = GetWatchedPriv(user);
160 return emptywatchedlist;
163 void UnwatchAll(LocalUser* user)
165 WatchedList* list = GetWatchedPriv(user);
169 while (!list->empty())
171 Entry* entry = list->front();
172 RemoveWatcher(entry->GetNick(), user, *list);
177 WatcherList* GetWatcherList(const std::string& nick)
179 Entry* entry = Find(nick);
181 return &entry->watchers;
185 static User* FindNick(const std::string& nick)
187 User* user = ServerInstance->FindNickOnly(nick);
188 if ((user) && (user->registered == REG_ALL))
194 typedef TR1NS::unordered_map<std::string, Entry, irc::insensitive, irc::StrHashComp> NickHash;
196 Entry* Find(const std::string& nick)
198 NickHash::iterator it = nicks.find(nick);
199 if (it != nicks.end())
204 Entry* AddWatcher(const std::string& nick, LocalUser* user)
206 std::pair<NickHash::iterator, bool> ret = nicks.insert(std::make_pair(nick, Entry()));
207 Entry& entry = ret.first->second;
213 bool RemoveWatcher(const std::string& nick, LocalUser* user, WatchedList& watchedlist)
215 NickHash::iterator it = nicks.find(nick);
216 // If nobody is watching this nick the user trying to remove it isn't watching it for sure
217 if (it == nicks.end())
220 Entry& entry = it->second;
221 // Erase from the user's list of watched nicks
222 if (!stdalgo::vector::swaperase(watchedlist, &entry))
223 return false; // User is not watching this nick
225 // Erase from the nick's list of watching users
226 stdalgo::vector::swaperase(entry.watchers, user);
228 // If nobody else is watching the nick remove map entry
229 if (entry.watchers.empty())
235 WatchedList* GetWatchedPriv(LocalUser* user, bool create = false)
237 ExtData* extdata = ext.get(user, create);
240 return &extdata->list;
245 WatchedList emptywatchedlist;
248 void IRCv3::Monitor::Manager::ExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
250 if (format == FORMAT_NETWORK)
253 irc::spacesepstream ss(value);
254 for (std::string nick; ss.GetToken(nick); )
255 manager.Watch(static_cast<LocalUser*>(container), nick, UINT_MAX);
258 #ifndef INSPIRCD_MONITOR_MANAGER_ONLY
263 RPL_MONOFFLINE = 731,
265 RPL_ENDOFMONLIST = 733,
266 ERR_MONLISTFULL = 734
269 class CommandMonitor : public SplitCommand
271 typedef Numeric::Builder<> ReplyBuilder;
272 // Additional penalty for the /MONITOR L and /MONITOR S commands that request a list from the server
273 static const unsigned int ListPenalty = 3000;
275 IRCv3::Monitor::Manager& manager;
277 void HandlePlus(LocalUser* user, const std::string& input)
279 ReplyBuilder online(user, RPL_MONONLINE);
280 ReplyBuilder offline(user, RPL_MONOFFLINE);
281 irc::commasepstream ss(input);
282 for (std::string nick; ss.GetToken(nick); )
284 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxmonitor);
285 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
287 // List is full, send error which includes the remaining nicks that were not processed
288 user->WriteNumeric(ERR_MONLISTFULL, maxmonitor, InspIRCd::Format("%s%s%s", nick.c_str(), (ss.StreamEnd() ? "" : ","), ss.GetRemaining().c_str()), "Monitor list is full");
291 else if (result != IRCv3::Monitor::Manager::WR_OK)
292 continue; // Already added or invalid nick
294 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(nick) ? online : offline);
302 void HandleMinus(LocalUser* user, const std::string& input)
304 irc::commasepstream ss(input);
305 for (std::string nick; ss.GetToken(nick); )
306 manager.Unwatch(user, nick);
310 unsigned int maxmonitor;
312 CommandMonitor(Module* mod, IRCv3::Monitor::Manager& managerref)
313 : SplitCommand(mod, "MONITOR", 1)
314 , manager(managerref)
317 allow_empty_last_param = false;
318 syntax = "[C|L|S|+ <nick1>[,<nick2>]|- <nick1>[,<nick2>]";
321 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
323 char subcmd = toupper(parameters[0][0]);
326 if (parameters.size() > 1)
327 HandlePlus(user, parameters[1]);
329 else if (subcmd == '-')
331 if (parameters.size() > 1)
332 HandleMinus(user, parameters[1]);
334 else if (subcmd == 'C')
336 manager.UnwatchAll(user);
338 else if (subcmd == 'L')
340 user->CommandFloodPenalty += ListPenalty;
341 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
342 ReplyBuilder out(user, RPL_MONLIST);
343 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
345 IRCv3::Monitor::Entry* entry = *i;
346 out.Add(entry->GetNick());
349 user->WriteNumeric(RPL_ENDOFMONLIST, "End of MONITOR list");
351 else if (subcmd == 'S')
353 user->CommandFloodPenalty += ListPenalty;
355 ReplyBuilder online(user, RPL_MONONLINE);
356 ReplyBuilder offline(user, RPL_MONOFFLINE);
358 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
359 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
361 IRCv3::Monitor::Entry* entry = *i;
362 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(entry->GetNick()) ? online : offline);
363 out.Add(entry->GetNick());
376 class ModuleMonitor : public Module
378 IRCv3::Monitor::Manager manager;
381 void SendAlert(unsigned int numeric, const std::string& nick)
383 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
387 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
389 LocalUser* curr = *i;
390 curr->WriteNumeric(numeric, nick);
396 : manager(this, "monitor")
401 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
403 ConfigTag* tag = ServerInstance->Config->ConfValue("monitor");
404 cmd.maxmonitor = tag->getInt("maxentries", 30, 1);
407 void OnPostConnect(User* user) CXX11_OVERRIDE
409 SendAlert(RPL_MONONLINE, user->nick);
412 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
414 // Detect and ignore nickname case change
415 if (ServerInstance->FindNickOnly(oldnick) == user)
418 SendAlert(RPL_MONOFFLINE, oldnick);
419 SendAlert(RPL_MONONLINE, user->nick);
422 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
424 LocalUser* localuser = IS_LOCAL(user);
426 manager.UnwatchAll(localuser);
427 SendAlert(RPL_MONOFFLINE, user->nick);
430 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
432 tokens["MONITOR"] = ConvToStr(cmd.maxmonitor);
435 Version GetVersion() CXX11_OVERRIDE
437 return Version("Provides MONITOR support", VF_VENDOR);
441 MODULE_INIT(ModuleMonitor)