1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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) */
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)
37 irc::commasepstream s(str);
41 lastnotify = ConvToInt(tok);
43 while (s.GetToken(tok))
50 User *u = ServerInstance->FindNick(tok);
59 std::string ToString(SerializeFormat format) const
61 std::ostringstream oss;
63 for (std::set<User*>::const_iterator i = accepting.begin(); i != accepting.end(); ++i)
67 oss << "," << (format == FORMAT_USER ? u->nick : u->uuid);
74 struct CallerIDExtInfo : public ExtensionItem
76 CallerIDExtInfo(Module* parent)
77 : ExtensionItem("callerid_data", parent)
81 std::string serialize(SerializeFormat format, const Extensible* container, void* item) const
83 callerid_data* dat = static_cast<callerid_data*>(item);
84 return dat->ToString(format);
87 void unserialize(SerializeFormat format, Extensible* container, const std::string& value)
89 callerid_data* dat = new callerid_data(value);
90 set_raw(container, dat);
93 callerid_data* get(User* user, bool create)
95 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
98 dat = new callerid_data;
104 void free(void* item)
106 callerid_data* dat = static_cast<callerid_data*>(item);
108 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
109 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
111 callerid_data *targ = this->get(*it, false);
114 continue; // shouldn't happen, but oh well.
116 for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
120 targ->wholistsme.erase(it2);
128 class User_g : public SimpleUserModeHandler
131 User_g(Module* Creator) : SimpleUserModeHandler(Creator, "callerid", 'g') { }
134 class CommandAccept : public Command
137 CallerIDExtInfo extInfo;
138 unsigned int maxaccepts;
139 CommandAccept(Module* Creator) : Command(Creator, "ACCEPT", 1),
142 syntax = "{[+|-]<nicks>}|*}";
143 TRANSLATE2(TR_CUSTOM, TR_END);
146 virtual void EncodeParameter(std::string& parameter, int index)
150 std::string out = "";
151 irc::commasepstream nicks(parameter);
153 while (nicks.GetToken(tok))
157 continue; // Drop list requests, since remote servers ignore them anyway.
165 tok.erase(0, 1); // Remove the dash.
167 User* u = ServerInstance->FindNick(tok);
184 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
185 * - in front of any nick removes, and an * lists. This effectively means you can do:
186 * /accept nick1,nick2,nick3,*
187 * to add 3 nicks and then show your list
189 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
191 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
193 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
194 * have a list already setup. */
196 std::string tok = parameters[0];
204 else if (tok[0] == '-')
206 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
208 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
214 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
216 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
219 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
225 void ListAccept(User* user)
227 callerid_data* dat = extInfo.get(user, false);
230 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
231 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
233 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
236 bool AddAccept(User* user, User* whotoadd, bool quiet)
238 // Add this user to my accept list first, so look me up..
239 callerid_data* dat = extInfo.get(user, true);
240 if (dat->accepting.size() >= maxaccepts)
243 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
247 if (!dat->accepting.insert(whotoadd).second)
250 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
255 // Now, look them up, and add me to their list
256 callerid_data *targ = extInfo.get(whotoadd, true);
257 targ->wholistsme.push_back(dat);
259 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
263 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
265 // Remove them from my list, so look up my list..
266 callerid_data* dat = extInfo.get(user, false);
270 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
274 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
275 if (i == dat->accepting.end())
278 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
283 dat->accepting.erase(i);
285 // Look up their list to remove me.
286 callerid_data *dat2 = extInfo.get(whotoremove, false);
289 // How the fuck is this possible.
293 for (std::list<callerid_data *>::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++)
298 dat2->wholistsme.erase(it);
303 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
308 class ModuleCallerID : public Module
314 // Configuration variables:
315 bool operoverride; // Operators can override callerid.
316 bool tracknick; // Allow ACCEPT entries to update with nick changes.
317 unsigned int notify_cooldown; // Seconds between notifications.
319 /** Removes a user from all accept lists
320 * @param who The user to remove from accepts
322 void RemoveFromAllAccepts(User* who)
324 // First, find the list of people who have me on accept
325 callerid_data *userdata = cmd.extInfo.get(who, false);
329 // Iterate over the list of people who accept me, and remove all entries
330 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
332 callerid_data *dat = *(it);
334 // Find me on their callerid list
335 std::set<User *>::iterator it2 = dat->accepting.find(who);
337 if (it2 != dat->accepting.end())
338 dat->accepting.erase(it2);
341 userdata->wholistsme.clear();
345 ModuleCallerID() : cmd(this), myumode(this)
353 ServerInstance->Modules->AddService(myumode);
354 ServerInstance->Modules->AddService(cmd);
355 ServerInstance->Modules->AddService(cmd.extInfo);
357 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
358 ServerInstance->Modules->Attach(eventlist, this, 6);
361 virtual ~ModuleCallerID()
365 virtual Version GetVersion()
367 return Version("Implementation of callerid (umode +g & /accept, ala hybrid etc)", VF_COMMON | VF_VENDOR);
370 virtual void On005Numeric(std::string& output)
372 output += " CALLERID=g";
375 ModResult PreText(User* user, User* dest, std::string& text, bool notice)
377 if (!dest->IsModeSet('g'))
378 return MOD_RES_PASSTHRU;
380 if (operoverride && IS_OPER(user))
381 return MOD_RES_PASSTHRU;
383 callerid_data* dat = cmd.extInfo.get(dest, true);
384 std::set<User*>::iterator i = dat->accepting.find(user);
386 if (i == dat->accepting.end())
388 time_t now = ServerInstance->Time();
389 /* +g and *not* accepted */
390 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
391 if (now > (dat->lastnotify + (time_t)notify_cooldown))
393 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
394 dest->SendText(":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
395 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());
396 dat->lastnotify = now;
400 return MOD_RES_PASSTHRU;
403 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
405 if (IS_LOCAL(user) && target_type == TYPE_USER)
406 return PreText(user, (User*)dest, text, true);
408 return MOD_RES_PASSTHRU;
411 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
413 if (IS_LOCAL(user) && target_type == TYPE_USER)
414 return PreText(user, (User*)dest, text, true);
416 return MOD_RES_PASSTHRU;
419 ModResult OnUserPreNick(User* user, const std::string& newnick)
422 RemoveFromAllAccepts(user);
423 return MOD_RES_PASSTHRU;
426 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
428 RemoveFromAllAccepts(user);
431 virtual void OnRehash(User* user)
434 cmd.maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
435 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
436 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
437 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
441 MODULE_INIT(ModuleCallerID)