c06a3af02f84dfcdd7382218598203d04b2e0015
[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 "whocmds.h"
26 #include "IPcheck.h"
27 #include "channel.h"
28 #include "client.h"
29 #include "hash.h"
30 #include "ircd.h"
31 #include "ircd_chattr.h"
32 #include "ircd_reply.h"
33 #include "ircd_string.h"
34 #include "list.h"
35 #include "match.h"
36 #include "numeric.h"
37 #include "numnicks.h"
38 #include "querycmds.h"
39 #include "random.h"
40 #include "s_bsd.h"
41 #include "s_conf.h"
42 #include "s_misc.h"
43 #include "s_user.h"
44 #include "send.h"
45 #include "sprintf_irc.h"
46 #include "struct.h"
47 #include "support.h"
48 #include "sys.h"
49 #include "userload.h"
50 #include "version.h"
51 #include "whowas.h"
52 #include "msg.h"
53
54 #include <arpa/inet.h>        /* inet_ntoa */
55 #include <fcntl.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sys/stat.h>
60 #include <unistd.h>
61
62 /*
63  * The function that actually prints out the WHO reply for a client found
64  */
65 void do_who(struct Client* sptr, struct Client* acptr, struct Channel* repchan,
66             int fields, char* qrt)
67 {
68   char *p1;
69   struct Channel *chptr = repchan;
70
71   static char buf1[512];
72   /* NOTE: with current fields list and sizes this _cannot_ overrun, 
73      and also the message finally sent shouldn't ever be truncated */
74
75   p1 = buf1;
76   buf1[1] = '\0';
77
78   /* If we don't have a channel and we need one... try to find it,
79      unless the listing is for a channel service, we already know
80      that there are no common channels, thus use PubChannel and not
81      SeeChannel */
82   if (!chptr && (!fields || (fields & (WHO_FIELD_CHA | WHO_FIELD_FLA))) &&
83       !IsChannelService(acptr))
84   {
85     struct Membership* chan;
86     for (chan = acptr->user->channel; chan && !chptr; chan = chan->next_channel)
87       if (PubChannel(chan->channel) &&
88           (acptr == sptr || !IsZombie(chan)))
89         chptr = chan->channel;
90   }
91
92   /* Place the fields one by one in the buffer and send it
93      note that fields == NULL means "default query" */
94
95   if (fields & WHO_FIELD_QTY)   /* Query type */
96   {
97     *(p1++) = ' ';
98     if (BadPtr(qrt))
99       *(p1++) = '0';
100     else
101       while ((*qrt) && (*(p1++) = *(qrt++)));
102   }
103
104   if (!fields || (fields & WHO_FIELD_CHA))
105   {
106     char *p2;
107     *(p1++) = ' ';
108     if ((p2 = (chptr ? chptr->chname : NULL)))
109       while ((*p2) && (*(p1++) = *(p2++)));
110     else
111       *(p1++) = '*';
112   }
113
114   if (!fields || (fields & WHO_FIELD_UID))
115   {
116     char *p2 = acptr->user->username;
117     *(p1++) = ' ';
118     while ((*p2) && (*(p1++) = *(p2++)));
119   }
120
121   if (fields & WHO_FIELD_NIP)
122   {
123     const char* p2 = ircd_ntoa((const char*) &acptr->ip);
124     *(p1++) = ' ';
125     while ((*p2) && (*(p1++) = *(p2++)));
126   }
127
128   if (!fields || (fields & WHO_FIELD_HOS))
129   {
130     char *p2 = acptr->user->host;
131     *(p1++) = ' ';
132     while ((*p2) && (*(p1++) = *(p2++)));
133   }
134
135   if (!fields || (fields & WHO_FIELD_SER))
136   {
137     char *p2 = acptr->user->server->name;
138     *(p1++) = ' ';
139     while ((*p2) && (*(p1++) = *(p2++)));
140   }
141
142   if (!fields || (fields & WHO_FIELD_NIC))
143   {
144     char *p2 = acptr->name;
145     *(p1++) = ' ';
146     while ((*p2) && (*(p1++) = *(p2++)));
147   }
148
149   if (!fields || (fields & WHO_FIELD_FLA))
150   {
151     *(p1++) = ' ';
152     if (acptr->user->away)
153       *(p1++) = 'G';
154     else
155       *(p1++) = 'H';
156     if (IsAnOper(acptr))
157       *(p1++) = '*';
158     if (fields) {
159       /* If you specified flags then we assume you know how to parse
160        * multiple channel status flags, as this is currently the only
161        * way to know if someone has @'s *and* is +'d.
162        */
163       if (chptr && is_chan_op(acptr, chptr))
164         *(p1++) = '@';
165       if (chptr && has_voice(acptr, chptr))
166         *(p1++) = '+';
167       if (chptr && is_zombie(acptr, chptr))
168         *(p1++) = '!';
169     }
170     else {
171       if (chptr && is_chan_op(acptr, chptr))
172         *(p1++) = '@';
173       else if (chptr && has_voice(acptr, chptr))
174         *(p1++) = '+';
175       else if (chptr && is_zombie(acptr, chptr))
176         *(p1++) = '!';
177     }
178     if (IsDeaf(acptr))
179       *(p1++) = 'd';
180     if (IsAnOper(sptr))
181     {
182       if (IsInvisible(acptr))
183         *(p1++) = 'i';
184       if (SendWallops(acptr))
185         *(p1++) = 'w';
186       if (SendDebug(acptr))
187         *(p1++) = 'g';
188     }
189   }
190
191   if (!fields || (fields & WHO_FIELD_DIS))
192   {
193     *p1++ = ' ';
194     if (!fields)
195       *p1++ = ':';              /* Place colon here for default reply */
196     p1 = sprintf_irc(p1, "%d", acptr->hopcount);
197   }
198
199   if (fields & WHO_FIELD_IDL)
200   {
201     *p1++ = ' ';
202     if (MyUser(acptr)) {
203             p1 = sprintf_irc(p1, "%d", CurrentTime - acptr->user->last);
204     }    
205     else {
206             *p1++ = '0';
207     }
208   }
209
210   if (!fields || (fields & WHO_FIELD_REN))
211   {
212     char *p2 = acptr->info;
213     *p1++ = ' ';
214     if (fields)
215       *p1++ = ':';              /* Place colon here for special reply */
216     while ((*p2) && (*(p1++) = *(p2++)));
217   }
218
219   /* The first char will always be an useless blank and we 
220      need to terminate buf1 */
221   *p1 = '\0';
222   p1 = buf1;
223   send_reply(sptr, fields ? RPL_WHOSPCRPL : RPL_WHOREPLY, ++p1);
224 }
225