]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_callerid.cpp
Update all wiki links to point to the new wiki. This was done automatically with...
[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         std::set<User*> accepting;
26
27         callerid_data() : lastnotify(0) { }
28         callerid_data(const std::string& str, InspIRCd* ServerInstance)
29         {
30                 irc::commasepstream s(str);
31                 std::string tok;
32                 if (s.GetToken(tok))
33                 {
34                         lastnotify = ConvToInt(tok);
35                 }
36                 while (s.GetToken(tok))
37                 {
38                         if (tok.empty())
39                         {
40                                 continue;
41                         }
42                         User* u = ServerInstance->FindUUID(tok);
43                         if (!u)
44                         {
45                                 u = ServerInstance->FindNick(tok);
46                         }
47                         if (!u)
48                         {
49                                 continue;
50                         }
51                         accepting.insert(u);
52                 }
53         }
54
55         std::string ToString(bool displayable) const
56         {
57                 std::ostringstream oss;
58                 oss << lastnotify;
59                 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
60                 {
61                         // Encode UIDs.
62                         oss << "," << (displayable ? (*i)->nick : (*i)->uuid);
63                 }
64                 oss << std::ends;
65                 return oss.str();
66         }
67 };
68
69 callerid_data* GetData(User* who, bool extend = true)
70 {
71         callerid_data* dat;
72         if (who->GetExt("callerid_data", dat))
73                 return dat;
74         else
75         {
76                 if (extend)
77                 {
78                         dat = new callerid_data;
79                         who->Extend("callerid_data", dat);
80                         return dat;
81                 }
82                 else
83                         return NULL;
84         }
85 }
86
87 void RemoveData(User* who)
88 {
89         callerid_data* dat;
90         who->GetExt("callerid_data", dat);
91
92         if (!dat)
93                 return;
94
95         who->Shrink("callerid_data");
96         delete dat;
97 }
98
99 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
100 {
101         for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
102         {
103                 callerid_data* dat = GetData(i->second, false);
104
105                 if (!dat)
106                         continue;
107
108                 std::set<User*>::iterator iter = dat->accepting.find(who);
109
110                 if (iter == dat->accepting.end())
111                         continue;
112
113                 dat->accepting.erase(iter);
114         }
115 }
116
117 class User_g : public SimpleUserModeHandler
118 {
119 public:
120         User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
121 };
122
123 class CommandAccept : public Command
124 {
125 private:
126         unsigned int& maxaccepts;
127 public:
128         CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
129         {
130                 source = "m_callerid.so";
131                 syntax = "{[+|-]<nicks>}|*}";
132                 TRANSLATE2(TR_CUSTOM, TR_END);
133         }
134
135         virtual void EncodeParameter(std::string& parameter, int index)
136         {
137                 if (index != 0)
138                         return;
139                 std::string out = "";
140                 irc::commasepstream nicks(parameter);
141                 std::string tok;
142                 while (nicks.GetToken(tok))
143                 {
144                         if (tok == "*")
145                         {
146                                 continue; // Drop list requests, since remote servers ignore them anyway.
147                         }
148                         if (!out.empty())
149                                 out.append(",");
150                         bool dash = false;
151                         if (tok[0] == '-')
152                         {
153                                 dash = true;
154                                 tok.erase(0, 1); // Remove the dash.
155                         }
156                         User* u = ServerInstance->FindNick(tok);
157                         if (u)
158                         {
159                                 if (dash)
160                                         out.append("-");
161                                 out.append(u->uuid);
162                         }
163                         else
164                         {
165                                 if (dash)
166                                         out.append("-");
167                                 out.append(tok);
168                         }
169                 }
170                 parameter = out;
171         }
172
173         /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
174          * - in front of any nick removes, and an * lists. This effectively means you can do:
175          * /accept nick1,nick2,nick3,*
176          * to add 3 nicks and then show your list
177          */
178         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
179         {
180                 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
181                         return CMD_SUCCESS;
182                 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
183                  * have a list already setup. */
184
185                 std::string tok = parameters[0];
186
187                 if (tok == "*")
188                 {
189                         if (IS_LOCAL(user))
190                                 ListAccept(user);
191                         return CMD_LOCALONLY;
192                 }
193                 else if (tok[0] == '-')
194                 {
195                         User* whotoremove = ServerInstance->FindNick(tok.substr(1));
196                         if (whotoremove)
197                                 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
198                         else
199                                 return CMD_FAILURE;
200                 }
201                 else
202                 {
203                         User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
204                         if (whotoadd)
205                                 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
206                         else
207                         {
208                                 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
209                                 return CMD_FAILURE;
210                         }
211                 }
212         }
213
214         void ListAccept(User* user)
215         {
216                 callerid_data* dat = GetData(user, false);
217                 if (dat)
218                 {
219                         for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
220                                 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
221                 }
222                 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
223         }
224
225         bool AddAccept(User* user, User* whotoadd, bool quiet)
226         {
227                 callerid_data* dat = GetData(user, true);
228                 if (dat->accepting.size() >= maxaccepts)
229                 {
230                         if (!quiet)
231                                 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
232
233                         return false;
234                 }
235                 if (!dat->accepting.insert(whotoadd).second)
236                 {
237                         if (!quiet)
238                                 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
239
240                         return false;
241                 }
242
243                 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
244                 return true;
245         }
246
247         bool RemoveAccept(User* user, User* whotoremove, bool quiet)
248         {
249                 callerid_data* dat = GetData(user, false);
250                 if (!dat)
251                 {
252                         if (!quiet)
253                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
254
255                         return false;
256                 }
257                 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
258                 if (i == dat->accepting.end())
259                 {
260                         if (!quiet)
261                                 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
262
263                         return false;
264                 }
265
266                 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
267                 dat->accepting.erase(i);
268                 return true;
269         }
270 };
271
272 class ModuleCallerID : public Module
273 {
274 private:
275         CommandAccept *mycommand;
276         User_g* myumode;
277
278         // Configuration variables:
279         unsigned int maxaccepts; // Maximum ACCEPT entries.
280         bool operoverride; // Operators can override callerid.
281         bool tracknick; // Allow ACCEPT entries to update with nick changes.
282         unsigned int notify_cooldown; // Seconds between notifications.
283
284 public:
285         ModuleCallerID(InspIRCd* Me) : Module(Me)
286         {
287                 OnRehash(NULL, "");
288                 mycommand = new CommandAccept(ServerInstance, maxaccepts);
289                 myumode = new User_g(ServerInstance);
290
291                 if (!ServerInstance->Modes->AddMode(myumode))
292                 {
293                         delete mycommand;
294                         delete myumode;
295                         throw ModuleException("Could not add usermode +g");
296                 }
297                 try
298                 {
299                         ServerInstance->AddCommand(mycommand);
300                 }
301                 catch (const ModuleException& e)
302                 {
303                         delete mycommand;
304                         delete myumode;
305                         throw ModuleException("Could not add command!");
306                 }
307
308                 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
309                 ServerInstance->Modules->Attach(eventlist, this, 7);
310         }
311
312         virtual ~ModuleCallerID()
313         {
314                 ServerInstance->Modes->DelMode(myumode);
315                 delete myumode;
316         }
317
318         virtual Version GetVersion()
319         {
320                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
321         }
322
323         virtual void On005Numeric(std::string& output)
324         {
325                 output += " CALLERID=g";
326         }
327
328         int PreText(User* user, User* dest, std::string& text, bool notice)
329         {
330                 if (!dest->IsModeSet('g'))
331                         return 0;
332
333                 if (operoverride && IS_OPER(user))
334                         return 0;
335
336                 callerid_data* dat = GetData(dest, true);
337                 std::set<User*>::iterator i = dat->accepting.find(user);
338
339                 if (i == dat->accepting.end())
340                 {
341                         time_t now = ServerInstance->Time();
342                         /* +g and *not* accepted */
343                         if (IS_LOCAL(user))
344                                 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
345                         else
346                                 ServerInstance->PI->PushToClient(user, std::string("::") + ServerInstance->Config->ServerName + " 716 " + user->nick + dest->nick + " :is in +g mode (server-side ignore).");
347                         if (now > (dat->lastnotify + (time_t)notify_cooldown))
348                         {
349                                 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
350                                 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());
351                                 dat->lastnotify = now;
352                         }
353                         return 1;
354                 }
355                 return 0;
356         }
357
358         virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
359         {
360                 if (IS_LOCAL(user) && target_type == TYPE_USER)
361                         return PreText(user, (User*)dest, text, true);
362
363                 return 0;
364         }
365
366         virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
367         {
368                 if (IS_LOCAL(user) && target_type == TYPE_USER)
369                         return PreText(user, (User*)dest, text, true);
370
371                 return 0;
372         }
373
374         virtual void OnCleanup(int type, void* item)
375         {
376                 if (type != TYPE_USER)
377                         return;
378
379                 User* u = (User*)item;
380                 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do.  */
381                 RemoveData(u);
382         }
383
384         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable)
385         {
386                 if (extname == "callerid_data")
387                 {
388                         callerid_data* dat = GetData(user, false);
389                         if (dat)
390                         {
391                                 std::string str = dat->ToString(displayable);
392                                 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str);
393                         }
394                 }
395         }
396
397         virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata)
398         {
399                 if (target_type == TYPE_USER && extname == "callerid_data")
400                 {
401                         User* u = (User*)target;
402                         callerid_data* dat = new callerid_data(extdata, ServerInstance);
403                         u->Extend("callerid_data", dat);
404                 }
405         }
406
407         virtual int OnUserPreNick(User* user, const std::string& newnick)
408         {
409                 if (!tracknick)
410                         RemoveFromAllAccepts(ServerInstance, user);
411                 return 0;
412         }
413
414         virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
415         {
416                 RemoveData(user);
417                 RemoveFromAllAccepts(ServerInstance, user);
418         }
419
420         virtual void OnRehash(User* user, const std::string& parameter)
421         {
422                 ConfigReader Conf(ServerInstance);
423                 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
424                 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
425                 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
426                 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
427         }
428 };
429
430 MODULE_INIT(ModuleCallerID)
431
432