]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_monitor.cpp
ea631b2e86830d6bf6ee31decb0f0a691d5ef3be
[user/henk/code/inspircd.git] / src / modules / m_monitor.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
5  *   Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #include "inspircd.h"
23
24 namespace IRCv3
25 {
26         namespace Monitor
27         {
28                 class ExtItem;
29                 struct Entry;
30                 class Manager;
31                 class ManagerInternal;
32
33                 typedef std::vector<Entry*> WatchedList;
34                 typedef std::vector<LocalUser*> WatcherList;
35         }
36 }
37
38 struct IRCv3::Monitor::Entry
39 {
40         WatcherList watchers;
41         std::string nick;
42
43         void SetNick(const std::string& Nick)
44         {
45                 nick.clear();
46                 // We may show this string to other users so do not leak the casing
47                 std::transform(Nick.begin(), Nick.end(), std::back_inserter(nick), ::tolower);
48         }
49
50         const std::string& GetNick() const { return nick; }
51 };
52
53 class IRCv3::Monitor::Manager
54 {
55         struct ExtData
56         {
57                 WatchedList list;
58         };
59
60         class ExtItem : public ExtensionItem
61         {
62                 Manager& manager;
63
64          public:
65                 ExtItem(Module* mod, const std::string& extname, Manager& managerref)
66                         : ExtensionItem(extname, ExtensionItem::EXT_USER, mod)
67                         , manager(managerref)
68                 {
69                 }
70
71                 ExtData* get(Extensible* container, bool create = false)
72                 {
73                         ExtData* extdata = static_cast<ExtData*>(get_raw(container));
74                         if ((!extdata) && (create))
75                         {
76                                 extdata = new ExtData;
77                                 set_raw(container, extdata);
78                         }
79                         return extdata;
80                 }
81
82                 void unset(Extensible* container)
83                 {
84                         free(container, unset_raw(container));
85                 }
86
87                 std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE
88                 {
89                         std::string ret;
90                         const ExtData* extdata = static_cast<ExtData*>(item);
91                         for (WatchedList::const_iterator i = extdata->list.begin(); i != extdata->list.end(); ++i)
92                         {
93                                 const Entry* entry = *i;
94                                 ret.append(entry->GetNick()).push_back(' ');
95                         }
96                         if (!ret.empty())
97                                 ret.erase(ret.size()-1);
98                         return ret;
99                 }
100
101                 void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
102
103                 void free(Extensible* container, void* item) CXX11_OVERRIDE
104                 {
105                         delete static_cast<ExtData*>(item);
106                 }
107         };
108
109  public:
110         Manager(Module* mod, const std::string& extname)
111                 : ext(mod, extname, *this)
112         {
113         }
114
115         enum WatchResult
116         {
117                 WR_OK,
118                 WR_TOOMANY,
119                 WR_ALREADYWATCHING,
120                 WR_INVALIDNICK
121         };
122
123         WatchResult Watch(LocalUser* user, const std::string& nick, unsigned int maxwatch)
124         {
125                 if (!ServerInstance->IsNick(nick))
126                         return WR_INVALIDNICK;
127
128                 WatchedList* watched = GetWatchedPriv(user, true);
129                 if (watched->size() >= maxwatch)
130                         return WR_TOOMANY;
131
132                 Entry* entry = AddWatcher(nick, user);
133                 if (stdalgo::isin(*watched, entry))
134                         return WR_ALREADYWATCHING;
135
136                 entry->watchers.push_back(user);
137                 watched->push_back(entry);
138                 return WR_OK;
139         }
140
141         bool Unwatch(LocalUser* user, const std::string& nick)
142         {
143                 WatchedList* list = GetWatchedPriv(user);
144                 if (!list)
145                         return false;
146
147                 bool ret = RemoveWatcher(nick, user, *list);
148                 // If no longer watching any nick unset ext
149                 if (list->empty())
150                         ext.unset(user);
151                 return ret;
152         }
153
154         const WatchedList& GetWatched(LocalUser* user)
155         {
156                 WatchedList* list = GetWatchedPriv(user);
157                 if (list)
158                         return *list;
159                 return emptywatchedlist;
160         }
161
162         void UnwatchAll(LocalUser* user)
163         {
164                 WatchedList* list = GetWatchedPriv(user);
165                 if (!list)
166                         return;
167
168                 while (!list->empty())
169                 {
170                         Entry* entry = list->front();
171                         RemoveWatcher(entry->GetNick(), user, *list);
172                 }
173                 ext.unset(user);
174         }
175
176         WatcherList* GetWatcherList(const std::string& nick)
177         {
178                 Entry* entry = Find(nick);
179                 if (entry)
180                         return &entry->watchers;
181                 return NULL;
182         }
183
184         static User* FindNick(const std::string& nick)
185         {
186                 User* user = ServerInstance->FindNickOnly(nick);
187                 if ((user) && (user->registered == REG_ALL))
188                         return user;
189                 return NULL;
190         }
191
192  private:
193         typedef TR1NS::unordered_map<std::string, Entry, irc::insensitive, irc::StrHashComp> NickHash;
194
195         Entry* Find(const std::string& nick)
196         {
197                 NickHash::iterator it = nicks.find(nick);
198                 if (it != nicks.end())
199                         return &it->second;
200                 return NULL;
201         }
202
203         Entry* AddWatcher(const std::string& nick, LocalUser* user)
204         {
205                 std::pair<NickHash::iterator, bool> ret = nicks.insert(std::make_pair(nick, Entry()));
206                 Entry& entry = ret.first->second;
207                 if (ret.second)
208                         entry.SetNick(nick);
209                 return &entry;
210         }
211
212         bool RemoveWatcher(const std::string& nick, LocalUser* user, WatchedList& watchedlist)
213         {
214                 NickHash::iterator it = nicks.find(nick);
215                 // If nobody is watching this nick the user trying to remove it isn't watching it for sure
216                 if (it == nicks.end())
217                         return false;
218
219                 Entry& entry = it->second;
220                 // Erase from the user's list of watched nicks
221                 if (!stdalgo::vector::swaperase(watchedlist, &entry))
222                         return false; // User is not watching this nick
223
224                 // Erase from the nick's list of watching users
225                 stdalgo::vector::swaperase(entry.watchers, user);
226
227                 // If nobody else is watching the nick remove map entry
228                 if (entry.watchers.empty())
229                         nicks.erase(it);
230
231                 return true;
232         }
233
234         WatchedList* GetWatchedPriv(LocalUser* user, bool create = false)
235         {
236                 ExtData* extdata = ext.get(user, create);
237                 if (!extdata)
238                         return NULL;
239                 return &extdata->list;
240         }
241
242         NickHash nicks;
243         ExtItem ext;
244         WatchedList emptywatchedlist;
245 };
246
247 void IRCv3::Monitor::Manager::ExtItem::FromInternal(Extensible* container, const std::string& value)
248 {
249         irc::spacesepstream ss(value);
250         for (std::string nick; ss.GetToken(nick); )
251                 manager.Watch(static_cast<LocalUser*>(container), nick, UINT_MAX);
252 }
253
254 #ifndef INSPIRCD_MONITOR_MANAGER_ONLY
255
256 enum
257 {
258         RPL_MONONLINE = 730,
259         RPL_MONOFFLINE = 731,
260         RPL_MONLIST = 732,
261         RPL_ENDOFMONLIST = 733,
262         ERR_MONLISTFULL = 734
263 };
264
265 class CommandMonitor : public SplitCommand
266 {
267         typedef Numeric::Builder<> ReplyBuilder;
268         // Additional penalty for the /MONITOR L and /MONITOR S commands that request a list from the server
269         static const unsigned int ListPenalty = 3000;
270
271         IRCv3::Monitor::Manager& manager;
272
273         void HandlePlus(LocalUser* user, const std::string& input)
274         {
275                 ReplyBuilder online(user, RPL_MONONLINE);
276                 ReplyBuilder offline(user, RPL_MONOFFLINE);
277                 irc::commasepstream ss(input);
278                 for (std::string nick; ss.GetToken(nick); )
279                 {
280                         IRCv3::Monitor::Manager::WatchResult result = manager.Watch(user, nick, maxmonitor);
281                         if (result == IRCv3::Monitor::Manager::WR_TOOMANY)
282                         {
283                                 // List is full, send error which includes the remaining nicks that were not processed
284                                 user->WriteNumeric(ERR_MONLISTFULL, maxmonitor, InspIRCd::Format("%s%s%s", nick.c_str(), (ss.StreamEnd() ? "" : ","), ss.GetRemaining().c_str()), "Monitor list is full");
285                                 break;
286                         }
287                         else if (result != IRCv3::Monitor::Manager::WR_OK)
288                                 continue; // Already added or invalid nick
289
290                         ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(nick) ? online : offline);
291                         out.Add(nick);
292                 }
293
294                 online.Flush();
295                 offline.Flush();
296         }
297
298         void HandleMinus(LocalUser* user, const std::string& input)
299         {
300                 irc::commasepstream ss(input);
301                 for (std::string nick; ss.GetToken(nick); )
302                         manager.Unwatch(user, nick);
303         }
304
305  public:
306         unsigned int maxmonitor;
307
308         CommandMonitor(Module* mod, IRCv3::Monitor::Manager& managerref)
309                 : SplitCommand(mod, "MONITOR", 1)
310                 , manager(managerref)
311         {
312                 Penalty = 2;
313                 allow_empty_last_param = false;
314                 syntax = "C|L|S|(+|-) <nick>[,<nick>]+";
315         }
316
317         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
318         {
319                 char subcmd = toupper(parameters[0][0]);
320                 if (subcmd == '+')
321                 {
322                         if (parameters.size() > 1)
323                                 HandlePlus(user, parameters[1]);
324                 }
325                 else if (subcmd == '-')
326                 {
327                         if (parameters.size() > 1)
328                                 HandleMinus(user, parameters[1]);
329                 }
330                 else if (subcmd == 'C')
331                 {
332                         manager.UnwatchAll(user);
333                 }
334                 else if (subcmd == 'L')
335                 {
336                         user->CommandFloodPenalty += ListPenalty;
337                         const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
338                         ReplyBuilder out(user, RPL_MONLIST);
339                         for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
340                         {
341                                 IRCv3::Monitor::Entry* entry = *i;
342                                 out.Add(entry->GetNick());
343                         }
344                         out.Flush();
345                         user->WriteNumeric(RPL_ENDOFMONLIST, "End of MONITOR list");
346                 }
347                 else if (subcmd == 'S')
348                 {
349                         user->CommandFloodPenalty += ListPenalty;
350
351                         ReplyBuilder online(user, RPL_MONONLINE);
352                         ReplyBuilder offline(user, RPL_MONOFFLINE);
353
354                         const IRCv3::Monitor::WatchedList& list = manager.GetWatched(user);
355                         for (IRCv3::Monitor::WatchedList::const_iterator i = list.begin(); i != list.end(); ++i)
356                         {
357                                 IRCv3::Monitor::Entry* entry = *i;
358                                 ReplyBuilder& out = (IRCv3::Monitor::Manager::FindNick(entry->GetNick()) ? online : offline);
359                                 out.Add(entry->GetNick());
360                         }
361
362                         online.Flush();
363                         offline.Flush();
364                 }
365                 else
366                         return CMD_FAILURE;
367
368                 return CMD_SUCCESS;
369         }
370 };
371
372 class ModuleMonitor : public Module
373 {
374         IRCv3::Monitor::Manager manager;
375         CommandMonitor cmd;
376
377         void SendAlert(unsigned int numeric, const std::string& nick)
378         {
379                 const IRCv3::Monitor::WatcherList* list = manager.GetWatcherList(nick);
380                 if (!list)
381                         return;
382
383                 for (IRCv3::Monitor::WatcherList::const_iterator i = list->begin(); i != list->end(); ++i)
384                 {
385                         LocalUser* curr = *i;
386                         curr->WriteNumeric(numeric, nick);
387                 }
388         }
389
390  public:
391         ModuleMonitor()
392                 : manager(this, "monitor")
393                 , cmd(this, manager)
394         {
395         }
396
397         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
398         {
399                 ConfigTag* tag = ServerInstance->Config->ConfValue("monitor");
400                 cmd.maxmonitor = tag->getUInt("maxentries", 30, 1);
401         }
402
403         void OnPostConnect(User* user) CXX11_OVERRIDE
404         {
405                 SendAlert(RPL_MONONLINE, user->nick);
406         }
407
408         void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
409         {
410                 // Detect and ignore nickname case change
411                 if (ServerInstance->FindNickOnly(oldnick) == user)
412                         return;
413
414                 SendAlert(RPL_MONOFFLINE, oldnick);
415                 SendAlert(RPL_MONONLINE, user->nick);
416         }
417
418         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
419         {
420                 LocalUser* localuser = IS_LOCAL(user);
421                 if (localuser)
422                         manager.UnwatchAll(localuser);
423                 SendAlert(RPL_MONOFFLINE, user->nick);
424         }
425
426         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
427         {
428                 tokens["MONITOR"] = ConvToStr(cmd.maxmonitor);
429         }
430
431         Version GetVersion() CXX11_OVERRIDE
432         {
433                 return Version("Adds the /MONITOR command which allows users to find out when their friends are connected to the server.", VF_VENDOR);
434         }
435 };
436
437 MODULE_INIT(ModuleMonitor)
438
439 #endif