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