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/>.
25 /* $ModDesc: Implementation of callerid, usermode +g, /accept */
32 /** Users I accept messages from
34 std::set<User*> accepting;
36 /** Users who list me as accepted
38 std::list<callerid_data *> wholistsme;
40 callerid_data() : lastnotify(0) { }
42 std::string ToString(SerializeFormat format) const
44 std::ostringstream oss;
46 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
50 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
56 struct CallerIDExtInfo : public ExtensionItem
58 CallerIDExtInfo(Module* parent)
59 : ExtensionItem("callerid_data", parent)
63 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
66 if (format != FORMAT_NETWORK)
68 callerid_data* dat = static_cast<callerid_data*>(item);
69 ret = dat->ToString(format);
74 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
76 if (format == FORMAT_NETWORK)
79 callerid_data* dat = new callerid_data;
80 irc::commasepstream s(value);
83 dat->lastnotify = ConvToInt(tok);
85 while (s.GetToken(tok))
87 User *u = ServerInstance->FindNick(tok);
88 if ((u) && (u->registered == REG_ALL) && (!u->quitting) && (!IS_SERVER(u)))
90 if (dat->accepting.insert(u).second)
92 callerid_data* other = this->get(u, true);
93 other->wholistsme.push_back(dat);
98 void* old = set_raw(container, dat);
103 callerid_data* get(User* user, bool create)
105 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
108 dat = new callerid_data;
114 void free(void* item)
116 callerid_data* dat = static_cast<callerid_data*>(item);
118 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
119 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
121 callerid_data *targ = this->get(*it, false);
125 ServerInstance->Logs->Log("m_callerid", LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
126 continue; // shouldn't happen, but oh well.
129 std::list<callerid_data*>::iterator it2 = std::find(targ->wholistsme.begin(), targ->wholistsme.end(), dat);
130 if (it2 != targ->wholistsme.end())
131 targ->wholistsme.erase(it2);
133 ServerInstance->Logs->Log("m_callerid", LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
139 class User_g : public SimpleUserModeHandler
142 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
145 class CommandAccept : public Command
147 /** Pair: first is the target, second is true to add, false to remove
149 typedef std::pair<User*, bool> ACCEPTAction;
151 ACCEPTAction GetTargetAndAction(std::string& tok)
153 bool remove = (tok[0] == '-');
154 if ((remove) || (tok[0] == '+'))
155 tok.erase(tok.begin());
157 User* target = ServerInstance->FindNick(tok);
158 if ((!target) || (target->registered != REG_ALL) || (target->quitting) || (IS_SERVER(target)))
161 return std::make_pair(target, !remove);
165 CallerIDExtInfo extInfo;
166 unsigned int maxaccepts;
167 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
170 allow_empty_last_param = false;
171 syntax = "{[+|-]<nicks>}|*}";
172 TRANSLATE1(TR_CUSTOM);
175 void EncodeParameter(std::string& parameter, int index)
177 // Send lists as-is (part of 2.0 compat)
178 if (parameter.find(',') != std::string::npos)
181 // Convert a [+|-]<nick> into a [-]<uuid>
182 ACCEPTAction action = GetTargetAndAction(parameter);
186 parameter = (action.second ? "" : "-") + action.first->uuid;
189 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
190 * - in front of any nick removes, and an * lists. This effectively means you can do:
191 * /accept nick1,nick2,nick3,*
192 * to add 3 nicks and then show your list
194 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
196 if (CommandParser::LoopCall(user, this, parameters, 0))
199 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
200 * have a list already setup. */
202 if (parameters[0] == "*")
208 std::string tok = parameters[0];
209 ACCEPTAction action = GetTargetAndAction(tok);
212 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
216 if ((!IS_LOCAL(user)) && (!IS_LOCAL(action.first)))
217 // Neither source nor target is local, forward the command to the server of target
220 // The second item in the pair is true if the first char is a '+' (or nothing), false if it's a '-'
222 return (AddAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
224 return (RemoveAccept(user, action.first) ? CMD_SUCCESS : CMD_FAILURE);
227 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
229 // There is a list in parameters[0] in two cases:
230 // Either when the source is remote, this happens because 2.0 servers send comma seperated uuid lists,
231 // we don't split those but broadcast them, as before.
233 // Or if the source is local then LoopCall() runs OnPostCommand() after each entry in the list,
234 // meaning the linking module has sent an ACCEPT already for each entry in the list to the
235 // appropiate server and the ACCEPT with the list of nicks (this) doesn't need to be sent anywhere.
236 if ((!IS_LOCAL(user)) && (parameters[0].find(',') != std::string::npos))
237 return ROUTE_BROADCAST;
240 std::string targetstring = parameters[0];
241 ACCEPTAction action = GetTargetAndAction(targetstring);
243 // Target is a "*" or source is local and the target is a list of nicks
244 return ROUTE_LOCALONLY;
246 // Route to the server of the target
247 return ROUTE_UNICAST(action.first->server);
250 void ListAccept(User* user)
252 callerid_data* dat = extInfo.get(user, false);
255 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
256 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
258 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
261 bool AddAccept(User* user, User* whotoadd)
263 // Add this user to my accept list first, so look me up..
264 callerid_data* dat = extInfo.get(user, true);
265 if (dat->accepting.size() >= maxaccepts)
267 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
270 if (!dat->accepting.insert(whotoadd).second)
272 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
276 // Now, look them up, and add me to their list
277 callerid_data *targ = extInfo.get(whotoadd, true);
278 targ->wholistsme.push_back(dat);
280 user->WriteNotice(whotoadd->nick + " is now on your accept list");
284 bool RemoveAccept(User* user, User* whotoremove)
286 // Remove them from my list, so look up my list..
287 callerid_data* dat = extInfo.get(user, false);
290 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
293 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
294 if (i == dat->accepting.end())
296 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
300 dat->accepting.erase(i);
302 // Look up their list to remove me.
303 callerid_data *dat2 = extInfo.get(whotoremove, false);
306 // How the fuck is this possible.
307 ServerInstance->Logs->Log("m_callerid", LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
311 std::list<callerid_data*>::iterator it = std::find(dat2->wholistsme.begin(), dat2->wholistsme.end(), dat);
312 if (it != dat2->wholistsme.end())
314 dat2->wholistsme.erase(it);
316 ServerInstance->Logs->Log("m_callerid", LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
319 user->WriteNotice(whotoremove->nick + " is no longer on your accept list");
324 class ModuleCallerID : public Module
329 // Configuration variables:
330 bool operoverride; // Operators can override callerid.
331 bool tracknick; // Allow ACCEPT entries to update with nick changes.
332 unsigned int notify_cooldown; // Seconds between notifications.
334 /** Removes a user from all accept lists
335 * @param who The user to remove from accepts
337 void RemoveFromAllAccepts(User* who)
339 // First, find the list of people who have me on accept
340 callerid_data *userdata = cmd.extInfo.get(who, false);
344 // Iterate over the list of people who accept me, and remove all entries
345 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
347 callerid_data *dat = *(it);
349 // Find me on their callerid list
350 std::set<User *>::iterator it2 = dat->accepting.find(who);
352 if (it2 != dat->accepting.end())
353 dat->accepting.erase(it2);
355 ServerInstance->Logs->Log("m_callerid", LOG_DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
358 userdata->wholistsme.clear();
362 ModuleCallerID() : cmd(this), myumode(this)
366 void init() CXX11_OVERRIDE
370 ServerInstance->Modules->AddService(myumode);
371 ServerInstance->Modules->AddService(cmd);
372 ServerInstance->Modules->AddService(cmd.extInfo);
374 Implementation eventlist[] = { I_OnRehash, I_OnUserPostNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreMessage };
375 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
378 Version GetVersion() CXX11_OVERRIDE
380 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
383 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
385 tokens["CALLERID"] = "g";
388 ModResult PreText(User* user, User* dest, std::string& text)
390 if (!dest->IsModeSet(myumode) || (user == dest))
391 return MOD_RES_PASSTHRU;
393 if (operoverride && user->IsOper())
394 return MOD_RES_PASSTHRU;
396 callerid_data* dat = cmd.extInfo.get(dest, true);
397 std::set<User*>::iterator i = dat->accepting.find(user);
399 if (i == dat->accepting.end())
401 time_t now = ServerInstance->Time();
402 /* +g and *not* accepted */
403 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
404 if (now > (dat->lastnotify + (time_t)notify_cooldown))
406 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
407 dest->SendText(":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
408 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());
409 dat->lastnotify = now;
413 return MOD_RES_PASSTHRU;
416 ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
418 if (IS_LOCAL(user) && target_type == TYPE_USER)
419 return PreText(user, (User*)dest, text);
421 return MOD_RES_PASSTHRU;
424 void OnUserPostNick(User* user, const std::string& oldnick) CXX11_OVERRIDE
427 RemoveFromAllAccepts(user);
430 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message) CXX11_OVERRIDE
432 RemoveFromAllAccepts(user);
435 void OnRehash(User* user) CXX11_OVERRIDE
437 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
438 cmd.maxaccepts = tag->getInt("maxaccepts", 16);
439 operoverride = tag->getBool("operoverride");
440 tracknick = tag->getBool("tracknick");
441 notify_cooldown = tag->getInt("cooldown", 60);
445 MODULE_INIT(ModuleCallerID)