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 RPL_ENDOFACCEPT = 282,
30 ERR_ACCEPTEXIST = 457,
40 typedef insp::flat_set<User*> UserSet;
41 typedef std::vector<callerid_data*> CallerIdDataSet;
45 /** Users I accept messages from
49 /** Users who list me as accepted
51 CallerIdDataSet wholistsme;
53 callerid_data() : lastnotify(0) { }
55 std::string ToString(SerializeFormat format) const
57 std::ostringstream oss;
59 for (UserSet::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
63 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
69 struct CallerIDExtInfo : public ExtensionItem
71 CallerIDExtInfo(Module* parent)
72 : ExtensionItem("callerid_data", ExtensionItem::EXT_USER, parent)
76 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
79 if (format != FORMAT_NETWORK)
81 callerid_data* dat = static_cast<callerid_data*>(item);
82 ret = dat->ToString(format);
87 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
89 if (format == FORMAT_NETWORK)
92 void* old = get_raw(container);
95 callerid_data* dat = new callerid_data;
96 set_raw(container, dat);
98 irc::commasepstream s(value);
101 dat->lastnotify = ConvToInt(tok);
103 while (s.GetToken(tok))
105 User *u = ServerInstance->FindNick(tok);
106 if ((u) && (u->registered == REG_ALL) && (!u->quitting))
108 if (dat->accepting.insert(u).second)
110 callerid_data* other = this->get(u, true);
111 other->wholistsme.push_back(dat);
117 callerid_data* get(User* user, bool create)
119 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
122 dat = new callerid_data;
128 void free(void* item)
130 callerid_data* dat = static_cast<callerid_data*>(item);
132 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
133 for (callerid_data::UserSet::iterator it = dat->accepting.begin(); it != dat->accepting.end(); ++it)
135 callerid_data *targ = this->get(*it, false);
139 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
140 continue; // shouldn't happen, but oh well.
143 if (!stdalgo::vector::swaperase(targ->wholistsme, dat))
144 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
150 class User_g : public SimpleUserModeHandler
153 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
156 class CommandAccept : public Command
158 /** Pair: first is the target, second is true to add, false to remove
160 typedef std::pair<User*, bool> ACCEPTAction;
162 static ACCEPTAction GetTargetAndAction(std::string& tok, User* cmdfrom = NULL)
164 bool remove = (tok[0] == '-');
165 if ((remove) || (tok[0] == '+'))
166 tok.erase(tok.begin());
169 if (!cmdfrom || !IS_LOCAL(cmdfrom))
170 target = ServerInstance->FindNick(tok);
172 target = ServerInstance->FindNickOnly(tok);
174 if ((!target) || (target->registered != REG_ALL) || (target->quitting))
177 return std::make_pair(target, !remove);
181 CallerIDExtInfo extInfo;
182 unsigned int maxaccepts;
183 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
186 allow_empty_last_param = false;
187 syntax = "*|(+|-)<nick>[,(+|-)<nick> ...]";
188 TRANSLATE1(TR_CUSTOM);
191 void EncodeParameter(std::string& parameter, int index)
193 // Send lists as-is (part of 2.0 compat)
194 if (parameter.find(',') != std::string::npos)
197 // Convert a [+|-]<nick> into a [-]<uuid>
198 ACCEPTAction action = GetTargetAndAction(parameter);
202 parameter = (action.second ? "" : "-") + action.first->uuid;
205 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
206 * - in front of any nick removes, and an * lists. This effectively means you can do:
207 * /accept nick1,nick2,nick3,*
208 * to add 3 nicks and then show your list
210 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
212 if (CommandParser::LoopCall(user, this, parameters, 0))
215 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
216 * have a list already setup. */
218 if (parameters[0] == "*")
224 std::string tok = parameters[0];
225 ACCEPTAction action = GetTargetAndAction(tok, user);
228 user->WriteNumeric(Numerics::NoSuchNick(tok));
232 if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
233 // Neither source nor target is local, forward the command to the server of target
236 // The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
238 return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
240 return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
243 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
245 // There is a list in parameters[0] in two cases:
246 // Either when the source is remote, this happens because 2.0 servers send comma seperated uuid lists,
247 // we don't split those but broadcast them, as before.
249 // Or if the source is local then LoopCall() runs OnPostCommand() after each entry in the list,
250 // meaning the linking module has sent an ACCEPT already for each entry in the list to the
251 // appropiate server and the ACCEPT with the list of nicks (this) doesn't need to be sent anywhere.
252 if ((!IS_LOCAL(user)) && (parameters[0].find(',') != std::string::npos))
253 return ROUTE_BROADCAST;
256 std::string targetstring = parameters[0];
257 ACCEPTAction action = GetTargetAndAction(targetstring, user);
259 // Target is a "*" or source is local and the target is a list of nicks
260 return ROUTE_LOCALONLY;
262 // Route to the server of the target
263 return ROUTE_UNICAST(action.first->server);
266 void ListAccept(User* user)
268 callerid_data* dat = extInfo.get(user, false);
271 for (callerid_data::UserSet::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
272 user->WriteNumeric(RPL_ACCEPTLIST, (*i)->nick);
274 user->WriteNumeric(RPL_ENDOFACCEPT, "End of ACCEPT list");
277 bool AddAccept(User* user, User* whotoadd)
279 // Add this user to my accept list first, so look me up..
280 callerid_data* dat = extInfo.get(user, true);
281 if (dat->accepting.size() >= maxaccepts)
283 user->WriteNumeric(ERR_ACCEPTFULL, InspIRCd::Format("Accept list is full (limit is %d)", maxaccepts));
286 if (!dat->accepting.insert(whotoadd).second)
288 user->WriteNumeric(ERR_ACCEPTEXIST, whotoadd->nick, "is already on your accept list");
292 // Now, look them up, and add me to their list
293 callerid_data *targ = extInfo.get(whotoadd, true);
294 targ->wholistsme.push_back(dat);
296 user->WriteNotice(whotoadd->nick + " is now on your accept list");
300 bool RemoveAccept(User* user, User* whotoremove)
302 // Remove them from my list, so look up my list..
303 callerid_data* dat = extInfo.get(user, false);
306 user->WriteNumeric(ERR_ACCEPTNOT, whotoremove->nick, "is not on your accept list");
309 if (!dat->accepting.erase(whotoremove))
311 user->WriteNumeric(ERR_ACCEPTNOT, whotoremove->nick, "is not on your accept list");
315 // Look up their list to remove me.
316 callerid_data *dat2 = extInfo.get(whotoremove, false);
319 // How the fuck is this possible.
320 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
324 if (!stdalgo::vector::swaperase(dat2->wholistsme, dat))
325 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
328 user->WriteNotice(whotoremove->nick + " is no longer on your accept list");
333 class ModuleCallerID : public Module
338 // Configuration variables:
339 bool operoverride; // Operators can override callerid.
340 bool tracknick; // Allow ACCEPT entries to update with nick changes.
341 unsigned int notify_cooldown; // Seconds between notifications.
343 /** Removes a user from all accept lists
344 * @param who The user to remove from accepts
346 void RemoveFromAllAccepts(User* who)
348 // First, find the list of people who have me on accept
349 callerid_data *userdata = cmd.extInfo.get(who, false);
353 // Iterate over the list of people who accept me, and remove all entries
354 for (callerid_data::CallerIdDataSet::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); ++it)
356 callerid_data *dat = *(it);
358 // Find me on their callerid list
359 if (!dat->accepting.erase(who))
360 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
363 userdata->wholistsme.clear();
367 ModuleCallerID() : cmd(this), myumode(this)
371 Version GetVersion() CXX11_OVERRIDE
373 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
376 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
378 tokens["CALLERID"] = "g";
381 ModResult OnUserPreMessage(User* user, void* voiddest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
383 if (!IS_LOCAL(user) || target_type != TYPE_USER)
384 return MOD_RES_PASSTHRU;
386 User* dest = static_cast<User*>(voiddest);
387 if (!dest->IsModeSet(myumode) || (user == dest))
388 return MOD_RES_PASSTHRU;
390 if (operoverride && user->IsOper())
391 return MOD_RES_PASSTHRU;
393 callerid_data* dat = cmd.extInfo.get(dest, true);
394 if (!dat->accepting.count(user))
396 time_t now = ServerInstance->Time();
397 /* +g and *not* accepted */
398 user->WriteNumeric(ERR_TARGUMODEG, dest->nick, "is in +g mode (server-side ignore).");
399 if (now > (dat->lastnotify + (time_t)notify_cooldown))
401 user->WriteNumeric(RPL_TARGNOTIFY, dest->nick, "has been informed that you messaged them.");
402 dest->WriteRemoteNumeric(RPL_UMODEGMSG, user->nick, InspIRCd::Format("%s@%s", user->ident.c_str(), user->dhost.c_str()), InspIRCd::Format("is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
403 user->nick.c_str()));
404 dat->lastnotify = now;
408 return MOD_RES_PASSTHRU;
411 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
414 RemoveFromAllAccepts(user);
417 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
419 RemoveFromAllAccepts(user);
422 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
424 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
425 cmd.maxaccepts = tag->getInt("maxaccepts", 16);
426 operoverride = tag->getBool("operoverride");
427 tracknick = tag->getBool("tracknick");
428 notify_cooldown = tag->getDuration("cooldown", 60);
431 void Prioritize() CXX11_OVERRIDE
433 // Want to be after modules like silence or services_account
434 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
438 MODULE_INIT(ModuleCallerID)