8 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
10 class callerid_data : public classbase
14 std::set<User*> accepting;
16 callerid_data() : lastnotify(0) { }
19 callerid_data* GetData(User* who, bool extend = true)
22 if (who->GetExt("callerid_data", dat))
28 dat = new callerid_data;
29 who->Extend("callerid_data", dat);
37 void RemoveData(User* who)
40 who->GetExt("callerid_data", dat);
45 who->Shrink("callerid_data");
49 void RemoveFromAllAccepts(InspIRCd* ServerInstance, User* who)
51 for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
53 callerid_data* dat = GetData(i->second, false);
58 std::set<User*>& accepting = dat->accepting;
59 std::set<User*>::iterator iter = accepting.find(who);
61 if (iter == accepting.end())
64 accepting.erase(iter);
68 class User_g : public SimpleUserModeHandler
71 User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
74 class CommandAccept : public Command
77 unsigned int& maxaccepts;
79 CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
81 source = "m_callerid.so";
82 syntax = "{[+|-]<nicks>}|*}";
85 /** Will take any number of nicks, which can be seperated by spaces, commas, or a mix.
86 * - in front of any nick removes, and an * lists. This effectively means you can do:
87 * /accept nick1,nick2,nick3,*
88 * to add 3 nicks and then show your list
90 CmdResult Handle(const std::vector<std::string> ¶meters, User* user)
92 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
93 * have a list already setup. */
94 bool atleastonechange = false;
95 for (unsigned int i = 0; i < parameters.size(); ++i)
97 const char* arg = parameters[i].c_str();
98 irc::commasepstream css(arg);
101 while (css.GetToken(tok))
113 else if (tok[0] == '-')
115 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
117 atleastonechange = RemoveAccept(user, whotoremove, false) || atleastonechange;
121 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
123 atleastonechange = AddAccept(user, whotoadd, false) || atleastonechange;
125 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), tok.c_str());
129 return atleastonechange ? CMD_FAILURE : CMD_SUCCESS;
132 void ListAccept(User* user)
134 callerid_data* dat = GetData(user, false);
137 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
138 user->WriteNumeric(281, "%s %s", user->nick.c_str(), (*i)->nick.c_str());
140 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick.c_str());
143 bool AddAccept(User* user, User* whotoadd, bool quiet)
145 callerid_data* dat = GetData(user, true);
146 std::set<User*>& accepting = dat->accepting;
147 if (accepting.size() >= maxaccepts)
150 user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick.c_str(), maxaccepts);
154 if (!accepting.insert(whotoadd).second)
157 user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick.c_str(), whotoadd->nick.c_str());
164 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
166 callerid_data* dat = GetData(user, false);
170 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
174 std::set<User*>& accepting = dat->accepting;
175 std::set<User*>::iterator i = accepting.find(whotoremove);
176 if (i == accepting.end())
179 user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick.c_str(), whotoremove->nick.c_str());
188 class ModuleCallerID : public Module
191 CommandAccept *mycommand;
194 // Configuration variables:
195 unsigned int maxaccepts; // Maximum ACCEPT entries.
196 bool operoverride; // Operators can override callerid.
197 bool tracknick; // Allow ACCEPT entries to update with nick changes.
198 unsigned int notify_cooldown; // Seconds between notifications.
201 ModuleCallerID(InspIRCd* Me) : Module(Me)
204 mycommand = new CommandAccept(ServerInstance, maxaccepts);
205 myumode = new User_g(ServerInstance);
209 ServerInstance->AddCommand(mycommand);
211 catch (const ModuleException& e)
214 throw ModuleException("Could not add command!");
216 if (!ServerInstance->Modes->AddMode(myumode))
220 throw ModuleException("Could not add usermode +g");
222 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
223 ServerInstance->Modules->Attach(eventlist, this, 7);
228 ServerInstance->Modes->DelMode(myumode);
234 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
237 void On005Numeric(std::string& output)
239 output += " CALLERID=g";
242 int PreText(User* user, User* dest, std::string& text, bool notice)
244 if (!dest->IsModeSet('g'))
247 if (operoverride && IS_OPER(user))
250 callerid_data* dat = GetData(dest, true);
251 std::set<User*>& accepting = dat->accepting;
252 time_t& lastnotify = dat->lastnotify;
253 std::set<User*>::iterator i = accepting.find(dest);
255 if (i == accepting.end())
257 time_t now = time(NULL);
258 /* +g and *not* accepted */
259 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick.c_str(), dest->nick.c_str());
260 if (now > (lastnotify + (time_t)notify_cooldown))
262 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick.c_str(), dest->nick.c_str());
263 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());
271 int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
273 if (IS_LOCAL(user) && target_type == TYPE_USER)
274 return PreText(user, (User*)dest, text, true);
279 int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
281 if (IS_LOCAL(user) && target_type == TYPE_USER)
282 return PreText(user, (User*)dest, text, true);
287 void OnCleanup(int type, void* item)
289 if (type != TYPE_USER)
292 User* u = (User*)item;
293 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do. */
297 int OnUserPreNick(User* user, const std::string& newnick)
300 RemoveFromAllAccepts(ServerInstance, user);
304 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
307 RemoveFromAllAccepts(ServerInstance, user);
310 void OnRehash(User* user, const std::string& parameter)
312 ConfigReader Conf(ServerInstance);
313 maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
314 operoverride = Conf.ReadFlag("callerid", "operoverride", "0", 0);
315 tracknick = Conf.ReadFlag("callerid", "tracknick", "0", 0);
316 notify_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
320 MODULE_INIT(ModuleCallerID)