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