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,
42 /** Users I accept messages from
44 std::set<User*> accepting;
46 /** Users who list me as accepted
48 std::list<callerid_data *> wholistsme;
50 callerid_data() : lastnotify(0) { }
52 std::string ToString(SerializeFormat format) const
54 std::ostringstream oss;
56 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
60 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
66 struct CallerIDExtInfo : public ExtensionItem
68 CallerIDExtInfo(Module* parent)
69 : ExtensionItem("callerid_data", parent)
73 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
76 if (format != FORMAT_NETWORK)
78 callerid_data* dat = static_cast<callerid_data*>(item);
79 ret = dat->ToString(format);
84 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
86 if (format == FORMAT_NETWORK)
89 callerid_data* dat = new callerid_data;
90 irc::commasepstream s(value);
93 dat->lastnotify = ConvToInt(tok);
95 while (s.GetToken(tok))
97 User *u = ServerInstance->FindNick(tok);
98 if ((u) && (u->registered == REG_ALL) && (!u->quitting) && (!IS_SERVER(u)))
100 if (dat->accepting.insert(u).second)
102 callerid_data* other = this->get(u, true);
103 other->wholistsme.push_back(dat);
108 void* old = set_raw(container, dat);
113 callerid_data* get(User* user, bool create)
115 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
118 dat = new callerid_data;
124 void free(void* item)
126 callerid_data* dat = static_cast<callerid_data*>(item);
128 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
129 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
131 callerid_data *targ = this->get(*it, false);
135 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
136 continue; // shouldn't happen, but oh well.
139 std::list<callerid_data*>::iterator it2 = std::find(targ->wholistsme.begin(), targ->wholistsme.end(), dat);
140 if (it2 != targ->wholistsme.end())
141 targ->wholistsme.erase(it2);
143 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
149 class User_g : public SimpleUserModeHandler
152 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
155 class CommandAccept : public Command
157 /** Pair: first is the target, second is true to add, false to remove
159 typedef std::pair<User*, bool> ACCEPTAction;
161 static ACCEPTAction GetTargetAndAction(std::string& tok, User* cmdfrom = NULL)
163 bool remove = (tok[0] == '-');
164 if ((remove) || (tok[0] == '+'))
165 tok.erase(tok.begin());
168 if (!cmdfrom || !IS_LOCAL(cmdfrom))
169 target = ServerInstance->FindNick(tok);
171 target = ServerInstance->FindNickOnly(tok);
173 if ((!target) || (target->registered != REG_ALL) || (target->quitting) || (IS_SERVER(target)))
176 return std::make_pair(target, !remove);
180 CallerIDExtInfo extInfo;
181 unsigned int maxaccepts;
182 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
185 allow_empty_last_param = false;
186 syntax = "{[+|-]<nicks>}|*}";
187 TRANSLATE1(TR_CUSTOM);
190 void EncodeParameter(std::string& parameter, int index)
192 // Send lists as-is (part of 2.0 compat)
193 if (parameter.find(',') != std::string::npos)
196 // Convert a [+|-]<nick> into a [-]<uuid>
197 ACCEPTAction action = GetTargetAndAction(parameter);
201 parameter = (action.second ? "" : "-") + action.first->uuid;
204 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
205 * - in front of any nick removes, and an * lists. This effectively means you can do:
206 * /accept nick1,nick2,nick3,*
207 * to add 3 nicks and then show your list
209 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
211 if (CommandParser::LoopCall(user, this, parameters, 0))
214 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
215 * have a list already setup. */
217 if (parameters[0] == "*")
223 std::string tok = parameters[0];
224 ACCEPTAction action = GetTargetAndAction(tok, user);
227 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", tok.c_str());
231 if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
232 // Neither source nor target is local, forward the command to the server of target
235 // The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
237 return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
239 return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
242 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
244 // There is a list in parameters[0] in two cases:
245 // Either when the source is remote, this happens because 2.0 servers send comma seperated uuid lists,
246 // we don't split those but broadcast them, as before.
248 // Or if the source is local then LoopCall() runs OnPostCommand() after each entry in the list,
249 // meaning the linking module has sent an ACCEPT already for each entry in the list to the
250 // appropiate server and the ACCEPT with the list of nicks (this) doesn't need to be sent anywhere.
251 if ((!IS_LOCAL(user)) && (parameters[0].find(',') != std::string::npos))
252 return ROUTE_BROADCAST;
255 std::string targetstring = parameters[0];
256 ACCEPTAction action = GetTargetAndAction(targetstring, user);
258 // Target is a "*" or source is local and the target is a list of nicks
259 return ROUTE_LOCALONLY;
261 // Route to the server of the target
262 return ROUTE_UNICAST(action.first->server);
265 void ListAccept(User* user)
267 callerid_data* dat = extInfo.get(user, false);
270 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
271 user->WriteNumeric(RPL_ACCEPTLIST, (*i)->nick);
273 user->WriteNumeric(RPL_ENDOFACCEPT, ":End of ACCEPT list");
276 bool AddAccept(User* user, User* whotoadd)
278 // Add this user to my accept list first, so look me up..
279 callerid_data* dat = extInfo.get(user, true);
280 if (dat->accepting.size() >= maxaccepts)
282 user->WriteNumeric(ERR_ACCEPTFULL, ":Accept list is full (limit is %d)", maxaccepts);
285 if (!dat->accepting.insert(whotoadd).second)
287 user->WriteNumeric(ERR_ACCEPTEXIST, "%s :is already on your accept list", whotoadd->nick.c_str());
291 // Now, look them up, and add me to their list
292 callerid_data *targ = extInfo.get(whotoadd, true);
293 targ->wholistsme.push_back(dat);
295 user->WriteNotice(whotoadd->nick + " is now on your accept list");
299 bool RemoveAccept(User* user, User* whotoremove)
301 // Remove them from my list, so look up my list..
302 callerid_data* dat = extInfo.get(user, false);
305 user->WriteNumeric(ERR_ACCEPTNOT, "%s :is not on your accept list", whotoremove->nick.c_str());
308 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
309 if (i == dat->accepting.end())
311 user->WriteNumeric(ERR_ACCEPTNOT, "%s :is not on your accept list", whotoremove->nick.c_str());
315 dat->accepting.erase(i);
317 // Look up their list to remove me.
318 callerid_data *dat2 = extInfo.get(whotoremove, false);
321 // How the fuck is this possible.
322 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
326 std::list<callerid_data*>::iterator it = std::find(dat2->wholistsme.begin(), dat2->wholistsme.end(), dat);
327 if (it != dat2->wholistsme.end())
329 dat2->wholistsme.erase(it);
331 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
334 user->WriteNotice(whotoremove->nick + " is no longer on your accept list");
339 class ModuleCallerID : public Module
344 // Configuration variables:
345 bool operoverride; // Operators can override callerid.
346 bool tracknick; // Allow ACCEPT entries to update with nick changes.
347 unsigned int notify_cooldown; // Seconds between notifications.
349 /** Removes a user from all accept lists
350 * @param who The user to remove from accepts
352 void RemoveFromAllAccepts(User* who)
354 // First, find the list of people who have me on accept
355 callerid_data *userdata = cmd.extInfo.get(who, false);
359 // Iterate over the list of people who accept me, and remove all entries
360 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
362 callerid_data *dat = *(it);
364 // Find me on their callerid list
365 std::set<User *>::iterator it2 = dat->accepting.find(who);
367 if (it2 != dat->accepting.end())
368 dat->accepting.erase(it2);
370 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
373 userdata->wholistsme.clear();
377 ModuleCallerID() : cmd(this), myumode(this)
381 Version GetVersion() CXX11_OVERRIDE
383 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
386 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
388 tokens["CALLERID"] = "g";
391 ModResult OnUserPreMessage(User* user, void* voiddest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
393 if (!IS_LOCAL(user) || target_type != TYPE_USER)
394 return MOD_RES_PASSTHRU;
396 User* dest = static_cast<User*>(voiddest);
397 if (!dest->IsModeSet(myumode) || (user == dest))
398 return MOD_RES_PASSTHRU;
400 if (operoverride && user->IsOper())
401 return MOD_RES_PASSTHRU;
403 callerid_data* dat = cmd.extInfo.get(dest, true);
404 std::set<User*>::iterator i = dat->accepting.find(user);
406 if (i == dat->accepting.end())
408 time_t now = ServerInstance->Time();
409 /* +g and *not* accepted */
410 user->WriteNumeric(ERR_TARGUMODEG, "%s :is in +g mode (server-side ignore).", dest->nick.c_str());
411 if (now > (dat->lastnotify + (time_t)notify_cooldown))
413 user->WriteNumeric(RPL_TARGNOTIFY, "%s :has been informed that you messaged them.", dest->nick.c_str());
414 dest->SendText(":%s %03d %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
415 ServerInstance->Config->ServerName.c_str(), RPL_UMODEGMSG, dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
416 dat->lastnotify = now;
420 return MOD_RES_PASSTHRU;
423 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
426 RemoveFromAllAccepts(user);
429 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
431 RemoveFromAllAccepts(user);
434 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
436 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
437 cmd.maxaccepts = tag->getInt("maxaccepts", 16);
438 operoverride = tag->getBool("operoverride");
439 tracknick = tag->getBool("tracknick");
440 notify_cooldown = tag->getInt("cooldown", 60);
444 MODULE_INIT(ModuleCallerID)