12 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
14 class callerid_data : public classbase
18 std::set<User*> accepting;
20 callerid_data() : lastnotify(0) { }
21 callerid_data(const std::string& str, InspIRCd* ServerInstance)
23 irc::commasepstream s(str);
27 lastnotify = ConvToInt(tok);
29 while (s.GetToken(tok))
35 User* u = ServerInstance->FindUUID(tok);
38 u = ServerInstance->FindNick(tok);
48 std::string ToString(bool displayable) const
50 std::ostringstream oss;
52 for (std::set<User*>::iterator i = accepting.begin(); i != accepting.end(); ++i)
55 oss << "," << (displayable ? (*i)->nick : (*i)->uuid);
62 callerid_data* GetData(User* who, bool extend = true)
65 if (who->GetExt("callerid_data", dat))
71 dat = new callerid_data;
72 who->Extend("callerid_data", dat);
80 void RemoveData(User* who)
83 who->GetExt("callerid_data", dat);
88 who->Shrink("callerid_data");
92 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
94 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
96 callerid_data* dat = GetData(i->second, false);
101 std::set<User*>::iterator iter = dat->accepting.find(who);
103 if (iter == dat->accepting.end())
106 dat->accepting.erase(iter);
110 class User_g : public SimpleUserModeHandler
113 User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
116 class CommandAccept : public Command
119 unsigned int& maxaccepts;
121 CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
123 source = "m_callerid.so";
124 syntax = "{[+|-]<nicks>}|*}";
125 TRANSLATE2(TR_CUSTOM, TR_END);
128 virtual void EncodeParameter(std::string& parameter, int index)
132 std::string out = "";
133 irc::commasepstream nicks(parameter);
135 while (nicks.GetToken(tok))
139 continue; // Drop list requests, since remote servers ignore them anyway.
147 tok.erase(0, 1); // Remove the dash.
149 User* u = ServerInstance->FindNick(tok);
166 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
167 * - in front of any nick removes, and an * lists. This effectively means you can do:
168 * /accept nick1,nick2,nick3,*
169 * to add 3 nicks and then show your list
171 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
173 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
175 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
176 * have a list already setup. */
177 bool atleastonechange = false;
183 return CMD_LOCALONLY;
185 else if (tok[0] == '-')
187 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
189 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
193 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
195 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
198 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
204 void ListAccept(User* user)
206 callerid_data* dat = GetData(user, false);
209 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
210 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
212 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
215 bool AddAccept(User* user, User* whotoadd, bool quiet)
217 callerid_data* dat = GetData(user, true);
218 if (dat->accepting.size() >= maxaccepts)
221 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
225 if (!dat->accepting.insert(whotoadd).second)
228 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
235 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
237 callerid_data* dat = GetData(user, false);
241 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
245 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
246 if (i == dat->accepting.end())
249 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
253 dat->accepting.erase(i);
258 class ModuleCallerID : public Module
261 CommandAccept *mycommand;
264 // Configuration variables:
265 unsigned int maxaccepts; // Maximum ACCEPT entries.
266 bool operoverride; // Operators can override callerid.
267 bool tracknick; // Allow ACCEPT entries to update with nick changes.
268 unsigned int notify_cooldown; // Seconds between notifications.
271 ModuleCallerID(InspIRCd* Me) : Module(Me)
274 mycommand = new CommandAccept(ServerInstance, maxaccepts);
275 myumode = new User_g(ServerInstance);
277 if (!ServerInstance->Modes->AddMode(myumode))
281 throw ModuleException("Could not add usermode +g");
285 ServerInstance->AddCommand(mycommand);
287 catch (const ModuleException& e)
291 throw ModuleException("Could not add command!");
294 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
295 ServerInstance->Modules->Attach(eventlist, this, 7);
298 virtual ~ModuleCallerID()
300 ServerInstance->Modes->DelMode(myumode);
304 virtual Version GetVersion()
306 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
309 virtual void On005Numeric(std::string& output)
311 output += " CALLERID=g";
314 int PreText(User* user, User* dest, std::string& text, bool notice)
316 if (!dest->IsModeSet('g'))
319 if (operoverride && IS_OPER(user))
322 callerid_data* dat = GetData(dest, true);
323 std::set<User*>::iterator i = dat->accepting.find(dest);
325 if (i == dat->accepting.end())
327 time_t now = time(NULL);
328 /* +g and *not* accepted */
329 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
330 if (now > (dat->lastnotify + (time_t)notify_cooldown))
332 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
333 dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g", dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str());
334 dat->lastnotify = now;
341 virtual int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
343 if (IS_LOCAL(user) && target_type == TYPE_USER)
344 return PreText(user, (User*)dest, text, true);
349 virtual int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
351 if (IS_LOCAL(user) && target_type == TYPE_USER)
352 return PreText(user, (User*)dest, text, true);
357 virtual void OnCleanup(int type, void* item)
359 if (type != TYPE_USER)
362 User* u = (User*)item;
363 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do. */
367 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string& extname, bool displayable)
369 if (extname == "callerid_data")
371 callerid_data* dat = GetData(user, false);
374 std::string str = dat->ToString(displayable);
375 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, str);
380 virtual void OnDecodeMetaData(int target_type, void* target, const std::string& extname, const std::string& extdata)
382 if (target_type == TYPE_USER && extname == "callerid_data")
384 User* u = (User*)target;
385 callerid_data* dat = new callerid_data(extdata, ServerInstance);
386 u->Extend("callerid_data", dat);
390 virtual int OnUserPreNick(User* user, const std::string& newnick)
393 RemoveFromAllAccepts(ServerInstance, user);
397 virtual void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
400 RemoveFromAllAccepts(ServerInstance, user);
403 virtual void OnRehash(User* user, const std::string& parameter)
405 ConfigReader Conf(ServerInstance);
406 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
407 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
408 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
409 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
413 MODULE_INIT(ModuleCallerID)