+class InvisibleWatcher : public ModeWatcher
+{
+ unsigned int& invisible;
+public:
+ InvisibleWatcher(Module* mod, unsigned int& Invisible)
+ : ModeWatcher(mod, "invisible", MODETYPE_USER), invisible(Invisible)
+ {
+ }
+
+ void AfterMode(User* source, User* dest, Channel* channel, const std::string& parameter, bool adding)
+ {
+ if (dest->registered != REG_ALL)
+ return;
+
+ if (adding)
+ invisible++;
+ else
+ invisible--;
+ }
+};
+
+class ModuleLusers : public Module
+{
+ UserModeReference invisiblemode;
+ LusersCounters counters;
+ CommandLusers cmd;
+ InvisibleWatcher mw;
+
+ unsigned int CountInvisible()
+ {
+ unsigned int c = 0;
+ for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); ++i)
+ {
+ User* u = i->second;
+ if (u->IsModeSet(invisiblemode))
+ c++;
+ }
+ return c;
+ }
+
+ public:
+ ModuleLusers()
+ : invisiblemode(this, "invisible")
+ , counters(CountInvisible())
+ , cmd(this, counters)
+ , mw(this, counters.invisible)
+ {
+ }
+
+ void init()
+ {
+ ServerInstance->Modules->AddService(cmd);
+ ServerInstance->Modes->AddModeWatcher(&mw);
+ }
+
+ void OnPostConnect(User* user)
+ {
+ counters.UpdateMaxUsers();
+ if (user->IsModeSet(invisiblemode))
+ counters.invisible++;
+ }
+
+ void OnUserQuit(User* user, const std::string& message, const std::string& oper_message)
+ {
+ if (user->IsModeSet(invisiblemode))
+ counters.invisible--;
+ }
+
+ ~ModuleLusers()
+ {
+ ServerInstance->Modes->DelModeWatcher(&mw);
+ }
+
+ Version GetVersion()
+ {
+ return Version("LUSERS", VF_VENDOR | VF_CORE);
+ }
+};
+
+MODULE_INIT(ModuleLusers)