fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[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  *  NAMES_OPS - Only list oped (+o) or voiced (+v) users
110  *
111  */
112
113 void do_names(struct Client* sptr, struct Channel* chptr, int filter)
114
115   int mlen;
116   int idx;
117   int flag;
118   int needs_space; 
119   int len; 
120   char buf[BUFSIZE];
121   struct Client *c2ptr;
122   struct Membership* member;
123   
124   assert(chptr);
125   assert(sptr);
126   assert((filter&NAMES_ALL) != (filter&NAMES_VIS));
127
128   /* Tag Pub/Secret channels accordingly. */
129
130   strcpy(buf, "* ");
131   if (PubChannel(chptr))
132     *buf = '=';
133   else if (SecretChannel(chptr))
134     *buf = '@';
135  
136   len = strlen(chptr->chname);
137   strcpy(buf + 2, chptr->chname);
138   strcpy(buf + 2 + len, " :");
139
140   idx = len + 4;
141   flag = 1;
142   needs_space = 0;
143
144   if (!ShowChannel(sptr, chptr)) { /* Don't list private channels unless we are on them. */
145     if (filter&NAMES_EON)
146       send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
147     return;
148   }
149
150   /* Iterate over all channel members, and build up the list. */
151
152   mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
153   
154   for (member = chptr->members; member; member = member->next_member)
155   {
156     c2ptr = member->user;
157
158     if (((filter&NAMES_VIS)!=0) && IsInvisible(c2ptr))
159       continue;
160
161     if (IsZombie(member) && member->user != sptr)
162       continue;
163
164     if (IsDelayedJoin(member) && (member->user != sptr) && !(filter & NAMES_DEL))
165         continue;
166
167     if ((!IsDelayedJoin(member) || (member->user == sptr)) && (filter & NAMES_DEL))
168         continue;
169                 
170         if (IsInvisibleJoin(member) && member->user != sptr)
171                 continue;
172         
173         if (!IsChanOpOrHalfOp(member) && !HasVoice(member) && member->user != sptr && (filter & NAMES_OPS))
174                 continue;
175
176     if (needs_space)
177       buf[idx++] = ' ';
178     needs_space=1;
179     if (IsZombie(member))
180       buf[idx++] = '!';
181     else if (IsChanOp(member))
182       buf[idx++] = '@';
183     else if (IsHalfOp(member))
184       buf[idx++] = '%';
185     else if (HasVoice(member))
186       buf[idx++] = '+';
187     strcpy(buf + idx, cli_name(c2ptr));
188     idx += strlen(cli_name(c2ptr));
189     flag = 1;
190     if (mlen + idx + NICKLEN + 5 > BUFSIZE)
191       /* space, modifier, nick, \r \n \0 */
192     {
193       send_reply(sptr, (filter & NAMES_DEL) ? RPL_DELNAMREPLY : RPL_NAMREPLY, buf);
194       idx = len + 4;
195       flag = 0;
196       needs_space=0;
197     }
198   }
199   if (flag)
200     send_reply(sptr, (filter & NAMES_DEL) ? RPL_DELNAMREPLY : RPL_NAMREPLY, buf); 
201   if (filter&NAMES_EON)
202     send_reply(sptr, RPL_ENDOFNAMES, chptr->chname);
203 }
204
205 /*
206  * m_names - generic message handler
207  *
208  * parv[0] = sender prefix
209  * parv[1] = channel
210  */
211
212 int m_names(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
213 {
214   struct Channel *chptr; 
215   struct Channel *ch2ptr; 
216   struct Client *c2ptr;
217   struct Membership* member; 
218   char* s;
219   char* para = parc > 1 ? parv[1] : 0; 
220   int showingdelayed = 0;
221
222   if (parc > 1 && !ircd_strcmp(parv[1], "-D")) {
223     para = (parc > 2) ? parv[2] : 0;
224     showingdelayed = NAMES_DEL;
225     if (parc > 3 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %s %C", 3, parc, parv))
226       return 0;
227   } else if (parc > 2 && hunt_server_cmd(sptr, CMD_NAMES, cptr, 1, "%s %C", 2, parc, parv))
228     return 0;
229
230   if (EmptyString(para)) {
231     send_reply(sptr, RPL_ENDOFNAMES, "*");
232     return 0;
233   }
234
235   do {
236     s = strchr(para, ',');
237     if (s)
238       *s++ = '\0';
239     /*
240      * Special Case 1: "/names 0". 
241      * Full list as per RFC. 
242      */
243     if ((*para == '0') || (*para == '\0'))
244     {
245       int idx; 
246       int mlen;
247       int flag;
248       struct Channel *ch3ptr;
249       char buf[BUFSIZE]; 
250
251       mlen = strlen(cli_name(&me)) + 10 + strlen(cli_name(sptr));
252
253       /* List all visible channels/visible members */ 
254       struct Membership *member;
255       for (ch2ptr = GlobalChannelList; ch2ptr; ch2ptr = ch2ptr->next)
256       { 
257         if (!ShowChannel(sptr, ch2ptr))
258           continue;                 /* Don't show secret chans. */ 
259         else if ((member = find_channel_member(sptr, ch2ptr))) {
260           do_names(sptr, ch2ptr, showingdelayed|NAMES_ALL|((ch2ptr->mode.mode & MODE_AUDITORIUM) && !(member->status & MODE_CHANOP) ? NAMES_OPS : 0)); /* Full list if we're in this chan. */
261         } else
262           do_names(sptr, ch2ptr, showingdelayed|NAMES_VIS|((ch2ptr->mode.mode & MODE_AUDITORIUM) ? NAMES_OPS : 0));
263       } 
264
265       /* List all remaining users on channel '*' */
266
267       strcpy(buf, "* * :");
268       idx = 5;
269       flag = 0;
270
271       for (c2ptr = GlobalClientList; c2ptr; c2ptr = cli_next(c2ptr))
272       {
273         int showflag = 0;
274
275         if (!IsUser(c2ptr) || (sptr != c2ptr && IsInvisible(c2ptr)))
276           continue;
277
278         member = cli_user(c2ptr)->channel;
279
280         while (member)
281         {
282           ch3ptr = member->channel;
283   
284           if (PubChannel(ch3ptr) || find_channel_member(sptr, ch3ptr))
285             showflag = 1;
286  
287           member = member->next_channel;
288         }
289
290         if (showflag)               /* Have we already shown them? */
291           continue;
292  
293         strcpy(buf + idx, cli_name(c2ptr));
294         idx += strlen(cli_name(c2ptr));
295         buf[idx++] = ' ';
296         flag = 1;
297
298         if (mlen + idx + NICKLEN + 3 > BUFSIZE)     /* space, \r\n\0 */
299         {
300           send_reply(sptr, RPL_NAMREPLY, buf);
301           strcpy(buf, "* * :");
302           idx = 5;
303           flag = 0;
304         }
305       }
306       if (flag)
307         send_reply(sptr, RPL_NAMREPLY, buf);
308       send_reply(sptr, RPL_ENDOFNAMES, "*");
309     }
310     else if ((chptr = FindChannel(para)) != NULL)
311     {
312       member = find_member_link(chptr, sptr);
313       if (member)
314       {
315         /*
316          *  Special Case 2: User is on this channel, requesting full names list.
317          *  (As performed with each /join) - ** High frequency usage **
318          */
319         do_names(sptr, chptr, showingdelayed|NAMES_ALL|NAMES_EON|((chptr->mode.mode & MODE_AUDITORIUM) && !(member->status & MODE_CHANOP) ? NAMES_OPS : 0));
320       }
321       else
322       {
323         /*
324          *  Special Case 3: User isn't on this channel, show all visible users, in 
325          *  non secret channels.
326          */ 
327         do_names(sptr, chptr, showingdelayed|NAMES_VIS|NAMES_EON|((chptr->mode.mode & MODE_AUDITORIUM) ? NAMES_OPS : 0));
328       } 
329     }
330     else
331         send_reply(sptr, RPL_ENDOFNAMES, para);
332   } while ((para = s) != NULL);
333
334   return 1;
335 }