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