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