2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
28 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
35 /** Users I accept messages from
37 std::set<User*> accepting;
39 /** Users who list me as accepted
41 std::list<callerid_data *> wholistsme;
43 callerid_data() : lastnotify(0) { }
44 callerid_data(const std::string& str)
46 irc::commasepstream s(str);
50 lastnotify = ConvToInt(tok);
52 while (s.GetToken(tok))
59 User *u = ServerInstance->FindNick(tok);
68 std::string ToString(SerializeFormat format) const
70 std::ostringstream oss;
72 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
76 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
82 struct CallerIDExtInfo : public ExtensionItem
84 CallerIDExtInfo(Module* parent)
85 : ExtensionItem("callerid_data", parent)
89 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
91 callerid_data* dat = static_cast<callerid_data*>(item);
92 return dat->ToString(format);
95 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
97 callerid_data* dat = new callerid_data(value);
98 set_raw(container, dat);
101 callerid_data* get(User* user, bool create)
103 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
106 dat = new callerid_data;
112 void free(void* item)
114 callerid_data* dat = static_cast<callerid_data*>(item);
116 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
117 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
119 callerid_data *targ = this->get(*it, false);
122 continue; // shouldn't happen, but oh well.
124 for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
128 targ->wholistsme.erase(it2);
136 class User_g : public SimpleUserModeHandler
139 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
142 class CommandAccept : public Command
145 CallerIDExtInfo extInfo;
146 unsigned int maxaccepts;
147 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
150 syntax = "{[+|-]<nicks>}|*}";
151 TRANSLATE2(TR_CUSTOM, TR_END);
154 virtual void EncodeParameter(std::string& parameter, int index)
158 std::string out = "";
159 irc::commasepstream nicks(parameter);
161 while (nicks.GetToken(tok))
165 continue; // Drop list requests, since remote servers ignore them anyway.
173 tok.erase(0, 1); // Remove the dash.
175 User* u = ServerInstance->FindNick(tok);
192 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
193 * - in front of any nick removes, and an * lists. This effectively means you can do:
194 * /accept nick1,nick2,nick3,*
195 * to add 3 nicks and then show your list
197 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
199 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
201 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
202 * have a list already setup. */
204 std::string tok = parameters[0];
212 else if (tok[0] == '-')
214 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
216 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
222 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
224 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
227 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
233 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
235 return ROUTE_BROADCAST;
238 void ListAccept(User* user)
240 callerid_data* dat = extInfo.get(user, false);
243 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
244 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
246 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
249 bool AddAccept(User* user, User* whotoadd, bool quiet)
251 // Add this user to my accept list first, so look me up..
252 callerid_data* dat = extInfo.get(user, true);
253 if (dat->accepting.size() >= maxaccepts)
256 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
260 if (!dat->accepting.insert(whotoadd).second)
263 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
268 // Now, look them up, and add me to their list
269 callerid_data *targ = extInfo.get(whotoadd, true);
270 targ->wholistsme.push_back(dat);
272 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
276 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
278 // Remove them from my list, so look up my list..
279 callerid_data* dat = extInfo.get(user, false);
283 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
287 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
288 if (i == dat->accepting.end())
291 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
296 dat->accepting.erase(i);
298 // Look up their list to remove me.
299 callerid_data *dat2 = extInfo.get(whotoremove, false);
302 // How the fuck is this possible.
306 for (std::list<callerid_data *>::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++)
311 dat2->wholistsme.erase(it);
316 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
321 class ModuleCallerID : public Module
327 // Configuration variables:
328 bool operoverride; // Operators can override callerid.
329 bool tracknick; // Allow ACCEPT entries to update with nick changes.
330 unsigned int notify_cooldown; // Seconds between notifications.
332 /** Removes a user from all accept lists
333 * @param who The user to remove from accepts
335 void RemoveFromAllAccepts(User* who)
337 // First, find the list of people who have me on accept
338 callerid_data *userdata = cmd.extInfo.get(who, false);
342 // Iterate over the list of people who accept me, and remove all entries
343 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
345 callerid_data *dat = *(it);
347 // Find me on their callerid list
348 std::set<User *>::iterator it2 = dat->accepting.find(who);
350 if (it2 != dat->accepting.end())
351 dat->accepting.erase(it2);
354 userdata->wholistsme.clear();
358 ModuleCallerID() : cmd(this), myumode(this)
366 ServerInstance->Modules->AddService(myumode);
367 ServerInstance->Modules->AddService(cmd);
368 ServerInstance->Modules->AddService(cmd.extInfo);
370 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
371 ServerInstance->Modules->Attach(eventlist, this, 6);
374 virtual ~ModuleCallerID()
378 virtual Version GetVersion()
380 return Version("Implementation of callerid (umode +g & /accept, ala hybrid etc)", VF_COMMON | VF_VENDOR);
383 virtual void On005Numeric(std::string& output)
385 output += " CALLERID=g";
388 ModResult PreText(User* user, User* dest, std::string& text, bool notice)
390 if (!dest->IsModeSet('g'))
391 return MOD_RES_PASSTHRU;
393 if (operoverride && IS_OPER(user))
394 return MOD_RES_PASSTHRU;
396 callerid_data* dat = cmd.extInfo.get(dest, true);
397 std::set<User*>::iterator i = dat->accepting.find(user);
399 if (i == dat->accepting.end())
401 time_t now = ServerInstance->Time();
402 /* +g and *not* accepted */
403 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
404 if (now > (dat->lastnotify + (time_t)notify_cooldown))
406 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
407 dest->SendText(":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
408 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());
409 dat->lastnotify = now;
413 return MOD_RES_PASSTHRU;
416 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
418 if (IS_LOCAL(user) && target_type == TYPE_USER)
419 return PreText(user, (User*)dest, text, true);
421 return MOD_RES_PASSTHRU;
424 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
426 if (IS_LOCAL(user) && target_type == TYPE_USER)
427 return PreText(user, (User*)dest, text, true);
429 return MOD_RES_PASSTHRU;
432 ModResult OnUserPreNick(User* user, const std::string& newnick)
435 RemoveFromAllAccepts(user);
436 return MOD_RES_PASSTHRU;
439 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
441 RemoveFromAllAccepts(user);
444 virtual void OnRehash(User* user)
447 cmd.maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
448 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
449 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
450 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
454 MODULE_INIT(ModuleCallerID)