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/>.
21 #include "modules/away.h"
23 #define INSPIRCD_MONITOR_MANAGER_ONLY
24 #include "m_monitor.cpp"
37 RPL_ENDOFWATCHLIST = 607,
38 // RPL_CLEARWATCH = 608, // unused
40 ERR_TOOMANYWATCH = 512,
41 ERR_INVALIDWATCHNICK = 942
44 class CommandWatch : public SplitCommand
46 // Additional penalty for /WATCH commands that request a list from the server
47 static const unsigned int ListPenalty = 4000;
49 IRCv3::Monitor::Manager& manager;
51 static void SendOnlineOffline(LocalUser* user, const std::string& nick, bool show_offline = true)
53 User* target = IRCv3::Monitor::Manager::FindNick(nick);
56 // The away state should only be sent if the client requests away notifications for a nick but 2.0 always sends them so we do that too
58 user->WriteNumeric(RPL_NOWISAWAY, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->awaytime, "is away");
60 user->WriteNumeric(RPL_NOWON, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->age, "is online");
62 else if (show_offline)
63 user->WriteNumeric(RPL_NOWOFF, nick, "*", "*", "0", "is offline");
66 void HandlePlus(LocalUser* user, const std::string& nick)
68 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxwatch);
69 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
71 // List is full, send error numeric
72 user->WriteNumeric(ERR_TOOMANYWATCH, nick, "Too many WATCH entries");
75 else if (result == IRCv3::Monitor::Manager::WR_INVALIDNICK)
77 user->WriteNumeric(ERR_INVALIDWATCHNICK, nick, "Invalid nickname");
80 else if (result != IRCv3::Monitor::Manager::WR_OK)
83 SendOnlineOffline(user, nick);
86 void HandleMinus(LocalUser* user, const std::string& nick)
88 if (!manager.Unwatch(user, nick))
91 User* target = IRCv3::Monitor::Manager::FindNick(nick);
93 user->WriteNumeric(RPL_WATCHOFF, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->age, "stopped watching");
95 user->WriteNumeric(RPL_WATCHOFF, nick, "*", "*", "0", "stopped watching");
98 void HandleList(LocalUser* user, bool show_offline)
100 user->CommandFloodPenalty += ListPenalty;
101 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
102 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
104 const IRCv3::Monitor::Entry* entry = *i;
105 SendOnlineOffline(user, entry->GetNick(), show_offline);
107 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH list");
110 void HandleStats(LocalUser* user)
112 user->CommandFloodPenalty += ListPenalty;
114 // Do not show how many clients are watching this nick, it's pointless
115 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
116 user->WriteNumeric(RPL_WATCHSTAT, InspIRCd::Format("You have %lu and are on 0 WATCH entries", (unsigned long)list.size()));
118 Numeric::Builder<' '> out(user, RPL_WATCHLIST);
119 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
121 const IRCv3::Monitor::Entry* entry = *i;
122 out.Add(entry->GetNick());
125 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH S");
129 unsigned int maxwatch;
131 CommandWatch(Module* mod, IRCv3::Monitor::Manager& managerref)
132 : SplitCommand(mod, "WATCH")
133 , manager(managerref)
135 allow_empty_last_param = false;
136 syntax = "[<C|L|S|l|+<nick1>|-<nick>>]";
139 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
141 if (parameters.empty())
143 HandleList(user, false);
147 bool watch_l_done = false;
148 bool watch_s_done = false;
150 for (std::vector<std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
152 const std::string& token = *i;
153 char subcmd = toupper(token[0]);
156 HandlePlus(user, token.substr(1));
158 else if (subcmd == '-')
160 HandleMinus(user, token.substr(1));
162 else if (subcmd == 'C')
164 manager.UnwatchAll(user);
166 else if ((subcmd == 'L') && (!watch_l_done))
169 // WATCH L requests a full list with online and offline nicks
170 // WATCH l requests a list with only online nicks
171 HandleList(user, (token[0] == 'L'));
173 else if ((subcmd == 'S') && (!watch_s_done))
185 , public Away::EventListener
187 IRCv3::Monitor::Manager manager;
190 void SendAlert(User* user, const std::string& nick, unsigned int numeric, const char* numerictext, time_t shownts)
192 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
196 Numeric::Numeric num(numeric);
197 num.push(nick).push(user->ident).push(user->GetDisplayedHost()).push(ConvToStr(shownts)).push(numerictext);
198 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
200 LocalUser* curr = *i;
201 curr->WriteNumeric(num);
205 void Online(User* user)
207 SendAlert(user, user->nick, RPL_LOGON, "arrived online", user->age);
210 void Offline(User* user, const std::string& nick)
212 SendAlert(user, nick, RPL_LOGOFF, "went offline", user->age);
217 : Away::EventListener(this)
218 , manager(this, "watch")
223 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
225 ConfigTag* tag = ServerInstance->Config->ConfValue("watch");
226 cmd.maxwatch = tag->getUInt("maxwatch", 30, 1);
229 void OnPostConnect(User* user) CXX11_OVERRIDE
234 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
236 // Detect and ignore nickname case change
237 if (ServerInstance->FindNickOnly(oldnick) == user)
240 Offline(user, oldnick);
244 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
246 LocalUser* localuser = IS_LOCAL(user);
248 manager.UnwatchAll(localuser);
249 Offline(user, user->nick);
252 void OnUserAway(User* user) CXX11_OVERRIDE
254 SendAlert(user, user->nick, RPL_GONEAWAY, user->awaymsg.c_str(), user->awaytime);
257 void OnUserBack(User* user) CXX11_OVERRIDE
259 SendAlert(user, user->nick, RPL_NOTAWAY, "is no longer away", ServerInstance->Time());
262 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
264 tokens["WATCH"] = ConvToStr(cmd.maxwatch);
267 Version GetVersion() CXX11_OVERRIDE
269 return Version("Provides WATCH support", VF_VENDOR);
273 MODULE_INIT(ModuleWatch)