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