]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_operwho.cpp
Updates, should be able to safely unload client modules with queries in progress...
[user/henk/code/inspircd.git] / src / modules / m_operwho.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 /* $ModDesc: Provides an extended version of /WHO for opers */
18
19 #include <string>
20 #include <vector>
21 #include "inspircd_config.h"
22 #include "users.h"
23 #include "modules.h"
24 #include "helperfuncs.h"
25 #include "message.h"
26
27 extern user_hash clientlist;
28 extern std::vector<userrec*> all_opers;
29
30 class ModuleOperWho : public Module
31 {
32         Server* Srv;
33  public:
34         ModuleOperWho(Server* Me) : Module::Module(Me)
35         {
36                 Srv = Me;
37         }
38
39         virtual void Implements(char* List)
40         {
41                 List[I_OnPreCommand] = 1;
42         }
43
44         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
45         {
46
47                 if ((!*user->oper) || (command != "WHO"))
48                         return 0;
49
50                 chanrec* Ptr = NULL;
51                 char tmp[10];
52         
53                 if (!pcnt)
54                 {
55                         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
56                         {
57                                 *tmp = 0;
58                                 Ptr = ((ucrec*)*(i->second->chans.begin()))->channel;
59                                 if (*i->second->awaymsg) {
60                                         strlcat(tmp, "G", 9);
61                                 } else {
62                                         strlcat(tmp, "H", 9);
63                                 }
64                                 if (*i->second->oper) { strlcat(tmp, "*", 9); }
65                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr ? Ptr->name : "*", i->second->ident, i->second->host, i->second->server, i->second->nick, tmp, i->second->fullname);
66                         }
67                         WriteServ(user->fd,"315 %s * :End of /WHO list.",user->nick);
68                         return 1;
69                 }
70                 if (pcnt == 1)
71                 {
72                         if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
73                         {
74                                 if ((user->chans.size()) && (((ucrec*)*(user->chans.begin()))->channel))
75                                 {
76                                         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
77                                         {
78                                                 Ptr = ((ucrec*)*(i->second->chans.begin()))->channel;
79                                                 // suggested by phidjit and FCS
80                                                 if ((!common_channels(user,i->second)) && (isnick(i->second->nick)))
81                                                 {
82                                                         // Bug Fix #29
83                                                         *tmp = 0;
84                                                         if (*i->second->awaymsg) {
85                                                                 strlcat(tmp, "G", 9);
86                                                         } else {
87                                                                 strlcat(tmp, "H", 9);
88                                                         }
89                                                         if (*i->second->oper) { strlcat(tmp, "*", 9); }
90                                                         WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr ? Ptr->name : "*", i->second->ident, i->second->host, i->second->server, i->second->nick, tmp, i->second->fullname);
91                                                 }
92                                         }
93                                 }
94                                 if (Ptr)
95                                 {
96                                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick , parameters[0]);
97                                 }
98                                 else
99                                 {
100                                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
101                                 }
102                                 return 1;
103                         }
104                         if (parameters[0][0] == '#')
105                         {
106                                 Ptr = FindChan(parameters[0]);
107                                 if (Ptr)
108                                 {
109                                         for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
110                                         {
111                                                 if ((Ptr->HasUser(i->second)) && (isnick(i->second->nick)))
112                                                 {
113                                                         // Fix Bug #29 - Part 2..
114                                                         *tmp = 0;
115                                                         if (*i->second->awaymsg) {
116                                                                 strlcat(tmp, "G", 9);
117                                                         } else {
118                                                                 strlcat(tmp, "H", 9);
119                                                         }
120                                                         if (*i->second->oper) { strlcat(tmp, "*", 9); }
121                                                         strlcat(tmp, cmode(i->second, Ptr),5);
122                                                         WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr->name, i->second->ident, i->second->host, i->second->server, i->second->nick, tmp, i->second->fullname);
123         
124                                                 }
125                                         }
126                                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
127                                 }
128                                 else
129                                 {
130                                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
131                                 }
132                                 return 1;
133                         }
134                         else
135                         {
136                                 userrec* u = Find(parameters[0]);
137                                 if (u)
138                                 {
139                                         // Bug Fix #29 -- Part 29..
140                                         *tmp = 0;
141                                         if (*u->awaymsg) {
142                                                 strlcat(tmp, "G" ,9);
143                                         } else {
144                                                 strlcat(tmp, "H" ,9);
145                                         }
146                                         if (*u->oper) { strlcat(tmp, "*" ,9); }
147                                         WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, u->chans.size() && ((ucrec*)*(u->chans.begin()))->channel ? ((ucrec*)*(u->chans.begin()))->channel->name
148                                         : "*", u->ident, u->dhost, u->server, u->nick, tmp, u->fullname);
149                                 }
150                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
151                                 return 1;
152                         }
153                 }
154                 if (pcnt == 2)
155                 {
156                         if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
157                         {
158                                 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
159                                 {
160                                         // If i were a rich man.. I wouldn't need to me making these bugfixes..
161                                         // But i'm a poor bastard with nothing better to do.
162                                         userrec* oper = *i;
163                                         *tmp = 0;
164                                         if (*oper->awaymsg) {
165                                                 strlcat(tmp, "G" ,9);
166                                         } else {
167                                                 strlcat(tmp, "H" ,9);
168                                         }
169                                         WriteServ(user->fd,"352 %s %s %s %s %s %s %s* :0 %s", user->nick, oper->chans.size() && ((ucrec*)*(oper->chans.begin()))->channel ?
170                                         ((ucrec*)*(oper->chans.begin()))->channel->name : "*", oper->ident, oper->host, oper->server, oper->nick, tmp, oper->fullname);
171                                 }
172                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
173                                 return 1;
174                         }
175                 }
176                 return 0;
177         }
178         
179         virtual ~ModuleOperWho()
180         {
181         }
182         
183         virtual Version GetVersion()
184         {
185                 return Version(1, 0, 0, 0, VF_VENDOR);
186         }
187 };
188
189
190 class ModuleOperWhoFactory : public ModuleFactory
191 {
192  public:
193         ModuleOperWhoFactory()
194         {
195         }
196         
197         ~ModuleOperWhoFactory()
198         {
199         }
200         
201         virtual Module * CreateModule(Server* Me)
202         {
203                 return new ModuleOperWho(Me);
204         }
205         
206 };
207
208
209 extern "C" void * init_module( void )
210 {
211         return new ModuleOperWhoFactory;
212 }