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, "%s %s %s %lu :is away", target->nick.c_str(), target->ident.c_str(), target->dhost.c_str(), (unsigned long)target->awaytime);
58 user->WriteNumeric(RPL_NOWON, "%s %s %s %lu :is online", target->nick.c_str(), target->ident.c_str(), target->dhost.c_str(), (unsigned long)target->age);
60 else if (show_offline)
61 user->WriteNumeric(RPL_NOWOFF, "%s * * 0 :is offline", nick.c_str());
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, "%s :Too many WATCH entries", nick.c_str());
73 else if (result == IRCv3::Monitor::Manager::WR_INVALIDNICK)
75 user->WriteNumeric(942, "%s :Invalid nickname", nick.c_str());
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, "%s %s %s %lu :stopped watching", target->nick.c_str(), target->ident.c_str(), target->dhost.c_str(), (unsigned long)target->age);
93 user->WriteNumeric(RPL_WATCHOFF, "%s * * 0 :stopped watching", nick.c_str());
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, ":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 std::string text = InspIRCd::Format("%s %s %s %lu :%s", nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) shownts, numerictext);
193 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
195 LocalUser* curr = *i;
196 curr->WriteNumeric(numeric, text);
200 void Online(User* user)
202 SendAlert(user, user->nick, RPL_LOGON, "arrived online", user->age);
205 void Offline(User* user, const std::string& nick)
207 SendAlert(user, nick, RPL_LOGOFF, "went offline", user->age);
212 : manager(this, "watch")
217 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
219 ConfigTag* tag = ServerInstance->Config->ConfValue("watch");
220 cmd.maxwatch = tag->getInt("maxwatch", 30, 1);
223 void OnPostConnect(User* user) CXX11_OVERRIDE
228 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
230 // Detect and ignore nickname case change
231 if (ServerInstance->FindNickOnly(oldnick) == user)
234 Offline(user, oldnick);
238 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
240 LocalUser* localuser = IS_LOCAL(user);
242 manager.UnwatchAll(localuser);
243 Offline(user, user->nick);
246 ModResult OnSetAway(User* user, const std::string& awaymsg) CXX11_OVERRIDE
249 SendAlert(user, user->nick, RPL_NOTAWAY, "is no longer away", ServerInstance->Time());
251 SendAlert(user, user->nick, RPL_GONEAWAY, awaymsg.c_str(), user->awaytime);
253 return MOD_RES_PASSTHRU;
256 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
258 tokens["WATCH"] = ConvToStr(cmd.maxwatch);
261 Version GetVersion() CXX11_OVERRIDE
263 return Version("Provides WATCH support", VF_VENDOR);
267 MODULE_INIT(ModuleWatch)