2 * IRC - Internet Relay Chat, ircd/s_user.c (formerly ircd/s_msg.c)
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include "ircd_chattr.h"
33 #include "ircd_features.h"
34 #include "ircd_reply.h"
35 #include "ircd_snprintf.h"
36 #include "ircd_string.h"
41 #include "querycmds.h"
56 #include <arpa/inet.h> /* inet_ntoa */
65 * The function that actually prints out the WHO reply for a client found
67 void do_who(struct Client* sptr, struct Client* acptr, struct Channel* repchan,
68 int fields, char* qrt)
71 struct Channel *chptr = repchan;
73 static char buf1[512];
74 /* NOTE: with current fields list and sizes this _cannot_ overrun,
75 and also the message finally sent shouldn't ever be truncated */
80 /* If we don't have a channel and we need one... try to find it,
81 unless the listing is for a channel service, we already know
82 that there are no common channels, thus use PubChannel and not
84 if (!chptr && (!fields || (fields & (WHO_FIELD_CHA | WHO_FIELD_FLA))) &&
85 !IsChannelService(acptr))
87 struct Membership* chan;
88 for (chan = cli_user(acptr)->channel; chan && !chptr; chan = chan->next_channel)
89 if (PubChannel(chan->channel) &&
90 (acptr == sptr || !IsZombie(chan)))
91 chptr = chan->channel;
94 /* Place the fields one by one in the buffer and send it
95 note that fields == NULL means "default query" */
97 if (fields & WHO_FIELD_QTY) /* Query type */
103 while ((*qrt) && (*(p1++) = *(qrt++)));
106 if (!fields || (fields & WHO_FIELD_CHA))
110 if ((p2 = (chptr ? chptr->chname : NULL)))
111 while ((*p2) && (*(p1++) = *(p2++)));
116 if (!fields || (fields & WHO_FIELD_UID))
118 char *p2 = cli_user(acptr)->username;
120 while ((*p2) && (*(p1++) = *(p2++)));
123 if (fields & WHO_FIELD_NIP)
125 const char* p2 = HasHiddenHost(acptr) && !IsAnOper(sptr) ?
126 feature_str(FEAT_HIDDEN_IP) :
127 ircd_ntoa((const char*) &(cli_ip(acptr)));
129 while ((*p2) && (*(p1++) = *(p2++)));
132 if (!fields || (fields & WHO_FIELD_HOS))
134 char *p2 = cli_user(acptr)->host;
136 while ((*p2) && (*(p1++) = *(p2++)));
139 if (!fields || (fields & WHO_FIELD_SER))
141 const char *p2 = (feature_bool(FEAT_HIS_WHO_SERVERNAME) && !IsAnOper(sptr)) ?
142 feature_str(FEAT_HIS_SERVERNAME) :
143 cli_name(cli_user(acptr)->server);
145 while ((*p2) && (*(p1++) = *(p2++)));
148 if (!fields || (fields & WHO_FIELD_NIC))
150 char *p2 = cli_name(acptr);
152 while ((*p2) && (*(p1++) = *(p2++)));
155 if (!fields || (fields & WHO_FIELD_FLA))
158 if (cli_user(acptr)->away)
162 if SeeOper(sptr,acptr)
165 /* If you specified flags then we assume you know how to parse
166 * multiple channel status flags, as this is currently the only
167 * way to know if someone has @'s *and* is +'d.
169 if (chptr && is_chan_op(acptr, chptr))
171 if (chptr && has_voice(acptr, chptr))
173 if (chptr && is_zombie(acptr, chptr))
177 if (chptr && is_chan_op(acptr, chptr))
179 else if (chptr && has_voice(acptr, chptr))
181 else if (chptr && is_zombie(acptr, chptr))
188 if (IsInvisible(acptr))
190 if (SendWallops(acptr))
192 if (SendDebug(acptr))
195 if (HasHiddenHost(acptr))
199 if (!fields || (fields & WHO_FIELD_DIS))
203 *p1++ = ':'; /* Place colon here for default reply */
204 if (feature_bool(FEAT_HIS_WHO_HOPCOUNT) && !IsAnOper(sptr))
205 *p1++ = (sptr == acptr) ? '0' : '3';
207 /* three digit hopcount maximum */
208 p1 += ircd_snprintf(0, p1, 3, "%d", cli_hopcount(acptr));
211 if (fields & WHO_FIELD_IDL)
215 (IsAnOper(sptr) || !feature_bool(FEAT_HIS_WHO_SERVERNAME) ||
217 p1 += ircd_snprintf(0, p1, 11, "%d",
218 CurrentTime - cli_user(acptr)->last);
223 if (fields & WHO_FIELD_ACC)
225 char *p2 = cli_user(acptr)->account;
228 while ((*p2) && (*(p1++) = *(p2++)));
233 if (!fields || (fields & WHO_FIELD_REN))
235 char *p2 = cli_info(acptr);
238 *p1++ = ':'; /* Place colon here for special reply */
239 while ((*p2) && (*(p1++) = *(p2++)));
242 /* The first char will always be an useless blank and we
243 need to terminate buf1 */
246 send_reply(sptr, fields ? RPL_WHOSPCRPL : RPL_WHOREPLY, ++p1);
250 count_users(char *mask)
252 struct Client *acptr;
254 char namebuf[USERLEN + HOSTLEN + 2];
255 char ipbuf[USERLEN + 16 + 2];
257 for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
261 ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
262 cli_user(acptr)->username, cli_user(acptr)->host);
263 ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
264 ircd_ntoa((const char *) &(cli_ip(acptr))));
266 if (!match(mask, namebuf) || !match(mask, ipbuf))