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