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