X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_watch.cpp;h=6f205fa2fba4cc420ca05a9f6850c8121a8f60f7;hb=6f4bf8ffd367f35b96265fea1ad01fb1acf2adcd;hp=008aada574fb117f320b4991294847ba20c7b3cf;hpb=1d029df57061524a1d30a6ffaa3fdee37181d638;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_watch.cpp b/src/modules/m_watch.cpp index 008aada57..6f205fa2f 100644 --- a/src/modules/m_watch.cpp +++ b/src/modules/m_watch.cpp @@ -2,14 +2,14 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * Inspire is copyright (C) 2002-2004 ChatSpike-Dev. - * E-mail: - * - * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * + * * * Written by Craig Edwards, Craig McLure, and others. * This program is free but copyrighted software; see - * the file COPYING for details. + * the file COPYING for details. * * --------------------------------------------------- */ @@ -22,236 +22,321 @@ using namespace std; #include "users.h" #include "channels.h" #include "modules.h" -#include "helperfuncs.h" #include "hashcomp.h" +#include "inspircd.h" /* $ModDesc: Provides support for the /watch command */ -Server *Srv; +/* nickname list of users watching the nick */ +typedef std::map > watchentries; -class watchentry -{ - public: - userrec* watcher; - std::string target; -}; +/* nickname 'ident host signon', or empty if not online */ +typedef std::map watchlist; -typedef std::vector watchlist; -watchlist watches; +/* Whos watching each nickname */ +watchentries whos_watching_me; -void handle_watch(char **parameters, int pcnt, userrec *user) +/** Handle /WATCH + */ +class cmd_watch : public command_t { - if (!pcnt) + unsigned int& MAX_WATCH; + public: + CmdResult remove_watch(userrec* user, const char* nick) { - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) - { - if (q->watcher == user) - { - userrec* targ = Srv->FindNick(q->target); - if (targ) - { - WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age); - } - } - } - WriteServ(user->fd,"607 %s :End of WATCH list",user->nick); + // removing an item from the list + if (!ServerInstance->IsNick(nick)) + { + user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick); + return CMD_FAILURE; + } + + watchlist* wl; + if (user->GetExt("watchlist", wl)) + { + /* Yup, is on my list */ + watchlist::iterator n = wl->find(nick); + if (n != wl->end()) + { + if (!n->second.empty()) + user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str()); + else + user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick); + + wl->erase(n); + } + + if (!wl->size()) + { + user->Shrink("watchlist"); + delete wl; + } + + watchentries::iterator x = whos_watching_me.find(nick); + if (x != whos_watching_me.end()) + { + /* People are watching this user, am i one of them? */ + std::deque::iterator n = std::find(x->second.begin(), x->second.end(), user); + if (n != x->second.end()) + /* I'm no longer watching you... */ + x->second.erase(n); + + if (!x->second.size()) + whos_watching_me.erase(nick); + } + } + + return CMD_SUCCESS; } - else if (pcnt > 0) + + CmdResult add_watch(userrec* user, const char* nick) { - for (int x = 0; x < pcnt; x++) + if (!ServerInstance->IsNick(nick)) + { + user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick); + return CMD_FAILURE; + } + + watchlist* wl; + if (!user->GetExt("watchlist", wl)) + { + wl = new watchlist(); + user->Extend("watchlist", wl); + } + + if (wl->size() == MAX_WATCH) { - char *nick = parameters[x]; - if (!strcasecmp(nick,"C")) + user->WriteServ("942 %s %s :Too many WATCH entries", user->nick, nick); + return CMD_FAILURE; + } + + watchlist::iterator n = wl->find(nick); + if (n == wl->end()) + { + /* Don't already have the user on my watch list, proceed */ + watchentries::iterator x = whos_watching_me.find(nick); + if (x != whos_watching_me.end()) { - // watch clear - bool done = false; - while (!done) - { - done = true; - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) - { - if (q->watcher == user) - { - done = false; - watches.erase(q); - break; - } - } - } + /* People are watching this user, add myself */ + x->second.push_back(user); } - else if (!strcasecmp(nick,"L")) + else { - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) - { - if (q->watcher == user) - { - userrec* targ = Srv->FindNick(q->target); - if (targ) - { - WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,targ->nick,targ->ident,targ->dhost,targ->age); - } - } - } - WriteServ(user->fd,"607 %s :End of WATCH list",user->nick); + std::deque newlist; + newlist.push_back(user); + whos_watching_me[nick] = newlist; } - else if (!strcasecmp(nick,"S")) + + userrec* target = ServerInstance->FindNick(nick); + if (target) { - std::string list = ""; - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) - { - if (q->watcher == user) - { - list = list + " " + q->target; - } - } - char* l = (char*)list.c_str(); - if (*l == ' ') - l++; - WriteServ(user->fd,"606 %s :%s",user->nick,l); - WriteServ(user->fd,"607 %s :End of WATCH S",user->nick); + (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age)); + user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str()); } - else if (nick[0] == '-') + else { - // removing an item from the list - nick++; - irc::string n1 = nick; - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) - { - if (q->watcher == user) - { - irc::string n2 = q->target.c_str(); - userrec* a = Srv->FindNick(q->target); - if (a) - { - WriteServ(user->fd,"602 %s %s %s %s %lu :stopped watching",user->nick,a->nick,a->ident,a->dhost,a->age); - } - else - { - WriteServ(user->fd,"602 %s %s * * 0 :stopped watching",user->nick,q->target.c_str()); - } - if (n1 == n2) - { - watches.erase(q); - break; - } - } + (*wl)[nick] = ""; + user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick); + } + } + + return CMD_SUCCESS; + } + + cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch) + { + this->source = "m_watch.so"; + syntax = "[C|L|S]|[+|-]"; + } + + CmdResult Handle (const char** parameters, int pcnt, userrec *user) + { + if (!pcnt) + { + watchlist* wl; + if (user->GetExt("watchlist", wl)) + { + for (watchlist::iterator q = wl->begin(); q != wl->end(); q++) + { + if (!q->second.empty()) + user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str()); } } - else if (nick[0] == '+') + user->WriteServ("607 %s :End of WATCH list",user->nick); + } + else if (pcnt > 0) + { + for (int x = 0; x < pcnt; x++) { - nick++; - irc::string n1 = nick; - bool exists = false; - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) + const char *nick = parameters[x]; + if (!strcasecmp(nick,"C")) + { + // watch clear + watchlist* wl; + if (user->GetExt("watchlist", wl)) + { + delete wl; + user->Shrink("watchlist"); + } + } + else if (!strcasecmp(nick,"L")) { - if (q->watcher == user) + watchlist* wl; + if (user->GetExt("watchlist", wl)) { - irc::string n2 = q->target.c_str(); - if (n1 == n2) + for (watchlist::iterator q = wl->begin(); q != wl->end(); q++) { - // already on watch list - exists = true; + if (!q->second.empty()) + user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str()); } } + user->WriteServ("607 %s :End of WATCH list",user->nick); + } + else if (!strcasecmp(nick,"S")) + { + watchlist* wl; + int you_have = 0; + int youre_on = 0; + std::string list; + + if (user->GetExt("watchlist", wl)) + { + for (watchlist::iterator q = wl->begin(); q != wl->end(); q++) + if (!q->second.empty()) + list.append(q->first.c_str()).append(" "); + you_have = wl->size(); + } + + watchentries::iterator x = whos_watching_me.find(user->nick); + if (x != whos_watching_me.end()) + youre_on = x->second.size(); + + user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on); + if (!list.empty()) + user->WriteServ("606 %s :%s",user->nick, list.c_str()); + user->WriteServ("607 %s :End of WATCH S",user->nick); } - if (!exists) + else if (nick[0] == '-') { - watchentry w; - w.watcher = user; - w.target = nick; - watches.push_back(w); - log(DEBUG,"*** Added %s to watchlist of %s",nick,user->nick); + nick++; + return remove_watch(user, nick); + } + else if (nick[0] == '+') + { + nick++; + return add_watch(user, nick); } - userrec* a = Srv->FindNick(nick); - if (a) - { - WriteServ(user->fd,"604 %s %s %s %s %lu :is online",user->nick,a->nick,a->ident,a->dhost,a->age); - } - else - { - WriteServ(user->fd,"605 %s %s * * 0 :is offline",user->nick,nick); - } } } + /* So that spanningtree doesnt pass the WATCH commands to the network! */ + return CMD_FAILURE; } - return; -} - +}; class Modulewatch : public Module { - + cmd_watch* mycommand; + unsigned int maxwatch; public: - Modulewatch() + Modulewatch(InspIRCd* Me) + : Module::Module(Me), maxwatch(32) { - Srv = new Server; - Srv->AddCommand("WATCH",handle_watch,0,0,"m_watch.so"); + mycommand = new cmd_watch(ServerInstance, maxwatch); + ServerInstance->AddCommand(mycommand); } - virtual void OnUserQuit(userrec* user) + void Implements(char* List) { - log(DEBUG,"*** WATCH: On global quit: user %s",user->nick); - irc::string n2 = user->nick; - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) + List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1; + } + + virtual void OnUserQuit(userrec* user, const std::string &reason) + { + ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick); + watchentries::iterator x = whos_watching_me.find(user->nick); + if (x != whos_watching_me.end()) { - irc::string n1 = q->target.c_str(); - if (n1 == n2) + for (std::deque::iterator n = x->second.begin(); n != x->second.end(); n++) { - log(DEBUG,"*** WATCH: On global quit: user %s is in notify of %s",user->nick,q->watcher->nick); - WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,user->nick,user->ident,user->dhost,time(NULL)); + (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time()); + watchlist* wl; + if ((*n)->GetExt("watchlist", wl)) + /* We were on somebody's notify list, set ourselves offline */ + (*wl)[user->nick] = ""; } } - bool done = false; - while (!done) + + /* Now im quitting, if i have a notify list, im no longer watching anyone */ + watchlist* wl; + if (user->GetExt("watchlist", wl)) { - done = true; - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) + /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */ + for (watchlist::iterator i = wl->begin(); i != wl->end(); i++) { - if (q->watcher == user) + watchentries::iterator x = whos_watching_me.find(i->first); + if (x != whos_watching_me.end()) { - done = false; - watches.erase(q); - break; + /* People are watching this user, am i one of them? */ + std::deque::iterator n = std::find(x->second.begin(), x->second.end(), user); + if (n != x->second.end()) + /* I'm no longer watching you... */ + x->second.erase(n); + + if (!x->second.size()) + whos_watching_me.erase(user->nick); } } } } - virtual void OnGlobalConnect(userrec* user) + virtual void OnPostConnect(userrec* user) { - irc::string n2 = user->nick; - log(DEBUG,"*** WATCH: On global connect: user %s",user->nick); - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) + ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick); + watchentries::iterator x = whos_watching_me.find(user->nick); + if (x != whos_watching_me.end()) { - irc::string n1 = q->target.c_str(); - if (n1 == n2) + for (std::deque::iterator n = x->second.begin(); n != x->second.end(); n++) { - log(DEBUG,"*** WATCH: On global connect: user %s is in notify of %s",user->nick,q->watcher->nick); - WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age); + (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age); + watchlist* wl; + if ((*n)->GetExt("watchlist", wl)) + /* We were on somebody's notify list, set ourselves online */ + (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age)); } } } - virtual void OnUserPostNick(userrec* user, std::string oldnick) + virtual void OnUserPostNick(userrec* user, const std::string &oldnick) { - irc::string n2 = oldnick.c_str(); - irc::string n3 = user->nick; - log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick); - for (watchlist::iterator q = watches.begin(); q != watches.end(); q++) + ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick); + + watchentries::iterator new_online = whos_watching_me.find(user->nick); + watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick)); + + if (new_online != whos_watching_me.end()) { - irc::string n1 = q->target.c_str(); - // changed from a nick on the watchlist to one that isnt - if (n1 == n2) + for (std::deque::iterator n = new_online->second.begin(); n != new_online->second.end(); n++) { - log(DEBUG,"*** WATCH: On global nickchange: old nick %s was on notify list of %s",oldnick.c_str(),q->watcher->nick); - WriteServ(q->watcher->fd,"601 %s %s %s %s %lu :went offline",q->watcher->nick,oldnick.c_str(),user->ident,user->dhost,time(NULL)); + watchlist* wl; + if ((*n)->GetExt("watchlist", wl)) + { + (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age)); + (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, (*wl)[user->nick].c_str()); + } } - else if (n1 == n3) + } + + if (new_offline != whos_watching_me.end()) + { + for (std::deque::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++) { - // changed from a nick not on notify to one that is - log(DEBUG,"*** WATCH: On global nickchange: new nick %s is on notify list of %s",user->nick,q->watcher->nick); - WriteServ(q->watcher->fd,"600 %s %s %s %s %lu :arrived online",q->watcher->nick,user->nick,user->ident,user->dhost,user->age); + watchlist* wl; + if ((*n)->GetExt("watchlist", wl)) + { + (*n)->WriteServ("601 %s %s %s :went offline", (*n)->nick, (*wl)[user->nick].c_str()); + (*wl)[user->nick] = ""; + } } } } @@ -259,17 +344,16 @@ class Modulewatch : public Module virtual void On005Numeric(std::string &output) { // we don't really have a limit... - output = output + " WATCH=999"; + output = output + " WATCH=32"; } virtual ~Modulewatch() { - delete Srv; } virtual Version GetVersion() { - return Version(1,0,0,1,VF_VENDOR); + return Version(1,1,0,1,VF_VENDOR,API_VERSION); } }; @@ -285,9 +369,9 @@ class ModulewatchFactory : public ModuleFactory { } - virtual Module * CreateModule() + virtual Module * CreateModule(InspIRCd* Me) { - return new Modulewatch; + return new Modulewatch(Me); } };