]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
0421d9170201b9d7d81f26d6b61feea4ee584c2d
[ngircd-alex.git] / src / ngircd / irc-channel.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * IRC channel commands
17  */
18
19 #include "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_WriteErrClient(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_WriteErrClient(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_WriteErrClient(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_WriteErrClient(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_WriteErrClient(Client, ERR_SECURECHANNEL_MSG,
128                                    Client_ID(Client), channame);
129                 return false;
130         }
131
132         if (Channel_HasMode(chan, 'O') && !Client_HasMode(Client, 'o')) {
133                 /* Only IRC operators are allowed! */
134                 IRC_WriteErrClient(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_WriteErrClient(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_GET_SENDER_OR_RETURN_(target, Req, Client)
313
314         /* Is argument "0"? */
315         if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
316                 return part_from_all_channels(Client, target);
317
318         /* Are channel keys given? */
319         if (Req->argc > 1)
320                 key = strtok_r(Req->argv[1], ",", &lastkey);
321
322         channame = Req->argv[0];
323         channame = strtok_r(channame, ",", &lastchan);
324
325         /* Make sure that "channame" is not the empty string ("JOIN :") */
326         if (!channame)
327                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
328                                           Client_ID(Client), Req->command);
329
330         while (channame) {
331                 flags = NULL;
332
333                 /* Did the server include channel-user-modes? */
334                 if (Client_Type(Client) == CLIENT_SERVER) {
335                         flags = strchr(channame, 0x7);
336                         if (flags) {
337                                 *flags = '\0';
338                                 flags++;
339                         }
340                 }
341
342                 chan = Channel_Search(channame);
343
344                 /* Local client? */
345                 if (Client_Type(Client) == CLIENT_USER) {
346                         if (chan) {
347                                 /* Already existing channel: already member? */
348                                 if (Channel_IsMemberOf(chan, Client))
349                                     goto join_next;
350                         } else {
351                                 /* Channel must be created */
352                                 if (!strchr(Conf_AllowedChannelTypes, channame[0])) {
353                                         /* ... but channel type is not allowed! */
354                                         IRC_WriteErrClient(Client,
355                                                 ERR_NOSUCHCHANNEL_MSG,
356                                                 Client_ID(Client), channame);
357                                         goto join_next;
358                                 }
359                         }
360
361                         /* Test if the user has reached the channel limit */
362                         if ((Conf_MaxJoins > 0) &&
363                             (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
364                                 if (!IRC_WriteErrClient(Client,
365                                                 ERR_TOOMANYCHANNELS_MSG,
366                                                 Client_ID(Client), channame))
367                                         return DISCONNECTED;
368                                 goto join_next;
369                         }
370
371                         if (chan) {
372                                 /* Already existing channel: check if the
373                                  * client is allowed to join */
374                                 if (!join_allowed(Client, chan, channame, key))
375                                         goto join_next;
376                         } else {
377                                 /* New channel: first user will become channel
378                                  * operator unless this is a modeless channel */
379                                 if (*channame != '+')
380                                         flags = "o";
381                         }
382
383                         /* Local client: update idle time */
384                         Conn_UpdateIdle(Client_Conn(Client));
385                 } else {
386                         /* Remote server: we don't need to know whether the
387                          * client is invited or not, but we have to make sure
388                          * that the "one shot" entries (generated by INVITE
389                          * commands) in this list become deleted when a user
390                          * joins a channel this way. */
391                         if (chan)
392                                 (void)Lists_Check(Channel_GetListInvites(chan),
393                                                   target);
394                 }
395
396                 /* Join channel (and create channel if it doesn't exist) */
397                 if (!Channel_Join(target, channame))
398                         goto join_next;
399
400                 if (!chan) { /* channel is new; it has been created above */
401                         chan = Channel_Search(channame);
402                         assert(chan != NULL);
403                         if (Channel_IsModeless(chan)) {
404                                 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
405                                 Channel_ModeAdd(chan, 'n'); /* no external msgs */
406                         }
407                 }
408                 assert(chan != NULL);
409
410                 join_set_channelmodes(chan, target, flags);
411
412                 join_forward(Client, target, chan, channame);
413
414                 if (!join_send_topic(Client, target, chan, channame))
415                         break; /* write error */
416
417         join_next:
418                 /* next channel? */
419                 channame = strtok_r(NULL, ",", &lastchan);
420                 if (channame && key)
421                         key = strtok_r(NULL, ",", &lastkey);
422         }
423         return CONNECTED;
424 } /* IRC_JOIN */
425
426 /**
427  * Handler for the IRC "PART" command.
428  *
429  * @param Client The client from which this command has been received.
430  * @param Req Request structure with prefix and all parameters.
431  * @return CONNECTED or DISCONNECTED.
432  */
433 GLOBAL bool
434 IRC_PART(CLIENT * Client, REQUEST * Req)
435 {
436         CLIENT *target;
437         char *chan;
438
439         assert(Client != NULL);
440         assert(Req != NULL);
441
442         _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
443
444         /* Loop over all the given channel names */
445         chan = strtok(Req->argv[0], ",");
446
447         /* Make sure that "chan" is not the empty string ("PART :") */
448         if (!chan)
449                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
450                                           Client_ID(Client), Req->command);
451
452         while (chan) {
453                 Channel_Part(target, Client, chan,
454                              Req->argc > 1 ? Req->argv[1] : Client_ID(target));
455                 chan = strtok(NULL, ",");
456         }
457
458         /* Update idle time, if local client */
459         if (Client_Conn(Client) > NONE)
460                 Conn_UpdateIdle(Client_Conn(Client));
461
462         return CONNECTED;
463 } /* IRC_PART */
464
465 /**
466  * Handler for the IRC "TOPIC" command.
467  *
468  * @param Client The client from which this command has been received.
469  * @param Req Request structure with prefix and all parameters.
470  * @return CONNECTED or DISCONNECTED.
471  */
472 GLOBAL bool
473 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
474 {
475         CHANNEL *chan;
476         CLIENT *from;
477         char *topic;
478         bool r, topic_power;
479
480         assert( Client != NULL );
481         assert( Req != NULL );
482
483         IRC_SetPenalty(Client, 1);
484
485         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
486
487         chan = Channel_Search(Req->argv[0]);
488         if (!chan)
489                 return IRC_WriteErrClient(from, ERR_NOSUCHCHANNEL_MSG,
490                                           Client_ID(from), Req->argv[0]);
491
492         /* Only remote servers and channel members are allowed to change the
493          * channel topic, and IRC operators when the Conf_OperCanMode option
494          * is set in the server configuration. */
495         if (Client_Type(Client) != CLIENT_SERVER) {
496                 topic_power = Client_HasMode(from, 'o');
497                 if (!Channel_IsMemberOf(chan, from)
498                     && !(Conf_OperCanMode && topic_power))
499                         return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
500                                                   Client_ID(from), Req->argv[0]);
501         } else
502                 topic_power = true;
503
504         if (Req->argc == 1) {
505                 /* Request actual topic */
506                 topic = Channel_Topic(chan);
507                 if (*topic) {
508                         r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
509                                                Client_ID(Client),
510                                                Channel_Name(chan), topic);
511 #ifndef STRICT_RFC
512                         if (!r)
513                                 return r;
514                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
515                                                Client_ID(Client),
516                                                Channel_Name(chan),
517                                                Channel_TopicWho(chan),
518                                                Channel_TopicTime(chan));
519 #endif
520                         return r;
521                 }
522                 else
523                         return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
524                                                   Client_ID(from),
525                                                   Channel_Name(chan));
526         }
527
528         if (Channel_HasMode(chan, 't')) {
529                 /* Topic Lock. Is the user a channel op or IRC operator? */
530                 if(!topic_power &&
531                    !Channel_UserHasMode(chan, from, 'h') &&
532                    !Channel_UserHasMode(chan, from, 'o') &&
533                    !Channel_UserHasMode(chan, from, 'a') &&
534                    !Channel_UserHasMode(chan, from, 'q'))
535                         return IRC_WriteErrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
536                                                   Client_ID(from),
537                                                   Channel_Name(chan));
538         }
539
540         /* Set new topic */
541         Channel_SetTopic(chan, from, Req->argv[1]);
542         LogDebug("%s \"%s\" set topic on \"%s\": %s",
543                  Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
544                  Req->argv[1][0] ? Req->argv[1] : "<none>");
545
546         if (Conf_OperServerMode)
547                 from = Client_ThisServer();
548
549         /* Update channel and forward new topic to other servers */
550         if (!Channel_IsLocal(chan))
551                 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
552                                           Req->argv[0], Req->argv[1]);
553         IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
554                                   Req->argv[0], Req->argv[1]);
555
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_SetPenalty(Client, 2);
582
583         _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
584
585         if (Req->argc > 0)
586                 pattern = strtok(Req->argv[0], ",");
587         else
588                 pattern = "*";
589
590         if (Req->argc == 2) {
591                 /* Forward to other server? */
592                 target = Client_Search(Req->argv[1]);
593                 if (! target || Client_Type(target) != CLIENT_SERVER)
594                         return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
595                                                   Client_ID(Client),
596                                                   Req->argv[1]);
597
598                 if (target != Client_ThisServer()) {
599                         /* Target is indeed an other server, forward it! */
600                         return IRC_WriteStrClientPrefix(target, from,
601                                                         "LIST %s :%s",
602                                                         Req->argv[0],
603                                                         Req->argv[1]);
604                 }
605         }
606
607         while (pattern) {
608                 /* Loop through all the channels */
609                 if (Req->argc > 0)
610                         ngt_LowerStr(pattern);
611                 chan = Channel_First();
612                 while (chan) {
613                         /* Check search pattern */
614                         if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
615                                 /* Gotcha! */
616                                 if (!Channel_HasMode(chan, 's')
617                                     || Channel_IsMemberOf(chan, from)
618                                     || (!Conf_MorePrivacy
619                                         && Client_HasMode(Client, 'o')
620                                         && Client_Conn(Client) > NONE))
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- */