]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_who.cpp
Fixed to correctly use iterator
[user/henk/code/inspircd.git] / src / cmd_who.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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd_io.h"
21 #include <time.h>
22 #include <string>
23 #ifdef GCC3
24 #include <ext/hash_map>
25 #else
26 #include <hash_map>
27 #endif
28 #include <map>
29 #include <sstream>
30 #include <vector>
31 #include <deque>
32 #include "users.h"
33 #include "ctables.h"
34 #include "globals.h"
35 #include "modules.h"
36 #include "dynamic.h"
37 #include "wildcard.h"
38 #include "message.h"
39 #include "commands.h"
40 #include "mode.h"
41 #include "xline.h"
42 #include "inspstring.h"
43 #include "dnsqueue.h"
44 #include "helperfuncs.h"
45 #include "hashcomp.h"
46 #include "socketengine.h"
47 #include "typedefs.h"
48 #include "command_parse.h"
49 #include "inspircd.h"
50 #include "cmd_who.h"
51
52 extern ServerConfig* Config;
53 extern InspIRCd* ServerInstance;
54 extern int MODCOUNT;
55 extern std::vector<Module*> modules;
56 extern std::vector<ircd_module*> factory;
57 extern time_t TIME;
58 extern user_hash clientlist;
59 extern chan_hash chanlist;
60 extern std::vector<userrec*> all_opers;
61 extern std::vector<userrec*> local_users;
62 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
63
64 void cmd_who::Handle (char **parameters, int pcnt, userrec *user)
65 {
66         chanrec* Ptr = NULL;
67         char tmp[10];
68         
69         /* theres more to do here, but for now just close the socket */
70         if (pcnt == 1)
71         {
72                 if ((IS_SINGLE(parameters[0],'0')) || (IS_SINGLE(parameters[0],'*')))
73                 {
74                         if ((user->chans.size()) && (((ucrec*)*(user->chans.begin()))->channel))
75                         {
76                                 int n_list = 0;
77                                 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
78                                 {
79                                         Ptr = ((ucrec*)*(i->second->chans.begin()))->channel;
80                                         // suggested by phidjit and FCS
81                                         if ((!common_channels(user,i->second)) && (isnick(i->second->nick)))
82                                         {
83                                                 // Bug Fix #29
84                                                 *tmp = 0;
85                                                 if (*i->second->awaymsg) {
86                                                         charlcat(tmp, 'G', 9);
87                                                 } else {
88                                                         charlcat(tmp, 'H', 9);
89                                                 }
90                                                 if (*i->second->oper) { charlcat(tmp, '*', 9); }
91                                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr ? Ptr->name : "*", i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
92                                                 if (n_list++ > Config->MaxWhoResults)
93                                                 {
94                                                         WriteServ(user->fd,"523 %s WHO :Command aborted: More results than configured limit",user->nick);
95                                                         break;
96                                                 }
97                                         }
98                                 }
99                         }
100                         if (Ptr)
101                         {
102                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick , parameters[0]);
103                         }
104                         else
105                         {
106                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
107                         }
108                         return;
109                 }
110                 if (parameters[0][0] == '#')
111                 {
112                         Ptr = FindChan(parameters[0]);
113                         if (Ptr)
114                         {
115                                 int n_list = 0;
116                                 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
117                                 {
118                                         if ((Ptr->HasUser(i->second)) && (isnick(i->second->nick)))
119                                         {
120                                                 // Fix Bug #29 - Part 2..
121                                                 *tmp = 0;
122                                                 if (*i->second->awaymsg) {
123                                                         charlcat(tmp, 'G', 9);
124                                                 } else {
125                                                         charlcat(tmp, 'H', 9);
126                                                 }
127                                                 if (*i->second->oper) { charlcat(tmp, '*', 9); }
128                                                 strlcat(tmp, cmode(i->second, Ptr),5);
129                                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, tmp, i->second->fullname);
130                                                 n_list++;
131                                                 if (n_list > Config->MaxWhoResults)
132                                                 {
133                                                         WriteServ(user->fd,"523 %s WHO :Command aborted: More results than configured limit",user->nick);
134                                                         break;
135                                                 }
136
137                                         }
138                                 }
139                                 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
140                         }
141                         else
142                         {
143                                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
144                         }
145                 }
146                 else
147                 {
148                         userrec* u = Find(parameters[0]);
149                         if (u)
150                         {
151                                 // Bug Fix #29 -- Part 29..
152                                 *tmp = 0;
153                                 if (*u->awaymsg) {
154                                         charlcat(tmp, 'G' ,9);
155                                 } else {
156                                         charlcat(tmp, 'H' ,9);
157                                 }
158                                 if (*u->oper) { charlcat(tmp, '*' ,9); }
159                                 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
160                                 : "*", u->ident, u->dhost, u->server, u->nick, tmp, u->fullname);
161                         }
162                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
163                 }
164         }
165         if (pcnt == 2)
166         {
167                 if ((IS_SINGLE(parameters[0],'0')) || (IS_SINGLE(parameters[0],'*')) && (IS_SINGLE(parameters[1],'o')))
168                 {
169                         for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
170                         {
171                                 // If i were a rich man.. I wouldn't need to me making these bugfixes..
172                                 // But i'm a poor bastard with nothing better to do.
173                                 userrec* oper = *i;
174                                 *tmp = 0;
175                                 if (*oper->awaymsg) {
176                                         charlcat(tmp, 'G' ,9);
177                                 } else {
178                                         charlcat(tmp, 'H' ,9);
179                                 }
180                                 WriteServ(user->fd,"352 %s %s %s %s %s %s %s* :0 %s", user->nick, oper->chans.size() && ((ucrec*)*(oper->chans.begin()))->channel ? ((ucrec*)*(oper->chans.begin()))->channel->name
181                                 : "*", oper->ident, oper->dhost, oper->server, oper->nick, tmp, oper->fullname);
182                         }
183                         WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, parameters[0]);
184                         return;
185                 }
186         }
187 }
188