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"
25 #include "modules/ctctags.h"
30 RPL_ENDOFACCEPT = 282,
32 ERR_ACCEPTEXIST = 457,
42 typedef insp::flat_set<User*> UserSet;
43 typedef std::vector<callerid_data*> CallerIdDataSet;
47 /** Users I accept messages from
51 /** Users who list me as accepted
53 CallerIdDataSet wholistsme;
55 callerid_data() : lastnotify(0) { }
57 std::string ToString(SerializeFormat format) const
59 std::ostringstream oss;
61 for (UserSet::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
65 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
71 struct CallerIDExtInfo : public ExtensionItem
73 CallerIDExtInfo(Module* parent)
74 : ExtensionItem("callerid_data", ExtensionItem::EXT_USER, parent)
78 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE
81 if (format != FORMAT_NETWORK)
83 callerid_data* dat = static_cast<callerid_data*>(item);
84 ret = dat->ToString(format);
89 void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE
91 if (format == FORMAT_NETWORK)
94 void* old = get_raw(container);
96 this->free(NULL, old);
97 callerid_data* dat = new callerid_data;
98 set_raw(container, dat);
100 irc::commasepstream s(value);
103 dat->lastnotify = ConvToNum<time_t>(tok);
105 while (s.GetToken(tok))
107 User *u = ServerInstance->FindNick(tok);
108 if ((u) && (u->registered == REG_ALL) && (!u->quitting))
110 if (dat->accepting.insert(u).second)
112 callerid_data* other = this->get(u, true);
113 other->wholistsme.push_back(dat);
119 callerid_data* get(User* user, bool create)
121 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
124 dat = new callerid_data;
130 void free(Extensible* container, void* item) CXX11_OVERRIDE
132 callerid_data* dat = static_cast<callerid_data*>(item);
134 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
135 for (callerid_data::UserSet::iterator it = dat->accepting.begin(); it != dat->accepting.end(); ++it)
137 callerid_data *targ = this->get(*it, false);
141 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
142 continue; // shouldn't happen, but oh well.
145 if (!stdalgo::vector::swaperase(targ->wholistsme, dat))
146 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
152 class CommandAccept : public Command
154 /** Pair: first is the target, second is true to add, false to remove
156 typedef std::pair<User*, bool> ACCEPTAction;
158 static ACCEPTAction GetTargetAndAction(std::string& tok, User* cmdfrom = NULL)
160 bool remove = (tok[0] == '-');
161 if ((remove) || (tok[0] == '+'))
162 tok.erase(tok.begin());
165 if (!cmdfrom || !IS_LOCAL(cmdfrom))
166 target = ServerInstance->FindNick(tok);
168 target = ServerInstance->FindNickOnly(tok);
170 if ((!target) || (target->registered != REG_ALL) || (target->quitting))
173 return std::make_pair(target, !remove);
177 CallerIDExtInfo extInfo;
178 unsigned int maxaccepts;
179 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
182 allow_empty_last_param = false;
183 syntax = "*|(+|-)<nick>[,(+|-)<nick>]+";
184 TRANSLATE1(TR_CUSTOM);
187 void EncodeParameter(std::string& parameter, unsigned int index) CXX11_OVERRIDE
189 // Send lists as-is (part of 2.0 compat)
190 if (parameter.find(',') != std::string::npos)
193 // Convert a (+|-)<nick> into a [-]<uuid>
194 ACCEPTAction action = GetTargetAndAction(parameter);
198 parameter = (action.second ? "" : "-") + action.first->uuid;
201 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
202 * - in front of any nick removes, and an * lists. This effectively means you can do:
203 * /accept nick1,nick2,nick3,*
204 * to add 3 nicks and then show your list
206 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
208 if (CommandParser::LoopCall(user, this, parameters, 0))
211 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
212 * have a list already setup. */
214 if (parameters[0] == "*")
220 std::string tok = parameters[0];
221 ACCEPTAction action = GetTargetAndAction(tok, user);
224 user->WriteNumeric(Numerics::NoSuchNick(tok));
228 if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
229 // Neither source nor target is local, forward the command to the server of target
232 // The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
234 return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
236 return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
239 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
241 // There is a list in parameters[0] in two cases:
242 // Either when the source is remote, this happens because 2.0 servers send comma seperated uuid lists,
243 // we don't split those but broadcast them, as before.
245 // Or if the source is local then LoopCall() runs OnPostCommand() after each entry in the list,
246 // meaning the linking module has sent an ACCEPT already for each entry in the list to the
247 // appropiate server and the ACCEPT with the list of nicks (this) doesn't need to be sent anywhere.
248 if ((!IS_LOCAL(user)) && (parameters[0].find(',') != std::string::npos))
249 return ROUTE_BROADCAST;
252 std::string targetstring = parameters[0];
253 ACCEPTAction action = GetTargetAndAction(targetstring, user);
255 // Target is a "*" or source is local and the target is a list of nicks
256 return ROUTE_LOCALONLY;
258 // Route to the server of the target
259 return ROUTE_UNICAST(action.first->server);
262 void ListAccept(User* user)
264 callerid_data* dat = extInfo.get(user, false);
267 for (callerid_data::UserSet::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
268 user->WriteNumeric(RPL_ACCEPTLIST, (*i)->nick);
270 user->WriteNumeric(RPL_ENDOFACCEPT, "End of ACCEPT list");
273 bool AddAccept(User* user, User* whotoadd)
275 // Add this user to my accept list first, so look me up..
276 callerid_data* dat = extInfo.get(user, true);
277 if (dat->accepting.size() >= maxaccepts)
279 user->WriteNumeric(ERR_ACCEPTFULL, InspIRCd::Format("Accept list is full (limit is %d)", maxaccepts));
282 if (!dat->accepting.insert(whotoadd).second)
284 user->WriteNumeric(ERR_ACCEPTEXIST, whotoadd->nick, "is already on your accept list");
288 // Now, look them up, and add me to their list
289 callerid_data *targ = extInfo.get(whotoadd, true);
290 targ->wholistsme.push_back(dat);
292 user->WriteNotice(whotoadd->nick + " is now on your accept list");
296 bool RemoveAccept(User* user, User* whotoremove)
298 // Remove them from my list, so look up my list..
299 callerid_data* dat = extInfo.get(user, false);
302 user->WriteNumeric(ERR_ACCEPTNOT, whotoremove->nick, "is not on your accept list");
305 if (!dat->accepting.erase(whotoremove))
307 user->WriteNumeric(ERR_ACCEPTNOT, whotoremove->nick, "is not on your accept list");
311 // Look up their list to remove me.
312 callerid_data *dat2 = extInfo.get(whotoremove, false);
315 // How the fuck is this possible.
316 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
320 if (!stdalgo::vector::swaperase(dat2->wholistsme, dat))
321 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
324 user->WriteNotice(whotoremove->nick + " is no longer on your accept list");
329 class CallerIDAPIImpl : public CallerID::APIBase
332 CallerIDExtInfo& ext;
335 CallerIDAPIImpl(Module* Creator, CallerIDExtInfo& Ext)
336 : CallerID::APIBase(Creator)
341 bool IsOnAcceptList(User* source, User* target) CXX11_OVERRIDE
343 callerid_data* dat = ext.get(target, true);
344 return dat->accepting.count(source);
351 , public CTCTags::EventListener
355 SimpleUserModeHandler myumode;
357 // Configuration variables:
358 bool tracknick; // Allow ACCEPT entries to update with nick changes.
359 unsigned int notify_cooldown; // Seconds between notifications.
361 /** Removes a user from all accept lists
362 * @param who The user to remove from accepts
364 void RemoveFromAllAccepts(User* who)
366 // First, find the list of people who have me on accept
367 callerid_data *userdata = cmd.extInfo.get(who, false);
371 // Iterate over the list of people who accept me, and remove all entries
372 for (callerid_data::CallerIdDataSet::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); ++it)
374 callerid_data *dat = *(it);
376 // Find me on their callerid list
377 if (!dat->accepting.erase(who))
378 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
381 userdata->wholistsme.clear();
386 : CTCTags::EventListener(this)
388 , api(this, cmd.extInfo)
389 , myumode(this, "callerid", 'g')
393 Version GetVersion() CXX11_OVERRIDE
395 return Version("Implementation of callerid, provides user mode +g and the ACCEPT command", VF_COMMON | VF_VENDOR);
398 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
400 tokens["ACCEPT"] = ConvToStr(cmd.maxaccepts);
401 tokens["CALLERID"] = ConvToStr(myumode.GetModeChar());
404 ModResult HandleMessage(User* user, const MessageTarget& target)
406 if (!IS_LOCAL(user) || target.type != MessageTarget::TYPE_USER)
407 return MOD_RES_PASSTHRU;
409 User* dest = target.Get<User>();
410 if (!dest->IsModeSet(myumode) || (user == dest))
411 return MOD_RES_PASSTHRU;
413 if (user->HasPrivPermission("users/ignore-callerid"))
414 return MOD_RES_PASSTHRU;
416 callerid_data* dat = cmd.extInfo.get(dest, true);
417 if (!dat->accepting.count(user))
419 time_t now = ServerInstance->Time();
420 /* +g and *not* accepted */
421 user->WriteNumeric(ERR_TARGUMODEG, dest->nick, "is in +g mode (server-side ignore).");
422 if (now > (dat->lastnotify + (time_t)notify_cooldown))
424 user->WriteNumeric(RPL_TARGNOTIFY, dest->nick, "has been informed that you messaged them.");
425 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 user mode +g set. Use /ACCEPT +%s to allow.",
426 user->nick.c_str()));
427 dat->lastnotify = now;
431 return MOD_RES_PASSTHRU;
434 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
436 return HandleMessage(user, target);
439 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
441 return HandleMessage(user, target);
444 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
447 RemoveFromAllAccepts(user);
450 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
452 RemoveFromAllAccepts(user);
455 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
457 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
458 cmd.maxaccepts = tag->getUInt("maxaccepts", 30);
459 tracknick = tag->getBool("tracknick");
460 notify_cooldown = tag->getDuration("cooldown", 60);
463 void Prioritize() CXX11_OVERRIDE
465 // Want to be after modules like silence or services_account
466 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
470 MODULE_INIT(ModuleCallerID)