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