1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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
25 std::set<User*> accepting;
27 callerid_data() : lastnotify(0) { }
28 callerid_data(const std::string& str, InspIRCd* ServerInstance)
30 irc::commasepstream s(str);
34 lastnotify = ConvToInt(tok);
36 while (s.GetToken(tok))
42 User* u = ServerInstance->FindUUID(tok);
45 u = ServerInstance->FindNick(tok);
55 std::string ToString(bool displayable) const
57 std::ostringstream oss;
59 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
62 oss << "," << (displayable ? (*i)->nick : (*i)->uuid);
69 callerid_data* GetData(User* who, bool extend = true)
72 if (who->GetExt("callerid_data", dat))
78 dat = new callerid_data;
79 who->Extend("callerid_data", dat);
87 void RemoveData(User* who)
90 who->GetExt("callerid_data", dat);
95 who->Shrink("callerid_data");
99 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
101 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
103 callerid_data* dat = GetData(i->second, false);
108 std::set<User*>::iterator iter = dat->accepting.find(who);
110 if (iter == dat->accepting.end())
113 dat->accepting.erase(iter);
117 class User_g : public SimpleUserModeHandler
120 User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
123 class CommandAccept : public Command
126 unsigned int& maxaccepts;
128 CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
130 source = "m_callerid.so";
131 syntax = "{[+|-]<nicks>}|*}";
132 TRANSLATE2(TR_CUSTOM, TR_END);
135 virtual void EncodeParameter(std::string& parameter, int index)
139 std::string out = "";
140 irc::commasepstream nicks(parameter);
142 while (nicks.GetToken(tok))
146 continue; // Drop list requests, since remote servers ignore them anyway.
154 tok.erase(0, 1); // Remove the dash.
156 User* u = ServerInstance->FindNick(tok);
173 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
174 * - in front of any nick removes, and an * lists. This effectively means you can do:
175 * /accept nick1,nick2,nick3,*
176 * to add 3 nicks and then show your list
178 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
180 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
182 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
183 * have a list already setup. */
185 std::string tok = parameters[0];
191 return CMD_LOCALONLY;
193 else if (tok[0] == '-')
195 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
197 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
203 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
205 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
208 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
214 void ListAccept(User* user)
216 callerid_data* dat = GetData(user, false);
219 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
220 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
222 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
225 bool AddAccept(User* user, User* whotoadd, bool quiet)
227 callerid_data* dat = GetData(user, true);
228 if (dat->accepting.size() >= maxaccepts)
231 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
235 if (!dat->accepting.insert(whotoadd).second)
238 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
245 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
247 callerid_data* dat = GetData(user, false);
251 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
255 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
256 if (i == dat->accepting.end())
259 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
263 dat->accepting.erase(i);
268 class ModuleCallerID : public Module
271 CommandAccept *mycommand;
274 // Configuration variables:
275 unsigned int maxaccepts; // Maximum ACCEPT entries.
276 bool operoverride; // Operators can override callerid.
277 bool tracknick; // Allow ACCEPT entries to update with nick changes.
278 unsigned int notify_cooldown; // Seconds between notifications.
281 ModuleCallerID(InspIRCd* Me) : Module(Me)
284 mycommand = new CommandAccept(ServerInstance, maxaccepts);
285 myumode = new User_g(ServerInstance);
287 if (!ServerInstance->Modes->AddMode(myumode))
291 throw ModuleException("Could not add usermode +g");
295 ServerInstance->AddCommand(mycommand);
297 catch (const ModuleException& e)
301 throw ModuleException("Could not add command!");
304 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
305 ServerInstance->Modules->Attach(eventlist, this, 7);
308 virtual ~ModuleCallerID()
310 ServerInstance->Modes->DelMode(myumode);
314 virtual Version GetVersion()
316 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
319 virtual void On005Numeric(std::string& output)
321 output += " CALLERID=g";
324 int PreText(User* user, User* dest, std::string& text, bool notice)
326 if (!dest->IsModeSet('g'))
329 if (operoverride && IS_OPER(user))
332 callerid_data* dat = GetData(dest, true);
333 std::set<User*>::iterator i = dat->accepting.find(user);
335 if (i == dat->accepting.end())
337 time_t now = time(NULL);
338 /* +g and *not* accepted */
339 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
340 if (now > (dat->lastnotify + (time_t)notify_cooldown))
342 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
343 dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
344 dat->lastnotify = now;
351 virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
353 if (IS_LOCAL(user) && target_type == TYPE_USER)
354 return PreText(user, (User*)dest, text, true);
359 virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
361 if (IS_LOCAL(user) && target_type == TYPE_USER)
362 return PreText(user, (User*)dest, text, true);
367 virtual void OnCleanup(int type, void* item)
369 if (type != TYPE_USER)
372 User* u = (User*)item;
373 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do. */
377 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable)
379 if (extname == "callerid_data")
381 callerid_data* dat = GetData(user, false);
384 std::string str = dat->ToString(displayable);
385 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str);
390 virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata)
392 if (target_type == TYPE_USER && extname == "callerid_data")
394 User* u = (User*)target;
395 callerid_data* dat = new callerid_data(extdata, ServerInstance);
396 u->Extend("callerid_data", dat);
400 virtual int OnUserPreNick(User* user, const std::string& newnick)
403 RemoveFromAllAccepts(ServerInstance, user);
407 virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
410 RemoveFromAllAccepts(ServerInstance, user);
413 virtual void OnRehash(User* user, const std::string& parameter)
415 ConfigReader Conf(ServerInstance);
416 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
417 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
418 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
419 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
423 MODULE_INIT(ModuleCallerID)