1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
21 class callerid_data : public classbase
26 /** Users I accept messages from
28 std::set<User*> accepting;
30 /** Users who list me as accepted
32 std::list<callerid_data *> wholistsme;
34 callerid_data() : lastnotify(0) { }
35 callerid_data(const std::string& str, InspIRCd* ServerInstance)
37 irc::commasepstream s(str);
41 lastnotify = ConvToInt(tok);
43 while (s.GetToken(tok))
50 User *u = ServerInstance->FindNick(tok);
59 std::string ToString(Module* proto) const
61 std::ostringstream oss;
63 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
66 oss << "," << proto->ProtoTranslate(*i);
73 /** Retrieves the callerid information for a given user record
74 * @param who The user to retrieve callerid information for
75 * @param extend true to add callerid information if it doesn't already exist, false to return NULL if it doesn't exist
76 * @return NULL if extend is false and it doesn't exist, a callerid_data instance otherwise.
78 callerid_data* GetData(User* who, bool extend = true)
81 if (who->GetExt("callerid_data", dat))
87 dat = new callerid_data;
88 who->Extend("callerid_data", dat);
96 void RemoveData(User* who)
99 who->GetExt("callerid_data", dat);
104 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
105 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
107 callerid_data *targ = GetData(*it, false);
110 continue; // shouldn't happen, but oh well.
112 for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
116 targ->wholistsme.erase(it2);
122 who->Shrink("callerid_data");
127 class User_g : public SimpleUserModeHandler
130 User_g(InspIRCd* Instance, Module* Creator) : SimpleUserModeHandler(Instance, Creator, 'g') { }
133 class CommandAccept : public Command
136 unsigned int maxaccepts;
137 CommandAccept(InspIRCd* Instance, Module* Creator) : Command(Instance, Creator, "ACCEPT", 0, 1)
139 syntax = "{[+|-]<nicks>}|*}";
140 TRANSLATE2(TR_CUSTOM, TR_END);
143 virtual void EncodeParameter(std::string& parameter, int index)
147 std::string out = "";
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);
181 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
182 * - in front of any nick removes, and an * lists. This effectively means you can do:
183 * /accept nick1,nick2,nick3,*
184 * to add 3 nicks and then show your list
186 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
188 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
190 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
191 * have a list already setup. */
193 std::string tok = parameters[0];
201 else if (tok[0] == '-')
203 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
205 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
211 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
213 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
216 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
222 void ListAccept(User* user)
224 callerid_data* dat = GetData(user, false);
227 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
228 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
230 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
233 bool AddAccept(User* user, User* whotoadd, bool quiet)
235 // Add this user to my accept list first, so look me up..
236 callerid_data* dat = GetData(user, true);
237 if (dat->accepting.size() >= maxaccepts)
240 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
244 if (!dat->accepting.insert(whotoadd).second)
247 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
252 // Now, look them up, and add me to their list
253 callerid_data *targ = GetData(whotoadd, true);
254 targ->wholistsme.push_back(dat);
256 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
260 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
262 // Remove them from my list, so look up my list..
263 callerid_data* dat = GetData(user, false);
267 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
271 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
272 if (i == dat->accepting.end())
275 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
280 dat->accepting.erase(i);
282 // Look up their list to remove me.
283 callerid_data *dat2 = GetData(whotoremove, false);
286 // How the fuck is this possible.
290 for (std::list<callerid_data *>::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++)
295 dat2->wholistsme.erase(it);
300 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
305 class ModuleCallerID : public Module
308 CommandAccept mycommand;
311 // Configuration variables:
312 bool operoverride; // Operators can override callerid.
313 bool tracknick; // Allow ACCEPT entries to update with nick changes.
314 unsigned int notify_cooldown; // Seconds between notifications.
316 /** Removes a user from all accept lists
317 * @param who The user to remove from accepts
319 void RemoveFromAllAccepts(User* who)
321 // First, find the list of people who have me on accept
322 callerid_data *userdata = GetData(who, false);
326 // Iterate over the list of people who accept me, and remove all entries
327 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
329 callerid_data *dat = *(it);
331 // Find me on their callerid list
332 std::set<User *>::iterator it2 = dat->accepting.find(who);
334 if (it2 != dat->accepting.end())
335 dat->accepting.erase(it2);
338 userdata->wholistsme.clear();
342 ModuleCallerID(InspIRCd* Me) : Module(Me), mycommand(Me, this), myumode(Me, this)
346 if (!ServerInstance->Modes->AddMode(&myumode))
347 throw ModuleException("Could not add usermode +g");
349 ServerInstance->AddCommand(&mycommand);
351 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
352 ServerInstance->Modules->Attach(eventlist, this, 7);
355 virtual ~ModuleCallerID()
357 ServerInstance->Modes->DelMode(&myumode);
360 virtual Version GetVersion()
362 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
365 virtual void On005Numeric(std::string& output)
367 output += " CALLERID=g";
370 ModResult PreText(User* user, User* dest, std::string& text, bool notice)
372 if (!dest->IsModeSet('g'))
373 return MOD_RES_PASSTHRU;
375 if (operoverride && IS_OPER(user))
376 return MOD_RES_PASSTHRU;
378 callerid_data* dat = GetData(dest, true);
379 std::set<User*>::iterator i = dat->accepting.find(user);
381 if (i == dat->accepting.end())
383 time_t now = ServerInstance->Time();
384 /* +g and *not* accepted */
385 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
386 if (now > (dat->lastnotify + (time_t)notify_cooldown))
388 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
389 ServerInstance->DumpText(dest, ":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
390 ServerInstance->Config->ServerName, dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
391 dat->lastnotify = now;
395 return MOD_RES_PASSTHRU;
398 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
400 if (IS_LOCAL(user) && target_type == TYPE_USER)
401 return PreText(user, (User*)dest, text, true);
403 return MOD_RES_PASSTHRU;
406 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
408 if (IS_LOCAL(user) && target_type == TYPE_USER)
409 return PreText(user, (User*)dest, text, true);
411 return MOD_RES_PASSTHRU;
414 virtual void OnCleanup(int type, void* item)
416 if (type != TYPE_USER)
419 User* u = (User*)item;
420 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do. */
424 virtual void OnSyncUser(User* user, Module* proto, void* opaque)
426 callerid_data* dat = GetData(user, false);
429 std::string str = dat->ToString(proto);
430 proto->ProtoSendMetaData(opaque, user, "callerid_data", str);
434 virtual void OnDecodeMetaData(Extensible* target, const std::string& extname, const std::string& extdata)
436 User* u = dynamic_cast<User*>(target);
437 if (u && extname == "callerid_data")
439 callerid_data* dat = new callerid_data(extdata, ServerInstance);
440 u->Extend("callerid_data", dat);
444 virtual ModResult OnUserPreNick(User* user, const std::string& newnick)
447 RemoveFromAllAccepts(user);
448 return MOD_RES_PASSTHRU;
451 virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
453 RemoveFromAllAccepts(user);
457 virtual void OnRehash(User* user)
459 ConfigReader Conf(ServerInstance);
460 mycommand.maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
461 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
462 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
463 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
467 MODULE_INIT(ModuleCallerID)