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/>.
22 #define INSPIRCD_MONITOR_MANAGER_ONLY
23 #include "m_monitor.cpp"
36 RPL_ENDOFWATCHLIST = 607,
37 // RPL_CLEARWATCH = 608, // unused
39 ERR_TOOMANYWATCH = 512
42 class CommandWatch : public SplitCommand
44 // Additional penalty for /WATCH commands that request a list from the server
45 static const unsigned int ListPenalty = 4000;
47 IRCv3::Monitor::Manager& manager;
49 static void SendOnlineOffline(LocalUser* user, const std::string& nick, bool show_offline = true)
51 User* target = IRCv3::Monitor::Manager::FindNick(nick);
54 // 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
56 user->WriteNumeric(RPL_NOWISAWAY, target->nick, target->ident, target->dhost, (unsigned long)target->awaytime, "is away");
58 user->WriteNumeric(RPL_NOWON, target->nick, target->ident, target->dhost, (unsigned long)target->age, "is online");
60 else if (show_offline)
61 user->WriteNumeric(RPL_NOWOFF, nick, "*", "*", "0", "is offline");
64 void HandlePlus(LocalUser* user, const std::string& nick)
66 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxwatch);
67 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
69 // List is full, send error numeric
70 user->WriteNumeric(ERR_TOOMANYWATCH, nick, "Too many WATCH entries");
73 else if (result == IRCv3::Monitor::Manager::WR_INVALIDNICK)
75 user->WriteNumeric(942, nick, "Invalid nickname");
78 else if (result != IRCv3::Monitor::Manager::WR_OK)
81 SendOnlineOffline(user, nick);
84 void HandleMinus(LocalUser* user, const std::string& nick)
86 if (!manager.Unwatch(user, nick))
89 User* target = IRCv3::Monitor::Manager::FindNick(nick);
91 user->WriteNumeric(RPL_WATCHOFF, target->nick, target->ident, target->dhost, (unsigned long)target->age, "stopped watching");
93 user->WriteNumeric(RPL_WATCHOFF, nick, "*", "*", "0", "stopped watching");
96 void HandleList(LocalUser* user, bool show_offline)
98 user->CommandFloodPenalty += ListPenalty;
99 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
100 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
102 const IRCv3::Monitor::Entry* entry = *i;
103 SendOnlineOffline(user, entry->GetNick(), show_offline);
105 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH list");
108 void HandleStats(LocalUser* user)
110 user->CommandFloodPenalty += ListPenalty;
112 // Do not show how many clients are watching this nick, it's pointless
113 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
114 user->WriteNumeric(RPL_WATCHSTAT, InspIRCd::Format("You have %lu and are on 0 WATCH entries", (unsigned long)list.size()));
116 Numeric::Builder<' '> out(user, RPL_WATCHLIST);
117 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
119 const IRCv3::Monitor::Entry* entry = *i;
120 out.Add(entry->GetNick());
123 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH S");
127 unsigned int maxwatch;
129 CommandWatch(Module* mod, IRCv3::Monitor::Manager& managerref)
130 : SplitCommand(mod, "WATCH")
131 , manager(managerref)
133 allow_empty_last_param = false;
134 syntax = "[<C|L|S|l|+<nick1>|-<nick>>]";
137 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
139 if (parameters.empty())
141 HandleList(user, false);
145 bool watch_l_done = false;
146 bool watch_s_done = false;
148 for (std::vector<std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
150 const std::string& token = *i;
151 char subcmd = toupper(token[0]);
154 HandlePlus(user, token.substr(1));
156 else if (subcmd == '-')
158 HandleMinus(user, token.substr(1));
160 else if (subcmd == 'C')
162 manager.UnwatchAll(user);
164 else if ((subcmd == 'L') && (!watch_l_done))
167 // WATCH L requests a full list with online and offline nicks
168 // WATCH l requests a list with only online nicks
169 HandleList(user, (token[0] == 'L'));
171 else if ((subcmd == 'S') && (!watch_s_done))
181 class ModuleWatch : public Module
183 IRCv3::Monitor::Manager manager;
186 void SendAlert(User* user, const std::string& nick, unsigned int numeric, const char* numerictext, time_t shownts)
188 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
192 Numeric::Numeric num(numeric);
193 num.push(nick).push(user->ident).push(user->dhost).push(ConvToStr(shownts)).push(numerictext);
194 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
196 LocalUser* curr = *i;
197 curr->WriteNumeric(num);
201 void Online(User* user)
203 SendAlert(user, user->nick, RPL_LOGON, "arrived online", user->age);
206 void Offline(User* user, const std::string& nick)
208 SendAlert(user, nick, RPL_LOGOFF, "went offline", user->age);
213 : manager(this, "watch")
218 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
220 ConfigTag* tag = ServerInstance->Config->ConfValue("watch");
221 cmd.maxwatch = tag->getInt("maxwatch", 30, 1);
224 void OnPostConnect(User* user) CXX11_OVERRIDE
229 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
231 // Detect and ignore nickname case change
232 if (ServerInstance->FindNickOnly(oldnick) == user)
235 Offline(user, oldnick);
239 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
241 LocalUser* localuser = IS_LOCAL(user);
243 manager.UnwatchAll(localuser);
244 Offline(user, user->nick);
247 ModResult OnSetAway(User* user, const std::string& awaymsg) CXX11_OVERRIDE
250 SendAlert(user, user->nick, RPL_NOTAWAY, "is no longer away", ServerInstance->Time());
252 SendAlert(user, user->nick, RPL_GONEAWAY, awaymsg.c_str(), user->awaytime);
254 return MOD_RES_PASSTHRU;
257 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
259 tokens["WATCH"] = ConvToStr(cmd.maxwatch);
262 Version GetVersion() CXX11_OVERRIDE
264 return Version("Provides WATCH support", VF_VENDOR);
268 MODULE_INIT(ModuleWatch)