ircu2.10.12 pk910 fork
[ircu2.10.12-pk.git] / ircd / m_names.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_names.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: m_names.c 1845 2007-11-25 02:42:54Z entrope $
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_log.h"
89 #include "ircd_reply.h"
90 #include "ircd_string.h"
91 #include "msg.h"
92 #include "numeric.h"
93 #include "numnicks.h"
94 #include "s_user.h"
95 #include "send.h"
96  
97 /* #include <assert.h> -- Now using assert in ircd_log.h */
98 #include <string.h>
99
100 /*
101  *  Sends a suitably formatted 'names' reply to 'sptr' consisting of nicks within
102  *  'chptr', depending on 'filter'.
103  *
104  *  NAMES_ALL - Lists all users on channel.
105  *  NAMES_VIS - Only list visible (-i) users. --Gte (04/06/2000).
106  *  NAMES_DEL - Show join-delayed names list.
107  *  NAMES_EON - When OR'd with the other two, adds an 'End of Names' numeric
108  *              used by m_join
109  *
110  */
111
112 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
113
114   int mlen;
115   int idx;
116   int flag;
117   int needs_space; 
118   int len; 
119   char buf[BUFSIZE];
120   struct Client *c2ptr;
121   struct Membership* member;
122   
123   assert(chptr);
124   assert(sptr);
125   assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
126
127   /* Tag Pub/Secret channels accordingly. */
128
129   strcpy(buf, "* ");
130   if (PubChannel(chptr))
131     *buf = '=';
132   else if (SecretChannel(chptr))
133     *buf = '@';
134  
135   len = strlen(chptr->chname);
136   strcpy(buf + 2, chptr->chname);
137   strcpy(buf + 2 + len, " :");
138
139   idx = len + 4;
140   flag = 1;
141   needs_space = 0;
142
143   if (!ShowChannel(sptr, chptr)) /* Don't list private channels unless we are on them. */
144     return;
145
146   /* Iterate over all channel members, and build up the list. */
147
148   mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
149   
150   for (member = chptr->members; member; member = member->next_member)
151   {
152     c2ptr = member->user;
153
154     if (((filter&NAMES_VIS)!=0) && IsInvisible(c2ptr))
155       continue;
156
157     if (IsZombie(member) && member->user != sptr)
158       continue;
159
160     if (IsDelayedJoin(member) && (member->user != sptr) && !(filter & NAMES_DEL))
161         continue;
162
163     if ((!IsDelayedJoin(member) || (member->user == sptr)) && (filter & NAMES_DEL))
164         continue;
165                 
166         if (IsInvisibleJoin(member) && member->user != sptr)
167                 continue;
168
169     if (needs_space)
170       buf[idx++] = ' ';
171     needs_space=1;
172     if (IsZombie(member))
173       buf[idx++] = '!';
174     else if (IsChanOp(member))
175       buf[idx++] = '@';
176     else if (HasVoice(member))
177       buf[idx++] = '+';
178     strcpy(buf + idx, cli_name(c2ptr));
179     idx += strlen(cli_name(c2ptr));
180     flag = 1;
181     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
182       /* space, modifier, nick, \r \n \0 */
183     {
184       send_reply(sptr, (filter & NAMES_DEL) ? RPL_DELNAMREPLY : RPL_NAMREPLY, buf);
185       idx = len + 4;
186       flag = 0;
187       needs_space=0;
188     }
189   }
190   if (flag)
191     send_reply(sptr, (filter & NAMES_DEL) ? RPL_DELNAMREPLY : RPL_NAMREPLY, buf); 
192   if (filter&NAMES_EON)
193     send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
194 }
195
196 /*
197  * m_names - generic message handler
198  *
199  * parv[0] = sender prefix
200  * parv[1] = channel
201  */
202
203 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
204 {
205   struct Channel *chptr; 
206   struct Channel *ch2ptr; 
207   struct Client *c2ptr;
208   struct Membership* member; 
209   char* s;
210   char* para = parc > 1 ? parv[1] : 0; 
211   int showingdelayed = 0;
212
213   if (parc > 1 && !ircd_strcmp(parv[1], "-D")) {
214     para = (parc > 2) ? parv[2] : 0;
215     showingdelayed = NAMES_DEL;
216     if (parc > 3 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %s %C", 3, parc, parv))
217       return 0;
218   } else if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
219     return 0;
220
221   if (EmptyString(para)) {
222     send_reply(sptr, RPL_ENDOFNAMES, "*");
223     return 0;
224   }
225
226   do {
227     s = strchr(para, ',');
228     if (s)
229       *s++ = '\0';
230     /*
231      * Special Case 1: "/names 0". 
232      * Full list as per RFC. 
233      */
234     if ((*para == '0') || (*para == '\0'))
235     {
236       int idx; 
237       int mlen;
238       int flag;
239       struct Channel *ch3ptr;
240       char buf[BUFSIZE]; 
241
242       mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
243
244       /* List all visible channels/visible members */ 
245
246       for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
247       { 
248         if (!ShowChannel(sptr, ch2ptr))
249           continue;                 /* Don't show secret chans. */ 
250         else if (find_channel_member(sptr, ch2ptr))
251           do_names(sptr, ch2ptr, showingdelayed|NAMES_ALL); /* Full list if we're in this chan. */
252         else
253           do_names(sptr, ch2ptr, showingdelayed|NAMES_VIS);
254       } 
255
256       /* List all remaining users on channel '*' */
257
258       strcpy(buf, "* * :");
259       idx = 5;
260       flag = 0;
261
262       for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
263       {
264         int showflag = 0;
265
266         if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
267           continue;
268
269         member = cli_user(c2ptr)->channel;
270
271         while (member)
272         {
273           ch3ptr = member->channel;
274   
275           if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
276             showflag = 1;
277  
278           member = member->next_channel;
279         }
280
281         if (showflag)               /* Have we already shown them? */
282           continue;
283  
284         strcpy(buf + idx, cli_name(c2ptr));
285         idx += strlen(cli_name(c2ptr));
286         buf[idx++] = ' ';
287         flag = 1;
288
289         if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
290         {
291           send_reply(sptr, RPL_NAMREPLY, buf);
292           strcpy(buf, "* * :");
293           idx = 5;
294           flag = 0;
295         }
296       }
297       if (flag)
298         send_reply(sptr, RPL_NAMREPLY, buf);
299       send_reply(sptr, RPL_ENDOFNAMES, "*");
300     }
301     else if ((chptr = FindChannel(para)) != NULL)
302     {
303       member = find_member_link(chptr, sptr);
304       if (member)
305       {
306         /*
307          *  Special Case 2: User is on this channel, requesting full names list.
308          *  (As performed with each /join) - ** High frequency usage **
309          */
310         do_names(sptr, chptr, showingdelayed|NAMES_ALL|NAMES_EON);
311       }
312       else
313       {
314         /*
315          *  Special Case 3: User isn't on this channel, show all visible users, in 
316          *  non secret channels.
317          */ 
318         do_names(sptr, chptr, showingdelayed|NAMES_VIS|NAMES_EON);
319       } 
320     }
321     else
322         send_reply(sptr, RPL_ENDOFNAMES, para);
323   } while ((para = s) != NULL);
324
325   return 1;
326 }