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