summaryrefslogtreecommitdiff
path: root/src/cmd_who.cpp
blob: 01c7bfff5900f2cb1abf1eb5ad5e35d20c54e45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*   +------------------------------------+
 *   | Inspire Internet Relay Chat Daemon |
 *   +------------------------------------+
 *
 *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
 *   E-mail:
 *<brain@chatspike.net>
 *<Craig@chatspike.net>
 *
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include "configreader.h"
#include "users.h"
#include "message.h"
#include "modules.h"
#include "commands.h"
#include "helperfuncs.h"
#include "commands/cmd_who.h"

extern ServerConfig* Config;
extern user_hash clientlist;
extern chan_hash chanlist;
extern std::vector<userrec*> all_opers;

/* get the last 'visible' chan of a user */
static char *getlastchanname(userrec *u)
{
	for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
	{
		ucrec* temp = (ucrec*)*v;

		if (temp->channel)
		{
			if (!temp->channel->IsModeSet('s'))
				return temp->channel->name;
		}
	}

	return "*";
}

void cmd_who::Handle (const char** parameters, int pcnt, userrec *user)
{
	/*
	 * XXX - RFC says:
	 *   The <name> passed to WHO is matched against users' host, server, real
	 *   name and nickname
	 * Currently, we support WHO #chan, WHO nick, WHO 0, WHO *, and the addition of a 'o' flag, as per RFC.
	 */

	bool opt_viewopersonly = false;
	chanrec *ch = NULL;
	std::vector<std::string> whoresults;
	std::string initial = "352 " + std::string(user->nick) + " ";

	if (pcnt == 2)
	{
		/* parse flags */
		const char *iter = parameters[1];

		while (*iter)
		{
			switch (*iter)
			{
				case 'o':
					opt_viewopersonly = true;
					break;
			}

			*iter++;
		}
	}


	/* who on a channel? */
	ch = FindChan(parameters[0]);

	if (ch)
	{
		/* who on a channel. */
		CUList *cu = ch->GetUsers();

		for (CUList::iterator i = cu->begin(); i != cu->end(); i++)
		{
			/* opers only, please */
			if (opt_viewopersonly && !*(i->second)->oper)
				continue;


			/* XXX - code duplication; this could be more efficient -- w00t */
			std::string wholine = initial;

			wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + i->second->dhost + " " + 
					i->second->server + " " + i->second->nick + " ";

			/* away? */
			if (*(i->second)->awaymsg)
			{
				wholine.append("G");
			}
			else
			{
				wholine.append("H");
			}

			/* oper? */
			if (*(i->second)->oper)
			{
				wholine.append("*");
			}

			wholine = wholine + cmode(i->second, ch) + " :0 " + i->second->fullname;
			whoresults.push_back(wholine);
		}
	}
	else
	{
		/* uhggle. who on .. something else. */
		userrec *u = Find(parameters[0]);

		if (u)
		{
			/* who on a single user */
			std::string wholine = initial;

			wholine = wholine + getlastchanname(u) + " " + u->ident + " " + u->dhost + " " + 
					u->server + " " + u->nick + " ";

			/* away? */
			if (*u->awaymsg)
			{
				wholine.append("G");
			}
			else
			{
				wholine.append("H");
			}

			/* oper? */
			if (*u->oper)
			{
				wholine.append("*");
			}

			wholine = wholine + cmode(u, ch) + " :0 " + u->fullname;
			whoresults.push_back(wholine);
		}

		if (*parameters[0] == '*' || *parameters[0] == '0')
		{
			if (!opt_viewopersonly && !*user->oper)
				return; /* No way, jose */

			if (opt_viewopersonly)
			{
				for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
				{
					userrec* oper = (userrec*)*i;

					std::string wholine = initial;

					wholine = wholine + getlastchanname(oper) + " " + oper->ident + " " + oper->dhost + " " + 
							oper->server + " " + oper->nick + " ";

					/* away? */
					if (*oper->awaymsg)
					{
						wholine.append("G");
					}
					else
					{
						wholine.append("H");
					}

					/* oper? */
					if (*oper->oper)
					{
						wholine.append("*");
					}

					wholine = wholine + cmode(oper, ch) + " :0 " + oper->fullname;
					whoresults.push_back(wholine);
				}
			}
			else
			{
				for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
				{
					std::string wholine = initial;

					wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + i->second->dhost + " " + 
							i->second->server + " " + i->second->nick + " ";

					/* away? */
					if (*(i->second)->awaymsg)
					{
						wholine.append("G");
					}
					else
					{
						wholine.append("H");
					}

					/* oper? */
					if (*(i->second)->oper)
					{
						wholine.append("*");
					}

					wholine = wholine + cmode(i->second, ch) + " :0 " + i->second->fullname;
					whoresults.push_back(wholine);
				}
			}
		}
	}
}