]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_watch.cpp
Remote server PRIVMSG/NOTICE to nickname support
[user/henk/code/inspircd.git] / src / modules / m_watch.cpp
index ceeaeef818c23697fd66c963b9befdca2d1a950d..19521dba6deef73bd7268daa7f218a8ed702aaf6 100644 (file)
 
 /* $ModDesc: Provides support for the /WATCH command */
 
+
+/*
+ * Okay, it's nice that this was documented and all, but I at least understood very little
+ * of it, so I'm going to attempt to explain the data structures in here a bit more.
+ *
+ * For efficiency, many data structures are kept.
+ *
+ * The first is a global list `watchentries':
+ *     hash_map<irc::string, std::deque<User*> >
+ *
+ * That is, if nick 'w00t' is being watched by user pointer 'Brain' and 'Om', <w00t, (Brain, Om)>
+ * will be in the watchentries list.
+ *
+ * The second is that each user has a per-user data structure attached to their user record via Extensible:
+ *     std::map<irc::string, std::string> watchlist;
+ * So, in the above example with w00t watched by Brain and Om, we'd have:
+ *     Brain-
+ *           `- w00t
+ *     Om-
+ *        `- w00t
+ *
+ * Hopefully this helps any brave soul that ventures into this file other than me. :-)
+ *             -- w00t (mar 30, 2008)
+ */
+
+
 /* This module has been refactored to provide a very efficient (in terms of cpu time)
  * implementation of /WATCH.
  *
@@ -213,6 +239,10 @@ 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, 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);
+                               }
                        }
                        else
                        {
@@ -285,7 +315,14 @@ class CommandWatch : public Command
                                                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 *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);
+                                                               }
+                                                       }
                                                        else
                                                                user->WriteNumeric(605, "%s %s * * 0 :is offline", user->nick, q->first.c_str());
                                                }
@@ -347,8 +384,8 @@ class Modulewatch : public Module
                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 };
-               ServerInstance->Modules->Attach(eventlist, this, 7);
+               Implementation eventlist[] = { I_OnRehash, I_OnGarbageCollect, I_OnCleanup, I_OnUserQuit, I_OnPostConnect, I_OnUserPostNick, I_On005Numeric, I_OnSetAway };
+               ServerInstance->Modules->Attach(eventlist, this, 8);
        }
 
        virtual void OnRehash(User* user, const std::string &parameter)
@@ -359,6 +396,34 @@ class Modulewatch : public Module
                        maxwatch = 32;
        }
 
+       virtual int OnSetAway(User *user, const std::string &awaymsg)
+       {
+               std::string numeric;
+               int inum;
+
+               if (awaymsg.empty())
+               {
+                       numeric = std::string(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;
+                       inum = 598;
+               }
+
+               watchentries::iterator x = whos_watching_me->find(user->nick);
+               if (x != whos_watching_me->end())
+               {
+                       for (std::deque<User*>::iterator n = x->second.begin(); n != x->second.end(); n++)
+                       {
+                               if (!user->Visibility || user->Visibility->VisibleTo(user))
+                                       (*n)->WriteNumeric(inum, numeric);
+                       }
+               }
+
+               return 0;
+       }
 
        virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
        {
@@ -368,7 +433,7 @@ class Modulewatch : public Module
                        for (std::deque<User*>::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, ServerInstance->Time());
+                                       (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, (unsigned long) ServerInstance->Time());
 
                                watchlist* wl;
                                if ((*n)->GetExt("watchlist", wl))
@@ -438,7 +503,7 @@ class Modulewatch : public Module
                        for (std::deque<User*>::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, user->age);
+                                       (*n)->WriteNumeric(600, "%s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, (unsigned long) user->age);
 
                                watchlist* wl;
                                if ((*n)->GetExt("watchlist", wl))
@@ -461,7 +526,7 @@ class Modulewatch : public Module
                                if ((*n)->GetExt("watchlist", 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, user->age);
+                                               (*n)->WriteNumeric(601, "%s %s %s %s %lu :went offline", (*n)->nick, oldnick.c_str(), user->ident, user->dhost, (unsigned long) user->age);
                                        (*wl)[oldnick.c_str()] = "";
                                }
                        }
@@ -495,7 +560,7 @@ class Modulewatch : public Module
        
        virtual Version GetVersion()
        {
-               return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
+               return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
        }
 };