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
65 callerid_data* dat = static_cast<callerid_data*>(item);
66 return dat->ToString(format);
69 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
71 callerid_data* dat = new callerid_data;
72 irc::commasepstream s(value);
75 dat->lastnotify = ConvToInt(tok);
77 while (s.GetToken(tok))
82 User *u = ServerInstance->FindNick(tok);
83 if ((u) && (u->registered == REG_ALL) && (!u->quitting) && (!IS_SERVER(u)))
85 if (dat->accepting.insert(u).second)
87 callerid_data* other = this->get(u, true);
88 other->wholistsme.push_back(dat);
93 void* old = set_raw(container, dat);
98 callerid_data* get(User* user, bool create)
100 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
103 dat = new callerid_data;
109 void free(void* item)
111 callerid_data* dat = static_cast<callerid_data*>(item);
113 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
114 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
116 callerid_data *targ = this->get(*it, false);
120 ServerInstance->Logs->Log("m_callerid", DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (1)");
121 continue; // shouldn't happen, but oh well.
124 std::list<callerid_data*>::iterator it2 = std::find(targ->wholistsme.begin(), targ->wholistsme.end(), dat);
125 if (it2 != targ->wholistsme.end())
126 targ->wholistsme.erase(it2);
128 ServerInstance->Logs->Log("m_callerid", DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (2)");
134 class User_g : public SimpleUserModeHandler
137 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
140 class CommandAccept : public Command
143 CallerIDExtInfo extInfo;
144 unsigned int maxaccepts;
145 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
148 allow_empty_last_param = false;
149 syntax = "*|(+|-)<nick>[,(+|-)<nick> ...]";
150 TRANSLATE2(TR_CUSTOM, TR_END);
153 virtual void EncodeParameter(std::string& parameter, int index)
158 irc::commasepstream nicks(parameter);
160 while (nicks.GetToken(tok))
164 continue; // Drop list requests, since remote servers ignore them anyway.
172 tok.erase(0, 1); // Remove the dash.
174 else if (tok[0] == '+')
177 User* u = ServerInstance->FindNick(tok);
178 if ((!u) || (u->registered != REG_ALL) || (u->quitting) || (IS_SERVER(u)))
188 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
189 * - in front of any nick removes, and an * lists. This effectively means you can do:
190 * /accept nick1,nick2,nick3,*
191 * to add 3 nicks and then show your list
193 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
195 if (ServerInstance->Parser->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 const std::string& tok = parameters[0];
208 else if (tok[0] == '-')
212 whotoremove = ServerInstance->FindNickOnly(tok.substr(1));
214 whotoremove = ServerInstance->FindNick(tok.substr(1));
217 return (RemoveAccept(user, whotoremove) ? CMD_SUCCESS : CMD_FAILURE);
223 const std::string target = (tok[0] == '+' ? tok.substr(1) : tok);
226 whotoadd = ServerInstance->FindNickOnly(target);
228 whotoadd = ServerInstance->FindNick(target);
230 if ((whotoadd) && (whotoadd->registered == REG_ALL) && (!whotoadd->quitting) && (!IS_SERVER(whotoadd)))
231 return (AddAccept(user, whotoadd) ? CMD_SUCCESS : CMD_FAILURE);
234 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
240 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
242 return ROUTE_BROADCAST;
245 void ListAccept(User* user)
247 callerid_data* dat = extInfo.get(user, false);
250 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
251 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
253 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
256 bool AddAccept(User* user, User* whotoadd)
258 // Add this user to my accept list first, so look me up..
259 callerid_data* dat = extInfo.get(user, true);
260 if (dat->accepting.size() >= maxaccepts)
262 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
265 if (!dat->accepting.insert(whotoadd).second)
267 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
271 // Now, look them up, and add me to their list
272 callerid_data *targ = extInfo.get(whotoadd, true);
273 targ->wholistsme.push_back(dat);
275 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
279 bool RemoveAccept(User* user, User* whotoremove)
281 // Remove them from my list, so look up my list..
282 callerid_data* dat = extInfo.get(user, false);
285 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
288 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
289 if (i == dat->accepting.end())
291 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
295 dat->accepting.erase(i);
297 // Look up their list to remove me.
298 callerid_data *dat2 = extInfo.get(whotoremove, false);
301 // How the fuck is this possible.
302 ServerInstance->Logs->Log("m_callerid", DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (3)");
306 std::list<callerid_data*>::iterator it = std::find(dat2->wholistsme.begin(), dat2->wholistsme.end(), dat);
307 if (it != dat2->wholistsme.end())
309 dat2->wholistsme.erase(it);
311 ServerInstance->Logs->Log("m_callerid", DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (4)");
314 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
319 class ModuleCallerID : public Module
325 // Configuration variables:
326 bool operoverride; // Operators can override callerid.
327 bool tracknick; // Allow ACCEPT entries to update with nick changes.
328 unsigned int notify_cooldown; // Seconds between notifications.
330 /** Removes a user from all accept lists
331 * @param who The user to remove from accepts
333 void RemoveFromAllAccepts(User* who)
335 // First, find the list of people who have me on accept
336 callerid_data *userdata = cmd.extInfo.get(who, false);
340 // Iterate over the list of people who accept me, and remove all entries
341 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
343 callerid_data *dat = *(it);
345 // Find me on their callerid list
346 std::set<User *>::iterator it2 = dat->accepting.find(who);
348 if (it2 != dat->accepting.end())
349 dat->accepting.erase(it2);
351 ServerInstance->Logs->Log("m_callerid", DEFAULT, "ERROR: Inconsistency detected in callerid state, please report (5)");
354 userdata->wholistsme.clear();
358 ModuleCallerID() : cmd(this), myumode(this)
366 ServerInstance->Modules->AddService(myumode);
367 ServerInstance->Modules->AddService(cmd);
368 ServerInstance->Modules->AddService(cmd.extInfo);
370 Implementation eventlist[] = { I_OnRehash, I_OnUserPostNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
371 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
374 virtual ~ModuleCallerID()
378 virtual Version GetVersion()
380 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
383 virtual void On005Numeric(std::string& output)
385 output += " CALLERID=g";
388 ModResult PreText(User* user, User* dest, std::string& text)
390 if (!dest->IsModeSet('g') || (user == dest))
391 return MOD_RES_PASSTHRU;
393 if (operoverride && IS_OPER(user))
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 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
418 if (IS_LOCAL(user) && target_type == TYPE_USER)
419 return PreText(user, (User*)dest, text);
421 return MOD_RES_PASSTHRU;
424 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
426 if (IS_LOCAL(user) && target_type == TYPE_USER)
427 return PreText(user, (User*)dest, text);
429 return MOD_RES_PASSTHRU;
432 void OnUserPostNick(User* user, const std::string& oldnick)
435 RemoveFromAllAccepts(user);
438 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
440 RemoveFromAllAccepts(user);
443 virtual void OnRehash(User* user)
445 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
446 cmd.maxaccepts = tag->getInt("maxaccepts", 16);
447 operoverride = tag->getBool("operoverride");
448 tracknick = tag->getBool("tracknick");
449 notify_cooldown = tag->getInt("cooldown", 60);
453 MODULE_INIT(ModuleCallerID)