8 /* $ModDesc: Implementation of callerid (umode +g & /accept, ala hybrid etc) */
10 class callerid_data : public classbase
14 std::set<User*> accepting;
17 callerid_data* GetData(User* who, bool extend = true)
20 if (who->GetExt("callerid_data", dat))
28 dat = new callerid_data;
29 dat->lastnotify = 0; // Can't init in struct.
30 who->Extend("callerid_data", dat);
40 void RemoveData(User* who)
43 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);
55 std::set<User*>& accepting = dat->accepting;
56 std::set<User*>::iterator iter = accepting.find(who);
57 if (iter == accepting.end()) continue;
58 accepting.erase(iter);
62 class User_g : public SimpleUserModeHandler
67 User_g(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'g') { }
70 class CommandAccept : public Command
73 unsigned int& maxaccepts;
75 CommandAccept(InspIRCd* Instance, unsigned int& max) : Command(Instance, "ACCEPT", 0, 1), maxaccepts(max)
77 source = "m_callerid.so";
78 syntax = "{[+|-]<nicks>}|*}";
81 /* Will take any number of nicks, which can be seperated by spaces, commas, or a mix.
82 * - in front of any nick removes, and an * lists. This effectively means you can do:
83 * /accept nick1,nick2,nick3 *
84 * to add 3 nicks and then show your list
86 CmdResult Handle(const char* const* parameters, int pcnt, User* user)
90 /* Command stuff should've dealt with this already */
93 /* Even if callerid mode is not set, we let them manage their ACCEPT list so that if they go +g they can
94 * have a list already setup. */
95 bool atleastonechange = false;
96 for (int i = 0; i < pcnt; ++i)
98 const char* arg = parameters[i];
99 irc::commasepstream css(arg);
101 while (css.GetToken(tok))
103 if (tok.length() < 1)
107 if (IS_LOCAL(user)) continue;
110 else if (tok[0] == '-')
112 User* whotoremove = ServerInstance->FindNick(tok.substr(1));
115 atleastonechange = RemoveAccept(user, whotoremove, false) || atleastonechange;
120 User* whotoadd = ServerInstance->FindNick(tok[0] == '+' ? tok.substr(1) : tok);
123 atleastonechange = AddAccept(user, whotoadd, false) || atleastonechange;
127 user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick, tok.c_str());
132 return atleastonechange ? CMD_FAILURE : CMD_SUCCESS;
135 void ListAccept(User* user)
137 callerid_data* dat = GetData(user, false);
140 for (std::set<User*>::iterator i = dat->accepting.begin(); i != dat->accepting.end(); ++i)
142 user->WriteNumeric(281, "%s %s", user->nick, (*i)->nick);
145 user->WriteNumeric(282, "%s :End of ACCEPT list", user->nick);
148 bool AddAccept(User* user, User* whotoadd, bool quiet)
150 callerid_data* dat = GetData(user, true);
151 std::set<User*>& accepting = dat->accepting;
152 if (accepting.size() >= maxaccepts)
154 if (!quiet) user->WriteNumeric(456, "%s :Accept list is full (limit is %d)", user->nick, maxaccepts);
157 if (!accepting.insert(whotoadd).second)
159 if (!quiet) user->WriteNumeric(457, "%s %s :is already on your accept list", user->nick, whotoadd->nick);
165 bool RemoveAccept(User* user, User* whotoremove, bool quiet)
167 callerid_data* dat = GetData(user, false);
170 if (!quiet) user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick, whotoremove->nick);
173 std::set<User*>& accepting = dat->accepting;
174 std::set<User*>::iterator i = accepting.find(whotoremove);
175 if (i == accepting.end())
177 if (!quiet) user->WriteNumeric(458, "%s %s :is not on your accept list", user->nick, whotoremove->nick);
185 class ModuleCallerID : public Module
188 CommandAccept *mycommand;
191 // Configuration variables:
192 unsigned int maxaccepts; // Maximum ACCEPT entries.
193 bool operoverride; // Operators can override callerid.
194 bool tracknick; // Allow ACCEPT entries to update with nick changes.
195 unsigned int notify_cooldown; // Seconds between notifications.
198 ModuleCallerID(InspIRCd* Me) : Module(Me)
201 mycommand = new CommandAccept(ServerInstance, maxaccepts);
202 myumode = new User_g(ServerInstance);
204 ServerInstance->AddCommand(mycommand);
205 } catch (const ModuleException& e) {
209 if (!ServerInstance->Modes->AddMode(myumode))
213 throw new ModuleException("Could not add usermode and command!");
215 Implementation eventlist[] = { I_OnRehash, I_OnUserPreNick, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage, I_OnCleanup };
216 ServerInstance->Modules->Attach(eventlist, this, 7);
226 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
229 void On005Numeric(std::string& output)
231 output += " CALLERID=g";
234 int PreText(User* user, User* dest, std::string& text, bool notice)
236 if (!dest->IsModeSet('g')) return 0;
237 if (operoverride && IS_OPER(user)) return 0;
238 callerid_data* dat = GetData(dest, true);
239 std::set<User*>& accepting = dat->accepting;
240 time_t& lastnotify = dat->lastnotify;
241 std::set<User*>::iterator i = accepting.find(dest);
242 if (i == accepting.end())
244 time_t now = time(NULL);
245 /* +g and *not* accepted */
246 user->WriteNumeric(716, "%s %s :is in +g mode (server-side ignore).", user->nick, dest->nick);
247 if (now > (lastnotify + (time_t)notify_cooldown))
249 user->WriteNumeric(717, "%s %s :has been informed that you messaged them.", user->nick, dest->nick);
250 dest->WriteNumeric(718, "%s %s %s@%s :is messaging you, and you have umode +g", dest->nick, user->nick, user->ident, user->dhost);
258 int OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
260 if (IS_LOCAL(user) && target_type == TYPE_USER)
261 return PreText(user, (User*)dest, text, true);
265 int OnUserPreNotice(User* user, void* dest, int target_type, std::string& text, char status, CUList &exempt_list)
267 if (IS_LOCAL(user) && target_type == TYPE_USER)
268 return PreText(user, (User*)dest, text, true);
272 void OnCleanup(int type, void* item)
274 if (type != TYPE_USER) return;
275 User* u = (User*)item;
276 /* Cleanup only happens on unload (before dtor), so keep this O(n) instead of O(n^2) which deferring to OnUserQuit would do. */
280 int OnUserPreNick(User* user, const std::string& newnick)
283 RemoveFromAllAccepts(ServerInstance, user);
287 void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
290 RemoveFromAllAccepts(ServerInstance, user);
293 void OnRehash(User* user, const std::string& parameter)
295 ConfigReader Conf(ServerInstance);
296 int new_maxaccepts, new_cooldown;
297 bool new_override, new_track;
298 new_maxaccepts = Conf.ReadInteger("callerid", "maxaccepts", "16", 0, true);
299 switch (Conf.GetError())
302 case CONF_VALUE_NOT_FOUND:
305 case CONF_NOT_A_NUMBER:
306 if (user) user->WriteServ("NOTICE %s :Invalid maxaccepts value '%s', not a number", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
307 throw ModuleException("Invalid maxaccepts value, not a number");
308 case CONF_INT_NEGATIVE:
309 if (user) user->WriteServ("NOTICE %s :Invalid maxaccepts value '%s', negative", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
310 throw ModuleException("Invalid maxaccepts value, negative");
313 throw ModuleException("Invalid maxaccepts value, unknown config error");
315 new_override = Conf.ReadFlag("callerid", "operoverride", "0", 0);
316 if (Conf.GetError() == CONF_VALUE_NOT_FOUND) new_override = false;
317 new_track = Conf.ReadFlag("callerid", "tracknick", "0", 0);
318 if (Conf.GetError() == CONF_VALUE_NOT_FOUND) new_track = false;
319 new_cooldown = Conf.ReadInteger("callerid", "cooldown", "60", 0, true);
320 switch (Conf.GetError())
323 case CONF_VALUE_NOT_FOUND:
326 case CONF_NOT_A_NUMBER:
327 if (user) user->WriteServ("NOTICE %s :Invalid cooldown value '%s', not a number", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
328 throw ModuleException("Invalid cooldown value, not a number");
329 case CONF_INT_NEGATIVE:
330 if (user) user->WriteServ("NOTICE %s :Invalid cooldown value '%s', negative", user->nick, Conf.ReadValue("callerid", "maxaccepts", "", 0).c_str());
331 throw ModuleException("Invalid cooldown value, negative");
334 throw ModuleException("Invalid cooldown value, unknown config error");
336 maxaccepts = new_maxaccepts;
337 notify_cooldown = new_cooldown;
338 operoverride = new_override;
339 tracknick = new_track;
343 MODULE_INIT(ModuleCallerID)