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