]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spy.cpp
Refactor port binding, warning not yet tested fully
[user/henk/code/inspircd.git] / src / modules / m_spy.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* NO, THIS MODULE DOES NOT SPY ON CHANNELS OR USERS.
15  * IT JUST ALLOWS OPERS TO SEE +s CHANNELS IN LIST AND
16  * WHOIS, WHICH IS SUPPORTED BY MOST IRCDS IN CORE.
17  */
18
19 /* $ModDesc: Provides SPYLIST and SPYNAMES capability, allowing opers to see who's in +s channels */
20
21 #include "inspircd_config.h"
22 #include "users.h" 
23 #include "channels.h"
24 #include "modules.h"
25 #include "inspircd.h"
26 #include "wildcard.h"
27
28 void spy_userlist(userrec *user, chanrec *c)
29 {
30         char list[MAXBUF];
31         size_t dlen, curlen;
32
33         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
34
35         int numusers = 0;
36         char* ptr = list + dlen;
37
38         CUList *ulist= c->GetUsers();
39
40         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
41         {
42                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", c->GetPrefixChar(i->first), i->first->nick);
43
44                 curlen += ptrlen;
45                 ptr += ptrlen;
46
47                 numusers++;
48
49                 if (curlen > (480-NICKMAX))
50                 {
51                         /* list overflowed into multiple numerics */
52                         user->WriteServ(std::string(list));
53
54                         /* reset our lengths */
55                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
56                         ptr = list + dlen;
57
58                         ptrlen = 0;
59                         numusers = 0;
60                 }
61         }
62
63         /* if whats left in the list isnt empty, send it */
64         if (numusers)
65         {
66                 user->WriteServ(std::string(list));
67         }
68
69         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, c->name);
70
71 }
72
73 /** Handle /SPYLIST
74  */
75 class cmd_spylist : public command_t
76 {
77   public:
78         cmd_spylist (InspIRCd* Instance) : command_t(Instance,"SPYLIST", 'o', 0)
79         {
80                 this->source = "m_spy.so";
81                 syntax = "";
82         }
83
84         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
85         {
86                 ServerInstance->WriteOpers("*** Oper %s used SPYLIST to list +s/+p channels and keys.",user->nick);
87                 user->WriteServ("321 %s Channel :Users Name",user->nick);
88                 for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
89                 {
90                         if (pcnt && !match(i->second->name, parameters[0]))
91                                 continue;
92                         user->WriteServ("322 %s %s %d :[+%s] %s",user->nick,i->second->name,i->second->GetUserCounter(),i->second->ChanModes(true),i->second->topic);
93                 }
94                 user->WriteServ("323 %s :End of channel list.",user->nick);
95
96                 /* Dont send out across the network */
97                 return CMD_FAILURE;
98         }
99 };
100
101 /** Handle /SPYNAMES
102  */
103 class cmd_spynames : public command_t
104 {
105   public:
106         cmd_spynames (InspIRCd* Instance) : command_t(Instance,"SPYNAMES", 'o', 0)
107         {
108                 this->source = "m_spy.so";
109                 syntax = "{<channel>{,<channel>}}";
110         }
111
112         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
113         {
114                 chanrec* c = NULL;
115
116                 if (!pcnt)
117                 {
118                         user->WriteServ("366 %s * :End of /NAMES list.",user->nick);
119                         return CMD_FAILURE;
120                 }
121
122                 if (ServerInstance->Parser->LoopCall(user, this, parameters, pcnt, 0))
123                         return CMD_FAILURE;
124
125                 c = ServerInstance->FindChan(parameters[0]);
126                 if (c)
127                 {
128                         ServerInstance->WriteOpers("*** Oper %s used SPYNAMES to view the users on %s", user->nick, parameters[0]);
129                         spy_userlist(user,c);
130                 }
131                 else
132                 {
133                         user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
134                 }
135
136                 return CMD_FAILURE;
137         }
138 };
139
140 class ModuleSpy : public Module
141 {
142         cmd_spylist *mycommand;
143         cmd_spynames *mycommand2;
144  public:
145         ModuleSpy(InspIRCd* Me) : Module::Module(Me)
146         {
147                 
148                 mycommand = new cmd_spylist(ServerInstance);
149                 mycommand2 = new cmd_spynames(ServerInstance);
150                 ServerInstance->AddCommand(mycommand);
151                 ServerInstance->AddCommand(mycommand2);
152         }
153         
154         virtual ~ModuleSpy()
155         {
156         }
157         
158         virtual Version GetVersion()
159         {
160                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
161         }
162 };
163
164
165 class ModuleSpyFactory : public ModuleFactory
166 {
167  public:
168         ModuleSpyFactory()
169         {
170         }
171         
172         ~ModuleSpyFactory()
173         {
174         }
175         
176         virtual Module * CreateModule(InspIRCd* Me)
177         {
178                 return new ModuleSpy(Me);
179         }
180         
181 };
182
183
184 extern "C" void * init_module( void )
185 {
186         return new ModuleSpyFactory;
187 }