]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_watch.cpp
Add method for writing server notices.
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
index 1cda4484fd6820149783c20e6dc558cceeead89b..fdb20be2d3c79bfc8e2b05ca0c66a30288f2b106 100644 (file)
@@ -1,16 +1,25 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://wiki.inspircd.org/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 <http://www.gnu.org/licenses/>.
  */
 
+
 #include "inspircd.h"
 
 /* $ModDesc: Provides support for the /WATCH command */
  * of users using WATCH.
  */
 
-/*
- * 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
- */
-#if defined(WINDOWS) && !defined(HASHMAP_DEPRECATED)
-       typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash_compare<irc::string, std::less<irc::string> > > watchentries;
-#else
-       typedef nspace::hash_map<irc::string, std::deque<User*>, nspace::hash<irc::string> > watchentries;
-#endif
+typedef TR1NS::unordered_map<irc::string, std::deque<User*>, irc::hash> watchentries;
 typedef std::map<irc::string, std::string> watchlist;
 
 /* Who's watching each nickname.
@@ -103,7 +104,7 @@ watchentries* whos_watching_me;
 class CommandSVSWatch : public Command
 {
  public:
-       CommandSVSWatch (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"SVSWATCH", 0, 2)
+       CommandSVSWatch(Module* Creator) : Command(Creator,"SVSWATCH", 2)
        {
                syntax = "<target> [C|L|S]|[+|-<nick>]";
                TRANSLATE3(TR_NICK, TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
@@ -125,6 +126,14 @@ class CommandSVSWatch : public Command
 
                return CMD_SUCCESS;
        }
+
+       RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
+       {
+               User* target = ServerInstance->FindNick(parameters[0]);
+               if (target)
+                       return ROUTE_OPT_UCAST(target->server);
+               return ROUTE_LOCALONLY;
+       }
 };
 
 /** Handle /WATCH
@@ -133,6 +142,7 @@ class CommandWatch : public Command
 {
        unsigned int& MAX_WATCH;
  public:
+       SimpleExtItem<watchlist> ext;
        CmdResult remove_watch(User* user, const char* nick)
        {
                // removing an item from the list
@@ -142,8 +152,8 @@ class CommandWatch : public Command
                        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);
@@ -163,8 +173,7 @@ class CommandWatch : public Command
 
                        if (wl->empty())
                        {
-                               user->Shrink("watchlist");
-                               delete wl;
+                               ext.unset(user);
                        }
 
                        watchentries::iterator x = whos_watching_me->find(nick);
@@ -182,7 +191,7 @@ class CommandWatch : public Command
                        }
                }
 
-               return CMD_LOCALONLY;
+               return CMD_SUCCESS;
        }
 
        CmdResult add_watch(User* user, const char* nick)
@@ -193,11 +202,11 @@ class CommandWatch : public Command
                        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)
@@ -228,22 +237,22 @@ class CommandWatch : public Command
                        {
                                (*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.c_str(), nick, (*wl)[nick].c_str());
-                               if (IS_AWAY(target))
+                               if (target->IsAway())
                                {
                                        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] = "";
+                               (*wl)[nick].clear();
                                user->WriteNumeric(605, "%s %s * * 0 :is offline",user->nick.c_str(), nick);
                        }
                }
 
-               return CMD_LOCALONLY;
+               return CMD_SUCCESS;
        }
 
-       CommandWatch (InspIRCd* Instance, Module* parent, unsigned int &maxwatch) : Command(Instance,parent,"WATCH",0,0), MAX_WATCH(maxwatch)
+       CommandWatch(Module* parent, unsigned int &maxwatch) : Command(parent,"WATCH", 0), MAX_WATCH(maxwatch), ext("watchlist", parent)
        {
                syntax = "[C|L|S]|[+|-<nick>]";
                TRANSLATE2(TR_TEXT, TR_END); /* we watch for a nick. not a UID. */
@@ -253,8 +262,8 @@ class CommandWatch : public Command
        {
                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++)
                                {
@@ -272,8 +281,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++)
                                                {
@@ -292,14 +301,13 @@ class CommandWatch : public Command
                                                        }
                                                }
 
-                                               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++)
                                                {
@@ -307,7 +315,7 @@ class CommandWatch : public Command
                                                        {
                                                                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))
+                                                               if (targ->IsAway())
                                                                {
                                                                        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);
                                                                }
@@ -320,12 +328,12 @@ class CommandWatch : public Command
                                }
                                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(" ");
@@ -352,7 +360,7 @@ class CommandWatch : public Command
                                }
                        }
                }
-               return CMD_LOCALONLY;
+               return CMD_SUCCESS;
        }
 };
 
@@ -363,21 +371,25 @@ class Modulewatch : public Module
        CommandSVSWatch sw;
 
  public:
-       Modulewatch(InspIRCd* Me)
-               : Module(Me), maxwatch(32), cmdw(Me, this, maxwatch), sw(Me,this) 
+       Modulewatch()
+               : maxwatch(32), cmdw(this, maxwatch), sw(this)
        {
-               OnRehash(NULL);
                whos_watching_me = new watchentries();
-               ServerInstance->AddCommand(&cmdw);
-               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);
+       }
+
+       void init()
+       {
+               OnRehash(NULL);
+               ServerInstance->Modules->AddService(cmdw);
+               ServerInstance->Modules->AddService(sw);
+               ServerInstance->Modules->AddService(cmdw.ext);
+               Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
+               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
        }
 
        virtual void OnRehash(User* user)
        {
-               ConfigReader Conf(ServerInstance);
-               maxwatch = Conf.ReadInteger("watch", "maxentries", 0, true);
+               maxwatch = ServerInstance->Config->ConfValue("watch")->getInt("maxentries", 32);
                if (!maxwatch)
                        maxwatch = 32;
        }
@@ -389,12 +401,12 @@ class Modulewatch : public Module
 
                if (awaymsg.empty())
                {
-                       numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
+                       numeric = user->nick + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :is no longer away";
                        inum = 599;
                }
                else
                {
-                       numeric = std::string(user->nick) + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
+                       numeric = user->nick + " " + user->ident + " " + user->dhost + " " + ConvToStr(ServerInstance->Time()) + " :" + awaymsg;
                        inum = 598;
                }
 
@@ -419,16 +431,16 @@ class Modulewatch : public Module
                        {
                                (*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.c_str()] = "";
+                                       (*wl)[user->nick.c_str()].clear();
                        }
                }
 
                /* 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++)
@@ -447,10 +459,6 @@ class Modulewatch : public Module
                                                        whos_watching_me->erase(i2);
                                }
                        }
-
-                       /* User's quitting, we're done with this. */
-                       delete wl;
-                       user->Shrink("watchlist");
                }
        }
 
@@ -465,21 +473,6 @@ 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.c_str());
@@ -489,8 +482,8 @@ class Modulewatch : public Module
                        {
                                (*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.c_str()] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
                        }
@@ -506,11 +499,11 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::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)
                                {
                                        (*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()] = "";
+                                       (*wl)[oldnick.c_str()].clear();
                                }
                        }
                }
@@ -519,8 +512,8 @@ class Modulewatch : public Module
                {
                        for (std::deque<User*>::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.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());
@@ -529,10 +522,9 @@ class Modulewatch : public Module
                }
        }
 
-       virtual void On005Numeric(std::string &output)
+       virtual void On005Numeric(std::map<std::string, std::string>& tokens)
        {
-               // we don't really have a limit...
-               output = output + " WATCH=" + ConvToStr(maxwatch);
+               tokens["WATCH"] = ConvToStr(maxwatch);
        }
 
        virtual ~Modulewatch()
@@ -542,9 +534,8 @@ class Modulewatch : public Module
 
        virtual Version GetVersion()
        {
-               return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
+               return Version("Provides support for the /WATCH command", VF_OPTCOMMON | VF_VENDOR);
        }
 };
 
 MODULE_INIT(Modulewatch)
-