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