]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-channel.c
Move "ClientHost" and "ClientUserNick" to end of [Global] section
[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                         r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
530                                                Client_ID(Client),
531                                                Channel_Name(chan),
532                                                Channel_TopicWho(chan),
533                                                Channel_TopicTime(chan));
534 #endif
535                         return r;
536                 }
537                 else
538                         return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
539                                                   Client_ID(from),
540                                                   Channel_Name(chan));
541         }
542
543         if (strchr(Channel_Modes(chan), 't')) {
544                 /* Topic Lock. Is the user a channel or IRC operator? */
545                 if (!topicok)
546                         return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
547                                                   Client_ID(from),
548                                                   Channel_Name(chan));
549         }
550
551         /* Set new topic */
552         Channel_SetTopic(chan, from, Req->argv[1]);
553         LogDebug("%s \"%s\" set topic on \"%s\": %s",
554                  Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
555                  Req->argv[1][0] ? Req->argv[1] : "<none>");
556
557         if (use_servermode)
558                 from = Client_ThisServer();
559
560         /* Update channel and forward new topic to other servers */
561         if (!Channel_IsLocal(chan))
562                 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
563                                           Req->argv[0], Req->argv[1]);
564         IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
565                                   Req->argv[0], Req->argv[1]);
566
567         if (Client_Type(Client) == CLIENT_USER)
568                 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
569                                                 Req->argv[0], Req->argv[1]);
570         else
571                 return CONNECTED;
572 } /* IRC_TOPIC */
573
574
575 /**
576  * Handler for the IRC "LIST" command.
577  *
578  * See RFC 2812, 3.2.6 "List message".
579  *
580  * This implementation handles the local case as well as the forwarding of the
581  * LIST command to other servers in the IRC network.
582  *
583  * @param Client        The client from which this command has been received
584  * @param Req           Request structure with prefix and all parameters
585  * @returns             CONNECTED or DISCONNECTED
586  */
587 GLOBAL bool
588 IRC_LIST( CLIENT *Client, REQUEST *Req )
589 {
590         char *pattern;
591         CHANNEL *chan;
592         CLIENT *from, *target;
593
594         assert( Client != NULL );
595         assert( Req != NULL );
596
597         /* Bad number of prameters? */
598         if( Req->argc > 2 )
599                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
600                         Client_ID( Client ), Req->command );
601
602         if( Req->argc > 0 )
603                 pattern = strtok( Req->argv[0], "," );
604         else
605                 pattern = "*";
606
607         /* Get sender from prefix, if any */
608         if( Client_Type( Client ) == CLIENT_SERVER )
609                 from = Client_Search( Req->prefix );
610         else
611                 from = Client;
612
613         if( ! from )
614                 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
615                                 Client_ID( Client ), Req->prefix );
616
617         if( Req->argc == 2 )
618         {
619                 /* Forward to other server? */
620                 target = Client_Search( Req->argv[1] );
621                 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
622                         return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
623                                         Client_ID( Client ), Req->argv[1] );
624
625                 if( target != Client_ThisServer( ))
626                 {
627                         /* Target is indeed an other server, forward it! */
628                         return IRC_WriteStrClientPrefix( target, from,
629                                         "LIST %s :%s", Client_ID( from ),
630                                         Req->argv[1] );
631                 }
632         }
633
634         while( pattern )
635         {
636                 /* Loop through all the channels */
637                 chan = Channel_First( );
638                 while( chan )
639                 {
640                         /* Check search pattern */
641                         if( Match( pattern, Channel_Name( chan )))
642                         {
643                                 /* Gotcha! */
644                                 if( ! strchr( Channel_Modes( chan ), 's' ) ||
645                                     Channel_IsMemberOf( chan, from ))
646                                 {
647                                         if( ! IRC_WriteStrClient( from,
648                                             RPL_LIST_MSG, Client_ID( from ),
649                                             Channel_Name( chan ),
650                                             Channel_MemberCount( chan ),
651                                             Channel_Topic( chan )))
652                                                 return DISCONNECTED;
653                                 }
654                         }
655                         chan = Channel_Next( chan );
656                 }
657
658                 /* Get next name ... */
659                 if( Req->argc > 0 )
660                         pattern = strtok( NULL, "," );
661                 else
662                         pattern = NULL;
663         }
664
665         return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
666 } /* IRC_LIST */
667
668
669 /**
670  * Handler for the IRC+ command "CHANINFO".
671  *
672  * See doc/Protocol.txt, section II.3:
673  * "Exchange channel-modes, topics, and persistent channels".
674  *
675  * @param Client        The client from which this command has been received
676  * @param Req           Request structure with prefix and all parameters
677  * @returns             CONNECTED or DISCONNECTED
678  */
679 GLOBAL bool
680 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
681 {
682         char modes_add[COMMAND_LEN], l[16], *ptr;
683         CLIENT *from;
684         CHANNEL *chan;
685         int arg_topic;
686
687         assert( Client != NULL );
688         assert( Req != NULL );
689
690         /* Bad number of parameters? */
691         if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
692
693         /* Compatibility kludge */
694         if( Req->argc == 5 ) arg_topic = 4;
695         else if( Req->argc == 3 ) arg_topic = 2;
696         else arg_topic = -1;
697
698         /* Search origin */
699         from = Client_Search( Req->prefix );
700         if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
701
702         /* Search or create channel */
703         chan = Channel_Search( Req->argv[0] );
704         if( ! chan ) chan = Channel_Create( Req->argv[0] );
705         if( ! chan ) return CONNECTED;
706
707         if( Req->argv[1][0] == '+' )
708         {
709                 ptr = Channel_Modes( chan );
710                 if( ! *ptr )
711                 {
712                         /* OK, this channel doesn't have modes jet, set the received ones: */
713                         Channel_SetModes( chan, &Req->argv[1][1] );
714
715                         if( Req->argc == 5 )
716                         {
717                                 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
718                                 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
719                         }
720                         else
721                         {
722                                 /* Delete modes which we never want to inherit */
723                                 Channel_ModeDel( chan, 'l' );
724                                 Channel_ModeDel( chan, 'k' );
725                         }
726
727                         strcpy( modes_add, "" );
728                         ptr = Channel_Modes( chan );
729                         while( *ptr )
730                         {
731                                 if( *ptr == 'l' )
732                                 {
733                                         snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
734                                         strlcat( modes_add, l, sizeof( modes_add ));
735                                 }
736                                 if( *ptr == 'k' )
737                                 {
738                                         strlcat( modes_add, " ", sizeof( modes_add ));
739                                         strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
740                                 }
741                                 ptr++;
742                         }
743
744                         /* Inform members of this channel */
745                         IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
746                 }
747         }
748         else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
749
750         if( arg_topic > 0 )
751         {
752                 /* We got a topic */
753                 ptr = Channel_Topic( chan );
754                 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
755                 {
756                         /* OK, there is no topic jet */
757                         Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
758                         IRC_WriteStrChannelPrefix(Client, chan, from, false,
759                              "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
760                 }
761         }
762
763         /* Forward CHANINFO to other serevrs */
764         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] );
765         else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
766         else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
767
768         return CONNECTED;
769 } /* IRC_CHANINFO */
770
771
772 /* -eof- */