New configuration option "OperChanPAutoOp"
[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, is_oper;
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 IRC opers and channel members allowed */
539         is_oper = Client_OperByMe(from);
540         if (!Channel_IsMemberOf(chan, from) && !is_oper)
541                 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
542                                           Client_ID(from), Req->argv[0]);
543
544         if (Req->argc == 1) {
545                 /* Request actual topic */
546                 topic = Channel_Topic(chan);
547                 if (*topic) {
548                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
549                                                Client_ID(Client),
550                                                Channel_Name(chan), topic);
551 #ifndef STRICT_RFC
552                         if (!r)
553                                 return r;
554                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
555                                                Client_ID(Client),
556                                                Channel_Name(chan),
557                                                Channel_TopicWho(chan),
558                                                Channel_TopicTime(chan));
559 #endif
560                         return r;
561                 }
562                 else
563                         return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
564                                                   Client_ID(from),
565                                                   Channel_Name(chan));
566         }
567
568         if (strchr(Channel_Modes(chan), 't')) {
569                 /* Topic Lock. Is the user a channel op or IRC operator? */
570                 if(!strchr(Channel_UserModes(chan, from), 'h') &&
571                    !strchr(Channel_UserModes(chan, from), 'o') &&
572                    !strchr(Channel_UserModes(chan, from), 'a') &&
573                    !strchr(Channel_UserModes(chan, from), 'q') &&
574                    !is_oper)
575                         return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
576                                                   Client_ID(from),
577                                                   Channel_Name(chan));
578         }
579
580         /* Set new topic */
581         Channel_SetTopic(chan, from, Req->argv[1]);
582         LogDebug("%s \"%s\" set topic on \"%s\": %s",
583                  Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
584                  Req->argv[1][0] ? Req->argv[1] : "<none>");
585
586         if (Conf_OperServerMode)
587                 from = Client_ThisServer();
588
589         /* Update channel and forward new topic to other servers */
590         if (!Channel_IsLocal(chan))
591                 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
592                                           Req->argv[0], Req->argv[1]);
593         IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
594                                   Req->argv[0], Req->argv[1]);
595
596         if (Client_Type(Client) == CLIENT_USER)
597                 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
598                                                 Req->argv[0], Req->argv[1]);
599         else
600                 return CONNECTED;
601 } /* IRC_TOPIC */
602
603
604 /**
605  * Handler for the IRC "LIST" command.
606  *
607  * See RFC 2812, 3.2.6 "List message".
608  *
609  * This implementation handles the local case as well as the forwarding of the
610  * LIST command to other servers in the IRC network.
611  *
612  * @param Client The client from which this command has been received.
613  * @param Req Request structure with prefix and all parameters.
614  * @return CONNECTED or DISCONNECTED.
615  */
616 GLOBAL bool
617 IRC_LIST( CLIENT *Client, REQUEST *Req )
618 {
619         char *pattern;
620         CHANNEL *chan;
621         CLIENT *from, *target;
622         int count = 0;
623
624         assert(Client != NULL);
625         assert(Req != NULL);
626
627         /* Bad number of prameters? */
628         if (Req->argc > 2)
629                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
630                                           Client_ID(Client), Req->command);
631
632         if (Req->argc > 0)
633                 pattern = strtok(Req->argv[0], ",");
634         else
635                 pattern = "*";
636
637         /* Get sender from prefix, if any */
638         if (Client_Type(Client) == CLIENT_SERVER)
639                 from = Client_Search(Req->prefix);
640         else
641                 from = Client;
642
643         if (!from)
644                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
645                                           Client_ID(Client), Req->prefix);
646
647         if (Req->argc == 2) {
648                 /* Forward to other server? */
649                 target = Client_Search(Req->argv[1]);
650                 if (! target || Client_Type(target) != CLIENT_SERVER)
651                         return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
652                                                   Client_ID(Client),
653                                                   Req->argv[1]);
654
655                 if (target != Client_ThisServer()) {
656                         /* Target is indeed an other server, forward it! */
657                         return IRC_WriteStrClientPrefix(target, from,
658                                                         "LIST %s :%s",
659                                                         Req->argv[0],
660                                                         Req->argv[1]);
661                 }
662         }
663
664         while (pattern) {
665                 /* Loop through all the channels */
666                 if (Req->argc > 0)
667                         ngt_LowerStr(pattern);
668                 chan = Channel_First();
669                 while (chan) {
670                         /* Check search pattern */
671                         if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
672                                 /* Gotcha! */
673                                 if (!strchr(Channel_Modes(chan), 's')
674                                     || Channel_IsMemberOf(chan, from)
675                                     || (!Conf_MorePrivacy && Client_OperByMe(Client))) {
676                                         if (IRC_CheckListTooBig(from, count,
677                                                                  MAX_RPL_LIST,
678                                                                  "LIST"))
679                                                 break;
680                                         if (!IRC_WriteStrClient(from,
681                                              RPL_LIST_MSG, Client_ID(from),
682                                              Channel_Name(chan),
683                                              Channel_MemberCount(chan),
684                                              Channel_Topic( chan )))
685                                                 return DISCONNECTED;
686                                         count++;
687                                 }
688                         }
689                         chan = Channel_Next(chan);
690                 }
691
692                 /* Get next name ... */
693                 if(Req->argc > 0)
694                         pattern = strtok(NULL, ",");
695                 else
696                         pattern = NULL;
697         }
698
699         IRC_SetPenalty(from, 2);
700         return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
701 } /* IRC_LIST */
702
703
704 /**
705  * Handler for the IRC+ command "CHANINFO".
706  *
707  * See doc/Protocol.txt, section II.3:
708  * "Exchange channel-modes, topics, and persistent channels".
709  *
710  * @param Client        The client from which this command has been received
711  * @param Req           Request structure with prefix and all parameters
712  * @returns             CONNECTED or DISCONNECTED
713  */
714 GLOBAL bool
715 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
716 {
717         char modes_add[COMMAND_LEN], l[16], *ptr;
718         CLIENT *from;
719         CHANNEL *chan;
720         int arg_topic;
721
722         assert( Client != NULL );
723         assert( Req != NULL );
724
725         /* Bad number of parameters? */
726         if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
727                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
728                                           Client_ID(Client), Req->command);
729
730         /* Compatibility kludge */
731         if( Req->argc == 5 ) arg_topic = 4;
732         else if( Req->argc == 3 ) arg_topic = 2;
733         else arg_topic = -1;
734
735         /* Search origin */
736         from = Client_Search( Req->prefix );
737         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
738
739         /* Search or create channel */
740         chan = Channel_Search( Req->argv[0] );
741         if( ! chan ) chan = Channel_Create( Req->argv[0] );
742         if( ! chan ) return CONNECTED;
743
744         if( Req->argv[1][0] == '+' )
745         {
746                 ptr = Channel_Modes( chan );
747                 if( ! *ptr )
748                 {
749                         /* OK, this channel doesn't have modes jet, set the received ones: */
750                         Channel_SetModes( chan, &Req->argv[1][1] );
751
752                         if( Req->argc == 5 )
753                         {
754                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
755                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
756                         }
757                         else
758                         {
759                                 /* Delete modes which we never want to inherit */
760                                 Channel_ModeDel( chan, 'l' );
761                                 Channel_ModeDel( chan, 'k' );
762                         }
763
764                         strcpy( modes_add, "" );
765                         ptr = Channel_Modes( chan );
766                         while( *ptr )
767                         {
768                                 if( *ptr == 'l' )
769                                 {
770                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
771                                         strlcat( modes_add, l, sizeof( modes_add ));
772                                 }
773                                 if( *ptr == 'k' )
774                                 {
775                                         strlcat( modes_add, " ", sizeof( modes_add ));
776                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
777                                 }
778                                 ptr++;
779                         }
780
781                         /* Inform members of this channel */
782                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
783                 }
784         }
785         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
786
787         if( arg_topic > 0 )
788         {
789                 /* We got a topic */
790                 ptr = Channel_Topic( chan );
791                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
792                 {
793                         /* OK, there is no topic jet */
794                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
795                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
796                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
797                 }
798         }
799
800         /* Forward CHANINFO to other serevrs */
801         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] );
802         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
803         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
804
805         return CONNECTED;
806 } /* IRC_CHANINFO */
807
808
809 /* -eof- */