]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_callerid.cpp
MetaData rework
[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, InspIRCd* ServerInstance)
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 /** Retrieves the callerid information for a given user record
74  * @param who The user to retrieve callerid information for
75  * @param extend true to add callerid information if it doesn't already exist, false to return NULL if it doesn't exist
76  * @return NULL if extend is false and it doesn't exist, a callerid_data instance otherwise.
77  */
78 callerid_data* GetData(User* who, bool extend = true)
79 {
80         callerid_data* dat;
81         if (who->GetExt("callerid_data", dat))
82                 return dat;
83         else
84         {
85                 if (extend)
86                 {
87                         dat = new callerid_data;
88                         who->Extend("callerid_data", dat);
89                         return dat;
90                 }
91                 else
92                         return NULL;
93         }
94 }
95
96 void RemoveData(User* who)
97 {
98         callerid_data* dat;
99         who->GetExt("callerid_data", dat);
100
101         if (!dat)
102                 return;
103
104         // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
105         for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
106         {
107                 callerid_data *targ = GetData(*it, false);
108
109                 if (!targ)
110                         continue; // shouldn't happen, but oh well.
111
112                 for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
113                 {
114                         if (*it2 == dat)
115                         {
116                                 targ->wholistsme.erase(it2);
117                                 break;
118                         }
119                 }
120         }
121
122         who->Shrink("callerid_data");
123         delete dat;
124 }
125
126
127 class User_g : public SimpleUserModeHandler
128 {
129 public:
130         User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
131 };
132
133 class CommandAccept : public Command
134 {
135 private:
136         unsigned int& maxaccepts;
137 public:
138         CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
139         {
140                 source = "m_callerid.so";
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_LOCALONLY;
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 = GetData(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 = GetData(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 = GetData(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 = GetData(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 = GetData(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 mycommand;
311         User_g myumode;
312
313         // Configuration variables:
314         unsigned int maxaccepts; // Maximum ACCEPT entries.
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 = GetData(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(InspIRCd* Me) : Module(Me), mycommand(Me, maxaccepts), myumode(Me)
346         {
347                 OnRehash(NULL);
348
349                 if (!ServerInstance->Modes->AddMode(&myumode))
350                         throw ModuleException("Could not add usermode +g");
351
352                 ServerInstance->AddCommand(&mycommand);
353
354                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
355                 ServerInstance->Modules->Attach(eventlist, this, 7);
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         int PreText(User* user, User* dest, std::string& text, bool notice)
374         {
375                 if (!dest->IsModeSet('g'))
376                         return 0;
377
378                 if (operoverride && IS_OPER(user))
379                         return 0;
380
381                 callerid_data* dat = GetData(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                                 if (IS_LOCAL(dest))
393                                 {
394                                         dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
395                                 }
396                                 else
397                                 {
398                                         ServerInstance->PI->PushToClient(dest, std::string("::") + ServerInstance->Config->ServerName + " 718 " + dest->nick + " " + user->nick + " " + user->ident + "@" + user->dhost + " :is messaging you,  and you have umode +g. Use /ACCEPT +" + user->nick + " to allow.");
399                                 }
400                                 dat->lastnotify = now;
401                         }
402                         return 1;
403                 }
404                 return 0;
405         }
406
407         virtual int 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 0;
413         }
414
415         virtual int 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 0;
421         }
422
423         virtual void OnCleanup(int type, void* item)
424         {
425                 if (type != TYPE_USER)
426                         return;
427
428                 User* u = (User*)item;
429                 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do.  */
430                 RemoveData(u);
431         }
432
433         virtual void OnSyncUser(User* user, Module* proto, void* opaque)
434         {
435                 callerid_data* dat = GetData(user, false);
436                 if (dat)
437                 {
438                         std::string str = dat->ToString(proto);
439                         proto->ProtoSendMetaData(opaque, user, "callerid_data", str);
440                 }
441         }
442
443         virtual void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata)
444         {
445                 User* u = dynamic_cast<User*>(target);
446                 if (u && extname == "callerid_data")
447                 {
448                         callerid_data* dat = new callerid_data(extdata, ServerInstance);
449                         u->Extend("callerid_data", dat);
450                 }
451         }
452
453         virtual int OnUserPreNick(User* user, const std::string& newnick)
454         {
455                 if (!tracknick)
456                         RemoveFromAllAccepts(user);
457                 return 0;
458         }
459
460         virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
461         {
462                 RemoveFromAllAccepts(user);
463                 RemoveData(user);
464         }
465
466         virtual void OnRehash(User* user)
467         {
468                 ConfigReader Conf(ServerInstance);
469                 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
470                 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
471                 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
472                 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
473         }
474 };
475
476 MODULE_INIT(ModuleCallerID)
477
478