2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Robby <robby@chatbelgie.be>
5 * Copyright (C) 2017-2018 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/>.
23 #include "modules/away.h"
25 #define INSPIRCD_MONITOR_MANAGER_ONLY
26 #include "m_monitor.cpp"
39 RPL_ENDOFWATCHLIST = 607,
40 // RPL_CLEARWATCH = 608, // unused
42 ERR_TOOMANYWATCH = 512,
43 ERR_INVALIDWATCHNICK = 942
46 class CommandWatch : public SplitCommand
48 // Additional penalty for /WATCH commands that request a list from the server
49 static const unsigned int ListPenalty = 4000;
51 IRCv3::Monitor::Manager& manager;
53 static void SendOnlineOffline(LocalUser* user, const std::string& nick, bool show_offline = true)
55 User* target = IRCv3::Monitor::Manager::FindNick(nick);
58 // 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
60 user->WriteNumeric(RPL_NOWISAWAY, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->awaytime, "is away");
62 user->WriteNumeric(RPL_NOWON, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->age, "is online");
64 else if (show_offline)
65 user->WriteNumeric(RPL_NOWOFF, nick, "*", "*", "0", "is offline");
68 void HandlePlus(LocalUser* user, const std::string& nick)
70 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxwatch);
71 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
73 // List is full, send error numeric
74 user->WriteNumeric(ERR_TOOMANYWATCH, nick, "Too many WATCH entries");
77 else if (result == IRCv3::Monitor::Manager::WR_INVALIDNICK)
79 user->WriteNumeric(ERR_INVALIDWATCHNICK, nick, "Invalid nickname");
82 else if (result != IRCv3::Monitor::Manager::WR_OK)
85 SendOnlineOffline(user, nick);
88 void HandleMinus(LocalUser* user, const std::string& nick)
90 if (!manager.Unwatch(user, nick))
93 User* target = IRCv3::Monitor::Manager::FindNick(nick);
95 user->WriteNumeric(RPL_WATCHOFF, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->age, "stopped watching");
97 user->WriteNumeric(RPL_WATCHOFF, nick, "*", "*", "0", "stopped watching");
100 void HandleList(LocalUser* user, bool show_offline)
102 user->CommandFloodPenalty += ListPenalty;
103 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
104 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
106 const IRCv3::Monitor::Entry* entry = *i;
107 SendOnlineOffline(user, entry->GetNick(), show_offline);
109 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH list");
112 void HandleStats(LocalUser* user)
114 user->CommandFloodPenalty += ListPenalty;
116 // Do not show how many clients are watching this nick, it's pointless
117 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
118 user->WriteNumeric(RPL_WATCHSTAT, InspIRCd::Format("You have %lu and are on 0 WATCH entries", (unsigned long)list.size()));
120 Numeric::Builder<' '> out(user, RPL_WATCHLIST);
121 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
123 const IRCv3::Monitor::Entry* entry = *i;
124 out.Add(entry->GetNick());
127 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH S");
131 unsigned int maxwatch;
133 CommandWatch(Module* mod, IRCv3::Monitor::Manager& managerref)
134 : SplitCommand(mod, "WATCH")
135 , manager(managerref)
137 allow_empty_last_param = false;
138 syntax = "C|L|l|S|(+|-)<nick> [(+|-)<nick>]+";
141 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
143 if (parameters.empty())
145 HandleList(user, false);
149 bool watch_l_done = false;
150 bool watch_s_done = false;
152 for (std::vector<std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
154 const std::string& token = *i;
155 char subcmd = toupper(token[0]);
158 HandlePlus(user, token.substr(1));
160 else if (subcmd == '-')
162 HandleMinus(user, token.substr(1));
164 else if (subcmd == 'C')
166 manager.UnwatchAll(user);
168 else if ((subcmd == 'L') && (!watch_l_done))
171 // WATCH L requests a full list with online and offline nicks
172 // WATCH l requests a list with only online nicks
173 HandleList(user, (token[0] == 'L'));
175 else if ((subcmd == 'S') && (!watch_s_done))
187 , public Away::EventListener
189 IRCv3::Monitor::Manager manager;
192 void SendAlert(User* user, const std::string& nick, unsigned int numeric, const char* numerictext, time_t shownts)
194 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
198 Numeric::Numeric num(numeric);
199 num.push(nick).push(user->ident).push(user->GetDisplayedHost()).push(ConvToStr(shownts)).push(numerictext);
200 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
202 LocalUser* curr = *i;
203 curr->WriteNumeric(num);
207 void Online(User* user)
209 SendAlert(user, user->nick, RPL_LOGON, "arrived online", user->age);
212 void Offline(User* user, const std::string& nick)
214 SendAlert(user, nick, RPL_LOGOFF, "went offline", user->age);
219 : Away::EventListener(this)
220 , manager(this, "watch")
225 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
227 ConfigTag* tag = ServerInstance->Config->ConfValue("watch");
228 cmd.maxwatch = tag->getUInt("maxwatch", 30, 1);
231 void OnPostConnect(User* user) CXX11_OVERRIDE
236 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
238 // Detect and ignore nickname case change
239 if (ServerInstance->FindNickOnly(oldnick) == user)
242 Offline(user, oldnick);
246 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
248 LocalUser* localuser = IS_LOCAL(user);
250 manager.UnwatchAll(localuser);
251 Offline(user, user->nick);
254 void OnUserAway(User* user) CXX11_OVERRIDE
256 SendAlert(user, user->nick, RPL_GONEAWAY, user->awaymsg.c_str(), user->awaytime);
259 void OnUserBack(User* user) CXX11_OVERRIDE
261 SendAlert(user, user->nick, RPL_NOTAWAY, "is no longer away", ServerInstance->Time());
264 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
266 tokens["WATCH"] = ConvToStr(cmd.maxwatch);
269 Version GetVersion() CXX11_OVERRIDE
271 return Version("Adds the /WATCH command which allows users to find out when their friends are connected to the server.", VF_VENDOR);
275 MODULE_INIT(ModuleWatch)