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