Make the maximum /list reply length a configurable limit.
[ngircd-alex.git] / src / ngircd / irc-channel.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC channel commands
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "defines.h"
26 #include "conn.h"
27 #include "channel.h"
28 #include "conn-func.h"
29 #include "lists.h"
30 #include "log.h"
31 #include "match.h"
32 #include "messages.h"
33 #include "parse.h"
34 #include "irc.h"
35 #include "irc-info.h"
36 #include "irc-write.h"
37 #include "conf.h"
38
39 #include "exp.h"
40 #include "irc-channel.h"
41
42
43 /**
44  * Part from all channels.
45  *
46  * RFC 2812, (3.2.1 Join message Command):
47  *  Note that this message accepts a special argument ("0"), which is a
48  *  special request to leave all channels the user is currently a member of.
49  *  The server will process this message as if the user had sent a PART
50  *  command (See Section 3.2.2) for each channel he is a member of.
51  *
52  * @param client        Client that initiated the part request
53  * @param target        Client that should part all joined channels
54  * @returns             CONNECTED or DISCONNECTED
55  */
56 static bool
57 part_from_all_channels(CLIENT* client, CLIENT *target)
58 {
59         CL2CHAN *cl2chan;
60         CHANNEL *chan;
61
62         while ((cl2chan = Channel_FirstChannelOf(target))) {
63                 chan = Channel_GetChannel(cl2chan);
64                 assert( chan != NULL );
65                 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
66         }
67         return CONNECTED;
68 } /* part_from_all_channels */
69
70
71 /**
72  * Check weather a local client is allowed to join an already existing
73  * channel or not.
74  *
75  * @param Client        Client that sent the JOIN command
76  * @param chan          Channel to check
77  * @param channame      Name of the channel
78  * @param key           Provided channel key (or NULL)
79  * @returns             true if client is allowed to join, false otherwise
80  */
81 static bool
82 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
83              const char *key)
84 {
85         bool is_invited, is_banned, is_exception;
86         const char *channel_modes;
87
88         /* Allow IRC operators to overwrite channel limits */
89         if (strchr(Client_Modes(Client), 'o'))
90                 return true;
91
92         is_banned = Lists_Check(Channel_GetListBans(chan), Client);
93         is_exception = Lists_Check(Channel_GetListExcepts(chan), Client);
94         is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
95
96         if (is_banned && !is_invited && !is_exception) {
97                 /* Client is banned from channel (and not on invite list) */
98                 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
99                                    Client_ID(Client), channame);
100                 return false;
101         }
102
103         channel_modes = Channel_Modes(chan);
104         if (strchr(channel_modes, 'i') && !is_invited) {
105                 /* Channel is "invite-only" and client is not on invite list */
106                 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
107                                    Client_ID(Client), channame);
108                 return false;
109         }
110
111         if (!Channel_CheckKey(chan, Client, key ? key : "")) {
112                 /* Channel is protected by a channel key and the client
113                  * didn't specify the correct one */
114                 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
115                                    Client_ID(Client), channame);
116                 return false;
117         }
118
119         if (strchr(channel_modes, 'l') &&
120             (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
121                 /* There are more clints joined to this channel than allowed */
122                 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
123                                    Client_ID(Client), channame);
124                 return false;
125         }
126
127         if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
128                 /* Only "secure" clients are allowed, but clients doesn't
129                  * use SSL encryption */
130                 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
131                                    Client_ID(Client), channame);
132                 return false;
133         }
134
135         if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
136                 /* Only IRC operators are allowed! */
137                 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
138                                    Client_ID(Client), channame);
139                 return false;
140         }
141
142         if (strchr(channel_modes, 'R') && !strchr(Client_Modes(Client), 'R')) {
143                 /* Only registered users are allowed! */
144                 IRC_WriteStrClient(Client, ERR_REGONLYCHANNEL_MSG,
145                                    Client_ID(Client), channame);
146                 return false;
147         }
148
149         return true;
150 } /* join_allowed */
151
152
153 /**
154  * Set user channel modes.
155  *
156  * @param chan          Channel
157  * @param target        User to set modes for
158  * @param flags         Channel modes to add
159  */
160 static void
161 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
162 {
163         if (flags) {
164                 while (*flags) {
165                         Channel_UserModeAdd(chan, target, *flags);
166                         flags++;
167                 }
168         }
169
170         /* If the channel is persistent (+P) and client is an IRC op:
171          * make client chanop, if not disabled in configuration. */
172         if (strchr(Channel_Modes(chan), 'P') && Conf_OperChanPAutoOp
173             && strchr(Client_Modes(target), 'o'))
174                 Channel_UserModeAdd(chan, target, 'o');
175 } /* join_set_channelmodes */
176
177
178 /**
179  * Forward JOIN command to a specific server
180  *
181  * This function diffentiates between servers using RFC 2813 mode that
182  * support the JOIN command with appended ASCII 7 character and channel
183  * modes, and servers using RFC 1459 protocol which require separate JOIN
184  * and MODE commands.
185  *
186  * @param To            Forward JOIN (and MODE) command to this peer server
187  * @param Prefix        Client used to prefix the genrated commands
188  * @param Data          Parameters of JOIN command to forward, probably
189  *                      containing channel modes separated by ASCII 7.
190  */
191 static void
192 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
193 {
194         CONN_ID conn;
195         char str[COMMAND_LEN], *ptr = NULL;
196
197         strlcpy(str, (char *)Data, sizeof(str));
198         conn = Client_Conn(To);
199
200         if (Conn_Options(conn) & CONN_RFC1459) {
201                 /* RFC 1459 compatibility mode, appended modes are NOT
202                  * supported, so strip them off! */
203                 ptr = strchr(str, 0x7);
204                 if (ptr)
205                         *ptr++ = '\0';
206         }
207
208         IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
209         if (ptr && *ptr)
210                 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
211                                          Client_ID(Prefix));
212 } /* cb_join_forward */
213
214
215 /**
216  * Forward JOIN command to all servers
217  *
218  * This function calls cb_join_forward(), which differentiates between
219  * protocol implementations (e.g. RFC 2812, RFC 1459).
220  *
221  * @param Client        Client used to prefix the genrated commands
222  * @param target        Forward JOIN (and MODE) command to this peer server
223  * @param chan          Channel structure
224  * @param channame      Channel name
225  */
226 static void
227 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
228                                         const char *channame)
229 {
230         char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
231
232         /* RFC 2813, 4.2.1: channel modes are separated from the channel
233          * name with ASCII 7, if any, and not spaces: */
234         strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
235         if (modes[1])
236                 modes[0] = 0x7;
237         else
238                 modes[0] = '\0';
239
240         /* forward to other servers (if it is not a local channel) */
241         if (!Channel_IsLocal(chan)) {
242                 snprintf(str, sizeof(str), "%s%s", channame, modes);
243                 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
244                                                  cb_join_forward, str);
245         }
246
247         /* tell users in this channel about the new client */
248         IRC_WriteStrChannelPrefix(Client, chan, target, false,
249                                   "JOIN :%s",  channame);
250
251         /* synchronize channel modes */
252         if (modes[1]) {
253                 IRC_WriteStrChannelPrefix(Client, chan, target, false,
254                                           "MODE %s +%s %s", channame,
255                                           &modes[1], Client_ID(target));
256         }
257 } /* join_forward */
258
259
260 /**
261  * Aknowledge user JOIN request and send "channel info" numerics.
262  *
263  * @param Client        Client used to prefix the genrated commands
264  * @param target        Forward commands/numerics to this user
265  * @param chan          Channel structure
266  * @param channame      Channel name
267  */
268 static bool
269 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
270                                         const char *channame)
271 {
272         const char *topic;
273
274         if (Client_Type(Client) != CLIENT_USER)
275                 return true;
276         /* acknowledge join */
277         if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
278                 return false;
279
280         /* Send topic to client, if any */
281         topic = Channel_Topic(chan);
282         assert(topic != NULL);
283         if (*topic) {
284                 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
285                         Client_ID(Client), channame, topic))
286                                 return false;
287 #ifndef STRICT_RFC
288                 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
289                         Client_ID(Client), channame,
290                         Channel_TopicWho(chan),
291                         Channel_TopicTime(chan)))
292                                 return false;
293 #endif
294         }
295         /* send list of channel members to client */
296         if (!IRC_Send_NAMES(Client, chan))
297                 return false;
298         return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
299                                   Channel_Name(chan));
300 } /* join_send_topic */
301
302
303 /**
304  * Handler for the IRC "JOIN" command.
305  *
306  * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
307  *
308  * @param Client The client from which this command has been received
309  * @param Req Request structure with prefix and all parameters
310  * @returns CONNECTED or DISCONNECTED
311  */
312 GLOBAL bool
313 IRC_JOIN( CLIENT *Client, REQUEST *Req )
314 {
315         char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
316         CLIENT *target;
317         CHANNEL *chan;
318
319         assert (Client != NULL);
320         assert (Req != NULL);
321
322         /* Bad number of arguments? */
323         if (Req->argc < 1 || Req->argc > 2)
324                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
325                                           Client_ID(Client), Req->command);
326
327         /* Who is the sender? */
328         if (Client_Type(Client) == CLIENT_SERVER)
329                 target = Client_Search(Req->prefix);
330         else
331                 target = Client;
332
333         if (!target)
334                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
335                                           Client_ID(Client), Req->prefix);
336
337         /* Is argument "0"? */
338         if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
339                 return part_from_all_channels(Client, target);
340
341         /* Are channel keys given? */
342         if (Req->argc > 1)
343                 key = strtok_r(Req->argv[1], ",", &lastkey);
344
345         channame = Req->argv[0];
346         channame = strtok_r(channame, ",", &lastchan);
347
348         /* Make sure that "channame" is not the empty string ("JOIN :") */
349         if (! channame)
350                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
351                                           Client_ID(Client), Req->command);
352
353         while (channame) {
354                 flags = NULL;
355
356                 /* Did the server include channel-user-modes? */
357                 if (Client_Type(Client) == CLIENT_SERVER) {
358                         flags = strchr(channame, 0x7);
359                         if (flags) {
360                                 *flags = '\0';
361                                 flags++;
362                         }
363                 }
364
365                 chan = Channel_Search(channame);
366                 if (!chan && Conf_PredefChannelsOnly) {
367                          /* channel must be created, but forbidden by config */
368                         IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
369                                            Client_ID(Client), channame);
370                         goto join_next;
371                 }
372
373                 /* Local client? */
374                 if (Client_Type(Client) == CLIENT_USER) {
375                         if (chan) {
376                                 /* Already existing channel: already member? */
377                                 if (Channel_IsMemberOf(chan, Client))
378                                     goto join_next;
379                         }
380
381                         /* Test if the user has reached the channel limit */
382                         if ((Conf_MaxJoins > 0) &&
383                             (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
384                                 if (!IRC_WriteStrClient(Client,
385                                                 ERR_TOOMANYCHANNELS_MSG,
386                                                 Client_ID(Client), channame))
387                                         return DISCONNECTED;
388                                 goto join_next;
389                         }
390
391                         if (chan) {
392                                 /* Already existing channel: check if the
393                                  * client is allowed to join */
394                                 if (!join_allowed(Client, chan, channame, key))
395                                         goto join_next;
396                         } else {
397                                 /* New channel: first user will become channel
398                                  * operator unless this is a modeless channel */
399                                 if (*channame != '+')
400                                         flags = "o";
401                         }
402
403                         /* Local client: update idle time */
404                         Conn_UpdateIdle(Client_Conn(Client));
405                 } else {
406                         /* Remote server: we don't need to know whether the
407                          * client is invited or not, but we have to make sure
408                          * that the "one shot" entries (generated by INVITE
409                          * commands) in this list become deleted when a user
410                          * joins a channel this way. */
411                         if (chan)
412                                 (void)Lists_Check(Channel_GetListInvites(chan),
413                                                   target);
414                 }
415
416                 /* Join channel (and create channel if it doesn't exist) */
417                 if (!Channel_Join(target, channame))
418                         goto join_next;
419
420                 if (!chan) { /* channel is new; it has been created above */
421                         chan = Channel_Search(channame);
422                         assert(chan != NULL);
423                         if (Channel_IsModeless(chan)) {
424                                 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
425                                 Channel_ModeAdd(chan, 'n'); /* no external msgs */
426                         }
427                 }
428                 assert(chan != NULL);
429
430                 join_set_channelmodes(chan, target, flags);
431
432                 join_forward(Client, target, chan, channame);
433
434                 if (!join_send_topic(Client, target, chan, channame))
435                         break; /* write error */
436
437         join_next:
438                 /* next channel? */
439                 channame = strtok_r(NULL, ",", &lastchan);
440                 if (channame && key)
441                         key = strtok_r(NULL, ",", &lastkey);
442         }
443         return CONNECTED;
444 } /* IRC_JOIN */
445
446
447 /**
448  * Handler for the IRC "PART" command.
449  *
450  * See RFC 2812, 3.2.2: "Part message".
451  *
452  * @param Client        The client from which this command has been received
453  * @param Req           Request structure with prefix and all parameters
454  * @returns             CONNECTED or DISCONNECTED
455  */
456 GLOBAL bool
457 IRC_PART(CLIENT * Client, REQUEST * Req)
458 {
459         CLIENT *target;
460         char *chan;
461
462         assert(Client != NULL);
463         assert(Req != NULL);
464
465         if (Req->argc < 1 || Req->argc > 2)
466                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
467                                           Client_ID(Client), Req->command);
468
469         /* Get the sender */
470         if (Client_Type(Client) == CLIENT_SERVER)
471                 target = Client_Search(Req->prefix);
472         else
473                 target = Client;
474         if (!target)
475                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
476                                           Client_ID(Client), Req->prefix);
477
478         /* Loop over all the given channel names */
479         chan = strtok(Req->argv[0], ",");
480
481         /* Make sure that "chan" is not the empty string ("PART :") */
482         if (! chan)
483                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
484                                           Client_ID(Client), Req->command);
485
486         while (chan) {
487                 Channel_Part(target, Client, chan,
488                              Req->argc > 1 ? Req->argv[1] : Client_ID(target));
489                 chan = strtok(NULL, ",");
490         }
491
492         /* Update idle time, if local client */
493         if (Client_Conn(Client) > NONE)
494                 Conn_UpdateIdle(Client_Conn(Client));
495
496         return CONNECTED;
497 } /* IRC_PART */
498
499
500 /**
501  * Handler for the IRC "TOPIC" command.
502  *
503  * See RFC 2812, 3.2.4 "Topic message".
504  *
505  * @param Client        The client from which this command has been received
506  * @param Req           Request structure with prefix and all parameters
507  * @returns             CONNECTED or DISCONNECTED
508  */
509 GLOBAL bool
510 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
511 {
512         CHANNEL *chan;
513         CLIENT *from;
514         char *topic;
515         bool r, topic_power;
516
517         assert( Client != NULL );
518         assert( Req != NULL );
519
520         if (Req->argc < 1 || Req->argc > 2)
521                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
522                                           Client_ID(Client), Req->command);
523
524         if (Client_Type(Client) == CLIENT_SERVER)
525                 from = Client_Search(Req->prefix);
526         else
527                 from = Client;
528
529         if (!from)
530                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
531                                           Client_ID(Client), Req->prefix);
532
533         chan = Channel_Search(Req->argv[0]);
534         if (!chan)
535                 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
536                                           Client_ID(from), Req->argv[0]);
537
538         /* Only remote servers and channel members are allowed to change the
539          * channel topic, and IRC opreators when the Conf_OperCanMode option
540          * is set in the server configuration. */
541         if (Client_Type(Client) != CLIENT_SERVER) {
542                 topic_power = Client_HasMode(from, 'o');
543                 if (!Channel_IsMemberOf(chan, from)
544                     && !(Conf_OperCanMode && topic_power))
545                         return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
546                                                   Client_ID(from), Req->argv[0]);
547         } else
548                 topic_power = true;
549
550         if (Req->argc == 1) {
551                 /* Request actual topic */
552                 topic = Channel_Topic(chan);
553                 if (*topic) {
554                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
555                                                Client_ID(Client),
556                                                Channel_Name(chan), topic);
557 #ifndef STRICT_RFC
558                         if (!r)
559                                 return r;
560                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
561                                                Client_ID(Client),
562                                                Channel_Name(chan),
563                                                Channel_TopicWho(chan),
564                                                Channel_TopicTime(chan));
565 #endif
566                         return r;
567                 }
568                 else
569                         return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
570                                                   Client_ID(from),
571                                                   Channel_Name(chan));
572         }
573
574         if (strchr(Channel_Modes(chan), 't')) {
575                 /* Topic Lock. Is the user a channel op or IRC operator? */
576                 if(!topic_power &&
577                    !strchr(Channel_UserModes(chan, from), 'h') &&
578                    !strchr(Channel_UserModes(chan, from), 'o') &&
579                    !strchr(Channel_UserModes(chan, from), 'a') &&
580                    !strchr(Channel_UserModes(chan, from), 'q'))
581                         return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
582                                                   Client_ID(from),
583                                                   Channel_Name(chan));
584         }
585
586         /* Set new topic */
587         Channel_SetTopic(chan, from, Req->argv[1]);
588         LogDebug("%s \"%s\" set topic on \"%s\": %s",
589                  Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
590                  Req->argv[1][0] ? Req->argv[1] : "<none>");
591
592         if (Conf_OperServerMode)
593                 from = Client_ThisServer();
594
595         /* Update channel and forward new topic to other servers */
596         if (!Channel_IsLocal(chan))
597                 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
598                                           Req->argv[0], Req->argv[1]);
599         IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
600                                   Req->argv[0], Req->argv[1]);
601
602         if (Client_Type(Client) == CLIENT_USER)
603                 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
604                                                 Req->argv[0], Req->argv[1]);
605         else
606                 return CONNECTED;
607 } /* IRC_TOPIC */
608
609
610 /**
611  * Handler for the IRC "LIST" command.
612  *
613  * See RFC 2812, 3.2.6 "List message".
614  *
615  * This implementation handles the local case as well as the forwarding of the
616  * LIST command to other servers in the IRC network.
617  *
618  * @param Client The client from which this command has been received.
619  * @param Req Request structure with prefix and all parameters.
620  * @return CONNECTED or DISCONNECTED.
621  */
622 GLOBAL bool
623 IRC_LIST( CLIENT *Client, REQUEST *Req )
624 {
625         char *pattern;
626         CHANNEL *chan;
627         CLIENT *from, *target;
628         int count = 0;
629
630         assert(Client != NULL);
631         assert(Req != NULL);
632
633         /* Bad number of prameters? */
634         if (Req->argc > 2)
635                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
636                                           Client_ID(Client), Req->command);
637
638         if (Req->argc > 0)
639                 pattern = strtok(Req->argv[0], ",");
640         else
641                 pattern = "*";
642
643         /* Get sender from prefix, if any */
644         if (Client_Type(Client) == CLIENT_SERVER)
645                 from = Client_Search(Req->prefix);
646         else
647                 from = Client;
648
649         if (!from)
650                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
651                                           Client_ID(Client), Req->prefix);
652
653         if (Req->argc == 2) {
654                 /* Forward to other server? */
655                 target = Client_Search(Req->argv[1]);
656                 if (! target || Client_Type(target) != CLIENT_SERVER)
657                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
658                                                   Client_ID(Client),
659                                                   Req->argv[1]);
660
661                 if (target != Client_ThisServer()) {
662                         /* Target is indeed an other server, forward it! */
663                         return IRC_WriteStrClientPrefix(target, from,
664                                                         "LIST %s :%s",
665                                                         Req->argv[0],
666                                                         Req->argv[1]);
667                 }
668         }
669
670         while (pattern) {
671                 /* Loop through all the channels */
672                 if (Req->argc > 0)
673                         ngt_LowerStr(pattern);
674                 chan = Channel_First();
675                 while (chan) {
676                         /* Check search pattern */
677                         if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
678                                 /* Gotcha! */
679                                 if (!strchr(Channel_Modes(chan), 's')
680                                     || Channel_IsMemberOf(chan, from)
681                                     || (!Conf_MorePrivacy && Client_OperByMe(Client))) {
682                                         if ((Conf_MaxListSize > 0)
683                                             && IRC_CheckListTooBig(from, count,
684                                                                    Conf_MaxListSize,
685                                                                    "LIST"))
686                                                 break;
687                                         if (!IRC_WriteStrClient(from,
688                                              RPL_LIST_MSG, Client_ID(from),
689                                              Channel_Name(chan),
690                                              Channel_MemberCount(chan),
691                                              Channel_Topic( chan )))
692                                                 return DISCONNECTED;
693                                         count++;
694                                 }
695                         }
696                         chan = Channel_Next(chan);
697                 }
698
699                 /* Get next name ... */
700                 if(Req->argc > 0)
701                         pattern = strtok(NULL, ",");
702                 else
703                         pattern = NULL;
704         }
705
706         IRC_SetPenalty(from, 2);
707         return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
708 } /* IRC_LIST */
709
710
711 /**
712  * Handler for the IRC+ command "CHANINFO".
713  *
714  * See doc/Protocol.txt, section II.3:
715  * "Exchange channel-modes, topics, and persistent channels".
716  *
717  * @param Client        The client from which this command has been received
718  * @param Req           Request structure with prefix and all parameters
719  * @returns             CONNECTED or DISCONNECTED
720  */
721 GLOBAL bool
722 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
723 {
724         char modes_add[COMMAND_LEN], l[16], *ptr;
725         CLIENT *from;
726         CHANNEL *chan;
727         int arg_topic;
728
729         assert( Client != NULL );
730         assert( Req != NULL );
731
732         /* Bad number of parameters? */
733         if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
734                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
735                                           Client_ID(Client), Req->command);
736
737         /* Compatibility kludge */
738         if( Req->argc == 5 ) arg_topic = 4;
739         else if( Req->argc == 3 ) arg_topic = 2;
740         else arg_topic = -1;
741
742         /* Search origin */
743         from = Client_Search( Req->prefix );
744         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
745
746         /* Search or create channel */
747         chan = Channel_Search( Req->argv[0] );
748         if( ! chan ) chan = Channel_Create( Req->argv[0] );
749         if( ! chan ) return CONNECTED;
750
751         if( Req->argv[1][0] == '+' )
752         {
753                 ptr = Channel_Modes( chan );
754                 if( ! *ptr )
755                 {
756                         /* OK, this channel doesn't have modes jet, set the received ones: */
757                         Channel_SetModes( chan, &Req->argv[1][1] );
758
759                         if( Req->argc == 5 )
760                         {
761                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
762                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
763                         }
764                         else
765                         {
766                                 /* Delete modes which we never want to inherit */
767                                 Channel_ModeDel( chan, 'l' );
768                                 Channel_ModeDel( chan, 'k' );
769                         }
770
771                         strcpy( modes_add, "" );
772                         ptr = Channel_Modes( chan );
773                         while( *ptr )
774                         {
775                                 if( *ptr == 'l' )
776                                 {
777                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
778                                         strlcat( modes_add, l, sizeof( modes_add ));
779                                 }
780                                 if( *ptr == 'k' )
781                                 {
782                                         strlcat( modes_add, " ", sizeof( modes_add ));
783                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
784                                 }
785                                 ptr++;
786                         }
787
788                         /* Inform members of this channel */
789                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
790                 }
791         }
792         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
793
794         if( arg_topic > 0 )
795         {
796                 /* We got a topic */
797                 ptr = Channel_Topic( chan );
798                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
799                 {
800                         /* OK, there is no topic jet */
801                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
802                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
803                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
804                 }
805         }
806
807         /* Forward CHANINFO to other serevrs */
808         if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
809         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
810         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
811
812         return CONNECTED;
813 } /* IRC_CHANINFO */
814
815
816 /* -eof- */