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