]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
Merge branch 'automake-am11-am12'
[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 channel persistent and client is ircop: make client chanop */
171         if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
172                 Channel_UserModeAdd(chan, target, 'o');
173 } /* join_set_channelmodes */
174
175
176 /**
177  * Forward JOIN command to a specific server
178  *
179  * This function diffentiates between servers using RFC 2813 mode that
180  * support the JOIN command with appended ASCII 7 character and channel
181  * modes, and servers using RFC 1459 protocol which require separate JOIN
182  * and MODE commands.
183  *
184  * @param To            Forward JOIN (and MODE) command to this peer server
185  * @param Prefix        Client used to prefix the genrated commands
186  * @param Data          Parameters of JOIN command to forward, probably
187  *                      containing channel modes separated by ASCII 7.
188  */
189 static void
190 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
191 {
192         CONN_ID conn;
193         char str[COMMAND_LEN], *ptr = NULL;
194
195         strlcpy(str, (char *)Data, sizeof(str));
196         conn = Client_Conn(To);
197
198         if (Conn_Options(conn) & CONN_RFC1459) {
199                 /* RFC 1459 compatibility mode, appended modes are NOT
200                  * supported, so strip them off! */
201                 ptr = strchr(str, 0x7);
202                 if (ptr)
203                         *ptr++ = '\0';
204         }
205
206         IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
207         if (ptr && *ptr)
208                 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
209                                          Client_ID(Prefix));
210 } /* cb_join_forward */
211
212
213 /**
214  * Forward JOIN command to all servers
215  *
216  * This function calls cb_join_forward(), which differentiates between
217  * protocol implementations (e.g. RFC 2812, RFC 1459).
218  *
219  * @param Client        Client used to prefix the genrated commands
220  * @param target        Forward JOIN (and MODE) command to this peer server
221  * @param chan          Channel structure
222  * @param channame      Channel name
223  */
224 static void
225 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
226                                         const char *channame)
227 {
228         char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
229
230         /* RFC 2813, 4.2.1: channel modes are separated from the channel
231          * name with ASCII 7, if any, and not spaces: */
232         strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
233         if (modes[1])
234                 modes[0] = 0x7;
235         else
236                 modes[0] = '\0';
237
238         /* forward to other servers (if it is not a local channel) */
239         if (!Channel_IsLocal(chan)) {
240                 snprintf(str, sizeof(str), "%s%s", channame, modes);
241                 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
242                                                  cb_join_forward, str);
243         }
244
245         /* tell users in this channel about the new client */
246         IRC_WriteStrChannelPrefix(Client, chan, target, false,
247                                   "JOIN :%s",  channame);
248
249         /* synchronize channel modes */
250         if (modes[1]) {
251                 IRC_WriteStrChannelPrefix(Client, chan, target, false,
252                                           "MODE %s +%s %s", channame,
253                                           &modes[1], Client_ID(target));
254         }
255 } /* join_forward */
256
257
258 /**
259  * Aknowledge user JOIN request and send "channel info" numerics.
260  *
261  * @param Client        Client used to prefix the genrated commands
262  * @param target        Forward commands/numerics to this user
263  * @param chan          Channel structure
264  * @param channame      Channel name
265  */
266 static bool
267 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
268                                         const char *channame)
269 {
270         const char *topic;
271
272         if (Client_Type(Client) != CLIENT_USER)
273                 return true;
274         /* acknowledge join */
275         if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
276                 return false;
277
278         /* Send topic to client, if any */
279         topic = Channel_Topic(chan);
280         assert(topic != NULL);
281         if (*topic) {
282                 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
283                         Client_ID(Client), channame, topic))
284                                 return false;
285 #ifndef STRICT_RFC
286                 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
287                         Client_ID(Client), channame,
288                         Channel_TopicWho(chan),
289                         Channel_TopicTime(chan)))
290                                 return false;
291 #endif
292         }
293         /* send list of channel members to client */
294         if (!IRC_Send_NAMES(Client, chan))
295                 return false;
296         return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
297                                   Channel_Name(chan));
298 } /* join_send_topic */
299
300
301 /**
302  * Handler for the IRC "JOIN" command.
303  *
304  * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
305  *
306  * @param Client The client from which this command has been received
307  * @param Req Request structure with prefix and all parameters
308  * @returns CONNECTED or DISCONNECTED
309  */
310 GLOBAL bool
311 IRC_JOIN( CLIENT *Client, REQUEST *Req )
312 {
313         char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
314         CLIENT *target;
315         CHANNEL *chan;
316
317         assert (Client != NULL);
318         assert (Req != NULL);
319
320         /* Bad number of arguments? */
321         if (Req->argc < 1 || Req->argc > 2)
322                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
323                                           Client_ID(Client), Req->command);
324
325         /* Who is the sender? */
326         if (Client_Type(Client) == CLIENT_SERVER)
327                 target = Client_Search(Req->prefix);
328         else
329                 target = Client;
330
331         if (!target)
332                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
333                                           Client_ID(Client), Req->prefix);
334
335         /* Is argument "0"? */
336         if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
337                 return part_from_all_channels(Client, target);
338
339         /* Are channel keys given? */
340         if (Req->argc > 1)
341                 key = strtok_r(Req->argv[1], ",", &lastkey);
342
343         channame = Req->argv[0];
344         channame = strtok_r(channame, ",", &lastchan);
345
346         /* Make sure that "channame" is not the empty string ("JOIN :") */
347         if (! channame)
348                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
349                                           Client_ID(Client), Req->command);
350
351         while (channame) {
352                 flags = NULL;
353
354                 /* Did the server include channel-user-modes? */
355                 if (Client_Type(Client) == CLIENT_SERVER) {
356                         flags = strchr(channame, 0x7);
357                         if (flags) {
358                                 *flags = '\0';
359                                 flags++;
360                         }
361                 }
362
363                 chan = Channel_Search(channame);
364                 if (!chan && Conf_PredefChannelsOnly) {
365                          /* channel must be created, but forbidden by config */
366                         IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
367                                            Client_ID(Client), channame);
368                         goto join_next;
369                 }
370
371                 /* Local client? */
372                 if (Client_Type(Client) == CLIENT_USER) {
373                         if (chan) {
374                                 /* Already existing channel: already member? */
375                                 if (Channel_IsMemberOf(chan, Client))
376                                     goto join_next;
377                         }
378
379                         /* Test if the user has reached the channel limit */
380                         if ((Conf_MaxJoins > 0) &&
381                             (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
382                                 if (!IRC_WriteStrClient(Client,
383                                                 ERR_TOOMANYCHANNELS_MSG,
384                                                 Client_ID(Client), channame))
385                                         return DISCONNECTED;
386                                 goto join_next;
387                         }
388
389                         if (chan) {
390                                 /* Already existing channel: check if the
391                                  * client is allowed to join */
392                                 if (!join_allowed(Client, chan, channame, key))
393                                         goto join_next;
394                         } else {
395                                 /* New channel: first user will become channel
396                                  * operator unless this is a modeless channel */
397                                 if (*channame != '+')
398                                         flags = "o";
399                         }
400
401                         /* Local client: update idle time */
402                         Conn_UpdateIdle(Client_Conn(Client));
403                 } else {
404                         /* Remote server: we don't need to know whether the
405                          * client is invited or not, but we have to make sure
406                          * that the "one shot" entries (generated by INVITE
407                          * commands) in this list become deleted when a user
408                          * joins a channel this way. */
409                         if (chan)
410                                 (void)Lists_Check(Channel_GetListInvites(chan),
411                                                   target);
412                 }
413
414                 /* Join channel (and create channel if it doesn't exist) */
415                 if (!Channel_Join(target, channame))
416                         goto join_next;
417
418                 if (!chan) { /* channel is new; it has been created above */
419                         chan = Channel_Search(channame);
420                         assert(chan != NULL);
421                         if (Channel_IsModeless(chan)) {
422                                 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
423                                 Channel_ModeAdd(chan, 'n'); /* no external msgs */
424                         }
425                 }
426                 assert(chan != NULL);
427
428                 join_set_channelmodes(chan, target, flags);
429
430                 join_forward(Client, target, chan, channame);
431
432                 if (!join_send_topic(Client, target, chan, channame))
433                         break; /* write error */
434
435         join_next:
436                 /* next channel? */
437                 channame = strtok_r(NULL, ",", &lastchan);
438                 if (channame && key)
439                         key = strtok_r(NULL, ",", &lastkey);
440         }
441         return CONNECTED;
442 } /* IRC_JOIN */
443
444
445 /**
446  * Handler for the IRC "PART" command.
447  *
448  * See RFC 2812, 3.2.2: "Part message".
449  *
450  * @param Client        The client from which this command has been received
451  * @param Req           Request structure with prefix and all parameters
452  * @returns             CONNECTED or DISCONNECTED
453  */
454 GLOBAL bool
455 IRC_PART(CLIENT * Client, REQUEST * Req)
456 {
457         CLIENT *target;
458         char *chan;
459
460         assert(Client != NULL);
461         assert(Req != NULL);
462
463         if (Req->argc < 1 || Req->argc > 2)
464                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
465                                           Client_ID(Client), Req->command);
466
467         /* Get the sender */
468         if (Client_Type(Client) == CLIENT_SERVER)
469                 target = Client_Search(Req->prefix);
470         else
471                 target = Client;
472         if (!target)
473                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
474                                           Client_ID(Client), Req->prefix);
475
476         /* Loop over all the given channel names */
477         chan = strtok(Req->argv[0], ",");
478
479         /* Make sure that "chan" is not the empty string ("PART :") */
480         if (! chan)
481                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
482                                           Client_ID(Client), Req->command);
483
484         while (chan) {
485                 Channel_Part(target, Client, chan,
486                              Req->argc > 1 ? Req->argv[1] : Client_ID(target));
487                 chan = strtok(NULL, ",");
488         }
489
490         /* Update idle time, if local client */
491         if (Client_Conn(Client) > NONE)
492                 Conn_UpdateIdle(Client_Conn(Client));
493
494         return CONNECTED;
495 } /* IRC_PART */
496
497
498 /**
499  * Handler for the IRC "TOPIC" command.
500  *
501  * See RFC 2812, 3.2.4 "Topic message".
502  *
503  * @param Client        The client from which this command has been received
504  * @param Req           Request structure with prefix and all parameters
505  * @returns             CONNECTED or DISCONNECTED
506  */
507 GLOBAL bool
508 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
509 {
510         CHANNEL *chan;
511         CLIENT *from;
512         char *topic;
513         bool r, is_oper;
514
515         assert( Client != NULL );
516         assert( Req != NULL );
517
518         if (Req->argc < 1 || Req->argc > 2)
519                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
520                                           Client_ID(Client), Req->command);
521
522         if (Client_Type(Client) == CLIENT_SERVER)
523                 from = Client_Search(Req->prefix);
524         else
525                 from = Client;
526
527         if (!from)
528                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
529                                           Client_ID(Client), Req->prefix);
530
531         chan = Channel_Search(Req->argv[0]);
532         if (!chan)
533                 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
534                                           Client_ID(from), Req->argv[0]);
535
536         /* Only IRC opers and channel members allowed */
537         is_oper = Client_OperByMe(from);
538         if (!Channel_IsMemberOf(chan, from) && !is_oper)
539                 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
540                                           Client_ID(from), Req->argv[0]);
541
542         if (Req->argc == 1) {
543                 /* Request actual topic */
544                 topic = Channel_Topic(chan);
545                 if (*topic) {
546                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
547                                                Client_ID(Client),
548                                                Channel_Name(chan), topic);
549 #ifndef STRICT_RFC
550                         if (!r)
551                                 return r;
552                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
553                                                Client_ID(Client),
554                                                Channel_Name(chan),
555                                                Channel_TopicWho(chan),
556                                                Channel_TopicTime(chan));
557 #endif
558                         return r;
559                 }
560                 else
561                         return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
562                                                   Client_ID(from),
563                                                   Channel_Name(chan));
564         }
565
566         if (strchr(Channel_Modes(chan), 't')) {
567                 /* Topic Lock. Is the user a channel op or IRC operator? */
568                 if(!strchr(Channel_UserModes(chan, from), 'h') &&
569                    !strchr(Channel_UserModes(chan, from), 'o') &&
570                    !strchr(Channel_UserModes(chan, from), 'a') &&
571                    !strchr(Channel_UserModes(chan, from), 'q') &&
572                    !is_oper)
573                         return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
574                                                   Client_ID(from),
575                                                   Channel_Name(chan));
576         }
577
578         /* Set new topic */
579         Channel_SetTopic(chan, from, Req->argv[1]);
580         LogDebug("%s \"%s\" set topic on \"%s\": %s",
581                  Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
582                  Req->argv[1][0] ? Req->argv[1] : "<none>");
583
584         if (Conf_OperServerMode)
585                 from = Client_ThisServer();
586
587         /* Update channel and forward new topic to other servers */
588         if (!Channel_IsLocal(chan))
589                 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
590                                           Req->argv[0], Req->argv[1]);
591         IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
592                                   Req->argv[0], Req->argv[1]);
593
594         if (Client_Type(Client) == CLIENT_USER)
595                 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
596                                                 Req->argv[0], Req->argv[1]);
597         else
598                 return CONNECTED;
599 } /* IRC_TOPIC */
600
601
602 /**
603  * Handler for the IRC "LIST" command.
604  *
605  * See RFC 2812, 3.2.6 "List message".
606  *
607  * This implementation handles the local case as well as the forwarding of the
608  * LIST command to other servers in the IRC network.
609  *
610  * @param Client The client from which this command has been received.
611  * @param Req Request structure with prefix and all parameters.
612  * @return CONNECTED or DISCONNECTED.
613  */
614 GLOBAL bool
615 IRC_LIST( CLIENT *Client, REQUEST *Req )
616 {
617         char *pattern;
618         CHANNEL *chan;
619         CLIENT *from, *target;
620         int count = 0;
621
622         assert(Client != NULL);
623         assert(Req != NULL);
624
625         /* Bad number of prameters? */
626         if (Req->argc > 2)
627                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
628                                           Client_ID(Client), Req->command);
629
630         if (Req->argc > 0)
631                 pattern = strtok(Req->argv[0], ",");
632         else
633                 pattern = "*";
634
635         /* Get sender from prefix, if any */
636         if (Client_Type(Client) == CLIENT_SERVER)
637                 from = Client_Search(Req->prefix);
638         else
639                 from = Client;
640
641         if (!from)
642                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
643                                           Client_ID(Client), Req->prefix);
644
645         if (Req->argc == 2) {
646                 /* Forward to other server? */
647                 target = Client_Search(Req->argv[1]);
648                 if (! target || Client_Type(target) != CLIENT_SERVER)
649                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
650                                                   Client_ID(Client),
651                                                   Req->argv[1]);
652
653                 if (target != Client_ThisServer()) {
654                         /* Target is indeed an other server, forward it! */
655                         return IRC_WriteStrClientPrefix(target, from,
656                                                         "LIST %s :%s",
657                                                         Req->argv[0],
658                                                         Req->argv[1]);
659                 }
660         }
661
662         while (pattern) {
663                 /* Loop through all the channels */
664                 if (Req->argc > 0)
665                         ngt_LowerStr(pattern);
666                 chan = Channel_First();
667                 while (chan) {
668                         /* Check search pattern */
669                         if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
670                                 /* Gotcha! */
671                                 if (!strchr(Channel_Modes(chan), 's')
672                                     || Channel_IsMemberOf(chan, from)) {
673                                         if (IRC_CheckListTooBig(from, count,
674                                                                  MAX_RPL_LIST,
675                                                                  "LIST"))
676                                                 break;
677                                         if (!IRC_WriteStrClient(from,
678                                              RPL_LIST_MSG, Client_ID(from),
679                                              Channel_Name(chan),
680                                              Channel_MemberCount(chan),
681                                              Channel_Topic( chan )))
682                                                 return DISCONNECTED;
683                                         count++;
684                                 }
685                         }
686                         chan = Channel_Next(chan);
687                 }
688
689                 /* Get next name ... */
690                 if(Req->argc > 0)
691                         pattern = strtok(NULL, ",");
692                 else
693                         pattern = NULL;
694         }
695
696         IRC_SetPenalty(from, 2);
697         return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
698 } /* IRC_LIST */
699
700
701 /**
702  * Handler for the IRC+ command "CHANINFO".
703  *
704  * See doc/Protocol.txt, section II.3:
705  * "Exchange channel-modes, topics, and persistent channels".
706  *
707  * @param Client        The client from which this command has been received
708  * @param Req           Request structure with prefix and all parameters
709  * @returns             CONNECTED or DISCONNECTED
710  */
711 GLOBAL bool
712 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
713 {
714         char modes_add[COMMAND_LEN], l[16], *ptr;
715         CLIENT *from;
716         CHANNEL *chan;
717         int arg_topic;
718
719         assert( Client != NULL );
720         assert( Req != NULL );
721
722         /* Bad number of parameters? */
723         if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
724                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
725                                           Client_ID(Client), Req->command);
726
727         /* Compatibility kludge */
728         if( Req->argc == 5 ) arg_topic = 4;
729         else if( Req->argc == 3 ) arg_topic = 2;
730         else arg_topic = -1;
731
732         /* Search origin */
733         from = Client_Search( Req->prefix );
734         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
735
736         /* Search or create channel */
737         chan = Channel_Search( Req->argv[0] );
738         if( ! chan ) chan = Channel_Create( Req->argv[0] );
739         if( ! chan ) return CONNECTED;
740
741         if( Req->argv[1][0] == '+' )
742         {
743                 ptr = Channel_Modes( chan );
744                 if( ! *ptr )
745                 {
746                         /* OK, this channel doesn't have modes jet, set the received ones: */
747                         Channel_SetModes( chan, &Req->argv[1][1] );
748
749                         if( Req->argc == 5 )
750                         {
751                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
752                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
753                         }
754                         else
755                         {
756                                 /* Delete modes which we never want to inherit */
757                                 Channel_ModeDel( chan, 'l' );
758                                 Channel_ModeDel( chan, 'k' );
759                         }
760
761                         strcpy( modes_add, "" );
762                         ptr = Channel_Modes( chan );
763                         while( *ptr )
764                         {
765                                 if( *ptr == 'l' )
766                                 {
767                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
768                                         strlcat( modes_add, l, sizeof( modes_add ));
769                                 }
770                                 if( *ptr == 'k' )
771                                 {
772                                         strlcat( modes_add, " ", sizeof( modes_add ));
773                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
774                                 }
775                                 ptr++;
776                         }
777
778                         /* Inform members of this channel */
779                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
780                 }
781         }
782         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
783
784         if( arg_topic > 0 )
785         {
786                 /* We got a topic */
787                 ptr = Channel_Topic( chan );
788                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
789                 {
790                         /* OK, there is no topic jet */
791                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
792                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
793                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
794                 }
795         }
796
797         /* Forward CHANINFO to other serevrs */
798         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] );
799         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
800         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
801
802         return CONNECTED;
803 } /* IRC_CHANINFO */
804
805
806 /* -eof- */