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/>.
24 #include "modules/callerid.h"
29 RPL_ENDOFACCEPT = 282,
31 ERR_ACCEPTEXIST = 457,
41 typedef insp::flat_set<User*> UserSet;
42 typedef std::vector<callerid_data*> CallerIdDataSet;
46 /** Users I accept messages from
50 /** Users who list me as accepted
52 CallerIdDataSet wholistsme;
54 callerid_data() : lastnotify(0) { }
56 std::string ToString(SerializeFormat format) const
58 std::ostringstream oss;
60 for (UserSet::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
64 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
70 struct CallerIDExtInfo : public ExtensionItem
72 CallerIDExtInfo(Module* parent)
73 : ExtensionItem("callerid_data", ExtensionItem::EXT_USER, parent)
77 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
80 if (format != FORMAT_NETWORK)
82 callerid_data* dat = static_cast<callerid_data*>(item);
83 ret = dat->ToString(format);
88 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
90 if (format == FORMAT_NETWORK)
93 void* old = get_raw(container);
95 this->free(NULL, old);
96 callerid_data* dat = new callerid_data;
97 set_raw(container, dat);
99 irc::commasepstream s(value);
102 dat->lastnotify = ConvToNum<time_t>(tok);
104 while (s.GetToken(tok))
106 User *u = ServerInstance->FindNick(tok);
107 if ((u) && (u->registered == REG_ALL) && (!u->quitting))
109 if (dat->accepting.insert(u).second)
111 callerid_data* other = this->get(u, true);
112 other->wholistsme.push_back(dat);
118 callerid_data* get(User* user, bool create)
120 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
123 dat = new callerid_data;
129 void free(Extensible* container, void* item) CXX11_OVERRIDE
131 callerid_data* dat = static_cast<callerid_data*>(item);
133 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
134 for (callerid_data::UserSet::iterator it = dat->accepting.begin(); it != dat->accepting.end(); ++it)
136 callerid_data *targ = this->get(*it, false);
140 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
141 continue; // shouldn't happen, but oh well.
144 if (!stdalgo::vector::swaperase(targ->wholistsme, dat))
145 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
151 class CommandAccept : public Command
153 /** Pair: first is the target, second is true to add, false to remove
155 typedef std::pair<User*, bool> ACCEPTAction;
157 static ACCEPTAction GetTargetAndAction(std::string& tok, User* cmdfrom = NULL)
159 bool remove = (tok[0] == '-');
160 if ((remove) || (tok[0] == '+'))
161 tok.erase(tok.begin());
164 if (!cmdfrom || !IS_LOCAL(cmdfrom))
165 target = ServerInstance->FindNick(tok);
167 target = ServerInstance->FindNickOnly(tok);
169 if ((!target) || (target->registered != REG_ALL) || (target->quitting))
172 return std::make_pair(target, !remove);
176 CallerIDExtInfo extInfo;
177 unsigned int maxaccepts;
178 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
181 allow_empty_last_param = false;
182 syntax = "*|(+|-)<nick>[,(+|-)<nick>]+";
183 TRANSLATE1(TR_CUSTOM);
186 void EncodeParameter(std::string& parameter, unsigned int index) CXX11_OVERRIDE
188 // Send lists as-is (part of 2.0 compat)
189 if (parameter.find(',') != std::string::npos)
192 // Convert a (+|-)<nick> into a [-]<uuid>
193 ACCEPTAction action = GetTargetAndAction(parameter);
197 parameter = (action.second ? "" : "-") + action.first->uuid;
200 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
201 * - in front of any nick removes, and an * lists. This effectively means you can do:
202 * /accept nick1,nick2,nick3,*
203 * to add 3 nicks and then show your list
205 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
207 if (CommandParser::LoopCall(user, this, parameters, 0))
210 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
211 * have a list already setup. */
213 if (parameters[0] == "*")
219 std::string tok = parameters[0];
220 ACCEPTAction action = GetTargetAndAction(tok, user);
223 user->WriteNumeric(Numerics::NoSuchNick(tok));
227 if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
228 // Neither source nor target is local, forward the command to the server of target
231 // The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
233 return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
235 return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
238 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
240 // There is a list in parameters[0] in two cases:
241 // Either when the source is remote, this happens because 2.0 servers send comma seperated uuid lists,
242 // we don't split those but broadcast them, as before.
244 // Or if the source is local then LoopCall() runs OnPostCommand() after each entry in the list,
245 // meaning the linking module has sent an ACCEPT already for each entry in the list to the
246 // appropiate server and the ACCEPT with the list of nicks (this) doesn't need to be sent anywhere.
247 if ((!IS_LOCAL(user)) && (parameters[0].find(',') != std::string::npos))
248 return ROUTE_BROADCAST;
251 std::string targetstring = parameters[0];
252 ACCEPTAction action = GetTargetAndAction(targetstring, user);
254 // Target is a "*" or source is local and the target is a list of nicks
255 return ROUTE_LOCALONLY;
257 // Route to the server of the target
258 return ROUTE_UNICAST(action.first->server);
261 void ListAccept(User* user)
263 callerid_data* dat = extInfo.get(user, false);
266 for (callerid_data::UserSet::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
267 user->WriteNumeric(RPL_ACCEPTLIST, (*i)->nick);
269 user->WriteNumeric(RPL_ENDOFACCEPT, "End of ACCEPT list");
272 bool AddAccept(User* user, User* whotoadd)
274 // Add this user to my accept list first, so look me up..
275 callerid_data* dat = extInfo.get(user, true);
276 if (dat->accepting.size() >= maxaccepts)
278 user->WriteNumeric(ERR_ACCEPTFULL, InspIRCd::Format("Accept list is full (limit is %d)", maxaccepts));
281 if (!dat->accepting.insert(whotoadd).second)
283 user->WriteNumeric(ERR_ACCEPTEXIST, whotoadd->nick, "is already on your accept list");
287 // Now, look them up, and add me to their list
288 callerid_data *targ = extInfo.get(whotoadd, true);
289 targ->wholistsme.push_back(dat);
291 user->WriteNotice(whotoadd->nick + " is now on your accept list");
295 bool RemoveAccept(User* user, User* whotoremove)
297 // Remove them from my list, so look up my list..
298 callerid_data* dat = extInfo.get(user, false);
301 user->WriteNumeric(ERR_ACCEPTNOT, whotoremove->nick, "is not on your accept list");
304 if (!dat->accepting.erase(whotoremove))
306 user->WriteNumeric(ERR_ACCEPTNOT, whotoremove->nick, "is not on your accept list");
310 // Look up their list to remove me.
311 callerid_data *dat2 = extInfo.get(whotoremove, false);
314 // How the fuck is this possible.
315 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
319 if (!stdalgo::vector::swaperase(dat2->wholistsme, dat))
320 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
323 user->WriteNotice(whotoremove->nick + " is no longer on your accept list");
328 class CallerIDAPIImpl : public CallerID::APIBase
331 CallerIDExtInfo& ext;
334 CallerIDAPIImpl(Module* Creator, CallerIDExtInfo& Ext)
335 : CallerID::APIBase(Creator)
340 bool IsOnAcceptList(User* source, User* target) CXX11_OVERRIDE
342 callerid_data* dat = ext.get(target, true);
343 return dat->accepting.count(source);
348 class ModuleCallerID : public Module
352 SimpleUserModeHandler myumode;
354 // Configuration variables:
355 bool tracknick; // Allow ACCEPT entries to update with nick changes.
356 unsigned int notify_cooldown; // Seconds between notifications.
358 /** Removes a user from all accept lists
359 * @param who The user to remove from accepts
361 void RemoveFromAllAccepts(User* who)
363 // First, find the list of people who have me on accept
364 callerid_data *userdata = cmd.extInfo.get(who, false);
368 // Iterate over the list of people who accept me, and remove all entries
369 for (callerid_data::CallerIdDataSet::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); ++it)
371 callerid_data *dat = *(it);
373 // Find me on their callerid list
374 if (!dat->accepting.erase(who))
375 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
378 userdata->wholistsme.clear();
384 , api(this, cmd.extInfo)
385 , myumode(this, "callerid", 'g')
389 Version GetVersion() CXX11_OVERRIDE
391 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
394 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
396 tokens["ACCEPT"] = ConvToStr(cmd.maxaccepts);
397 tokens["CALLERID"] = ConvToStr(myumode.GetModeChar());
400 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
402 if (!IS_LOCAL(user) || target.type != MessageTarget::TYPE_USER)
403 return MOD_RES_PASSTHRU;
405 User* dest = target.Get<User>();
406 if (!dest->IsModeSet(myumode) || (user == dest))
407 return MOD_RES_PASSTHRU;
409 if (user->HasPrivPermission("users/ignore-callerid"))
410 return MOD_RES_PASSTHRU;
412 callerid_data* dat = cmd.extInfo.get(dest, true);
413 if (!dat->accepting.count(user))
415 time_t now = ServerInstance->Time();
416 /* +g and *not* accepted */
417 user->WriteNumeric(ERR_TARGUMODEG, dest->nick, "is in +g mode (server-side ignore).");
418 if (now > (dat->lastnotify + (time_t)notify_cooldown))
420 user->WriteNumeric(RPL_TARGNOTIFY, dest->nick, "has been informed that you messaged them.");
421 dest->WriteRemoteNumeric(RPL_UMODEGMSG, user->nick, InspIRCd::Format("%s@%s", user->ident.c_str(), user->GetDisplayedHost().c_str()), InspIRCd::Format("is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
422 user->nick.c_str()));
423 dat->lastnotify = now;
427 return MOD_RES_PASSTHRU;
430 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
433 RemoveFromAllAccepts(user);
436 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
438 RemoveFromAllAccepts(user);
441 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
443 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
444 cmd.maxaccepts = tag->getUInt("maxaccepts", 30);
445 tracknick = tag->getBool("tracknick");
446 notify_cooldown = tag->getDuration("cooldown", 60);
449 void Prioritize() CXX11_OVERRIDE
451 // Want to be after modules like silence or services_account
452 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
456 MODULE_INIT(ModuleCallerID)