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(container, unset_raw(container));
85 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
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) CXX11_OVERRIDE;
104 void free(Extensible* container, void* item) CXX11_OVERRIDE
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 // inline is needed in static builds to support m_watch including the Manager code from this file
249 inline void IRCv3::Monitor::Manager::ExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
251 if (format == FORMAT_NETWORK)
254 irc::spacesepstream ss(value);
255 for (std::string nick; ss.GetToken(nick); )
256 manager.Watch(static_cast<LocalUser*>(container), nick, UINT_MAX);
259 #ifndef INSPIRCD_MONITOR_MANAGER_ONLY
264 RPL_MONOFFLINE = 731,
266 RPL_ENDOFMONLIST = 733,
267 ERR_MONLISTFULL = 734
270 class CommandMonitor : public SplitCommand
272 typedef Numeric::Builder<> ReplyBuilder;
273 // Additional penalty for the /MONITOR L and /MONITOR S commands that request a list from the server
274 static const unsigned int ListPenalty = 3000;
276 IRCv3::Monitor::Manager& manager;
278 void HandlePlus(LocalUser* user, const std::string& input)
280 ReplyBuilder online(user, RPL_MONONLINE);
281 ReplyBuilder offline(user, RPL_MONOFFLINE);
282 irc::commasepstream ss(input);
283 for (std::string nick; ss.GetToken(nick); )
285 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxmonitor);
286 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
288 // List is full, send error which includes the remaining nicks that were not processed
289 user->WriteNumeric(ERR_MONLISTFULL, maxmonitor, InspIRCd::Format("%s%s%s", nick.c_str(), (ss.StreamEnd() ? "" : ","), ss.GetRemaining().c_str()), "Monitor list is full");
292 else if (result != IRCv3::Monitor::Manager::WR_OK)
293 continue; // Already added or invalid nick
295 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(nick) ? online : offline);
303 void HandleMinus(LocalUser* user, const std::string& input)
305 irc::commasepstream ss(input);
306 for (std::string nick; ss.GetToken(nick); )
307 manager.Unwatch(user, nick);
311 unsigned int maxmonitor;
313 CommandMonitor(Module* mod, IRCv3::Monitor::Manager& managerref)
314 : SplitCommand(mod, "MONITOR", 1)
315 , manager(managerref)
318 allow_empty_last_param = false;
319 syntax = "[C|L|S|+ <nick1>[,<nick2>]|- <nick1>[,<nick2>]";
322 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
324 char subcmd = toupper(parameters[0][0]);
327 if (parameters.size() > 1)
328 HandlePlus(user, parameters[1]);
330 else if (subcmd == '-')
332 if (parameters.size() > 1)
333 HandleMinus(user, parameters[1]);
335 else if (subcmd == 'C')
337 manager.UnwatchAll(user);
339 else if (subcmd == 'L')
341 user->CommandFloodPenalty += ListPenalty;
342 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
343 ReplyBuilder out(user, RPL_MONLIST);
344 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
346 IRCv3::Monitor::Entry* entry = *i;
347 out.Add(entry->GetNick());
350 user->WriteNumeric(RPL_ENDOFMONLIST, "End of MONITOR list");
352 else if (subcmd == 'S')
354 user->CommandFloodPenalty += ListPenalty;
356 ReplyBuilder online(user, RPL_MONONLINE);
357 ReplyBuilder offline(user, RPL_MONOFFLINE);
359 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
360 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
362 IRCv3::Monitor::Entry* entry = *i;
363 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(entry->GetNick()) ? online : offline);
364 out.Add(entry->GetNick());
377 class ModuleMonitor : public Module
379 IRCv3::Monitor::Manager manager;
382 void SendAlert(unsigned int numeric, const std::string& nick)
384 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
388 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
390 LocalUser* curr = *i;
391 curr->WriteNumeric(numeric, nick);
397 : manager(this, "monitor")
402 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
404 ConfigTag* tag = ServerInstance->Config->ConfValue("monitor");
405 cmd.maxmonitor = tag->getUInt("maxentries", 30, 1);
408 void OnPostConnect(User* user) CXX11_OVERRIDE
410 SendAlert(RPL_MONONLINE, user->nick);
413 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
415 // Detect and ignore nickname case change
416 if (ServerInstance->FindNickOnly(oldnick) == user)
419 SendAlert(RPL_MONOFFLINE, oldnick);
420 SendAlert(RPL_MONONLINE, user->nick);
423 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
425 LocalUser* localuser = IS_LOCAL(user);
427 manager.UnwatchAll(localuser);
428 SendAlert(RPL_MONOFFLINE, user->nick);
431 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
433 tokens["MONITOR"] = ConvToStr(cmd.maxmonitor);
436 Version GetVersion() CXX11_OVERRIDE
438 return Version("Provides MONITOR support", VF_VENDOR);
442 MODULE_INIT(ModuleMonitor)