]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_callerid.cpp
b84ee2c9caab73e4ecd06a1541f705132419264b
[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 : public classbase
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(Module* proto) 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                         // Encode UIDs.
66                         oss << "," << proto->ProtoTranslate(*i);
67                 }
68                 oss << std::ends;
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(Module* requestor, const Extensible* container, void* item)
81         {
82                 callerid_data* dat = static_cast<callerid_data*>(item);
83                 return dat->ToString(requestor);
84         }
85
86         void unserialize(Module* requestor, 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 (!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(InspIRCd* Instance, Module* Creator) : SimpleUserModeHandler(Creator, 'g') { }
131 };
132
133 class CommandAccept : public Command
134 {
135 public:
136         CallerIDExtInfo extInfo;
137         unsigned int maxaccepts;
138         CommandAccept(InspIRCd* Instance, Module* Creator) : Command(Instance, Creator, "ACCEPT", 0, 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         void ListAccept(User* user)
225         {
226                 callerid_data* dat = extInfo.get(user, false);
227                 if (dat)
228                 {
229                         for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
230                                 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
231                 }
232                 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
233         }
234
235         bool AddAccept(User* user, User* whotoadd, bool quiet)
236         {
237                 // Add this user to my accept list first, so look me up..
238                 callerid_data* dat = extInfo.get(user, true);
239                 if (dat->accepting.size() >= maxaccepts)
240                 {
241                         if (!quiet)
242                                 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
243
244                         return false;
245                 }
246                 if (!dat->accepting.insert(whotoadd).second)
247                 {
248                         if (!quiet)
249                                 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
250
251                         return false;
252                 }
253
254                 // Now, look them up, and add me to their list
255                 callerid_data *targ = extInfo.get(whotoadd, true);
256                 targ->wholistsme.push_back(dat);
257
258                 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
259                 return true;
260         }
261
262         bool RemoveAccept(User* user, User* whotoremove, bool quiet)
263         {
264                 // Remove them from my list, so look up my list..
265                 callerid_data* dat = extInfo.get(user, false);
266                 if (!dat)
267                 {
268                         if (!quiet)
269                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
270
271                         return false;
272                 }
273                 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
274                 if (i == dat->accepting.end())
275                 {
276                         if (!quiet)
277                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
278
279                         return false;
280                 }
281
282                 dat->accepting.erase(i);
283
284                 // Look up their list to remove me.
285                 callerid_data *dat2 = extInfo.get(whotoremove, false);
286                 if (!dat2)
287                 {
288                         // How the fuck is this possible.
289                         return false;
290                 }
291
292                 for (std::list<callerid_data *>::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++)
293                 {
294                         // Found me!
295                         if (*it == dat)
296                         {
297                                 dat2->wholistsme.erase(it);
298                                 break;
299                         }
300                 }
301
302                 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
303                 return true;
304         }
305 };
306
307 class ModuleCallerID : public Module
308 {
309 private:
310         CommandAccept cmd;
311         User_g myumode;
312
313         // Configuration variables:
314         bool operoverride; // Operators can override callerid.
315         bool tracknick; // Allow ACCEPT entries to update with nick changes.
316         unsigned int notify_cooldown; // Seconds between notifications.
317
318         /** Removes a user from all accept lists
319          * @param who The user to remove from accepts
320          */
321         void RemoveFromAllAccepts(User* who)
322         {
323                 // First, find the list of people who have me on accept
324                 callerid_data *userdata = cmd.extInfo.get(who, false);
325                 if (!userdata)
326                         return;
327
328                 // Iterate over the list of people who accept me, and remove all entries
329                 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
330                 {
331                         callerid_data *dat = *(it);
332
333                         // Find me on their callerid list
334                         std::set<User *>::iterator it2 = dat->accepting.find(who);
335
336                         if (it2 != dat->accepting.end())
337                                 dat->accepting.erase(it2);
338                 }
339
340                 userdata->wholistsme.clear();
341         }
342
343 public:
344         ModuleCallerID(InspIRCd* Me) : Module(Me), cmd(Me, this), myumode(Me, this)
345         {
346                 OnRehash(NULL);
347
348                 if (!ServerInstance->Modes->AddMode(&myumode))
349                         throw ModuleException("Could not add usermode +g");
350
351                 ServerInstance->AddCommand(&cmd);
352                 Extensible::Register(&cmd.extInfo);
353
354                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
355                 ServerInstance->Modules->Attach(eventlist, this, 6);
356         }
357
358         virtual ~ModuleCallerID()
359         {
360                 ServerInstance->Modes->DelMode(&myumode);
361         }
362
363         virtual Version GetVersion()
364         {
365                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
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, 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(ServerInstance);
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