]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_callerid.cpp
Remove more classbase
[user/henk/code/inspircd.git] / src / modules / m_callerid.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <set>
16 #include <sstream>
17 #include <algorithm>
18
19 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
20
21 class callerid_data
22 {
23  public:
24         time_t lastnotify;
25
26         /** Users I accept messages from
27          */
28         std::set<User*> accepting;
29
30         /** Users who list me as accepted
31          */
32         std::list<callerid_data *> wholistsme;
33
34         callerid_data() : lastnotify(0) { }
35         callerid_data(const std::string& str)
36         {
37                 irc::commasepstream s(str);
38                 std::string tok;
39                 if (s.GetToken(tok))
40                 {
41                         lastnotify = ConvToInt(tok);
42                 }
43                 while (s.GetToken(tok))
44                 {
45                         if (tok.empty())
46                         {
47                                 continue;
48                         }
49
50                         User *u = ServerInstance->FindNick(tok);
51                         if (!u)
52                         {
53                                 continue;
54                         }
55                         accepting.insert(u);
56                 }
57         }
58
59         std::string ToString(SerializeFormat format) const
60         {
61                 std::ostringstream oss;
62                 oss << lastnotify;
63                 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
64                 {
65                         User* u = *i;
66                         // Encode UIDs.
67                         oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
68                 }
69                 oss << std::ends;
70                 return oss.str();
71         }
72 };
73
74 struct CallerIDExtInfo : public ExtensionItem
75 {
76         CallerIDExtInfo(Module* parent)
77                 : ExtensionItem("callerid_data", parent)
78         {
79         }
80
81         std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
82         {
83                 callerid_data* dat = static_cast<callerid_data*>(item);
84                 return dat->ToString(format);
85         }
86
87         void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
88         {
89                 callerid_data* dat = new callerid_data(value);
90                 set_raw(container, dat);
91         }
92
93         callerid_data* get(User* user, bool create)
94         {
95                 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
96                 if (!dat)
97                 {
98                         dat = new callerid_data;
99                         set_raw(user, dat);
100                 }
101                 return dat;
102         }
103
104         void free(void* item)
105         {
106                 callerid_data* dat = static_cast<callerid_data*>(item);
107
108                 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
109                 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
110                 {
111                         callerid_data *targ = this->get(*it, false);
112
113                         if (!targ)
114                                 continue; // shouldn't happen, but oh well.
115
116                         for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
117                         {
118                                 if (*it2 == dat)
119                                 {
120                                         targ->wholistsme.erase(it2);
121                                         break;
122                                 }
123                         }
124                 }
125         }
126 };
127
128 class User_g : public SimpleUserModeHandler
129 {
130 public:
131         User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
132 };
133
134 class CommandAccept : public Command
135 {
136 public:
137         CallerIDExtInfo extInfo;
138         unsigned int maxaccepts;
139         CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
140                 extInfo(Creator)
141         {
142                 syntax = "{[+|-]<nicks>}|*}";
143                 TRANSLATE2(TR_CUSTOM, TR_END);
144         }
145
146         virtual void EncodeParameter(std::string& parameter, int index)
147         {
148                 if (index != 0)
149                         return;
150                 std::string out = "";
151                 irc::commasepstream nicks(parameter);
152                 std::string tok;
153                 while (nicks.GetToken(tok))
154                 {
155                         if (tok == "*")
156                         {
157                                 continue; // Drop list requests, since remote servers ignore them anyway.
158                         }
159                         if (!out.empty())
160                                 out.append(",");
161                         bool dash = false;
162                         if (tok[0] == '-')
163                         {
164                                 dash = true;
165                                 tok.erase(0, 1); // Remove the dash.
166                         }
167                         User* u = ServerInstance->FindNick(tok);
168                         if (u)
169                         {
170                                 if (dash)
171                                         out.append("-");
172                                 out.append(u->uuid);
173                         }
174                         else
175                         {
176                                 if (dash)
177                                         out.append("-");
178                                 out.append(tok);
179                         }
180                 }
181                 parameter = out;
182         }
183
184         /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
185          * - in front of any nick removes, and an * lists. This effectively means you can do:
186          * /accept nick1,nick2,nick3,*
187          * to add 3 nicks and then show your list
188          */
189         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
190         {
191                 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
192                         return CMD_SUCCESS;
193                 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
194                  * have a list already setup. */
195
196                 std::string tok = parameters[0];
197
198                 if (tok == "*")
199                 {
200                         if (IS_LOCAL(user))
201                                 ListAccept(user);
202                         return CMD_SUCCESS;
203                 }
204                 else if (tok[0] == '-')
205                 {
206                         User* whotoremove = ServerInstance->FindNick(tok.substr(1));
207                         if (whotoremove)
208                                 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
209                         else
210                                 return CMD_FAILURE;
211                 }
212                 else
213                 {
214                         User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
215                         if (whotoadd)
216                                 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
217                         else
218                         {
219                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
220                                 return CMD_FAILURE;
221                         }
222                 }
223         }
224
225         void ListAccept(User* user)
226         {
227                 callerid_data* dat = extInfo.get(user, false);
228                 if (dat)
229                 {
230                         for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
231                                 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
232                 }
233                 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
234         }
235
236         bool AddAccept(User* user, User* whotoadd, bool quiet)
237         {
238                 // Add this user to my accept list first, so look me up..
239                 callerid_data* dat = extInfo.get(user, true);
240                 if (dat->accepting.size() >= maxaccepts)
241                 {
242                         if (!quiet)
243                                 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
244
245                         return false;
246                 }
247                 if (!dat->accepting.insert(whotoadd).second)
248                 {
249                         if (!quiet)
250                                 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
251
252                         return false;
253                 }
254
255                 // Now, look them up, and add me to their list
256                 callerid_data *targ = extInfo.get(whotoadd, true);
257                 targ->wholistsme.push_back(dat);
258
259                 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
260                 return true;
261         }
262
263         bool RemoveAccept(User* user, User* whotoremove, bool quiet)
264         {
265                 // Remove them from my list, so look up my list..
266                 callerid_data* dat = extInfo.get(user, false);
267                 if (!dat)
268                 {
269                         if (!quiet)
270                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
271
272                         return false;
273                 }
274                 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
275                 if (i == dat->accepting.end())
276                 {
277                         if (!quiet)
278                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
279
280                         return false;
281                 }
282
283                 dat->accepting.erase(i);
284
285                 // Look up their list to remove me.
286                 callerid_data *dat2 = extInfo.get(whotoremove, false);
287                 if (!dat2)
288                 {
289                         // How the fuck is this possible.
290                         return false;
291                 }
292
293                 for (std::list<callerid_data *>::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++)
294                 {
295                         // Found me!
296                         if (*it == dat)
297                         {
298                                 dat2->wholistsme.erase(it);
299                                 break;
300                         }
301                 }
302
303                 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
304                 return true;
305         }
306 };
307
308 class ModuleCallerID : public Module
309 {
310 private:
311         CommandAccept cmd;
312         User_g myumode;
313
314         // Configuration variables:
315         bool operoverride; // Operators can override callerid.
316         bool tracknick; // Allow ACCEPT entries to update with nick changes.
317         unsigned int notify_cooldown; // Seconds between notifications.
318
319         /** Removes a user from all accept lists
320          * @param who The user to remove from accepts
321          */
322         void RemoveFromAllAccepts(User* who)
323         {
324                 // First, find the list of people who have me on accept
325                 callerid_data *userdata = cmd.extInfo.get(who, false);
326                 if (!userdata)
327                         return;
328
329                 // Iterate over the list of people who accept me, and remove all entries
330                 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
331                 {
332                         callerid_data *dat = *(it);
333
334                         // Find me on their callerid list
335                         std::set<User *>::iterator it2 = dat->accepting.find(who);
336
337                         if (it2 != dat->accepting.end())
338                                 dat->accepting.erase(it2);
339                 }
340
341                 userdata->wholistsme.clear();
342         }
343
344 public:
345         ModuleCallerID() : cmd(this), myumode(this)
346         {
347                 OnRehash(NULL);
348
349                 if (!ServerInstance->Modes->AddMode(&myumode))
350                         throw ModuleException("Could not add usermode +g");
351
352                 ServerInstance->AddCommand(&cmd);
353                 ServerInstance->Extensions.Register(&cmd.extInfo);
354
355                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
356                 ServerInstance->Modules->Attach(eventlist, this, 6);
357         }
358
359         virtual ~ModuleCallerID()
360         {
361         }
362
363         virtual Version GetVersion()
364         {
365                 return Version("Implementation of callerid (umode +g & /accept, ala hybrid etc)", VF_COMMON | VF_VENDOR);
366         }
367
368         virtual void On005Numeric(std::string& output)
369         {
370                 output += " CALLERID=g";
371         }
372
373         ModResult PreText(User* user, User* dest, std::string& text, bool notice)
374         {
375                 if (!dest->IsModeSet('g'))
376                         return MOD_RES_PASSTHRU;
377
378                 if (operoverride && IS_OPER(user))
379                         return MOD_RES_PASSTHRU;
380
381                 callerid_data* dat = cmd.extInfo.get(dest, true);
382                 std::set<User*>::iterator i = dat->accepting.find(user);
383
384                 if (i == dat->accepting.end())
385                 {
386                         time_t now = ServerInstance->Time();
387                         /* +g and *not* accepted */
388                         user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
389                         if (now > (dat->lastnotify + (time_t)notify_cooldown))
390                         {
391                                 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
392                                 ServerInstance->DumpText(dest, ":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
393                                         ServerInstance->Config->ServerName.c_str(), dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
394                                 dat->lastnotify = now;
395                         }
396                         return MOD_RES_DENY;
397                 }
398                 return MOD_RES_PASSTHRU;
399         }
400
401         virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
402         {
403                 if (IS_LOCAL(user) && target_type == TYPE_USER)
404                         return PreText(user, (User*)dest, text, true);
405
406                 return MOD_RES_PASSTHRU;
407         }
408
409         virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
410         {
411                 if (IS_LOCAL(user) && target_type == TYPE_USER)
412                         return PreText(user, (User*)dest, text, true);
413
414                 return MOD_RES_PASSTHRU;
415         }
416
417         ModResult OnUserPreNick(User* user, const std::string& newnick)
418         {
419                 if (!tracknick)
420                         RemoveFromAllAccepts(user);
421                 return MOD_RES_PASSTHRU;
422         }
423
424         void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
425         {
426                 RemoveFromAllAccepts(user);
427         }
428
429         virtual void OnRehash(User* user)
430         {
431                 ConfigReader Conf;
432                 cmd.maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
433                 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
434                 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
435                 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
436         }
437 };
438
439 MODULE_INIT(ModuleCallerID)
440
441