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