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