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) { }
41 callerid_data(const std::string& str)
43 irc::commasepstream s(str);
47 lastnotify = ConvToInt(tok);
49 while (s.GetToken(tok))
56 User *u = ServerInstance->FindNick(tok);
57 if ((u) && (u->registered == REG_ALL) && (!u->quitting) && (!IS_SERVER(u)))
62 std::string ToString(SerializeFormat format) const
64 std::ostringstream oss;
66 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
70 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
76 struct CallerIDExtInfo : public ExtensionItem
78 CallerIDExtInfo(Module* parent)
79 : ExtensionItem("callerid_data", parent)
83 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
85 callerid_data* dat = static_cast<callerid_data*>(item);
86 return dat->ToString(format);
89 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
91 callerid_data* dat = new callerid_data(value);
92 set_raw(container, dat);
95 callerid_data* get(User* user, bool create)
97 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
100 dat = new callerid_data;
106 void free(void* item)
108 callerid_data* dat = static_cast<callerid_data*>(item);
110 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
111 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
113 callerid_data *targ = this->get(*it, false);
116 continue; // shouldn't happen, but oh well.
118 std::list<callerid_data*>::iterator it2 = std::find(targ->wholistsme.begin(), targ->wholistsme.end(), dat);
119 if (it2 != targ->wholistsme.end())
120 targ->wholistsme.erase(it2);
125 class User_g : public SimpleUserModeHandler
128 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
131 class CommandAccept : public Command
134 CallerIDExtInfo extInfo;
135 unsigned int maxaccepts;
136 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
139 syntax = "{[+|-]<nicks>}|*}";
140 TRANSLATE2(TR_CUSTOM, TR_END);
143 virtual void EncodeParameter(std::string& parameter, int index)
148 irc::commasepstream nicks(parameter);
150 while (nicks.GetToken(tok))
154 continue; // Drop list requests, since remote servers ignore them anyway.
162 tok.erase(0, 1); // Remove the dash.
164 User* u = ServerInstance->FindNick(tok);
165 if ((!u) || (u->registered != REG_ALL) || (u->quitting) || (IS_SERVER(u)))
175 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
176 * - in front of any nick removes, and an * lists. This effectively means you can do:
177 * /accept nick1,nick2,nick3,*
178 * to add 3 nicks and then show your list
180 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
182 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
184 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
185 * have a list already setup. */
187 std::string tok = parameters[0];
195 else if (tok[0] == '-')
197 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
199 return (RemoveAccept(user, whotoremove) ? CMD_SUCCESS : CMD_FAILURE);
205 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
206 if ((whotoadd) && (whotoadd->registered == REG_ALL) && (!whotoadd->quitting) && (!IS_SERVER(whotoadd)))
207 return (AddAccept(user, whotoadd) ? CMD_SUCCESS : CMD_FAILURE);
210 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
216 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
218 return ROUTE_BROADCAST;
221 void ListAccept(User* user)
223 callerid_data* dat = extInfo.get(user, false);
226 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
227 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
229 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
232 bool AddAccept(User* user, User* whotoadd)
234 // Add this user to my accept list first, so look me up..
235 callerid_data* dat = extInfo.get(user, true);
236 if (dat->accepting.size() >= maxaccepts)
238 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
241 if (!dat->accepting.insert(whotoadd).second)
243 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
247 // Now, look them up, and add me to their list
248 callerid_data *targ = extInfo.get(whotoadd, true);
249 targ->wholistsme.push_back(dat);
251 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
255 bool RemoveAccept(User* user, User* whotoremove)
257 // Remove them from my list, so look up my list..
258 callerid_data* dat = extInfo.get(user, false);
261 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
264 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
265 if (i == dat->accepting.end())
267 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
271 dat->accepting.erase(i);
273 // Look up their list to remove me.
274 callerid_data *dat2 = extInfo.get(whotoremove, false);
277 // How the fuck is this possible.
281 std::list<callerid_data*>::iterator it = std::find(dat2->wholistsme.begin(), dat2->wholistsme.end(), dat);
282 if (it != dat2->wholistsme.end())
284 dat2->wholistsme.erase(it);
286 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
291 class ModuleCallerID : public Module
297 // Configuration variables:
298 bool operoverride; // Operators can override callerid.
299 bool tracknick; // Allow ACCEPT entries to update with nick changes.
300 unsigned int notify_cooldown; // Seconds between notifications.
302 /** Removes a user from all accept lists
303 * @param who The user to remove from accepts
305 void RemoveFromAllAccepts(User* who)
307 // First, find the list of people who have me on accept
308 callerid_data *userdata = cmd.extInfo.get(who, false);
312 // Iterate over the list of people who accept me, and remove all entries
313 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
315 callerid_data *dat = *(it);
317 // Find me on their callerid list
318 std::set<User *>::iterator it2 = dat->accepting.find(who);
320 if (it2 != dat->accepting.end())
321 dat->accepting.erase(it2);
324 userdata->wholistsme.clear();
328 ModuleCallerID() : cmd(this), myumode(this)
336 ServerInstance->Modules->AddService(myumode);
337 ServerInstance->Modules->AddService(cmd);
338 ServerInstance->Modules->AddService(cmd.extInfo);
340 Implementation eventlist[] = { I_OnRehash, I_OnUserPostNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
341 ServerInstance->Modules->Attach(eventlist, this, 6);
344 virtual ~ModuleCallerID()
348 virtual Version GetVersion()
350 return Version("Implementation of callerid, usermode +g, /accept", VF_COMMON | VF_VENDOR);
353 virtual void On005Numeric(std::string& output)
355 output += " CALLERID=g";
358 ModResult PreText(User* user, User* dest, std::string& text)
360 if (!dest->IsModeSet('g'))
361 return MOD_RES_PASSTHRU;
363 if (operoverride && IS_OPER(user))
364 return MOD_RES_PASSTHRU;
366 callerid_data* dat = cmd.extInfo.get(dest, true);
367 std::set<User*>::iterator i = dat->accepting.find(user);
369 if (i == dat->accepting.end())
371 time_t now = ServerInstance->Time();
372 /* +g and *not* accepted */
373 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
374 if (now > (dat->lastnotify + (time_t)notify_cooldown))
376 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
377 dest->SendText(":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
378 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());
379 dat->lastnotify = now;
383 return MOD_RES_PASSTHRU;
386 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
388 if (IS_LOCAL(user) && target_type == TYPE_USER)
389 return PreText(user, (User*)dest, text);
391 return MOD_RES_PASSTHRU;
394 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
396 if (IS_LOCAL(user) && target_type == TYPE_USER)
397 return PreText(user, (User*)dest, text);
399 return MOD_RES_PASSTHRU;
402 void OnUserPostNick(User* user, const std::string& oldnick)
405 RemoveFromAllAccepts(user);
408 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
410 RemoveFromAllAccepts(user);
413 virtual void OnRehash(User* user)
415 ConfigTag* tag = ServerInstance->Config->ConfValue("callerid");
416 cmd.maxaccepts = tag->getInt("maxaccepts", 16);
417 operoverride = tag->getBool("operoverride");
418 tracknick = tag->getBool("tracknick");
419 notify_cooldown = tag->getInt("cooldown", 60);
423 MODULE_INIT(ModuleCallerID)