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