X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_watch.cpp;h=029055db4ee9cb8716f66115a32650ee80e11c49;hb=a17421dca39d4afabf7143797baa6e13a19a5a97;hp=ddb6f26670eabdb085101613c76de437213f5fe0;hpb=bf5a8dc97fd0e8dee0d0a1946d4fcd31bebb4a24;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_watch.cpp b/src/modules/m_watch.cpp index ddb6f2667..029055db4 100644 --- a/src/modules/m_watch.cpp +++ b/src/modules/m_watch.cpp @@ -1,16 +1,25 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2005-2008 Craig Edwards + * Copyright (C) 2006-2008 Robin Burchell + * Copyright (C) 2007 Dennis Friis * - * InspIRCd: (C) 2002-2008 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. * - * This program is free but copyrighted software; see - * the file COPYING for details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. * - * --------------------------------------------------- + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + #include "inspircd.h" /* $ModDesc: Provides support for the /WATCH command */ @@ -49,7 +58,7 @@ * * KEY: Brain ---> Watched by: Boo, w00t, Om * KEY: Boo ---> Watched by: Brain, w00t - * + * * This is used when we want to tell all the users that are watching someone that * they are now available or no longer available. For example, if the hash was * populated as shown above, then when Brain signs on, messages are sent to Boo, w00t @@ -87,10 +96,10 @@ * Before you start screaming, this definition is only used here, so moving it to a header is pointless. * Yes, it's horrid. Blame cl for being different. -- w00t */ -#ifdef WINDOWS -typedef nspace::hash_map, nspace::hash_compare > > watchentries; +#if defined(WINDOWS) && !defined(HASHMAP_DEPRECATED) + typedef nspace::hash_map, nspace::hash_compare > > watchentries; #else -typedef nspace::hash_map, nspace::hash > watchentries; + typedef nspace::hash_map, irc::hash> watchentries; #endif typedef std::map watchlist; @@ -103,9 +112,8 @@ watchentries* whos_watching_me; class CommandSVSWatch : public Command { public: - CommandSVSWatch (InspIRCd* Instance) : Command(Instance,"SVSWATCH", 0, 2) + CommandSVSWatch(Module* Creator) : Command(Creator,"SVSWATCH", 2) { - this->source = "m_watch.so"; syntax = " [C|L|S]|[+|-]"; TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */ } @@ -114,18 +122,26 @@ class CommandSVSWatch : public Command { if (!ServerInstance->ULine(user->server)) return CMD_FAILURE; - + User *u = ServerInstance->FindNick(parameters[0]); if (!u) return CMD_FAILURE; - + if (IS_LOCAL(u)) { ServerInstance->Parser->CallHandler("WATCH", parameters, u); } - + return CMD_SUCCESS; } + + RouteDescriptor GetRouting(User* user, const std::vector& parameters) + { + User* target = ServerInstance->FindNick(parameters[0]); + if (target) + return ROUTE_OPT_UCAST(target->server); + return ROUTE_LOCALONLY; + } }; /** Handle /WATCH @@ -134,17 +150,18 @@ class CommandWatch : public Command { unsigned int& MAX_WATCH; public: + SimpleExtItem ext; CmdResult remove_watch(User* user, const char* nick) { // removing an item from the list - if (!ServerInstance->IsNick(nick)) + if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax)) { - user->WriteNumeric(942, "%s %s :Invalid nickname", user->nick, nick); + user->WriteNumeric(942, "%s %s :Invalid nickname", user->nick.c_str(), nick); return CMD_FAILURE; } - watchlist* wl; - if (user->GetExt("watchlist", wl)) + watchlist* wl = ext.get(user); + if (wl) { /* Yup, is on my list */ watchlist::iterator n = wl->find(nick); @@ -155,17 +172,16 @@ class CommandWatch : public Command if (n != wl->end()) { if (!n->second.empty()) - user->WriteNumeric(602, "%s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str()); + user->WriteNumeric(602, "%s %s %s :stopped watching", user->nick.c_str(), n->first.c_str(), n->second.c_str()); else - user->WriteNumeric(602, "%s %s * * 0 :stopped watching", user->nick, nick); + user->WriteNumeric(602, "%s %s * * 0 :stopped watching", user->nick.c_str(), nick); wl->erase(n); } - if (!wl->size()) + if (wl->empty()) { - user->Shrink("watchlist"); - delete wl; + ext.unset(user); } watchentries::iterator x = whos_watching_me->find(nick); @@ -177,36 +193,33 @@ class CommandWatch : public Command /* I'm no longer watching you... */ x->second.erase(n2); - if (!x->second.size()) + if (x->second.empty()) + /* nobody else is, either. */ whos_watching_me->erase(nick); } } - /* This might seem confusing, but we return CMD_FAILURE - * to indicate that this message shouldnt be routed across - * the network to other linked servers. - */ - return CMD_FAILURE; + return CMD_SUCCESS; } CmdResult add_watch(User* user, const char* nick) { - if (!ServerInstance->IsNick(nick)) + if (!ServerInstance->IsNick(nick, ServerInstance->Config->Limits.NickMax)) { - user->WriteNumeric(942, "%s %s :Invalid nickname",user->nick,nick); + user->WriteNumeric(942, "%s %s :Invalid nickname",user->nick.c_str(),nick); return CMD_FAILURE; } - watchlist* wl; - if (!user->GetExt("watchlist", wl)) + watchlist* wl = ext.get(user); + if (!wl) { wl = new watchlist(); - user->Extend("watchlist", wl); + ext.set(user, wl); } if (wl->size() == MAX_WATCH) { - user->WriteNumeric(512, "%s %s :Too many WATCH entries", user->nick, nick); + user->WriteNumeric(512, "%s %s :Too many WATCH entries", user->nick.c_str(), nick); return CMD_FAILURE; } @@ -230,51 +243,43 @@ class CommandWatch : public Command User* target = ServerInstance->FindNick(nick); if (target) { - if (target->Visibility && !target->Visibility->VisibleTo(user)) - { - (*wl)[nick] = ""; - user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick, nick); - return CMD_FAILURE; - } - (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age)); - user->WriteNumeric(604, "%s %s %s :is online",user->nick, nick, (*wl)[nick].c_str()); + user->WriteNumeric(604, "%s %s %s :is online",user->nick.c_str(), nick, (*wl)[nick].c_str()); if (IS_AWAY(target)) { - user->WriteNumeric(609, "%s %s %s %s %lu :is away", user->nick, target->nick, target->ident, target->dhost, (unsigned long) target->awaytime); + user->WriteNumeric(609, "%s %s %s %s %lu :is away", user->nick.c_str(), target->nick.c_str(), target->ident.c_str(), target->dhost.c_str(), (unsigned long) target->awaytime); } } else { (*wl)[nick] = ""; - user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick, nick); + user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick); } } - return CMD_FAILURE; + return CMD_SUCCESS; } - CommandWatch (InspIRCd* Instance, unsigned int &maxwatch) : Command(Instance,"WATCH",0,0), MAX_WATCH(maxwatch) + CommandWatch(Module* parent, unsigned int &maxwatch) : Command(parent,"WATCH", 0), MAX_WATCH(maxwatch), ext("watchlist", parent) { - this->source = "m_watch.so"; syntax = "[C|L|S]|[+|-]"; TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */ } CmdResult Handle (const std::vector ¶meters, User *user) { - if (!parameters.size()) + if (parameters.empty()) { - watchlist* wl; - if (user->GetExt("watchlist", wl)) + watchlist* wl = ext.get(user); + if (wl) { for (watchlist::iterator q = wl->begin(); q != wl->end(); q++) { if (!q->second.empty()) - user->WriteNumeric(604, "%s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str()); + user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str()); } } - user->WriteNumeric(607, "%s :End of WATCH list",user->nick); + user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str()); } else if (parameters.size() > 0) { @@ -284,8 +289,8 @@ class CommandWatch : public Command if (!strcasecmp(nick,"C")) { // watch clear - watchlist* wl; - if (user->GetExt("watchlist", wl)) + watchlist* wl = ext.get(user); + if (wl) { for (watchlist::iterator i = wl->begin(); i != wl->end(); i++) { @@ -298,58 +303,58 @@ class CommandWatch : public Command /* I'm no longer watching you... */ i2->second.erase(n); - if (!i2->second.size()) - whos_watching_me->erase(user->nick); + if (i2->second.empty()) + /* nobody else is, either. */ + whos_watching_me->erase(i2); } } - delete wl; - user->Shrink("watchlist"); + ext.unset(user); } } else if (!strcasecmp(nick,"L")) { - watchlist* wl; - if (user->GetExt("watchlist", wl)) + watchlist* wl = ext.get(user); + if (wl) { for (watchlist::iterator q = wl->begin(); q != wl->end(); q++) { if (!q->second.empty()) { - user->WriteNumeric(604, "%s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str()); + user->WriteNumeric(604, "%s %s %s :is online", user->nick.c_str(), q->first.c_str(), q->second.c_str()); User *targ = ServerInstance->FindNick(q->first.c_str()); if (IS_AWAY(targ)) { - user->WriteNumeric(609, "%s %s %s %s %lu :is away", user->nick, targ->nick, targ->ident, targ->dhost, (unsigned long) targ->awaytime); + user->WriteNumeric(609, "%s %s %s %s %lu :is away", user->nick.c_str(), targ->nick.c_str(), targ->ident.c_str(), targ->dhost.c_str(), (unsigned long) targ->awaytime); } } else - user->WriteNumeric(605, "%s %s * * 0 :is offline", user->nick, q->first.c_str()); + user->WriteNumeric(605, "%s %s * * 0 :is offline", user->nick.c_str(), q->first.c_str()); } } - user->WriteNumeric(607, "%s :End of WATCH list",user->nick); + user->WriteNumeric(607, "%s :End of WATCH list",user->nick.c_str()); } else if (!strcasecmp(nick,"S")) { - watchlist* wl; + watchlist* wl = ext.get(user); int you_have = 0; int youre_on = 0; std::string list; - if (user->GetExt("watchlist", wl)) + if (wl) { for (watchlist::iterator q = wl->begin(); q != wl->end(); q++) list.append(q->first.c_str()).append(" "); you_have = wl->size(); } - watchentries::iterator i2 = whos_watching_me->find(user->nick); + watchentries::iterator i2 = whos_watching_me->find(user->nick.c_str()); if (i2 != whos_watching_me->end()) youre_on = i2->second.size(); - user->WriteNumeric(603, "%s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on); - user->WriteNumeric(606, "%s :%s",user->nick, list.c_str()); - user->WriteNumeric(607, "%s :End of WATCH S",user->nick); + user->WriteNumeric(603, "%s :You have %d and are on %d WATCH entries", user->nick.c_str(), you_have, youre_on); + user->WriteNumeric(606, "%s :%s",user->nick.c_str(), list.c_str()); + user->WriteNumeric(607, "%s :End of WATCH S",user->nick.c_str()); } else if (nick[0] == '-') { @@ -363,40 +368,38 @@ class CommandWatch : public Command } } } - /* So that spanningtree doesnt pass the WATCH commands to the network! */ - return CMD_FAILURE; + return CMD_SUCCESS; } }; class Modulewatch : public Module { - CommandWatch* mycommand; - CommandSVSWatch *sw; unsigned int maxwatch; - + CommandWatch cmdw; + CommandSVSWatch sw; + public: - Modulewatch(InspIRCd* Me) - : Module(Me), maxwatch(32) + Modulewatch() + : maxwatch(32), cmdw(this, maxwatch), sw(this) { - OnRehash(NULL, ""); + OnRehash(NULL); whos_watching_me = new watchentries(); - mycommand = new CommandWatch(ServerInstance, maxwatch); - ServerInstance->AddCommand(mycommand); - sw = new CommandSVSWatch(ServerInstance); - ServerInstance->AddCommand(sw); - Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway }; - ServerInstance->Modules->Attach(eventlist, this, 8); + ServerInstance->AddCommand(&cmdw); + ServerInstance->AddCommand(&sw); + ServerInstance->Extensions.Register(&cmdw.ext); + Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway }; + ServerInstance->Modules->Attach(eventlist, this, 7); } - virtual void OnRehash(User* user, const std::string ¶meter) + virtual void OnRehash(User* user) { - ConfigReader Conf(ServerInstance); + ConfigReader Conf; maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true); if (!maxwatch) maxwatch = 32; } - virtual int OnSetAway(User *user, const std::string &awaymsg) + virtual ModResult OnSetAway(User *user, const std::string &awaymsg) { std::string numeric; int inum; @@ -412,39 +415,37 @@ class Modulewatch : public Module inum = 598; } - watchentries::iterator x = whos_watching_me->find(user->nick); + watchentries::iterator x = whos_watching_me->find(user->nick.c_str()); if (x != whos_watching_me->end()) { for (std::deque::iterator n = x->second.begin(); n != x->second.end(); n++) { - if (!user->Visibility || user->Visibility->VisibleTo(user)) - (*n)->WriteNumeric(inum, numeric); + (*n)->WriteNumeric(inum, numeric); } } - return 0; + return MOD_RES_PASSTHRU; } virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message) { - watchentries::iterator x = whos_watching_me->find(user->nick); + watchentries::iterator x = whos_watching_me->find(user->nick.c_str()); if (x != whos_watching_me->end()) { for (std::deque::iterator n = x->second.begin(); n != x->second.end(); n++) { - if (!user->Visibility || user->Visibility->VisibleTo(user)) - (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, (unsigned long) ServerInstance->Time()); + (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str() ,user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) ServerInstance->Time()); - watchlist* wl; - if ((*n)->GetExt("watchlist", wl)) + watchlist* wl = cmdw.ext.get(*n); + if (wl) /* We were on somebody's notify list, set ourselves offline */ - (*wl)[user->nick] = ""; + (*wl)[user->nick.c_str()] = ""; } } /* Now im quitting, if i have a notify list, im no longer watching anyone */ - watchlist* wl; - if (user->GetExt("watchlist", wl)) + watchlist* wl = cmdw.ext.get(user); + if (wl) { /* 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++) @@ -457,15 +458,12 @@ class Modulewatch : public Module if (n != i2->second.end()) /* I'm no longer watching you... */ i2->second.erase(n); - - if (!i2->second.size()) - whos_watching_me->erase(user->nick); + + if (i2->second.empty()) + /* and nobody else is, either. */ + whos_watching_me->erase(i2); } } - - /* User's quitting, we're done with this. */ - delete wl; - user->Shrink("watchlist"); } } @@ -480,53 +478,36 @@ class Modulewatch : public Module delete old_watch; } - virtual void OnCleanup(int target_type, void* item) - { - if (target_type == TYPE_USER) - { - watchlist* wl; - User* user = (User*)item; - - if (user->GetExt("watchlist", wl)) - { - user->Shrink("watchlist"); - delete wl; - } - } - } - virtual void OnPostConnect(User* user) { - watchentries::iterator x = whos_watching_me->find(user->nick); + watchentries::iterator x = whos_watching_me->find(user->nick.c_str()); if (x != whos_watching_me->end()) { for (std::deque::iterator n = x->second.begin(); n != x->second.end(); n++) { - if (!user->Visibility || user->Visibility->VisibleTo(user)) - (*n)->WriteNumeric(600, "%s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, (unsigned long) user->age); + (*n)->WriteNumeric(600, "%s %s %s %s %lu :arrived online", (*n)->nick.c_str(), user->nick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age); - watchlist* wl; - if ((*n)->GetExt("watchlist", wl)) + watchlist* wl = cmdw.ext.get(*n); + if (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)); + (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age)); } } } virtual void OnUserPostNick(User* user, const std::string &oldnick) { - watchentries::iterator new_offline = whos_watching_me->find(assign(oldnick)); - watchentries::iterator new_online = whos_watching_me->find(user->nick); + watchentries::iterator new_offline = whos_watching_me->find(oldnick.c_str()); + watchentries::iterator new_online = whos_watching_me->find(user->nick.c_str()); if (new_offline != whos_watching_me->end()) { for (std::deque::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++) { - watchlist* wl; - if ((*n)->GetExt("watchlist", wl)) + watchlist* wl = cmdw.ext.get(*n); + if (wl) { - if (!user->Visibility || user->Visibility->VisibleTo(user)) - (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, (unsigned long) user->age); + (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick.c_str(), oldnick.c_str(), user->ident.c_str(), user->dhost.c_str(), (unsigned long) user->age); (*wl)[oldnick.c_str()] = ""; } } @@ -536,31 +517,30 @@ class Modulewatch : public Module { for (std::deque::iterator n = new_online->second.begin(); n != new_online->second.end(); n++) { - watchlist* wl; - if ((*n)->GetExt("watchlist", wl)) + watchlist* wl = cmdw.ext.get(*n); + if (wl) { - (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age)); - if (!user->Visibility || user->Visibility->VisibleTo(user)) - (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick, user->nick, (*wl)[user->nick].c_str()); + (*wl)[user->nick.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age)); + (*n)->WriteNumeric(600, "%s %s %s :arrived online", (*n)->nick.c_str(), user->nick.c_str(), (*wl)[user->nick.c_str()].c_str()); } } } - } + } virtual void On005Numeric(std::string &output) { // we don't really have a limit... output = output + " WATCH=" + ConvToStr(maxwatch); } - + virtual ~Modulewatch() { delete whos_watching_me; } - + virtual Version GetVersion() { - return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION); + return Version("Provides support for the /WATCH command", VF_OPTCOMMON | VF_VENDOR); } };