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,
40 ERR_INVALIDWATCHNICK = 942
43 class CommandWatch : public SplitCommand
45 // Additional penalty for /WATCH commands that request a list from the server
46 static const unsigned int ListPenalty = 4000;
48 IRCv3::Monitor::Manager& manager;
50 static void SendOnlineOffline(LocalUser* user, const std::string& nick, bool show_offline = true)
52 User* target = IRCv3::Monitor::Manager::FindNick(nick);
55 // 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
57 user->WriteNumeric(RPL_NOWISAWAY, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->awaytime, "is away");
59 user->WriteNumeric(RPL_NOWON, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->age, "is online");
61 else if (show_offline)
62 user->WriteNumeric(RPL_NOWOFF, nick, "*", "*", "0", "is offline");
65 void HandlePlus(LocalUser* user, const std::string& nick)
67 IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxwatch);
68 if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
70 // List is full, send error numeric
71 user->WriteNumeric(ERR_TOOMANYWATCH, nick, "Too many WATCH entries");
74 else if (result == IRCv3::Monitor::Manager::WR_INVALIDNICK)
76 user->WriteNumeric(ERR_INVALIDWATCHNICK, nick, "Invalid nickname");
79 else if (result != IRCv3::Monitor::Manager::WR_OK)
82 SendOnlineOffline(user, nick);
85 void HandleMinus(LocalUser* user, const std::string& nick)
87 if (!manager.Unwatch(user, nick))
90 User* target = IRCv3::Monitor::Manager::FindNick(nick);
92 user->WriteNumeric(RPL_WATCHOFF, target->nick, target->ident, target->GetDisplayedHost(), (unsigned long)target->age, "stopped watching");
94 user->WriteNumeric(RPL_WATCHOFF, nick, "*", "*", "0", "stopped watching");
97 void HandleList(LocalUser* user, bool show_offline)
99 user->CommandFloodPenalty += ListPenalty;
100 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
101 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
103 const IRCv3::Monitor::Entry* entry = *i;
104 SendOnlineOffline(user, entry->GetNick(), show_offline);
106 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH list");
109 void HandleStats(LocalUser* user)
111 user->CommandFloodPenalty += ListPenalty;
113 // Do not show how many clients are watching this nick, it's pointless
114 const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
115 user->WriteNumeric(RPL_WATCHSTAT, InspIRCd::Format("You have %lu and are on 0 WATCH entries", (unsigned long)list.size()));
117 Numeric::Builder<' '> out(user, RPL_WATCHLIST);
118 for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
120 const IRCv3::Monitor::Entry* entry = *i;
121 out.Add(entry->GetNick());
124 user->WriteNumeric(RPL_ENDOFWATCHLIST, "End of WATCH S");
128 unsigned int maxwatch;
130 CommandWatch(Module* mod, IRCv3::Monitor::Manager& managerref)
131 : SplitCommand(mod, "WATCH")
132 , manager(managerref)
134 allow_empty_last_param = false;
135 syntax = "[<C|L|S|l|+<nick1>|-<nick>>]";
138 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
140 if (parameters.empty())
142 HandleList(user, false);
146 bool watch_l_done = false;
147 bool watch_s_done = false;
149 for (std::vector<std::string>::const_iterator i = parameters.begin(); i != parameters.end(); ++i)
151 const std::string& token = *i;
152 char subcmd = toupper(token[0]);
155 HandlePlus(user, token.substr(1));
157 else if (subcmd == '-')
159 HandleMinus(user, token.substr(1));
161 else if (subcmd == 'C')
163 manager.UnwatchAll(user);
165 else if ((subcmd == 'L') && (!watch_l_done))
168 // WATCH L requests a full list with online and offline nicks
169 // WATCH l requests a list with only online nicks
170 HandleList(user, (token[0] == 'L'));
172 else if ((subcmd == 'S') && (!watch_s_done))
182 class ModuleWatch : public Module
184 IRCv3::Monitor::Manager manager;
187 void SendAlert(User* user, const std::string& nick, unsigned int numeric, const char* numerictext, time_t shownts)
189 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
193 Numeric::Numeric num(numeric);
194 num.push(nick).push(user->ident).push(user->GetDisplayedHost()).push(ConvToStr(shownts)).push(numerictext);
195 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
197 LocalUser* curr = *i;
198 curr->WriteNumeric(num);
202 void Online(User* user)
204 SendAlert(user, user->nick, RPL_LOGON, "arrived online", user->age);
207 void Offline(User* user, const std::string& nick)
209 SendAlert(user, nick, RPL_LOGOFF, "went offline", user->age);
214 : manager(this, "watch")
219 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
221 ConfigTag* tag = ServerInstance->Config->ConfValue("watch");
222 cmd.maxwatch = tag->getUInt("maxwatch", 30, 1);
225 void OnPostConnect(User* user) CXX11_OVERRIDE
230 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
232 // Detect and ignore nickname case change
233 if (ServerInstance->FindNickOnly(oldnick) == user)
236 Offline(user, oldnick);
240 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
242 LocalUser* localuser = IS_LOCAL(user);
244 manager.UnwatchAll(localuser);
245 Offline(user, user->nick);
248 ModResult OnSetAway(User* user, const std::string& awaymsg) CXX11_OVERRIDE
251 SendAlert(user, user->nick, RPL_NOTAWAY, "is no longer away", ServerInstance->Time());
253 SendAlert(user, user->nick, RPL_GONEAWAY, awaymsg.c_str(), user->awaytime);
255 return MOD_RES_PASSTHRU;
258 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
260 tokens["WATCH"] = ConvToStr(cmd.maxwatch);
263 Version GetVersion() CXX11_OVERRIDE
265 return Version("Provides WATCH support", VF_VENDOR);
269 MODULE_INIT(ModuleWatch)