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