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/>.
30 /** Users I accept messages from
32 std::set<User*> accepting;
34 /** Users who list me as accepted
36 std::list<callerid_data *> wholistsme;
38 callerid_data() : lastnotify(0) { }
40 std::string ToString(SerializeFormat format) const
42 std::ostringstream oss;
44 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
48 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
54 struct CallerIDExtInfo : public ExtensionItem
56 CallerIDExtInfo(Module* parent)
57 : ExtensionItem("callerid_data", parent)
61 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
64 if (format != FORMAT_NETWORK)
66 callerid_data* dat = static_cast<callerid_data*>(item);
67 ret = dat->ToString(format);
72 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
74 if (format == FORMAT_NETWORK)
77 callerid_data* dat = new callerid_data;
78 irc::commasepstream s(value);
81 dat->lastnotify = ConvToInt(tok);
83 while (s.GetToken(tok))
85 User *u = ServerInstance->FindNick(tok);
86 if ((u) && (u->registered == REG_ALL) && (!u->quitting) && (!IS_SERVER(u)))
88 if (dat->accepting.insert(u).second)
90 callerid_data* other = this->get(u, true);
91 other->wholistsme.push_back(dat);
96 void* old = 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);
123 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
124 continue; // shouldn't happen, but oh well.
127 std::list<callerid_data*>::iterator it2 = std::find(targ->wholistsme.begin(), targ->wholistsme.end(), dat);
128 if (it2 != targ->wholistsme.end())
129 targ->wholistsme.erase(it2);
131 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
137 class User_g : public SimpleUserModeHandler
140 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
143 class CommandAccept : public Command
145 /** Pair: first is the target, second is true to add, false to remove
147 typedef std::pair<User*, bool> ACCEPTAction;
149 ACCEPTAction GetTargetAndAction(std::string& tok)
151 bool remove = (tok[0] == '-');
152 if ((remove) || (tok[0] == '+'))
153 tok.erase(tok.begin());
155 User* target = ServerInstance->FindNick(tok);
156 if ((!target) || (target->registered != REG_ALL) || (target->quitting) || (IS_SERVER(target)))
159 return std::make_pair(target, !remove);
163 CallerIDExtInfo extInfo;
164 unsigned int maxaccepts;
165 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
168 allow_empty_last_param = false;
169 syntax = "{[+|-]<nicks>}|*}";
170 TRANSLATE1(TR_CUSTOM);
173 void EncodeParameter(std::string& parameter, int index)
175 // Send lists as-is (part of 2.0 compat)
176 if (parameter.find(',') != std::string::npos)
179 // Convert a [+|-]<nick> into a [-]<uuid>
180 ACCEPTAction action = GetTargetAndAction(parameter);
184 parameter = (action.second ? "" : "-") + action.first->uuid;
187 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
188 * - in front of any nick removes, and an * lists. This effectively means you can do:
189 * /accept nick1,nick2,nick3,*
190 * to add 3 nicks and then show your list
192 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
194 if (CommandParser::LoopCall(user, this, parameters, 0))
197 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
198 * have a list already setup. */
200 if (parameters[0] == "*")
206 std::string tok = parameters[0];
207 ACCEPTAction action = GetTargetAndAction(tok);
210 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
214 if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
215 // Neither source nor target is local, forward the command to the server of target
218 // The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
220 return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
222 return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
225 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
227 // There is a list in parameters[0] in two cases:
228 // Either when the source is remote, this happens because 2.0 servers send comma seperated uuid lists,
229 // we don't split those but broadcast them, as before.
231 // Or if the source is local then LoopCall() runs OnPostCommand() after each entry in the list,
232 // meaning the linking module has sent an ACCEPT already for each entry in the list to the
233 // appropiate server and the ACCEPT with the list of nicks (this) doesn't need to be sent anywhere.
234 if ((!IS_LOCAL(user)) && (parameters[0].find(',') != std::string::npos))
235 return ROUTE_BROADCAST;
238 std::string targetstring = parameters[0];
239 ACCEPTAction action = GetTargetAndAction(targetstring);
241 // Target is a "*" or source is local and the target is a list of nicks
242 return ROUTE_LOCALONLY;
244 // Route to the server of the target
245 return ROUTE_UNICAST(action.first->server);
248 void ListAccept(User* user)
250 callerid_data* dat = extInfo.get(user, false);
253 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
254 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
256 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
259 bool AddAccept(User* user, User* whotoadd)
261 // Add this user to my accept list first, so look me up..
262 callerid_data* dat = extInfo.get(user, true);
263 if (dat->accepting.size() >= maxaccepts)
265 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
268 if (!dat->accepting.insert(whotoadd).second)
270 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
274 // Now, look them up, and add me to their list
275 callerid_data *targ = extInfo.get(whotoadd, true);
276 targ->wholistsme.push_back(dat);
278 user->WriteNotice(whotoadd->nick + " is now on your accept list");
282 bool RemoveAccept(User* user, User* whotoremove)
284 // Remove them from my list, so look up my list..
285 callerid_data* dat = extInfo.get(user, false);
288 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
291 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
292 if (i == dat->accepting.end())
294 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
298 dat->accepting.erase(i);
300 // Look up their list to remove me.
301 callerid_data *dat2 = extInfo.get(whotoremove, false);
304 // How the fuck is this possible.
305 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
309 std::list<callerid_data*>::iterator it = std::find(dat2->wholistsme.begin(), dat2->wholistsme.end(), dat);
310 if (it != dat2->wholistsme.end())
312 dat2->wholistsme.erase(it);
314 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
317 user->WriteNotice(whotoremove->nick + " is no longer on your accept list");
322 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);
353 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
356 userdata->wholistsme.clear();
360 ModuleCallerID() : cmd(this), myumode(this)
364 Version GetVersion() CXX11_OVERRIDE
366 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
369 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
371 tokens["CALLERID"] = "g";
374 ModResult PreText(User* user, User* dest, std::string& text)
376 if (!dest->IsModeSet(myumode) || (user == dest))
377 return MOD_RES_PASSTHRU;
379 if (operoverride && user->IsOper())
380 return MOD_RES_PASSTHRU;
382 callerid_data* dat = cmd.extInfo.get(dest, true);
383 std::set<User*>::iterator i = dat->accepting.find(user);
385 if (i == dat->accepting.end())
387 time_t now = ServerInstance->Time();
388 /* +g and *not* accepted */
389 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
390 if (now > (dat->lastnotify + (time_t)notify_cooldown))
392 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
393 dest->SendText(":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
394 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());
395 dat->lastnotify = now;
399 return MOD_RES_PASSTHRU;
402 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
404 if (IS_LOCAL(user) && target_type == TYPE_USER)
405 return PreText(user, (User*)dest, text);
407 return MOD_RES_PASSTHRU;
410 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
413 RemoveFromAllAccepts(user);
416 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
418 RemoveFromAllAccepts(user);
421 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
423 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
424 cmd.maxaccepts = tag->getInt("maxaccepts", 16);
425 operoverride = tag->getBool("operoverride");
426 tracknick = tag->getBool("tracknick");
427 notify_cooldown = tag->getInt("cooldown", 60);
431 MODULE_INIT(ModuleCallerID)