summaryrefslogtreecommitdiff
path: root/src/cmd_who.cpp
blob: 0a66372420bef16c393d8d9ce95dbdf3adc365ca (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*   +------------------------------------+
 *   | 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 "wildcard.h"
#include "commands/cmd_who.h"

extern InspIRCd* ServerInstance;
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 "*";
}

bool whomatch(userrec* user, const char* matchtext, bool opt_realname, bool opt_showrealhost)
{
	bool realhost = false;
	bool realname = false;

	if (opt_realname)
		realname = match(user->fullname, matchtext);

	if (opt_showrealhost)
		realhost = match(user->host, matchtext);

	return ((realname) || (realhost) || (match(user->dhost, matchtext)) || (match(user->nick, matchtext)) || (match(user->server, matchtext)));
}

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.
	 */

	/* WHO options */
	bool opt_viewopersonly = false;
	bool opt_showrealhost = false;
	bool opt_unlimit = false;
	bool opt_realname = false;

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

	const char* matchtext = NULL;

	/* Change '0' into '*' so the wildcard matcher can grok it */
	matchtext = parameters[0];
	if (!strcmp(matchtext,"0"))
		matchtext = "*";

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

		while (*iter)
		{
			switch (*iter)
			{
				case 'o':
					opt_viewopersonly = true;
				break;
				case 'h':
					if (*user->oper)
						opt_showrealhost = true;
				break;
				case 'u':
					if (*user->oper)
						opt_unlimit = true;
				break;
				case 'r':
					opt_realname = true;
				break;
			}

			*iter++;
		}
	}


	/* who on a channel? */
	ch = FindChan(matchtext);

	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 + ch->name + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : 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
	{
		/* Match against wildcard of nick, server or host */

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

				if (whomatch(oper, matchtext, opt_realname, opt_showrealhost))
				{
					std::string wholine = initial;
	
					wholine = wholine + getlastchanname(oper) + " " + oper->ident + " " + (opt_showrealhost ? oper->host : 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++)
			{
				if (whomatch(i->second, matchtext, opt_realname, opt_showrealhost))
				{
					std::string wholine = initial;
	
					wholine = wholine + getlastchanname(i->second) + " " + i->second->ident + " " + (opt_showrealhost ? i->second->host : 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);
				}
			}
		}
	}
	/* Send the results out */
	if ((whoresults.size() < (size_t)ServerInstance->Config->MaxWhoResults) && (!opt_unlimit))
	{
		for (std::vector<std::string>::const_iterator n = whoresults.begin(); n != whoresults.end(); n++)
			user->WriteServ(*n);
		user->WriteServ("315 %s %s :End of /WHO list.",user->nick, parameters[0]);
	}
	else
	{
		/* BZZT! Too many results. */
		user->WriteServ("315 %s %s :Too many results",user->nick, parameters[0]);
	}
}