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