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