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