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 struct CallerIDExtInfo : public ExtensionItem
75 CallerIDExtInfo(Module* parent)
76 : ExtensionItem("callerid_data", parent)
80 std::string serialize(Module* requestor, const Extensible* container, void* item)
82 callerid_data* dat = static_cast<callerid_data*>(item);
83 return dat->ToString(requestor);
86 void unserialize(Module* requestor, Extensible* container, const std::string& value)
88 callerid_data* dat = new callerid_data(value, requestor->ServerInstance);
89 set_raw(container, dat);
92 callerid_data* get(User* user, bool create)
94 callerid_data* dat = static_cast<callerid_data*>(get_raw(user));
97 dat = new callerid_data;
103 void free(void* item)
105 callerid_data* dat = static_cast<callerid_data*>(item);
107 // We need to walk the list of users on our accept list, and remove ourselves from their wholistsme.
108 for (std::set<User *>::iterator it = dat->accepting.begin(); it != dat->accepting.end(); it++)
110 callerid_data *targ = this->get(*it, false);
113 continue; // shouldn't happen, but oh well.
115 for (std::list<callerid_data *>::iterator it2 = targ->wholistsme.begin(); it2 != targ->wholistsme.end(); it2++)
119 targ->wholistsme.erase(it2);
127 class User_g : public SimpleUserModeHandler
130 User_g(InspIRCd* Instance, Module* Creator) : SimpleUserModeHandler(Creator, 'g') { }
133 class CommandAccept : public Command
136 CallerIDExtInfo extInfo;
137 unsigned int maxaccepts;
138 CommandAccept(InspIRCd* Instance, Module* Creator) : Command(Instance, Creator, "ACCEPT", 0, 1),
141 syntax = "{[+|-]<nicks>}|*}";
142 TRANSLATE2(TR_CUSTOM, TR_END);
145 virtual void EncodeParameter(std::string& parameter, int index)
149 std::string out = "";
150 irc::commasepstream nicks(parameter);
152 while (nicks.GetToken(tok))
156 continue; // Drop list requests, since remote servers ignore them anyway.
164 tok.erase(0, 1); // Remove the dash.
166 User* u = ServerInstance->FindNick(tok);
183 /** Will take any number of nicks (up to MaxTargets), which can be seperated by commas.
184 * - in front of any nick removes, and an * lists. This effectively means you can do:
185 * /accept nick1,nick2,nick3,*
186 * to add 3 nicks and then show your list
188 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
190 if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
192 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
193 * have a list already setup. */
195 std::string tok = parameters[0];
203 else if (tok[0] == '-')
205 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
207 return (RemoveAccept(user, whotoremove, false) ? CMD_SUCCESS : CMD_FAILURE);
213 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
215 return (AddAccept(user, whotoadd, false) ? CMD_SUCCESS : CMD_FAILURE);
218 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
224 void ListAccept(User* user)
226 callerid_data* dat = extInfo.get(user, false);
229 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
230 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
232 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
235 bool AddAccept(User* user, User* whotoadd, bool quiet)
237 // Add this user to my accept list first, so look me up..
238 callerid_data* dat = extInfo.get(user, true);
239 if (dat->accepting.size() >= maxaccepts)
242 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
246 if (!dat->accepting.insert(whotoadd).second)
249 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
254 // Now, look them up, and add me to their list
255 callerid_data *targ = extInfo.get(whotoadd, true);
256 targ->wholistsme.push_back(dat);
258 user->WriteServ("NOTICE %s :%s is now on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
262 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
264 // Remove them from my list, so look up my list..
265 callerid_data* dat = extInfo.get(user, false);
269 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
273 std::set<User*>::iterator i = dat->accepting.find(whotoremove);
274 if (i == dat->accepting.end())
277 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
282 dat->accepting.erase(i);
284 // Look up their list to remove me.
285 callerid_data *dat2 = extInfo.get(whotoremove, false);
288 // How the fuck is this possible.
292 for (std::list<callerid_data *>::iterator it = dat2->wholistsme.begin(); it != dat2->wholistsme.end(); it++)
297 dat2->wholistsme.erase(it);
302 user->WriteServ("NOTICE %s :%s is no longer on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
307 class ModuleCallerID : public Module
313 // Configuration variables:
314 bool operoverride; // Operators can override callerid.
315 bool tracknick; // Allow ACCEPT entries to update with nick changes.
316 unsigned int notify_cooldown; // Seconds between notifications.
318 /** Removes a user from all accept lists
319 * @param who The user to remove from accepts
321 void RemoveFromAllAccepts(User* who)
323 // First, find the list of people who have me on accept
324 callerid_data *userdata = cmd.extInfo.get(who, false);
328 // Iterate over the list of people who accept me, and remove all entries
329 for (std::list<callerid_data *>::iterator it = userdata->wholistsme.begin(); it != userdata->wholistsme.end(); it++)
331 callerid_data *dat = *(it);
333 // Find me on their callerid list
334 std::set<User *>::iterator it2 = dat->accepting.find(who);
336 if (it2 != dat->accepting.end())
337 dat->accepting.erase(it2);
340 userdata->wholistsme.clear();
344 ModuleCallerID(InspIRCd* Me) : Module(Me), cmd(Me, this), myumode(Me, this)
348 if (!ServerInstance->Modes->AddMode(&myumode))
349 throw ModuleException("Could not add usermode +g");
351 ServerInstance->AddCommand(&cmd);
352 Extensible::Register(&cmd.extInfo);
354 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
355 ServerInstance->Modules->Attach(eventlist, this, 6);
358 virtual ~ModuleCallerID()
360 ServerInstance->Modes->DelMode(&myumode);
363 virtual Version GetVersion()
365 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
368 virtual void On005Numeric(std::string& output)
370 output += " CALLERID=g";
373 ModResult PreText(User* user, User* dest, std::string& text, bool notice)
375 if (!dest->IsModeSet('g'))
376 return MOD_RES_PASSTHRU;
378 if (operoverride && IS_OPER(user))
379 return MOD_RES_PASSTHRU;
381 callerid_data* dat = cmd.extInfo.get(dest, true);
382 std::set<User*>::iterator i = dat->accepting.find(user);
384 if (i == dat->accepting.end())
386 time_t now = ServerInstance->Time();
387 /* +g and *not* accepted */
388 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
389 if (now > (dat->lastnotify + (time_t)notify_cooldown))
391 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
392 ServerInstance->DumpText(dest, ":%s 718 %s %s %s@%s :is messaging you, and you have umode +g. Use /ACCEPT +%s to allow.",
393 ServerInstance->Config->ServerName, dest->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), user->nick.c_str());
394 dat->lastnotify = now;
398 return MOD_RES_PASSTHRU;
401 virtual ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
403 if (IS_LOCAL(user) && target_type == TYPE_USER)
404 return PreText(user, (User*)dest, text, true);
406 return MOD_RES_PASSTHRU;
409 virtual ModResult OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
411 if (IS_LOCAL(user) && target_type == TYPE_USER)
412 return PreText(user, (User*)dest, text, true);
414 return MOD_RES_PASSTHRU;
417 ModResult OnUserPreNick(User* user, const std::string& newnick)
420 RemoveFromAllAccepts(user);
421 return MOD_RES_PASSTHRU;
424 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
426 RemoveFromAllAccepts(user);
429 virtual void OnRehash(User* user)
431 ConfigReader Conf(ServerInstance);
432 cmd.maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
433 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
434 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
435 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
439 MODULE_INIT(ModuleCallerID)