34f7ee72892f4aefb92ae50d8eeae16ba7300867
[ircu2.10.12-pk.git] / ircd / whocmds.c
1 /*
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
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
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)
12  * any later version.
13  *
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.
18  *
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.
22  */
23 /** @file
24  * @brief Support functions for /WHO-like commands.
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "whocmds.h"
30 #include "channel.h"
31 #include "client.h"
32 #include "hash.h"
33 #include "ircd.h"
34 #include "ircd_chattr.h"
35 #include "ircd_features.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
39 #include "list.h"
40 #include "match.h"
41 #include "numeric.h"
42 #include "numnicks.h"
43 #include "querycmds.h"
44 #include "random.h"
45 #include "s_bsd.h"
46 #include "s_conf.h"
47 #include "s_misc.h"
48 #include "s_user.h"
49 #include "send.h"
50 #include "struct.h"
51 #include "sys.h"
52 #include "userload.h"
53 #include "version.h"
54 #include "whowas.h"
55 #include "msg.h"
56
57 #include <fcntl.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sys/stat.h>
62 #include <unistd.h>
63
64 /** Send a WHO reply to a client who asked.
65  * @param[in] sptr Client who is searching for other users.
66  * @param[in] acptr Client who may be shown to \a sptr.
67  * @param[in] repchan Shared channel that provides visibility.
68  * @param[in] fields Bitmask of WHO_FIELD_* values, indicating what to show.
69  * @param[in] qrt Query type string (ignored unless \a fields & WHO_FIELD_QTY).
70  */
71 void do_who(struct Client* sptr, struct Client* acptr, struct Channel* repchan,
72             int fields, char* qrt)
73 {
74   char *p1;
75   struct Channel *chptr = repchan;
76
77   static char buf1[512];
78   /* NOTE: with current fields list and sizes this _cannot_ overrun, 
79      and also the message finally sent shouldn't ever be truncated */
80
81   p1 = buf1;
82   buf1[1] = '\0';
83
84   /* If we don't have a channel and we need one... try to find it,
85      unless the listing is for a channel service, we already know
86      that there are no common channels, thus use PubChannel and not
87      SeeChannel */
88   if (!chptr && (!fields || (fields & (WHO_FIELD_CHA | WHO_FIELD_FLA))) &&
89       !IsChannelService(acptr))
90   {
91     struct Membership* chan;
92     for (chan = cli_user(acptr)->channel; chan && !chptr; chan = chan->next_channel)
93       if (PubChannel(chan->channel) &&
94           (acptr == sptr || !IsZombie(chan)))
95         chptr = chan->channel;
96   }
97
98   /* Place the fields one by one in the buffer and send it
99      note that fields == NULL means "default query" */
100
101   if (fields & WHO_FIELD_QTY)   /* Query type */
102   {
103     *(p1++) = ' ';
104     if (BadPtr(qrt))
105       *(p1++) = '0';
106     else
107       while ((*qrt) && (*(p1++) = *(qrt++)));
108   }
109
110   if (!fields || (fields & WHO_FIELD_CHA))
111   {
112     char *p2;
113     *(p1++) = ' ';
114     if ((p2 = (chptr ? chptr->chname : NULL)))
115       while ((*p2) && (*(p1++) = *(p2++)));
116     else
117       *(p1++) = '*';
118   }
119
120   if (!fields || (fields & WHO_FIELD_UID))
121   {
122     char *p2 = cli_user(acptr)->username;
123     *(p1++) = ' ';
124     while ((*p2) && (*(p1++) = *(p2++)));
125   }
126
127   if (fields & WHO_FIELD_NIP)
128   {
129     const char* p2 = HasHiddenHost(acptr) && !IsAnOper(sptr) ?
130       feature_str(FEAT_HIDDEN_IP) :
131       ircd_ntoa(&cli_ip(acptr));
132     *(p1++) = ' ';
133     while ((*p2) && (*(p1++) = *(p2++)));
134   }
135
136   if (!fields || (fields & WHO_FIELD_HOS))
137   {
138     char *p2 = cli_user(acptr)->host;
139     *(p1++) = ' ';
140     while ((*p2) && (*(p1++) = *(p2++)));
141   }
142
143   if (!fields || (fields & WHO_FIELD_SER))
144   {
145     const char *p2 = (feature_bool(FEAT_HIS_WHO_SERVERNAME) && !IsAnOper(sptr)) ?
146                        feature_str(FEAT_HIS_SERVERNAME) :
147                        cli_name(cli_user(acptr)->server);
148     *(p1++) = ' ';
149     while ((*p2) && (*(p1++) = *(p2++)));
150   }
151
152   if (!fields || (fields & WHO_FIELD_NIC))
153   {
154     char *p2 = cli_name(acptr);
155     *(p1++) = ' ';
156     while ((*p2) && (*(p1++) = *(p2++)));
157   }
158
159   if (!fields || (fields & WHO_FIELD_FLA))
160   {
161     *(p1++) = ' ';
162     if (cli_user(acptr)->away)
163       *(p1++) = 'G';
164     else
165       *(p1++) = 'H';
166     if SeeOper(sptr,acptr)
167       *(p1++) = '*';
168     if (fields) {
169       /* If you specified flags then we assume you know how to parse
170        * multiple channel status flags, as this is currently the only
171        * way to know if someone has @'s *and* is +'d.
172        */
173       if (chptr && is_chan_op(acptr, chptr))
174         *(p1++) = '@';
175       if (chptr && has_voice(acptr, chptr))
176         *(p1++) = '+';
177       if (chptr && is_zombie(acptr, chptr))
178         *(p1++) = '!';
179     }
180     else {
181       if (chptr && is_chan_op(acptr, chptr))
182         *(p1++) = '@';
183       else if (chptr && has_voice(acptr, chptr))
184         *(p1++) = '+';
185       else if (chptr && is_zombie(acptr, chptr))
186         *(p1++) = '!';
187     }
188     if (IsDeaf(acptr))
189       *(p1++) = 'd';
190     if (IsAnOper(sptr))
191     {
192       if (IsInvisible(acptr))
193         *(p1++) = 'i';
194       if (SendWallops(acptr))
195         *(p1++) = 'w';
196       if (SendDebug(acptr))
197         *(p1++) = 'g';
198     }
199     if (HasHiddenHost(acptr))
200       *(p1++) = 'x';
201   }
202
203   if (!fields || (fields & WHO_FIELD_DIS))
204   {
205     *p1++ = ' ';
206     if (!fields)
207       *p1++ = ':';              /* Place colon here for default reply */
208     if (feature_bool(FEAT_HIS_WHO_HOPCOUNT) && !IsAnOper(sptr))
209       *p1++ = (sptr == acptr) ? '0' : '3';
210     else
211       /* three digit hopcount maximum */
212       p1 += ircd_snprintf(0, p1, 3, "%d", cli_hopcount(acptr));
213   }
214
215   if (fields & WHO_FIELD_IDL)
216   {
217     *p1++ = ' ';
218     if (MyUser(acptr) &&
219         (IsAnOper(sptr) || !feature_bool(FEAT_HIS_WHO_SERVERNAME) ||
220          acptr == sptr))
221       p1 += ircd_snprintf(0, p1, 11, "%d",
222                           CurrentTime - cli_user(acptr)->last);
223     else
224       *p1++ = '0';
225   }
226
227   if (fields & WHO_FIELD_ACC)
228   {
229     char *p2 = cli_user(acptr)->account;
230     *(p1++) = ' ';
231     if (*p2)
232       while ((*p2) && (*(p1++) = *(p2++)));
233     else
234       *(p1++) = '0';
235   }
236
237   if (!fields || (fields & WHO_FIELD_REN))
238   {
239     char *p2 = cli_info(acptr);
240     *p1++ = ' ';
241     if (fields)
242       *p1++ = ':';              /* Place colon here for special reply */
243     while ((*p2) && (*(p1++) = *(p2++)));
244   }
245
246   /* The first char will always be an useless blank and we 
247      need to terminate buf1 */
248   *p1 = '\0';
249   p1 = buf1;
250   send_reply(sptr, fields ? RPL_WHOSPCRPL : RPL_WHOREPLY, ++p1);
251 }
252
253 /** Count number of users who match \a mask.
254  * @param[in] mask user\@host or user\@ip mask to check.
255  * @return Count of matching users.
256  */
257 int
258 count_users(char *mask)
259 {
260   struct Client *acptr;
261   int count = 0;
262   char namebuf[USERLEN + HOSTLEN + 2];
263   char ipbuf[USERLEN + 16 + 2];
264
265   for (acptr = GlobalClientList; acptr; acptr = cli_next(acptr)) {
266     if (!IsUser(acptr))
267       continue;
268
269     ircd_snprintf(0, namebuf, sizeof(namebuf), "%s@%s",
270                   cli_user(acptr)->username, cli_user(acptr)->host);
271     ircd_snprintf(0, ipbuf, sizeof(ipbuf), "%s@%s", cli_user(acptr)->username,
272                   ircd_ntoa(&cli_ip(acptr)));
273
274     if (!match(mask, namebuf) || !match(mask, ipbuf))
275       count++;
276   }
277
278   return count;
279 }