1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
28 /* $ModDesc: Provides support for the /watch command */
30 /* nickname list of users watching the nick */
31 typedef std::map<irc::string, std::deque<userrec*> > watchentries;
33 /* nickname 'ident host signon', or empty if not online */
34 typedef std::map<irc::string, std::string> watchlist;
36 /* Whos watching each nickname */
37 watchentries whos_watching_me;
41 class cmd_watch : public command_t
43 unsigned int& MAX_WATCH;
45 CmdResult remove_watch(userrec* user, const char* nick)
47 // removing an item from the list
48 if (!ServerInstance->IsNick(nick))
50 user->WriteServ("942 %s %s :Invalid nickname", user->nick, nick);
55 if (user->GetExt("watchlist", wl))
57 /* Yup, is on my list */
58 watchlist::iterator n = wl->find(nick);
61 if (!n->second.empty())
62 user->WriteServ("602 %s %s %s :stopped watching", user->nick, n->first.c_str(), n->second.c_str());
64 user->WriteServ("602 %s %s * * 0 :stopped watching", user->nick, nick);
71 user->Shrink("watchlist");
75 watchentries::iterator x = whos_watching_me.find(nick);
76 if (x != whos_watching_me.end())
78 /* People are watching this user, am i one of them? */
79 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
80 if (n != x->second.end())
81 /* I'm no longer watching you... */
84 if (!x->second.size())
85 whos_watching_me.erase(nick);
92 CmdResult add_watch(userrec* user, const char* nick)
94 if (!ServerInstance->IsNick(nick))
96 user->WriteServ("942 %s %s :Invalid nickname",user->nick,nick);
101 if (!user->GetExt("watchlist", wl))
103 wl = new watchlist();
104 user->Extend("watchlist", wl);
107 if (wl->size() == MAX_WATCH)
109 user->WriteServ("942 %s %s :Too many WATCH entries", user->nick, nick);
113 watchlist::iterator n = wl->find(nick);
116 /* Don't already have the user on my watch list, proceed */
117 watchentries::iterator x = whos_watching_me.find(nick);
118 if (x != whos_watching_me.end())
120 /* People are watching this user, add myself */
121 x->second.push_back(user);
125 std::deque<userrec*> newlist;
126 newlist.push_back(user);
127 whos_watching_me[nick] = newlist;
130 userrec* target = ServerInstance->FindNick(nick);
133 (*wl)[nick] = std::string(target->ident).append(" ").append(target->dhost).append(" ").append(ConvToStr(target->age));
134 user->WriteServ("604 %s %s %s :is online",user->nick, nick, (*wl)[nick].c_str());
139 user->WriteServ("605 %s %s * * 0 :is offline",user->nick, nick);
146 cmd_watch (InspIRCd* Instance, unsigned int &maxwatch) : command_t(Instance,"WATCH",0,0), MAX_WATCH(maxwatch)
148 this->source = "m_watch.so";
149 syntax = "[C|L|S]|[+|-<nick>]";
152 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
157 if (user->GetExt("watchlist", wl))
159 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
161 if (!q->second.empty())
162 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
165 user->WriteServ("607 %s :End of WATCH list",user->nick);
169 for (int x = 0; x < pcnt; x++)
171 const char *nick = parameters[x];
172 if (!strcasecmp(nick,"C"))
176 if (user->GetExt("watchlist", wl))
179 user->Shrink("watchlist");
182 else if (!strcasecmp(nick,"L"))
185 if (user->GetExt("watchlist", wl))
187 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
189 if (!q->second.empty())
190 user->WriteServ("604 %s %s %s :is online", user->nick, q->first.c_str(), q->second.c_str());
193 user->WriteServ("607 %s :End of WATCH list",user->nick);
195 else if (!strcasecmp(nick,"S"))
202 if (user->GetExt("watchlist", wl))
204 for (watchlist::iterator q = wl->begin(); q != wl->end(); q++)
205 if (!q->second.empty())
206 list.append(q->first.c_str()).append(" ");
207 you_have = wl->size();
210 watchentries::iterator x = whos_watching_me.find(user->nick);
211 if (x != whos_watching_me.end())
212 youre_on = x->second.size();
214 user->WriteServ("603 %s :You have %d and are on %d WATCH entries", user->nick, you_have, youre_on);
216 user->WriteServ("606 %s :%s",user->nick, list.c_str());
217 user->WriteServ("607 %s :End of WATCH S",user->nick);
219 else if (nick[0] == '-')
222 return remove_watch(user, nick);
224 else if (nick[0] == '+')
227 return add_watch(user, nick);
231 /* So that spanningtree doesnt pass the WATCH commands to the network! */
236 class Modulewatch : public Module
238 cmd_watch* mycommand;
239 unsigned int maxwatch;
242 Modulewatch(InspIRCd* Me)
243 : Module::Module(Me), maxwatch(32)
245 mycommand = new cmd_watch(ServerInstance, maxwatch);
246 ServerInstance->AddCommand(mycommand);
249 void Implements(char* List)
251 List[I_OnUserQuit] = List[I_OnPostConnect] = List[I_OnUserPostNick] = List[I_On005Numeric] = 1;
254 virtual void OnUserQuit(userrec* user, const std::string &reason)
256 ServerInstance->Log(DEBUG,"*** WATCH: On global quit: user %s",user->nick);
257 watchentries::iterator x = whos_watching_me.find(user->nick);
258 if (x != whos_watching_me.end())
260 for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
262 (*n)->WriteServ("601 %s %s %s %s %lu :went offline", (*n)->nick ,user->nick, user->ident, user->dhost, ServerInstance->Time());
264 if ((*n)->GetExt("watchlist", wl))
265 /* We were on somebody's notify list, set ourselves offline */
266 (*wl)[user->nick] = "";
270 /* Now im quitting, if i have a notify list, im no longer watching anyone */
272 if (user->GetExt("watchlist", wl))
274 /* Iterate every user on my watch list, and take me out of the whos_watching_me map for each one we're watching */
275 for (watchlist::iterator i = wl->begin(); i != wl->end(); i++)
277 watchentries::iterator x = whos_watching_me.find(i->first);
278 if (x != whos_watching_me.end())
280 /* People are watching this user, am i one of them? */
281 std::deque<userrec*>::iterator n = std::find(x->second.begin(), x->second.end(), user);
282 if (n != x->second.end())
283 /* I'm no longer watching you... */
286 if (!x->second.size())
287 whos_watching_me.erase(user->nick);
293 virtual void OnPostConnect(userrec* user)
295 ServerInstance->Log(DEBUG,"*** WATCH: On global connect: user %s",user->nick);
296 watchentries::iterator x = whos_watching_me.find(user->nick);
297 if (x != whos_watching_me.end())
299 for (std::deque<userrec*>::iterator n = x->second.begin(); n != x->second.end(); n++)
301 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, user->nick, user->ident, user->dhost, user->age);
303 if ((*n)->GetExt("watchlist", wl))
304 /* We were on somebody's notify list, set ourselves online */
305 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
310 virtual void OnUserPostNick(userrec* user, const std::string &oldnick)
312 ServerInstance->Log(DEBUG,"*** WATCH: On global nickchange: old nick: %s new nick: %s",oldnick.c_str(),user->nick);
314 watchentries::iterator new_online = whos_watching_me.find(user->nick);
315 watchentries::iterator new_offline = whos_watching_me.find(assign(oldnick));
317 if (new_online != whos_watching_me.end())
319 for (std::deque<userrec*>::iterator n = new_online->second.begin(); n != new_online->second.end(); n++)
322 if ((*n)->GetExt("watchlist", wl))
324 (*wl)[user->nick] = std::string(user->ident).append(" ").append(user->dhost).append(" ").append(ConvToStr(user->age));
325 (*n)->WriteServ("600 %s %s %s %s %lu :arrived online", (*n)->nick, (*wl)[user->nick].c_str());
330 if (new_offline != whos_watching_me.end())
332 for (std::deque<userrec*>::iterator n = new_offline->second.begin(); n != new_offline->second.end(); n++)
335 if ((*n)->GetExt("watchlist", wl))
337 (*n)->WriteServ("601 %s %s %s :went offline", (*n)->nick, (*wl)[user->nick].c_str());
338 (*wl)[user->nick] = "";
344 virtual void On005Numeric(std::string &output)
346 // we don't really have a limit...
347 output = output + " WATCH=32";
350 virtual ~Modulewatch()
354 virtual Version GetVersion()
356 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
361 class ModulewatchFactory : public ModuleFactory
368 ~ModulewatchFactory()
372 virtual Module * CreateModule(InspIRCd* Me)
374 return new Modulewatch(Me);
380 extern "C" void * init_module( void )
382 return new ModulewatchFactory;